12#include <ksanecore_debug.h>
17BaseOption::BaseOption() :
QObject()
21BaseOption::BaseOption(
const SANE_Handle handle,
const int index)
22 :
QObject(), m_handle(handle), m_index(index)
26BaseOption::~BaseOption()
34void BaseOption::readOption()
40void BaseOption::beginOptionReload()
42 if (m_handle !=
nullptr) {
43 m_optDesc = sane_get_option_descriptor(m_handle, m_index);
47void BaseOption::endOptionReload()
49 Q_EMIT optionReloaded();
52Option::OptionState BaseOption::state()
const
54 if (m_optDesc ==
nullptr) {
55 return Option::StateHidden;
58 if (((m_optDesc->cap & SANE_CAP_SOFT_DETECT) == 0) || (m_optDesc->cap & SANE_CAP_INACTIVE) || ((m_optDesc->size == 0) && (
type() != Option::TypeAction))) {
59 return Option::StateHidden;
60 }
else if ((m_optDesc->cap & SANE_CAP_SOFT_SELECT) == 0) {
61 return Option::StateDisabled;
63 return Option::StateActive;
66bool BaseOption::needsPolling()
const
72 if ((m_optDesc->cap & SANE_CAP_SOFT_DETECT) && !(m_optDesc->cap & SANE_CAP_SOFT_SELECT)) {
73 qCDebug(KSANECORE_LOG) <<
name() <<
"optDesc->cap =" << m_optDesc->cap;
82 if (m_optDesc ==
nullptr) {
88QString BaseOption::title()
const
90 if (m_optDesc ==
nullptr) {
93 return sane_i18n(m_optDesc->title);
96QString BaseOption::description()
const
98 if (m_optDesc ==
nullptr) {
101 return sane_i18n(m_optDesc->desc);
104Option::OptionType BaseOption::type()
const
109bool BaseOption::writeData(
void *data)
114 if (state() == Option::StateDisabled) {
118 status = sane_control_option(m_handle, m_index, SANE_ACTION_SET_VALUE, data, &res);
119 if (
status != SANE_STATUS_GOOD) {
120 qCDebug(KSANECORE_LOG) << m_optDesc->name <<
"sane_control_option returned:" << sane_strstatus(
status);
125 if (res & SANE_INFO_INEXACT) {
130 if (res & SANE_INFO_RELOAD_OPTIONS) {
131 Q_EMIT optionsNeedReload();
133 }
else if (res & SANE_INFO_RELOAD_PARAMS) {
135 Q_EMIT valuesNeedReload();
141void BaseOption::readValue() {}
143SANE_Word BaseOption::toSANE_Word(
unsigned char *data)
147#if __BYTE_ORDER == __LITTLE_ENDIAN
148 tmp = (data[0] & 0xff);
149 tmp += ((SANE_Word)(data[1] & 0xff)) << 8;
150 tmp += ((SANE_Word)(data[2] & 0xff)) << 16;
151 tmp += ((SANE_Word)(data[3] & 0xff)) << 24;
153 tmp = (data[3] & 0xff);
154 tmp += ((SANE_Word)(data[2] & 0xff)) << 8;
155 tmp += ((SANE_Word)(data[1] & 0xff)) << 16;
156 tmp += ((SANE_Word)(data[0] & 0xff)) << 24;
161void BaseOption::fromSANE_Word(
unsigned char *data, SANE_Word from)
164#if __BYTE_ORDER == __LITTLE_ENDIAN
165 data[0] = (from & 0x000000FF);
166 data[1] = (from & 0x0000FF00) >> 8;
167 data[2] = (from & 0x00FF0000) >> 16;
168 data[3] = (from & 0xFF000000) >> 24;
170 data[3] = (from & 0x000000FF);
171 data[2] = (from & 0x0000FF00) >> 8;
172 data[1] = (from & 0x00FF0000) >> 16;
173 data[0] = (from & 0xFF000000) >> 24;
182QVariant BaseOption::minimumValue()
const
187QVariant BaseOption::maximumValue()
const
192QVariant BaseOption::stepValue()
const
197QVariantList BaseOption::valueList()
const
199 return QVariantList();
202QVariantList BaseOption::internalValueList()
const
204 return QVariantList();
207Option::OptionUnit BaseOption::valueUnit()
const
209 if (m_optDesc !=
nullptr) {
210 switch (m_optDesc->unit) {
211 case SANE_UNIT_PIXEL:
212 return Option::UnitPixel;
214 return Option::UnitBit;
216 return Option::UnitMilliMeter;
218 return Option::UnitDPI;
219 case SANE_UNIT_PERCENT:
220 return Option::UnitPercent;
221 case SANE_UNIT_MICROSECOND:
222 return Option::UnitMicroSecond;
224 return Option::UnitNone;
227 return Option::UnitNone;
231int BaseOption::valueSize()
const
233 if (m_optDesc !=
nullptr) {
234 return m_optDesc->size /
sizeof(SANE_Word);
239QString BaseOption::valueAsString()
const
244bool BaseOption::setValue(
const QVariant &)
249bool BaseOption::storeCurrentData()
255 if (state() == Option::StateHidden) {
260 if (m_data !=
nullptr) {
263 m_data = (
unsigned char *)malloc(m_optDesc->size);
264 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, m_data, &res);
265 if (
status != SANE_STATUS_GOOD) {
266 qCDebug(KSANECORE_LOG) << m_optDesc->name <<
"sane_control_option returned" <<
status;
272bool BaseOption::restoreSavedData()
275 if (m_data ==
nullptr) {
280 if (state() == Option::StateHidden) {
283 if (state() == Option::StateDisabled) {
292Option::OptionType BaseOption::optionType(
const SANE_Option_Descriptor *optDesc)
295 return Option::TypeDetectFail;
298 switch (optDesc->constraint_type) {
299 case SANE_CONSTRAINT_NONE:
300 switch (optDesc->type) {
302 return Option::TypeBool;
304 if (optDesc->size ==
sizeof(SANE_Word)) {
305 return Option::TypeInteger;
307 qCDebug(KSANECORE_LOG) <<
"Can not handle:" << optDesc->title;
308 qCDebug(KSANECORE_LOG) <<
"SANE_CONSTRAINT_NONE && SANE_TYPE_INT";
309 qCDebug(KSANECORE_LOG) <<
"size" << optDesc->size <<
"!= sizeof(SANE_Word)";
311 case SANE_TYPE_FIXED:
312 if (optDesc->size ==
sizeof(SANE_Word)) {
313 return Option::TypeDouble;
315 qCDebug(KSANECORE_LOG) <<
"Can not handle:" << optDesc->title;
316 qCDebug(KSANECORE_LOG) <<
"SANE_CONSTRAINT_NONE && SANE_TYPE_FIXED";
317 qCDebug(KSANECORE_LOG) <<
"size" << optDesc->size <<
"!= sizeof(SANE_Word)";
319 case SANE_TYPE_BUTTON:
320 return Option::TypeAction;
321 case SANE_TYPE_STRING:
322 return Option::TypeString;
323 case SANE_TYPE_GROUP:
324 return Option::TypeDetectFail;
327 case SANE_CONSTRAINT_RANGE:
328 switch (optDesc->type) {
330 return Option::TypeBool;
332 if (optDesc->size ==
sizeof(SANE_Word)) {
333 return Option::TypeInteger;
336 if ((strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR) == 0) ||
337 (strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR_R) == 0) ||
338 (strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR_G) == 0) ||
339 (strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR_B) == 0)) {
340 return Option::TypeGamma;
342 qCDebug(KSANECORE_LOG) <<
"Can not handle:" << optDesc->title;
343 qCDebug(KSANECORE_LOG) <<
"SANE_CONSTRAINT_RANGE && SANE_TYPE_INT && !SANE_NAME_GAMMA_VECTOR...";
344 qCDebug(KSANECORE_LOG) <<
"size" << optDesc->size <<
"!= sizeof(SANE_Word)";
346 case SANE_TYPE_FIXED:
347 if (optDesc->size ==
sizeof(SANE_Word)) {
348 return Option::TypeDouble;
350 qCDebug(KSANECORE_LOG) <<
"Can not handle:" << optDesc->title;
351 qCDebug(KSANECORE_LOG) <<
"SANE_CONSTRAINT_RANGE && SANE_TYPE_FIXED";
352 qCDebug(KSANECORE_LOG) <<
"size" << optDesc->size <<
"!= sizeof(SANE_Word)";
353 qCDebug(KSANECORE_LOG) <<
"Analog Gamma vector?";
355 case SANE_TYPE_STRING:
356 qCDebug(KSANECORE_LOG) <<
"Can not handle:" << optDesc->title;
357 qCDebug(KSANECORE_LOG) <<
"SANE_CONSTRAINT_RANGE && SANE_TYPE_STRING";
358 return Option::TypeDetectFail;
359 case SANE_TYPE_BUTTON:
360 return Option::TypeAction;
361 case SANE_TYPE_GROUP:
362 return Option::TypeDetectFail;
365 case SANE_CONSTRAINT_WORD_LIST:
366 case SANE_CONSTRAINT_STRING_LIST:
367 return Option::TypeValueList;
369 return Option::TypeDetectFail;
374#include "moc_baseoption.cpp"
Q_SCRIPTABLE CaptureState status()
Type type(const QSqlDatabase &db)
QString name(StandardAction id)
QString fromUtf8(QByteArrayView str)