File indexing completed on 2024-12-08 07:19:38

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 "booloption.h"
0009 
0010 #include <QVarLengthArray>
0011 
0012 #include <ksanecore_debug.h>
0013 
0014 namespace KSaneCore
0015 {
0016 
0017 BoolOption::BoolOption(const SANE_Handle handle, const int index)
0018     : BaseOption(handle, index)
0019 {
0020     m_optionType = Option::TypeBool;
0021 }
0022 
0023 bool BoolOption::setValue(const QVariant &value)
0024 {
0025     if (state() == Option::StateHidden) {
0026         return false;
0027     }
0028 
0029     bool toggled = value.toBool();
0030 
0031     if (m_checked != toggled) {
0032         unsigned char data[4];
0033 
0034         m_checked = toggled;
0035         fromSANE_Word(data, (toggled) ? 1 : 0);
0036         writeData(data);
0037         Q_EMIT valueChanged(m_checked);
0038     }
0039     return true;
0040 }
0041 
0042 void BoolOption::readValue()
0043 {
0044     if (state() == Option::StateHidden) {
0045         return;
0046     }
0047 
0048     // read the current value
0049     QVarLengthArray<unsigned char> data(m_optDesc->size);
0050     SANE_Status status;
0051     SANE_Int res;
0052     status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
0053     if (status != SANE_STATUS_GOOD) {
0054         return;
0055     }
0056     bool old = m_checked;
0057     m_checked = (toSANE_Word(data.data()) != 0) ? true : false;
0058 
0059     if ((old != m_checked) && ((m_optDesc->cap & SANE_CAP_SOFT_SELECT) == 0)) {
0060         Q_EMIT valueChanged(m_checked);
0061     }
0062 }
0063 
0064 QVariant BoolOption::value() const
0065 {
0066     if (state() == Option::StateHidden) {
0067         return QVariant();
0068     }
0069     return m_checked;
0070 }
0071 
0072 QString BoolOption::valueAsString() const
0073 {
0074     if (state() == Option::StateHidden) {
0075         return QString();
0076     }
0077     if (m_checked) {
0078         return QStringLiteral("true");
0079     } else {
0080         return QStringLiteral("false");
0081     }
0082 }
0083 
0084 } // namespace KSaneCore
0085 
0086 #include "moc_booloption.cpp"