KSaneCore

integeroption.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 "integeroption.h"
9
10#include <QVarLengthArray>
11
12static const int KSW_INT_MAX = 2147483647;
13static const int KSW_INT_MIN = -2147483647 - 1; // prevent warning
14
15namespace KSaneCore
16{
17
18IntegerOption::IntegerOption(const SANE_Handle handle, const int index)
19 : BaseOption(handle, index)
20{
21 m_optionType = Option::TypeInteger;
22}
23
24void IntegerOption::readValue()
25{
26 if (state() == Option::StateHidden) {
27 return;
28 }
29
30 // read that current value
31 QVarLengthArray<unsigned char> data(m_optDesc->size);
32 SANE_Status status;
33 SANE_Int res;
34 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
35 if (status != SANE_STATUS_GOOD) {
36 return;
37 }
38
39 int newValue = toSANE_Word(data.data());
40 if (newValue != m_iVal) {
41 m_iVal = newValue;
42 Q_EMIT valueChanged(m_iVal);
43 }
44}
45
46QVariant IntegerOption::minimumValue() const
47{
48 QVariant value;
49 if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
50 value = static_cast<int>(m_optDesc->constraint.range->min);
51 } else {
52 value = KSW_INT_MIN;
53 }
54 return value;
55}
56
57QVariant IntegerOption::maximumValue() const
58{
59 QVariant value;
60 if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
61 value = static_cast<int>(m_optDesc->constraint.range->max);
62 } else {
63 value = KSW_INT_MAX;
64 }
65 return value;
66}
67
68QVariant IntegerOption::stepValue() const
69{
70 QVariant value;
71 if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
72 value = static_cast<int>(m_optDesc->constraint.range->quant);
73 // guard against possible broken backends
74 if (value == 0) {
75 value = 1;
76 }
77 } else {
78 value = 1;
79 }
80 return value;
81}
82
83QVariant IntegerOption::value() const
84{
85 QVariant value;
86 if (state() == Option::StateHidden) {
87 return value;
88 }
89 value = m_iVal;
90 return value;
91}
92
93QString IntegerOption::valueAsString() const
94{
95 if (state() == Option::StateHidden) {
96 return QString();
97 }
98 return QString::number(m_iVal);
99}
100
101bool IntegerOption::setValue(const QVariant &val)
102{
103 bool ok;
104 int newValue = val.toInt(&ok);
105 if (ok && newValue != m_iVal) {
106 unsigned char data[4];
107 m_iVal = newValue;
108 fromSANE_Word(data, newValue);
109 writeData(data);
110 Q_EMIT valueChanged(m_iVal);
111 }
112 return ok;
113}
114
115} // namespace KSaneCore
116
117#include "moc_integeroption.cpp"
Q_SCRIPTABLE CaptureState status()
QString number(double n, char format, int precision)
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:57:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.