KSaneCore

doubleoption.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 * SPDX-FileCopyrightText: 2021 Alexander Stippich <a.stippich@gmx.net>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "doubleoption.h"
10
11#include <QVarLengthArray>
12
13#include <ksanecore_debug.h>
14
15static const double FIXED_MAX = 32767.9999;
16static const double FIXED_MIN = -32768.0;
17static const double MIN_FIXED_STEP = 0.0001;
18static const double FIXED_PRECISION = 1.0 / 65536;
19
20namespace KSaneCore
21{
22
23DoubleOption::DoubleOption(const SANE_Handle handle, const int index)
24 : BaseOption(handle, index)
25{
26 m_optionType = Option::TypeDouble;
27}
28
29void DoubleOption::readOption()
30{
31 beginOptionReload();
32
33 double step = MIN_FIXED_STEP;
34 if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
35 step = SANE_UNFIX(m_optDesc->constraint.range->quant);
36 if (step < MIN_FIXED_STEP) {
37 step = MIN_FIXED_STEP;
38 }
39 }
40 m_minChange = step;
41
42 endOptionReload();
43}
44
45void DoubleOption::readValue()
46{
47 if (state() == Option::StateHidden) {
48 return;
49 }
50
51 // read that current value
52 QVarLengthArray<unsigned char> data(m_optDesc->size);
53 SANE_Status status;
54 SANE_Int res;
55 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
56 if (status != SANE_STATUS_GOOD) {
57 return;
58 }
59
60 double newValue = SANE_UNFIX(toSANE_Word(data.data()));
61 if (abs(newValue - m_value) >= FIXED_PRECISION) {
62 m_value = newValue;
63 Q_EMIT valueChanged(m_value);
64 }
65}
66
67bool DoubleOption::setValue(const QVariant &value)
68{
69 if (state() == Option::StateHidden) {
70 return false;
71 }
72 bool ok;
73 double newValue = value.toDouble(&ok);
74 if (ok && abs(newValue - m_value) >= m_minChange) {
75 unsigned char data[4];
76 SANE_Word fixed;
77 //qCDebug(KSANE_LOG) <<m_optDesc->name << fVal << "!=" << val;
78 m_value = newValue;
79 fixed = SANE_FIX(newValue);
80 fromSANE_Word(data, fixed);
81 writeData(data);
82 Q_EMIT valueChanged(m_value);
83 }
84 return ok;
85}
86
87QVariant DoubleOption::minimumValue() const
88{
89 QVariant value;
90 if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
91 value = SANE_UNFIX(m_optDesc->constraint.range->min);
92 } else {
93 value = FIXED_MIN;
94 }
95 return value;
96}
97
98QVariant DoubleOption::maximumValue() const
99{
100 QVariant value;
101 if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
102 value = SANE_UNFIX(m_optDesc->constraint.range->max);
103 } else {
104 value = FIXED_MAX;
105 }
106 return value;
107}
108
109QVariant DoubleOption::stepValue() const
110{
111 QVariant value;
112 if (m_optDesc->constraint_type == SANE_CONSTRAINT_RANGE) {
113 value = SANE_UNFIX(m_optDesc->constraint.range->quant);
114 /* work around broken backends, the step value must never be zero
115 * assume a minimum step value of 0.1 */
116 if (value == 0) {
117 value = 0.1;
118 }
119 } else {
120 value = MIN_FIXED_STEP;
121 }
122 return value;
123}
124
125QVariant DoubleOption::value() const
126{
127 if (state() == Option::StateHidden) {
128 return QVariant();
129 }
130 return QVariant(m_value);
131}
132
133QString DoubleOption::valueAsString() const
134{
135 if (state() == Option::StateHidden) {
136 return QString();
137 }
138 return QString::number(m_value, 'F', 6);
139}
140
141} // namespace KSaneCore
142
143#include "moc_doubleoption.cpp"
Q_SCRIPTABLE CaptureState status()
QString number(double n, char format, int precision)
QTextStream & fixed(QTextStream &stream)
double toDouble(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.