• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegraphics API Reference
  • KDE Home
  • Contact Us
 

libs/libksane/libksane

  • sources
  • kde-4.14
  • kdegraphics
  • libs
  • libksane
  • libksane
  • options
ksane_option.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the KDE project
4  *
5  * Date : 2009-01-21
6  * Description : Sane interface for KDE
7  *
8  * Copyright (C) 2009 by Kare Sars <kare dot sars at iki dot fi>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) version 3, or any
14  * later version accepted by the membership of KDE e.V. (or its
15  * successor approved by the membership of KDE e.V.), which shall
16  * act as a proxy defined in Section 6 of version 3 of the license.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this program. If not, see <http://www.gnu.org/licenses/>.
25  *
26  * ============================================================ */
27 // Local includes
28 #include "ksane_option.h"
29 #include "ksane_option.moc"
30 
31 // KDE includes
32 #include <KDebug>
33 #include <KLocale>
34 
35 #include "ksane_option_widget.h"
36 
37 namespace KSaneIface
38 {
39 
40 KSaneOption::KSaneOption(const SANE_Handle handle, const int index)
41  : QObject(), m_handle(handle), m_index(index)
42 {
43  m_widget = 0;
44  m_data = 0;
45  readOption();
46 }
47 
48 KSaneOption::~KSaneOption()
49 {
50  if (m_data) {
51  free(m_data);
52  m_data = 0;
53  }
54  // delete the frame, just in case if no parent is set
55  delete m_widget;
56  m_widget = 0;
57 }
58 
59 void KSaneOption::createWidget(QWidget *parent)
60 {
61  if (!m_widget) {
62  m_widget = new KSaneOptionWidget(parent, "");
63  }
64 
65  if (m_optDesc) {
66  m_widget->setToolTip(i18n(m_optDesc->desc));
67  }
68 
69  readOption();
70  readValue();
71 }
72 
73 void KSaneOption::readOption()
74 {
75  m_optDesc = sane_get_option_descriptor(m_handle, m_index);
76  updateVisibility();
77 }
78 
79 void KSaneOption::updateVisibility()
80 {
81  if (!m_widget) return;
82 
83  if (state() == STATE_HIDDEN) {
84  m_widget->hide();
85  }
86  else {
87  m_widget->show();
88  m_widget->setEnabled(state() == STATE_SHOWN);
89  }
90 }
91 
92 KSaneOption::KSaneOptWState KSaneOption::state()
93 {
94  if (!m_optDesc) return STATE_HIDDEN;
95 
96  if (((m_optDesc->cap & SANE_CAP_SOFT_DETECT) == 0) ||
97  (m_optDesc->cap & SANE_CAP_INACTIVE) ||
98  ((m_optDesc->size == 0) && (optionType(m_optDesc) != TYPE_BUTTON)))
99  {
100  return STATE_HIDDEN;
101  }
102  else if ((m_optDesc->cap & SANE_CAP_SOFT_SELECT) == 0) {
103  return STATE_DISABLED;
104  }
105  return STATE_SHOWN;
106 }
107 
108 bool KSaneOption::needsPolling()
109 {
110  if (!m_optDesc) return false;
111 
112  if ((m_optDesc->cap & SANE_CAP_SOFT_DETECT) && !(m_optDesc->cap & SANE_CAP_SOFT_SELECT)) {
113  kDebug() << name() << "optDesc->cap =" << m_optDesc->cap;
114  return true;
115  }
116 
117  return false;
118 }
119 
120 QString KSaneOption::name()
121 {
122  if (m_optDesc == 0) return QString("");
123  return QString(m_optDesc->name);
124 }
125 
126 bool KSaneOption::writeData(void *data)
127 {
128  SANE_Status status;
129  SANE_Int res;
130 
131  if (state() == STATE_DISABLED) {
132  return false;
133  }
134 
135  status = sane_control_option (m_handle, m_index, SANE_ACTION_SET_VALUE, data, &res);
136  if (status != SANE_STATUS_GOOD) {
137  kDebug() << m_optDesc->name << "sane_control_option returned:" << sane_strstatus(status);
138  // write failed. re read the current setting
139  readValue();
140  return false;
141  }
142  if ((res & SANE_INFO_INEXACT) && (m_widget != 0)) {
143  //kDebug() << "write was inexact. Reload value just in case...";
144  readValue();
145  }
146 
147  if (res & SANE_INFO_RELOAD_OPTIONS) {
148  emit optsNeedReload();
149  // optReload reloads also the values
150  }
151  else if (res & SANE_INFO_RELOAD_PARAMS) {
152  // 'else if' because with optReload we force also valReload :)
153  emit valsNeedReload();
154  }
155 
156  return true;
157 }
158 
159 
160 void KSaneOption::readValue() {}
161 
162 SANE_Word KSaneOption::toSANE_Word(unsigned char *data)
163 {
164  SANE_Word tmp;
165  // if __BYTE_ORDER is not defined we get #if 0 == 0
166  #if __BYTE_ORDER == __LITTLE_ENDIAN
167  tmp = (data[0]&0xff);
168  tmp += ((SANE_Word)(data[1]&0xff))<<8;
169  tmp += ((SANE_Word)(data[2]&0xff))<<16;
170  tmp += ((SANE_Word)(data[3]&0xff))<<24;
171  #else
172  tmp = (data[3]&0xff);
173  tmp += ((SANE_Word)(data[2]&0xff))<<8;
174  tmp += ((SANE_Word)(data[1]&0xff))<<16;
175  tmp += ((SANE_Word)(data[0]&0xff))<<24;
176  #endif
177  return tmp;
178 }
179 
180 void KSaneOption::fromSANE_Word(unsigned char *data, SANE_Word from)
181 {
182  // if __BYTE_ORDER is not defined we get #if 0 == 0
183  #if __BYTE_ORDER == __LITTLE_ENDIAN
184  data[0] = (from & 0x000000FF);
185  data[1] = (from & 0x0000FF00)>>8;
186  data[2] = (from & 0x00FF0000)>>16;
187  data[3] = (from & 0xFF000000)>>24;
188  #else
189  data[3] = (from & 0x000000FF);
190  data[2] = (from & 0x0000FF00)>>8;
191  data[1] = (from & 0x00FF0000)>>16;
192  data[0] = (from & 0xFF000000)>>24;
193  #endif
194 }
195 
196 bool KSaneOption::getMinValue(float &) {return false;}
197 bool KSaneOption::getMaxValue(float &) {return false;}
198 bool KSaneOption::getValue(float &) {return false;}
199 bool KSaneOption::setValue(float) {return false;}
200 bool KSaneOption::getValue(QString &) {return false;}
201 bool KSaneOption::setValue(const QString &) {return false;}
202 int KSaneOption::getUnit() {return m_optDesc->unit;}
203 
204 bool KSaneOption::storeCurrentData()
205 {
206  SANE_Status status;
207  SANE_Int res;
208 
209  // check if we can read the value
210  if (!hasGui()) return false;
211  if (state() == STATE_HIDDEN) return false;
212 
213  // read that current value
214  if (m_data != 0) free(m_data);
215  m_data = (unsigned char *)malloc(m_optDesc->size);
216  status = sane_control_option (m_handle, m_index, SANE_ACTION_GET_VALUE, m_data, &res);
217  if (status != SANE_STATUS_GOOD) {
218  kDebug() << m_optDesc->name << "sane_control_option returned" << status;
219  return false;
220  }
221  return true;
222 }
223 
224 bool KSaneOption::restoreSavedData()
225 {
226  // check if we have saved any data
227  if (m_data == 0) return false;
228 
229  // check if we can write the value
230  if (!hasGui()) return false;
231  if (state() == STATE_HIDDEN) return false;
232  if (state() == STATE_DISABLED) return false;
233 
234  writeData(m_data);
235  readValue();
236  return true;
237 }
238 
239 KSaneOption::KSaneOptType KSaneOption::optionType(const SANE_Option_Descriptor *optDesc)
240 {
241  if (!optDesc) return TYPE_DETECT_FAIL;
242 
243  switch (optDesc->constraint_type) {
244  case SANE_CONSTRAINT_NONE:
245  switch(optDesc->type)
246  {
247  case SANE_TYPE_BOOL:
248  return TYPE_CHECKBOX;
249  case SANE_TYPE_INT:
250  if (optDesc->size == sizeof(SANE_Word)) return TYPE_SLIDER;
251  kDebug() << "Can not handle:"<< optDesc->title;
252  kDebug() << "SANE_CONSTRAINT_NONE && SANE_TYPE_INT";
253  kDebug() << "size" << optDesc->size<< "!= sizeof(SANE_Word)";
254  break;
255  case SANE_TYPE_FIXED:
256  if (optDesc->size == sizeof(SANE_Word)) return TYPE_F_SLIDER;
257  kDebug() << "Can not handle:"<< optDesc->title;
258  kDebug() << "SANE_CONSTRAINT_NONE && SANE_TYPE_FIXED";
259  kDebug() << "size" << optDesc->size<< "!= sizeof(SANE_Word)";
260  break;
261  case SANE_TYPE_BUTTON:
262  return TYPE_BUTTON;
263  case SANE_TYPE_STRING:
264  return TYPE_ENTRY;
265  case SANE_TYPE_GROUP:
266  return TYPE_DETECT_FAIL;
267  }
268  break;
269  case SANE_CONSTRAINT_RANGE:
270  switch(optDesc->type) {
271  case SANE_TYPE_BOOL:
272  return TYPE_CHECKBOX;
273  case SANE_TYPE_INT:
274  if (optDesc->size == sizeof(SANE_Word)) return TYPE_SLIDER;
275 
276  if ((strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR) == 0) ||
277  (strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR_R) == 0) ||
278  (strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR_G) == 0) ||
279  (strcmp(optDesc->name, SANE_NAME_GAMMA_VECTOR_B) == 0))
280  {
281  return TYPE_GAMMA;
282  }
283  kDebug() << "Can not handle:"<< optDesc->title;
284  kDebug() << "SANE_CONSTRAINT_RANGE && SANE_TYPE_INT && !SANE_NAME_GAMMA_VECTOR...";
285  kDebug() << "size" << optDesc->size<< "!= sizeof(SANE_Word)";
286  break;
287  case SANE_TYPE_FIXED:
288  if (optDesc->size == sizeof(SANE_Word)) return TYPE_F_SLIDER;
289  kDebug() << "Can not handle:"<< optDesc->title;
290  kDebug() << "SANE_CONSTRAINT_RANGE && SANE_TYPE_FIXED";
291  kDebug() << "size" << optDesc->size<< "!= sizeof(SANE_Word)";
292  kDebug() << "Analog Gamma vector?";
293  break;
294  case SANE_TYPE_STRING:
295  kDebug() << "Can not handle:" << optDesc->title;
296  kDebug() << "SANE_CONSTRAINT_RANGE && SANE_TYPE_STRING";
297  return TYPE_DETECT_FAIL;
298  case SANE_TYPE_BUTTON:
299  return TYPE_BUTTON;
300  case SANE_TYPE_GROUP:
301  return TYPE_DETECT_FAIL;
302  }
303  break;
304  case SANE_CONSTRAINT_WORD_LIST:
305  case SANE_CONSTRAINT_STRING_LIST:
306  return TYPE_COMBO;
307  }
308  return TYPE_DETECT_FAIL;
309 }
310 
311 KLocalizedString KSaneOption::unitString()
312 {
313  switch(m_optDesc->unit)
314  {
315  case SANE_UNIT_NONE: return KLocalizedString();
316  case SANE_UNIT_PIXEL: return ki18ncp("SpinBox parameter unit", " Pixel", " Pixels");
317  case SANE_UNIT_BIT: return ki18ncp("SpinBox parameter unit", " Bit", " Bits");
318  case SANE_UNIT_MM: return ki18nc("SpinBox parameter unit (Millimeter)", " mm");
319  case SANE_UNIT_DPI: return ki18nc("SpinBox parameter unit (Dots Per Inch)", " DPI");
320  case SANE_UNIT_PERCENT: return ki18nc("SpinBox parameter unit (Percentage)", " %");
321  case SANE_UNIT_MICROSECOND: return ki18nc("SpinBox parameter unit (Microseconds)", " µs");
322  }
323  return KLocalizedString();
324 }
325 
326 QString KSaneOption::unitDoubleString()
327 {
328  switch(m_optDesc->unit)
329  {
330  case SANE_UNIT_NONE: return QString("");
331  case SANE_UNIT_PIXEL: return i18nc("Double numbers. SpinBox parameter unit", " Pixels");
332  case SANE_UNIT_BIT: return i18nc("Double numbers. SpinBox parameter unit", " Bits");
333  case SANE_UNIT_MM: return i18nc("Double numbers. SpinBox parameter unit (Millimeter)", " mm");
334  case SANE_UNIT_DPI: return i18nc("Double numbers. SpinBox parameter unit (Dots Per Inch)", " DPI");
335  case SANE_UNIT_PERCENT: return i18nc("Double numbers. SpinBox parameter unit (Percentage)", " %");
336  case SANE_UNIT_MICROSECOND: return i18nc("Double numbers. SpinBox parameter unit (Microseconds)", " µs");
337  }
338  return QString("");
339 }
340 
341 
342 
343 } // NameSpace KSaneIface
QWidget
KSaneIface::KSaneOption::optsNeedReload
void optsNeedReload()
KSaneIface::KSaneOption::TYPE_GAMMA
Definition: ksane_option.h:63
KSaneIface::KSaneOption::optionType
static KSaneOptType optionType(const SANE_Option_Descriptor *optDesc)
Definition: ksane_option.cpp:239
KSaneIface::KSaneOption::storeCurrentData
bool storeCurrentData()
Definition: ksane_option.cpp:204
KSaneIface::KSaneOption::createWidget
virtual void createWidget(QWidget *parent)
Definition: ksane_option.cpp:59
KSaneIface::KSaneOption::m_index
int m_index
Definition: ksane_option.h:114
KSaneIface::KSaneOption::fromSANE_Word
void fromSANE_Word(unsigned char *data, SANE_Word from)
Definition: ksane_option.cpp:180
KSaneIface::KSaneOption::toSANE_Word
SANE_Word toSANE_Word(unsigned char *data)
Definition: ksane_option.cpp:162
KSaneIface::KSaneOption::TYPE_F_SLIDER
Definition: ksane_option.h:60
ksane_option.h
KSaneIface::KSaneOption::STATE_SHOWN
Definition: ksane_option.h:71
KSaneIface::KSaneOption::TYPE_COMBO
Definition: ksane_option.h:61
KSaneIface::KSaneOption::hasGui
virtual bool hasGui()
Definition: ksane_option.h:79
KSaneIface::KSaneOption::m_data
unsigned char * m_data
Definition: ksane_option.h:116
KSaneIface::KSaneOption::needsPolling
bool needsPolling()
Definition: ksane_option.cpp:108
KSaneIface::KSaneOption::state
KSaneOptWState state()
Definition: ksane_option.cpp:92
QWidget::setEnabled
void setEnabled(bool)
KSaneIface::KSaneOption::m_optDesc
const SANE_Option_Descriptor * m_optDesc
This pointer is provided by sane.
Definition: ksane_option.h:115
KSaneIface::KSaneOption::m_handle
SANE_Handle m_handle
Definition: ksane_option.h:113
KSaneIface::KSaneOption::TYPE_SLIDER
Definition: ksane_option.h:59
QObject
KSaneIface::KSaneOptionWidget
A wrapper for a checkbox.
Definition: ksane_option_widget.h:45
KSaneIface::KSaneOption::setValue
virtual bool setValue(float val)
Definition: ksane_option.cpp:199
KSaneIface::KSaneOption::TYPE_ENTRY
Definition: ksane_option.h:62
KSaneIface::KSaneOption::TYPE_DETECT_FAIL
Definition: ksane_option.h:57
KSaneIface::KSaneOption::KSaneOptWState
KSaneOptWState
Definition: ksane_option.h:67
KSaneIface::KSaneOption::getMaxValue
virtual bool getMaxValue(float &max)
Definition: ksane_option.cpp:197
ksane_option_widget.h
QString
QWidget::hide
void hide()
KSaneIface::KSaneOption::~KSaneOption
~KSaneOption()
Definition: ksane_option.cpp:48
KSaneIface::KSaneOption::KSaneOptType
KSaneOptType
Definition: ksane_option.h:55
KSaneIface::KSaneOption::getUnit
virtual int getUnit()
Definition: ksane_option.cpp:202
KSaneIface::KSaneOption::restoreSavedData
bool restoreSavedData()
Definition: ksane_option.cpp:224
KSaneIface::KSaneOption::STATE_DISABLED
Definition: ksane_option.h:70
KSaneIface::KSaneOption::getMinValue
virtual bool getMinValue(float &max)
Definition: ksane_option.cpp:196
KSaneIface::KSaneOption::STATE_HIDDEN
Definition: ksane_option.h:69
KSaneIface::KSaneOption::readValue
virtual void readValue()
Definition: ksane_option.cpp:160
KSaneIface::KSaneOption::writeData
bool writeData(void *data)
Definition: ksane_option.cpp:126
KSaneIface::KSaneOption::readOption
virtual void readOption()
Definition: ksane_option.cpp:73
KSaneIface::KSaneOption::TYPE_CHECKBOX
Definition: ksane_option.h:58
KSaneIface::KSaneOption::KSaneOption
KSaneOption(const SANE_Handle handle, const int index)
Definition: ksane_option.cpp:40
KSaneIface::KSaneOption::TYPE_BUTTON
Definition: ksane_option.h:64
KSaneIface::KSaneOption::updateVisibility
void updateVisibility()
Definition: ksane_option.cpp:79
KSaneIface::KSaneOption::m_widget
KSaneOptionWidget * m_widget
Definition: ksane_option.h:117
KSaneIface::KSaneOption::unitDoubleString
QString unitDoubleString()
Definition: ksane_option.cpp:326
QWidget::show
void show()
KSaneIface::KSaneOption::unitString
KLocalizedString unitString()
Definition: ksane_option.cpp:311
QWidget::setToolTip
void setToolTip(const QString &)
KSaneIface::KSaneOption::valsNeedReload
void valsNeedReload()
KSaneIface::KSaneOption::name
QString name()
Definition: ksane_option.cpp:120
KSaneIface::KSaneOption::getValue
virtual bool getValue(float &val)
Definition: ksane_option.cpp:198
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:19:47 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libs/libksane/libksane

Skip menu "libs/libksane/libksane"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegraphics API Reference

Skip menu "kdegraphics API Reference"
  •     libkdcraw
  •     libkexiv2
  •     libkipi
  •     libksane
  • okular

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal