Commit 61b7ed11 authored by Alexander Stippich's avatar Alexander Stippich
Browse files

change KSaneOption to a QVariant based API

parent 99fd4112
......@@ -698,7 +698,8 @@ void KSaneWidget::getOptVals(QMap <QString, QString> &opts)
for (int i = 0; i < d->m_optList.size(); i++) {
option = d->m_optList.at(i);
if (option->getValue(tmp)) {
tmp = option->getValueAsString();
if (!tmp.isEmpty()) {
opts[option->name()] = tmp;
}
}
......@@ -711,7 +712,8 @@ bool KSaneWidget::getOptVal(const QString &optname, QString &value)
KSaneOption *option;
if ((option = d->getOption(optname)) != nullptr) {
return option->getValue(value);
value = option->getValueAsString();
return !value.isEmpty();
}
// Special handling for non sane option
if (optname == InvetColorsOption) {
......@@ -774,12 +776,10 @@ int KSaneWidget::setOptVals(const QMap <QString, QString> &opts)
(d->m_optGamB)) {
// check if the current gamma values are identical. if they are identical,
// uncheck the "Separate color intensity tables" checkbox
QString redGamma;
QString greenGamma;
QString blueGamma;
d->m_optGamR->getValue(redGamma);
d->m_optGamG->getValue(greenGamma);
d->m_optGamB->getValue(blueGamma);
QString redGamma = d->m_optGamR->getValueAsString();
QString greenGamma = d->m_optGamG->getValueAsString();
QString blueGamma = d->m_optGamB->getValueAsString();
if ((redGamma == greenGamma) && (greenGamma == blueGamma)) {
d->m_splitGamChB->setChecked(false);
// set the values to the common gamma widget
......@@ -822,12 +822,9 @@ bool KSaneWidget::setOptVal(const QString &option, const QString &value)
(opt == d->m_optGamB))) {
// check if the current gamma values are identical. if they are identical,
// uncheck the "Separate color intensity tables" checkbox
QString redGamma;
QString greenGamma;
QString blueGamma;
d->m_optGamR->getValue(redGamma);
d->m_optGamG->getValue(greenGamma);
d->m_optGamB->getValue(blueGamma);
QString redGamma = d->m_optGamR->getValueAsString();
QString greenGamma = d->m_optGamG->getValueAsString();
QString blueGamma = d->m_optGamB->getValueAsString();
if ((redGamma == greenGamma) && (greenGamma == blueGamma)) {
d->m_splitGamChB->setChecked(false);
// set the values to the common gamma widget
......@@ -880,9 +877,9 @@ void KSaneWidget::enableAutoSelect(bool enable)
float KSaneWidget::currentDPI()
{
if (d->m_optRes) {
float value;
if (d->m_optRes->getValue(value)) {
return value;
QVariant value = d->m_optRes->getValue();
if (!value.isNull()) {
return value.toFloat();
}
}
return 0.0; // Failure to read DPI
......@@ -893,7 +890,7 @@ float KSaneWidget::scanAreaWidth()
float result = 0.0;
if (d->m_optBrX) {
if (d->m_optBrX->getUnit() == KSaneOption::UnitPixel) {
d->m_optBrX->getMaxValue(result);
result = d->m_optBrX->getMaxValue().toFloat();
float dpi = currentDPI();
if (dpi < 1) {
qCDebug(KSANE_LOG) << "Broken DPI value";
......@@ -901,7 +898,7 @@ float KSaneWidget::scanAreaWidth()
}
result = result / dpi / 25.4;
} else if (d->m_optBrX->getUnit() == KSaneOption::UnitMilliMeter) {
d->m_optBrX->getMaxValue(result);
result = d->m_optBrX->getMaxValue().toFloat();
}
}
return result;
......@@ -912,7 +909,7 @@ float KSaneWidget::scanAreaHeight()
float result = 0.0;
if (d->m_optBrY) {
if (d->m_optBrY->getUnit() == KSaneOption::UnitPixel) {
d->m_optBrY->getMaxValue(result);
result = d->m_optBrY->getMaxValue().toFloat();
float dpi = currentDPI();
if (dpi < 1) {
qCDebug(KSANE_LOG) << "Broken DPI value";
......@@ -920,7 +917,7 @@ float KSaneWidget::scanAreaHeight()
}
result = result / dpi / 25.4;
} else if (d->m_optBrY->getUnit() == KSaneOption::UnitMilliMeter) {
d->m_optBrY->getMaxValue(result);
result = d->m_optBrY->getMaxValue().toFloat();
}
}
return result;
......
......@@ -232,8 +232,7 @@ float KSaneWidgetPrivate::ratioToScanAreaX(float ratio)
if (!m_optBrX) {
return 0.0;
}
float max;
m_optBrX->getMaxValue(max);
float max = m_optBrX->getMaxValue().toFloat();
return max * ratio;
}
......@@ -243,8 +242,7 @@ float KSaneWidgetPrivate::ratioToScanAreaY(float ratio)
if (!m_optBrY) {
return 0.0;
}
float max;
m_optBrY->getMaxValue(max);
float max = m_optBrY->getMaxValue().toFloat();
return max * ratio;
}
......@@ -254,8 +252,7 @@ float KSaneWidgetPrivate::scanAreaToRatioX(float scanArea)
if (!m_optBrX) {
return 0.0;
}
float max;
m_optBrX->getMaxValue(max);
float max = m_optBrX->getMaxValue().toFloat();
if (scanArea > max) {
return 1.0;
......@@ -273,8 +270,7 @@ float KSaneWidgetPrivate::scanAreaToRatioY(float scanArea)
if (!m_optBrY) {
return 0.0;
}
float max;
m_optBrY->getMaxValue(max);
float max = m_optBrY->getMaxValue().toFloat();
if (scanArea > max) {
return 1.0;
......@@ -310,8 +306,7 @@ float KSaneWidgetPrivate::ratioToDispUnitX(float ratio)
}
else if (m_optBrX->getUnit() == KSaneOption::UnitPixel && m_optRes) {
// get current DPI
float dpi;
m_optRes->getValue(dpi);
float dpi = m_optRes->getValue().toFloat();
if (dpi > 1) {
result = result / (dpi / 25.4);
return mmToDispUnit(result);
......@@ -334,8 +329,7 @@ float KSaneWidgetPrivate::ratioToDispUnitY(float ratio)
}
else if (m_optBrY->getUnit() == KSaneOption::UnitPixel && m_optRes) {
// get current DPI
float dpi;
m_optRes->getValue(dpi);
float dpi = m_optRes->getValue().toFloat();
if (dpi > 1) {
result = result / (dpi / 25.4);
return mmToDispUnit(result);
......@@ -848,9 +842,13 @@ void KSaneWidgetPrivate::setBRX(const QVariant &x)
float ratio = scanAreaToRatioX(fbrx);
m_previewViewer->setBRX(ratio);
float tlx = 0;
if (m_optTlX && m_optTlX->getValue(tlx)) {
float tlxRatio = scanAreaToRatioX(tlx);
if (!m_optTlX) {
return;
}
QVariant tlx = m_optTlX->getValue();
if (!tlx.isNull()) {
float tlxRatio = scanAreaToRatioX(tlx.toFloat());
m_scanareaWidth->setValue(ratioToDispUnitX(ratio) - ratioToDispUnitX(tlxRatio));
}
}
......@@ -866,10 +864,13 @@ void KSaneWidgetPrivate::setBRY(const QVariant &y)
float ratio = scanAreaToRatioY(fbry);
m_previewViewer->setBRY(ratio);
float tly = 0;
if (m_optTlY && m_optTlY->getValue(tly)) {
float tlyRatio = scanAreaToRatioY(tly);
if (!m_optTlY) {
return;
}
QVariant tly = m_optTlY->getValue();
if (!tly.isNull()) {
float tlyRatio = scanAreaToRatioY(tly.toFloat());
m_scanareaHeight->setValue(ratioToDispUnitY(ratio) - ratioToDispUnitY(tlyRatio));
}
}
......@@ -883,10 +884,10 @@ void KSaneWidgetPrivate::updatePreviewSize()
// check if an update is necessary
if (m_optBrX != nullptr) {
m_optBrX->getMaxValue(max_x);
max_x = m_optBrX->getMaxValue().toFloat();
}
if (m_optBrY != nullptr) {
m_optBrY->getMaxValue(max_y);
max_y = m_optBrY->getMaxValue().toFloat();
}
if ((max_x == m_previewWidth) && (max_y == m_previewHeight)) {
//qCDebug(KSANE_LOG) << "no preview size change";
......@@ -982,8 +983,8 @@ void KSaneWidgetPrivate::startPreviewScan()
if ((m_optTlX != nullptr) && (m_optTlY != nullptr) &&
(m_optBrX != nullptr) && (m_optBrY != nullptr)) {
// get maximums
m_optBrX->getMaxValue(max_x);
m_optBrY->getMaxValue(max_y);
max_x = m_optBrX->getMaxValue().toFloat();
max_y = m_optBrY->getMaxValue().toFloat();
// select the whole area
m_optTlX->setValue(0);
m_optTlY->setValue(0);
......@@ -1004,7 +1005,7 @@ void KSaneWidgetPrivate::startPreviewScan()
} else {
// set the resolution to getMinValue and increase if necessary
SANE_Parameters params;
m_optRes->getMinValue(dpi);
dpi = m_optRes->getMinValue().toFloat();
do {
m_optRes->setValue(dpi);
if ((m_optResY != nullptr) && (m_optRes->name() == QStringLiteral(SANE_NAME_SCAN_X_RESOLUTION))) {
......@@ -1028,7 +1029,7 @@ void KSaneWidgetPrivate::startPreviewScan()
if (params.pixels_per_line == 0) {
// This is a security measure for broken backends
m_optRes->getMinValue(dpi);
dpi = m_optRes->getMinValue().toFloat();
m_optRes->setValue(dpi);
qCDebug(KSANE_LOG) << "Setting minimum DPI value for a broken back-end";
}
......@@ -1155,8 +1156,7 @@ bool KSaneWidgetPrivate::scanSourceADF()
return false;
}
QString source;
m_optSource->getValue(source);
QString source = m_optSource->getValue().toString();
return source.contains(QStringLiteral("Automatic Document Feeder")) ||
source.contains(QStringLiteral("ADF")) ||
......@@ -1203,8 +1203,7 @@ void KSaneWidgetPrivate::oneFinalScanDone()
// Check if we have a "wait for button" batch scanning
if (m_optWaitForBtn) {
qCDebug(KSANE_LOG) << m_optWaitForBtn->name();
QString wait;
m_optWaitForBtn->getValue(wait);
QString wait = m_optWaitForBtn->getValue().toString();
qCDebug(KSANE_LOG) << "wait ==" << wait;
if (wait == QStringLiteral("true")) {
......@@ -1324,10 +1323,8 @@ void KSaneWidgetPrivate::checkInvert()
return;
}
QString source;
QString filmtype;
m_optSource->getValue(source);
m_optFilmType->getValue(filmtype);
QString source = m_optSource->getValue().toString();
QString filmtype = m_optFilmType->getValue().toString();
if ((source.contains(i18nc("This is compared to the option string returned by sane",
"Transparency"), Qt::CaseInsensitive)) &&
......@@ -1403,24 +1400,28 @@ void KSaneWidgetPrivate::pollPollOptions()
void KSaneWidgetPrivate::updateScanSelection()
{
float maxX = 0;
if (m_optBrX) m_optBrX->getMaxValue(maxX);
QVariant maxX;
if (m_optBrX) {
maxX = m_optBrX->getMaxValue();
}
float maxY = 0;
if (m_optBrY) m_optBrY->getMaxValue(maxY);
QVariant maxY;
if (m_optBrY) {
maxY = m_optBrY->getMaxValue();
}
float x1 = m_scanareaX->value();
float y1 = m_scanareaY->value();
float w = m_scanareaWidth->value();
float h = m_scanareaHeight->value();
float x1Max = maxX - w;
float x1Max = maxX.toFloat() - w;
m_scanareaX->setRange(0.0, x1Max);
if (x1 > x1Max) {
m_scanareaX->setValue(x1Max);
}
float y1Max = maxY - h;
float y1Max = maxY.toFloat() - h;
m_scanareaY->setRange(0.0, y1Max);
if (y1 > y1Max) {
m_scanareaY->setValue(y1Max);
......
......@@ -67,25 +67,26 @@ void KSaneOptCheckBox::readValue()
Q_EMIT buttonPressed(name(), sane_i18n(m_optDesc->title), m_checked);
Q_EMIT valueChanged(m_checked);
}
}
bool KSaneOptCheckBox::getValue(float &val)
QVariant KSaneOptCheckBox::getValue() const
{
if (state() == StateHidden) {
return false;
return QVariant();
}
val = m_checked ? 1.0 : 0.0;
return true;
return m_checked;
}
bool KSaneOptCheckBox::getValue(QString &val)
QString KSaneOptCheckBox::getValueAsString() const
{
if (state() == StateHidden) {
return false;
return QString();
}
if (m_checked) {
return QStringLiteral("true");
} else {
return QStringLiteral("false");
}
val = m_checked ? QStringLiteral("true") : QStringLiteral("false");
return true;
}
} // NameSpace KSaneIface
......@@ -28,8 +28,8 @@ public:
void readValue() override;
bool getValue(float &val) override;
bool getValue(QString &val) override;
QVariant getValue() const override;
QString getValueAsString() const override;
public Q_SLOTS:
bool setValue(const QVariant &value) override;
......
......@@ -106,42 +106,42 @@ bool KSaneOptCombo::setValue(const QVariant &value)
return success;
}
bool KSaneOptCombo::getMinValue(float &val)
QVariant KSaneOptCombo::getMinValue() const
{
QVariant value;
if (state() == StateHidden) {
return false;
return value;
}
float fValueMin;
int iValueMin;
switch (m_optDesc->type) {
case SANE_TYPE_INT:
val = static_cast<float>(m_optDesc->constraint.word_list[1]);
iValueMin = static_cast<int>(m_optDesc->constraint.word_list[1]);
for (int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) {
val = qMin(static_cast<float>(m_optDesc->constraint.word_list[i]), val);
iValueMin = qMin(static_cast<int>(m_optDesc->constraint.word_list[i]), iValueMin);
}
value = iValueMin;
break;
case SANE_TYPE_FIXED:
val = static_cast<float>(SANE_UNFIX(m_optDesc->constraint.word_list[1]));
fValueMin = static_cast<float>(SANE_UNFIX(m_optDesc->constraint.word_list[1]));
for (int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) {
val = qMin(static_cast<float>(SANE_UNFIX(m_optDesc->constraint.word_list[i])), val);
fValueMin = qMin(static_cast<float>(SANE_UNFIX(m_optDesc->constraint.word_list[i])), fValueMin);
}
value = fValueMin;
break;
default:
qCDebug(KSANE_LOG) << "can not handle type:" << m_optDesc->type;
return false;
return value;
}
return true;
return value;
}
bool KSaneOptCombo::getValue(float &val)
QVariant KSaneOptCombo::getValue() const
{
if (state() == StateHidden) {
return false;
return QVariant();
}
bool ok;
const float currentValue = m_currentValue.toFloat(&ok);
if (ok) {
val = currentValue;
}
return ok;
return m_currentValue;
}
bool KSaneOptCombo::setValue(float value)
......@@ -188,13 +188,12 @@ bool KSaneOptCombo::setValue(float value)
return false;
}
bool KSaneOptCombo::getValue(QString &val)
QString KSaneOptCombo::getValueAsString() const
{
if (state() == StateHidden) {
return false;
return QString();
}
val = m_currentValue.toString();
return true;
return m_currentValue.toString();
}
bool KSaneOptCombo::setValue(const QString &value)
......
......@@ -30,9 +30,9 @@ public:
void readValue() override;
bool getMinValue(float &max) override;
bool getValue(float &val) override;
bool getValue(QString &val) override;
QVariant getMinValue() const override;
QVariant getValue() const override;
QString getValueAsString() const override;
QVariantList getEntryList() const override;
public Q_SLOTS:
......
......@@ -62,18 +62,17 @@ void KSaneOptEntry::readValue()
Q_EMIT valueChanged(m_string);
}
bool KSaneOptEntry::getValue(float &)
QVariant KSaneOptEntry::getValue() const
{
return false;
return QVariant(m_string);
}
bool KSaneOptEntry::getValue(QString &val)
QString KSaneOptEntry::getValueAsString() const
{
if (state() == StateHidden) {
return false;
return QString();
}
val = m_string;
return true;
return m_string;
}
} // NameSpace KSaneIface
......@@ -28,8 +28,8 @@ public:
void readValue() override;
bool getValue(float &val) override;
bool getValue(QString &val) override;
QVariant getValue() const override;
QString getValueAsString() const override;
public Q_SLOTS:
bool setValue(const QVariant &val) override;
......
......@@ -84,52 +84,53 @@ bool KSaneOptFSlider::setValue(const QVariant &value)
return ok;
}
bool KSaneOptFSlider::getMinValue(float &val)
QVariant KSaneOptFSlider::getMinValue() const
{
QVariant value;
if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
val = SANE_UNFIX(m_optDesc->constraint.range->min);
value = SANE_UNFIX(m_optDesc->constraint.range->min);
} else {
val = FIXED_MIN;
value = FIXED_MIN;
}
return true;
return value;
}
bool KSaneOptFSlider::getMaxValue(float &max)
QVariant KSaneOptFSlider::getMaxValue() const
{
QVariant value;
if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
max = SANE_UNFIX(m_optDesc->constraint.range->max);
value = SANE_UNFIX(m_optDesc->constraint.range->max);
} else {
max = FIXED_MAX;
value = FIXED_MAX;
}
return true;
return value;
}
bool KSaneOptFSlider::getStepValue(float &val)
QVariant KSaneOptFSlider::getStepValue() const
{
QVariant value;
if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
val = SANE_UNFIX(m_optDesc->constraint.range->quant);
value = SANE_UNFIX(m_optDesc->constraint.range->quant);
} else {
val = 0.0001;
value = 0.0001;
}
return true;
return value;
}
bool KSaneOptFSlider::getValue(float &val)
QVariant KSaneOptFSlider::getValue() const
{
if (state() == StateHidden) {
return false;
return QVariant();
}
val = m_fVal;
return true;
return QVariant(m_fVal);
}
bool KSaneOptFSlider::getValue(QString &val)
QString KSaneOptFSlider::getValueAsString() const
{
if (state() == StateHidden) {
return false;
return QString();
}
val = QString::number(m_fVal, 'F', 6);
return true;
return QString::number(m_fVal, 'F', 6);
}
} // NameSpace KSaneIface
......@@ -29,11 +29,11 @@ public:
void readValue() override;
void readOption() override;
bool getMinValue(float &min) override;
bool getMaxValue(float &max) override;
bool getStepValue(float &step) override;
bool getValue(float &val) override;
bool getValue(QString &val) override;
QVariant getMinValue() const override;
QVariant getMaxValue() const override;
QVariant getStepValue() const override;
QVariant getValue() const override;
QString getValueAsString() const override;
public Q_SLOTS:
bool setValue(const QVariant &value) override;
......
......@@ -98,28 +98,31 @@ void KSaneOptGamma::readValue()
// not easy nor fast.. ergo not done
}
bool KSaneOptGamma::getValue(float &)
QVariant KSaneOptGamma::getValue() const
{
return false;
if (state() == StateHidden) {
return QVariant();
}
return QVariant::fromValue(QVector<int>{ m_brightness, m_contrast, m_gamma });
}
bool KSaneOptGamma::getMaxValue(float &value)
QVariant KSaneOptGamma::getMaxValue() const
{
QVariant value;
if (m_optDesc) {
value = static_cast<float>(m_optDesc->constraint.range->max);
return true;
return value;
}
return false;
return value;
}
bool KSaneOptGamma::getValue(QString &val)
QString KSaneOptGamma::getValueAsString() const
{
if (state() == StateHidden) {
return false;
return QString();
}