10#include <QVarLengthArray>
12#include <ksanecore_debug.h>
17ListOption::ListOption(
const SANE_Handle handle,
const int index)
18 : BaseOption(handle, index)
20 m_optionType = Option::TypeValueList;
23void ListOption::readValue()
25 if (BaseOption::state() == Option::StateHidden) {
33 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
34 if (
status != SANE_STATUS_GOOD) {
39 switch (m_optDesc->type) {
41 newValue =
static_cast<int>(toSANE_Word(data.data()));
44 newValue = SANE_UNFIX(toSANE_Word(data.data()));
46 case SANE_TYPE_STRING:
47 newValue = sane_i18n(
reinterpret_cast<char *
>(data.data()));
53 if (newValue != m_currentValue) {
54 m_currentValue = newValue;
55 Q_EMIT valueChanged(m_currentValue);
59void ListOption::readOption()
66QVariantList ListOption::valueList()
const
72 switch (m_optDesc->type) {
74 for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) {
75 list << static_cast<int>(m_optDesc->constraint.word_list[i]);;
79 for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) {
80 list << SANE_UNFIX(m_optDesc->constraint.word_list[i]);
83 case SANE_TYPE_STRING:
85 while (m_optDesc->constraint.string_list[i] !=
nullptr) {
86 list << sane_i18n(m_optDesc->constraint.string_list[i]);
91 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
97QVariantList ListOption::internalValueList()
const
103 switch (m_optDesc->type) {
105 for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) {
106 list << static_cast<int>(m_optDesc->constraint.word_list[i]);;
109 case SANE_TYPE_FIXED:
110 for (i = 1; i <= m_optDesc->constraint.word_list[0]; ++i) {
111 list << SANE_UNFIX(m_optDesc->constraint.word_list[i]);
114 case SANE_TYPE_STRING:
116 while (m_optDesc->constraint.string_list[i] !=
nullptr) {
122 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
128bool ListOption::setValue(
const QVariant &value)
130 bool success =
false;
131#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
136 success = setValue(value.
toString());
138 success = setValue(value.
toDouble());
144QVariant ListOption::minimumValue()
const
147 if (BaseOption::state() == Option::StateHidden) {
152 switch (m_optDesc->type) {
154 iValueMin =
static_cast<int>(m_optDesc->constraint.word_list[1]);
155 for (
int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) {
156 iValueMin = qMin(
static_cast<int>(m_optDesc->constraint.word_list[i]), iValueMin);
160 case SANE_TYPE_FIXED:
161 dValueMin = SANE_UNFIX(m_optDesc->constraint.word_list[1]);
162 for (
int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) {
163 dValueMin = qMin(SANE_UNFIX(m_optDesc->constraint.word_list[i]), dValueMin);
168 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
176 if (BaseOption::state() == Option::StateHidden) {
179 return m_currentValue;
182bool ListOption::setValue(
double value)
184 unsigned char data[4];
190 switch (m_optDesc->type) {
192 tmp =
static_cast<double>(m_optDesc->constraint.word_list[minIndex]);
193 minDiff = qAbs(value - tmp);
194 for (i = 2; i <= m_optDesc->constraint.word_list[0]; ++i) {
195 tmp =
static_cast<double>(m_optDesc->constraint.word_list[i]);
196 if (qAbs(value - tmp) < minDiff) {
197 minDiff = qAbs(value - tmp);
201 fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
204 return (minDiff < 1.0);
205 case SANE_TYPE_FIXED:
206 tmp = SANE_UNFIX(m_optDesc->constraint.word_list[minIndex]);
207 minDiff = qAbs(value - tmp);
208 for (i = 2; i <= m_optDesc->constraint.word_list[0]; ++i) {
209 tmp = SANE_UNFIX(m_optDesc->constraint.word_list[i]);
210 if (qAbs(value - tmp) < minDiff) {
211 minDiff = qAbs(value - tmp);
215 fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
218 return (minDiff < 1.0);
220 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
226QString ListOption::valueAsString()
const
228 if (BaseOption::state() == Option::StateHidden) {
231 return m_currentValue.toString();
234bool ListOption::setValue(
const QString &value)
236 if (BaseOption::state() == Option::StateHidden) {
240 unsigned char data[4];
241 void* data_ptr =
nullptr;
248 switch (m_optDesc->type) {
250 i = value.
toInt(&ok);
252 fromSANE_Word(data, i);
259 case SANE_TYPE_FIXED:
263 fromSANE_Word(data, fixed);
270 case SANE_TYPE_STRING:
272 while (m_optDesc->constraint.string_list[i] !=
nullptr) {
275 tmp = sane_i18n(m_optDesc->constraint.string_list[i]);
278 data_ptr = (
void *)m_optDesc->constraint.string_list[i];
283 if (m_optDesc->constraint.string_list[i] ==
nullptr) {
288 qCDebug(KSANECORE_LOG) <<
"can only handle SANE_TYPE: INT, FIXED and STRING";
297void ListOption::countEntries()
301 switch (m_optDesc->type) {
304 case SANE_TYPE_FIXED:
305 m_entriesCount = m_optDesc->constraint.word_list[0];
308 case SANE_TYPE_STRING:
309 while (m_optDesc->constraint.string_list[m_entriesCount] !=
nullptr) {
315 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
320Option::OptionState ListOption::state()
const
322 if (m_entriesCount <= 1) {
323 return Option::StateHidden;
325 return BaseOption::state();
331#include "moc_listoption.cpp"
Q_SCRIPTABLE CaptureState status()
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void reserve(qsizetype size)
QString fromLatin1(QByteArrayView str)
double toDouble(bool *ok) const const
int toInt(bool *ok, int base) const const
QTextStream & fixed(QTextStream &stream)
double toDouble(bool *ok) const const
QString toString() const const
int userType() const const