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;
132 success = setValue(value.
toString());
134 success = setValue(value.
toDouble());
140QVariant ListOption::minimumValue()
const
143 if (BaseOption::state() == Option::StateHidden) {
148 switch (m_optDesc->type) {
150 iValueMin =
static_cast<int>(m_optDesc->constraint.word_list[1]);
151 for (
int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) {
152 iValueMin = qMin(
static_cast<int>(m_optDesc->constraint.word_list[i]), iValueMin);
156 case SANE_TYPE_FIXED:
157 dValueMin = SANE_UNFIX(m_optDesc->constraint.word_list[1]);
158 for (
int i = 2; i <= m_optDesc->constraint.word_list[0]; i++) {
159 dValueMin = qMin(SANE_UNFIX(m_optDesc->constraint.word_list[i]), dValueMin);
164 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
172 if (BaseOption::state() == Option::StateHidden) {
175 return m_currentValue;
178bool ListOption::setValue(
double value)
180 unsigned char data[4];
186 switch (m_optDesc->type) {
188 tmp =
static_cast<double>(m_optDesc->constraint.word_list[minIndex]);
189 minDiff = qAbs(value - tmp);
190 for (i = 2; i <= m_optDesc->constraint.word_list[0]; ++i) {
191 tmp =
static_cast<double>(m_optDesc->constraint.word_list[i]);
192 if (qAbs(value - tmp) < minDiff) {
193 minDiff = qAbs(value - tmp);
197 fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
200 return (minDiff < 1.0);
201 case SANE_TYPE_FIXED:
202 tmp = SANE_UNFIX(m_optDesc->constraint.word_list[minIndex]);
203 minDiff = qAbs(value - tmp);
204 for (i = 2; i <= m_optDesc->constraint.word_list[0]; ++i) {
205 tmp = SANE_UNFIX(m_optDesc->constraint.word_list[i]);
206 if (qAbs(value - tmp) < minDiff) {
207 minDiff = qAbs(value - tmp);
211 fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
214 return (minDiff < 1.0);
216 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
222QString ListOption::valueAsString()
const
224 if (BaseOption::state() == Option::StateHidden) {
227 return m_currentValue.toString();
230bool ListOption::setValue(
const QString &value)
232 if (BaseOption::state() == Option::StateHidden) {
236 unsigned char data[4];
237 void* data_ptr =
nullptr;
244 switch (m_optDesc->type) {
246 i = value.
toInt(&ok);
248 fromSANE_Word(data, i);
255 case SANE_TYPE_FIXED:
259 fromSANE_Word(data, fixed);
266 case SANE_TYPE_STRING:
268 while (m_optDesc->constraint.string_list[i] !=
nullptr) {
271 tmp = sane_i18n(m_optDesc->constraint.string_list[i]);
274 data_ptr = (
void *)m_optDesc->constraint.string_list[i];
279 if (m_optDesc->constraint.string_list[i] ==
nullptr) {
284 qCDebug(KSANECORE_LOG) <<
"can only handle SANE_TYPE: INT, FIXED and STRING";
293void ListOption::countEntries()
297 switch (m_optDesc->type) {
300 case SANE_TYPE_FIXED:
301 m_entriesCount = m_optDesc->constraint.word_list[0];
304 case SANE_TYPE_STRING:
305 while (m_optDesc->constraint.string_list[m_entriesCount] !=
nullptr) {
311 qCDebug(KSANECORE_LOG) <<
"can not handle type:" << m_optDesc->type;
316Option::OptionState ListOption::state()
const
318 if (m_entriesCount <= 1) {
319 return Option::StateHidden;
321 return BaseOption::state();
327#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