KSaneCore

booloption.cpp
1/*
2 * SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
3 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "booloption.h"
9
10#include <QVarLengthArray>
11
12#include <ksanecore_debug.h>
13
14namespace KSaneCore
15{
16
17BoolOption::BoolOption(const SANE_Handle handle, const int index)
18 : BaseOption(handle, index)
19{
20 m_optionType = Option::TypeBool;
21}
22
23bool BoolOption::setValue(const QVariant &value)
24{
25 if (state() == Option::StateHidden) {
26 return false;
27 }
28
29 bool toggled = value.toBool();
30
31 if (m_checked != toggled) {
32 unsigned char data[4];
33
34 m_checked = toggled;
35 fromSANE_Word(data, (toggled) ? 1 : 0);
36 writeData(data);
37 Q_EMIT valueChanged(m_checked);
38 }
39 return true;
40}
41
42void BoolOption::readValue()
43{
44 if (state() == Option::StateHidden) {
45 return;
46 }
47
48 // read the current value
49 QVarLengthArray<unsigned char> data(m_optDesc->size);
50 SANE_Status status;
51 SANE_Int res;
52 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
53 if (status != SANE_STATUS_GOOD) {
54 return;
55 }
56 bool old = m_checked;
57 m_checked = (toSANE_Word(data.data()) != 0) ? true : false;
58
59 if ((old != m_checked) && ((m_optDesc->cap & SANE_CAP_SOFT_SELECT) == 0)) {
60 Q_EMIT valueChanged(m_checked);
61 }
62}
63
64QVariant BoolOption::value() const
65{
66 if (state() == Option::StateHidden) {
67 return QVariant();
68 }
69 return m_checked;
70}
71
72QString BoolOption::valueAsString() const
73{
74 if (state() == Option::StateHidden) {
75 return QString();
76 }
77 if (m_checked) {
78 return QStringLiteral("true");
79 } else {
80 return QStringLiteral("false");
81 }
82}
83
84} // namespace KSaneCore
85
86#include "moc_booloption.cpp"
Q_SCRIPTABLE CaptureState status()
bool toBool() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.