File indexing completed on 2024-12-01 07:26:15

0001 /*
0002  * SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
0003  * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 #include "integeroption.h"
0009 
0010 #include <QVarLengthArray>
0011 
0012 static const int KSW_INT_MAX = 2147483647;
0013 static const int KSW_INT_MIN = -2147483647 - 1; // prevent warning
0014 
0015 namespace KSaneCore
0016 {
0017 
0018 IntegerOption::IntegerOption(const SANE_Handle handle, const int index)
0019     : BaseOption(handle, index)
0020 {
0021     m_optionType = Option::TypeInteger;
0022 }
0023 
0024 void IntegerOption::readValue()
0025 {
0026     if (state() == Option::StateHidden) {
0027         return;
0028     }
0029 
0030     // read that current value
0031     QVarLengthArray<unsigned char> data(m_optDesc->size);
0032     SANE_Status status;
0033     SANE_Int res;
0034     status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
0035     if (status != SANE_STATUS_GOOD) {
0036         return;
0037     }
0038 
0039     int newValue = toSANE_Word(data.data());
0040     if (newValue != m_iVal) {
0041         m_iVal = newValue;
0042         Q_EMIT valueChanged(m_iVal);
0043     }
0044 }
0045 
0046 QVariant IntegerOption::minimumValue() const
0047 {
0048     QVariant value;
0049     if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
0050         value = static_cast<int>(m_optDesc->constraint.range->min);
0051     } else {
0052         value = KSW_INT_MIN;
0053     }
0054     return value;
0055 }
0056 
0057 QVariant IntegerOption::maximumValue() const
0058 {
0059     QVariant value;
0060     if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
0061         value = static_cast<int>(m_optDesc->constraint.range->max);
0062     } else {
0063         value = KSW_INT_MAX;
0064     }
0065     return value;
0066 }
0067 
0068 QVariant IntegerOption::stepValue() const
0069 {
0070     QVariant value;
0071     if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
0072         value = static_cast<int>(m_optDesc->constraint.range->quant);
0073         // guard against possible broken backends
0074         if (value == 0) {
0075             value = 1;
0076         }
0077     } else {
0078         value = 1;
0079     }
0080     return value;
0081 }
0082 
0083 QVariant IntegerOption::value() const
0084 {
0085     QVariant value;
0086     if (state() == Option::StateHidden) {
0087         return value;
0088     }
0089     value = m_iVal;
0090     return value;
0091 }
0092 
0093 QString IntegerOption::valueAsString() const
0094 {
0095     if (state() == Option::StateHidden) {
0096         return QString();
0097     }
0098     return QString::number(m_iVal);
0099 }
0100 
0101 bool IntegerOption::setValue(const QVariant &val)
0102 {
0103     bool ok;
0104     int newValue = val.toInt(&ok);
0105     if (ok && newValue != m_iVal) {
0106         unsigned char data[4];
0107         m_iVal = newValue;
0108         fromSANE_Word(data, newValue);
0109         writeData(data);
0110         Q_EMIT valueChanged(m_iVal);
0111     }
0112     return ok;
0113 }
0114 
0115 } // namespace KSaneCore
0116 
0117 #include "moc_integeroption.cpp"