• 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_opt_combo.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_opt_combo.h"
29 #include "ksane_opt_combo.moc"
30 
31 #include "labeled_combo.h"
32 
33 // Qt includes
34 #include <QtCore/QVarLengthArray>
35 
36 // KDE includes
37 #include <kicon.h>
38 #include <KDebug>
39 #include <KLocale>
40 
41 namespace KSaneIface
42 {
43 static const char tmp_binary[] = "Binary";
44 
45 KSaneOptCombo::KSaneOptCombo(const SANE_Handle handle, const int index)
46 : KSaneOption(handle, index), m_combo(0)
47 {
48 }
49 
50 void KSaneOptCombo::createWidget(QWidget *parent)
51 {
52  if (m_widget) return;
53 
54  m_widget = m_combo = new LabeledCombo(parent, "", QStringList());
55  readOption();
56  m_widget->setToolTip(i18n(m_optDesc->desc));
57  connect(m_combo, SIGNAL(activated(int)), this, SLOT(comboboxChangedIndex(int)));
58  readValue();
59 }
60 
61 void KSaneOptCombo::readValue()
62 {
63  if (state() == STATE_HIDDEN) return;
64 
65  // read that current value
66  QVarLengthArray<unsigned char> data(m_optDesc->size);
67  SANE_Status status;
68  SANE_Int res;
69  status = sane_control_option (m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
70  if (status != SANE_STATUS_GOOD) {
71  return;
72  }
73 
74 
75  m_currentText = getSaneComboString(data.data());
76  if (m_combo != 0) {
77  if (m_combo->currentText() != m_currentText) {
78  m_combo->setCurrentText(m_currentText);
79  emit valueChanged();
80  }
81  }
82 }
83 
84 void KSaneOptCombo::readOption()
85 {
86  KSaneOption::readOption();
87 
88  if (!m_combo) return;
89 
90  QString saved = m_combo->currentText();
91 
92  m_strList = genComboStringList();
93  m_combo->clear();
94  m_combo->setLabelText(i18n(m_optDesc->title));
95  m_combo->addItems(m_strList);
96  m_combo->setIcon(KIcon("color"), getSaneComboString((unsigned char*)SANE_VALUE_SCAN_MODE_COLOR));
97  m_combo->setIcon(KIcon("gray-scale"),
98  getSaneComboString((unsigned char*)SANE_VALUE_SCAN_MODE_GRAY));
99  m_combo->setIcon(KIcon("black-white"),
100  getSaneComboString((unsigned char*)SANE_VALUE_SCAN_MODE_LINEART));
101  // The epkowa/epson backend uses "Binary" which is the same as "Lineart"
102  m_combo->setIcon(KIcon("black-white"), i18n(tmp_binary));
103 
104  // set the previous value
105  m_combo->setCurrentText(saved);
106 }
107 
108 
109 QStringList &KSaneOptCombo::genComboStringList()
110 {
111  int i;
112  m_strList.clear();
113 
114  switch (m_optDesc->type)
115  {
116  case SANE_TYPE_INT:
117  for (i=1; i<=m_optDesc->constraint.word_list[0]; ++i) {
118  m_strList += getSaneComboString((int)m_optDesc->constraint.word_list[i]);
119  }
120  break;
121  case SANE_TYPE_FIXED:
122  for (i=1; i<=m_optDesc->constraint.word_list[0]; ++i) {
123  m_strList += getSaneComboString((float)SANE_UNFIX(m_optDesc->constraint.word_list[i]));
124  }
125  break;
126  case SANE_TYPE_STRING:
127  i=0;
128  while (m_optDesc->constraint.string_list[i] != 0) {
129  m_strList += getSaneComboString((unsigned char *)m_optDesc->constraint.string_list[i]);
130  i++;
131  }
132  break;
133  default :
134  m_strList += "NOT HANDELED";
135  break;
136  }
137  return m_strList;
138 }
139 
140 QString KSaneOptCombo::getSaneComboString(int ival)
141 {
142  switch(m_optDesc->unit)
143  {
144  case SANE_UNIT_NONE: break;
145  case SANE_UNIT_PIXEL: return i18np("%1 Pixel","%1 Pixels", ival);
146  case SANE_UNIT_BIT: return i18np("%1 Bit","%1 Bits", ival);
147  case SANE_UNIT_MM: return i18np("%1 mm","%1 mm", ival);
148  case SANE_UNIT_DPI: return i18np("%1 DPI","%1 DPI", ival);
149  case SANE_UNIT_PERCENT: return i18np("%1 %","%1 %", ival);
150  case SANE_UNIT_MICROSECOND: return i18np("%1 µs","%1 µs", ival);
151  }
152  return QString::number(ival);
153 }
154 
155 QString KSaneOptCombo::getSaneComboString(float fval)
156 {
157  switch(m_optDesc->unit)
158  {
159  case SANE_UNIT_NONE: break;
160  case SANE_UNIT_PIXEL: return i18ncp("Parameter and Unit","%1 Pixel", "%1 Pixels", fval);
161  case SANE_UNIT_BIT: return i18ncp("Parameter and Unit","%1 Bit","%1 Bits", fval);
162  case SANE_UNIT_MM: return i18nc("Parameter and Unit (Millimeter)","%1 mm", fval);
163  case SANE_UNIT_DPI: return i18nc("Parameter and Unit (Dots Per Inch)","%1 DPI", fval);
164  case SANE_UNIT_PERCENT: return i18nc("Parameter and Unit (Percentage)","%1 %", fval);
165  case SANE_UNIT_MICROSECOND: return i18nc("Parameter and Unit (Microseconds)","%1 µs", fval);
166  }
167  return QString::number(fval, 'F', 4);
168 }
169 
170 QString KSaneOptCombo::getSaneComboString(unsigned char *data)
171 {
172  QString tmp;
173  if (data == 0) return QString();
174 
175  switch (m_optDesc->type)
176  {
177  case SANE_TYPE_INT:
178  return getSaneComboString((int)toSANE_Word(data));
179  case SANE_TYPE_FIXED:
180  return getSaneComboString((float)SANE_UNFIX(toSANE_Word(data)));
181  case SANE_TYPE_STRING:
182  tmp = i18n(reinterpret_cast<char*>(data));
183  tmp = tmp.simplified();
184  return tmp;
185  default :
186  break;
187  }
188  return QString();
189 }
190 
191 
192 
193 void KSaneOptCombo::comboboxChangedIndex(int i)
194 {
195  if (m_combo && (m_combo->currentText() == m_currentText)) {
196  return;
197  }
198 
199  unsigned char data[4];
200  void *dataPtr;
201 
202  switch (m_optDesc->type)
203  {
204  case SANE_TYPE_INT:
205  case SANE_TYPE_FIXED:
206  fromSANE_Word(data, m_optDesc->constraint.word_list[i+1]);
207  dataPtr = data;
208  break;
209  case SANE_TYPE_STRING:
210  dataPtr = (void *)m_optDesc->constraint.string_list[i];
211  break;
212  default:
213  kDebug() << "can not handle type:" << m_optDesc->type;
214  return;
215  }
216  writeData(dataPtr);
217  readValue();
218  emit valueChanged();
219 }
220 
221 bool KSaneOptCombo::getMinValue(float &val)
222 {
223  if (state() == STATE_HIDDEN) return false;
224  switch (m_optDesc->type)
225  {
226  case SANE_TYPE_INT:
227  val = (float)m_optDesc->constraint.word_list[1];
228  for (int i=2; i<=m_optDesc->constraint.word_list[0]; i++) {
229  val = qMin((float)m_optDesc->constraint.word_list[i], val);
230  }
231  break;
232  case SANE_TYPE_FIXED:
233  val = (float)SANE_UNFIX(m_optDesc->constraint.word_list[1]);
234  for (int i=2; i<=m_optDesc->constraint.word_list[0]; i++) {
235  val = qMin((float)SANE_UNFIX(m_optDesc->constraint.word_list[i]), val);
236  }
237  break;
238  default:
239  kDebug() << "can not handle type:" << m_optDesc->type;
240  return false;
241  }
242  return true;
243 }
244 
245 bool KSaneOptCombo::getValue(float &val)
246 {
247  if (state() == STATE_HIDDEN) return false;
248 
249  // read that current value
250  QVarLengthArray<unsigned char> data(m_optDesc->size);
251  SANE_Status status;
252  SANE_Int res;
253  status = sane_control_option (m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
254  if (status != SANE_STATUS_GOOD) {
255  kDebug() << m_optDesc->name << "sane_control_option returned" << status;
256  return false;
257  }
258 
259  switch (m_optDesc->type)
260  {
261  case SANE_TYPE_INT:
262  val = (float)toSANE_Word(data.data());
263  return true;
264  case SANE_TYPE_FIXED:
265  val = SANE_UNFIX(toSANE_Word(data.data()));
266  return true;
267  default:
268  kDebug() << "Type" << m_optDesc->type << "not supported!";
269  break;
270  }
271  return false;
272 }
273 
274 bool KSaneOptCombo::setValue(float value)
275 {
276  unsigned char data[4];
277  float tmp;
278  float minDiff;
279  int i;
280  int minIndex = 1;
281 
282  switch (m_optDesc->type)
283  {
284  case SANE_TYPE_INT:
285  tmp = (float)m_optDesc->constraint.word_list[minIndex];
286  minDiff = qAbs(value - tmp);
287  for (i=2; i<=m_optDesc->constraint.word_list[0]; ++i) {
288  tmp = (float)m_optDesc->constraint.word_list[i];
289  if (qAbs(value - tmp) < minDiff) {
290  minDiff = qAbs(value - tmp);
291  minIndex = i;
292  }
293  }
294  fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
295  writeData(data);
296  readValue();
297  return (minDiff < 1.0);
298  case SANE_TYPE_FIXED:
299  tmp = (float)SANE_UNFIX(m_optDesc->constraint.word_list[minIndex]);
300  minDiff = qAbs(value - tmp);
301  for (i=2; i<=m_optDesc->constraint.word_list[0]; ++i) {
302  tmp = (float)SANE_UNFIX(m_optDesc->constraint.word_list[i]);
303  if (qAbs(value - tmp) < minDiff) {
304  minDiff = qAbs(value - tmp);
305  minIndex = i;
306  }
307  }
308  fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
309  writeData(data);
310  readValue();
311  return (minDiff < 1.0);
312  default:
313  kDebug() << "can not handle type:" << m_optDesc->type;
314  break;
315  }
316  return false;
317 }
318 
319 bool KSaneOptCombo::getValue(QString &val)
320 {
321  if (state() == STATE_HIDDEN) return false;
322  val = m_currentText;
323  return true;
324 }
325 
326 bool KSaneOptCombo::setValue(const QString &val)
327 {
328  if (state() == STATE_HIDDEN) return false;
329  if (val == m_currentText) return true;
330 
331  unsigned char data[4];
332  void *data_ptr;
333  SANE_Word fixed;
334  int i;
335  float f;
336  bool ok;
337  QString tmp;
338 
339  switch (m_optDesc->type)
340  {
341  case SANE_TYPE_INT:
342  tmp = val.left(val.indexOf(' ')); // strip the unit
343  // accept float formating of the string
344  i = (int)(tmp.toFloat(&ok));
345  if (ok == false) return false;
346  fromSANE_Word(data, i);
347  data_ptr = data;
348  break;
349  case SANE_TYPE_FIXED:
350  tmp = val.left(val.indexOf(' ')); // strip the unit
351  f = tmp.toFloat(&ok);
352  if (ok == false) return false;
353  fixed = SANE_FIX(f);
354  fromSANE_Word(data, fixed);
355  data_ptr = data;
356  break;
357  case SANE_TYPE_STRING:
358  i = 0;
359  while (m_optDesc->constraint.string_list[i] != 0) {
360  tmp = getSaneComboString((unsigned char *)m_optDesc->constraint.string_list[i]);
361  if (val == tmp) {
362  data_ptr = (void *)m_optDesc->constraint.string_list[i];
363  break;
364  }
365  i++;
366  }
367  if (m_optDesc->constraint.string_list[i] == 0) return false;
368  break;
369  default:
370  kDebug() << "can only handle SANE_TYPE: INT, FIXED and STRING";
371  return false;
372  }
373  writeData(data_ptr);
374 
375  readValue();
376  return true;
377 }
378 
379 
380 } // NameSpace KSaneIface
labeled_combo.h
QList::clear
void clear()
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QWidget
KSaneIface::KSaneOptCombo::KSaneOptCombo
KSaneOptCombo(const SANE_Handle handle, const int index)
Definition: ksane_opt_combo.cpp:45
KSaneIface::KSaneOptionWidget::setLabelText
void setLabelText(const QString &text)
Definition: ksane_option_widget.cpp:54
KSaneIface::KSaneOptCombo::readValue
void readValue()
Definition: ksane_opt_combo.cpp:61
KSaneIface::KSaneOptCombo::getMinValue
bool getMinValue(float &max)
Definition: ksane_opt_combo.cpp:221
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::LabeledCombo::setCurrentText
void setCurrentText(const QString &)
If the given string can be found in the comobox, activate that entry.
Definition: labeled_combo.cpp:69
QString::simplified
QString simplified() const
KSaneIface::KSaneOption::toSANE_Word
SANE_Word toSANE_Word(unsigned char *data)
Definition: ksane_option.cpp:162
KSaneIface::LabeledCombo::addItems
void addItems(const QStringList &list)
Add string entries to the combobox.
Definition: labeled_combo.cpp:57
QVarLengthArray
ksane_opt_combo.h
KSaneIface::KSaneOption::state
KSaneOptWState state()
Definition: ksane_option.cpp:92
QString::number
QString number(int n, int base)
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::KSaneOptCombo::getValue
bool getValue(float &val)
Definition: ksane_opt_combo.cpp:245
KSaneIface::KSaneOptCombo::readOption
void readOption()
Definition: ksane_opt_combo.cpp:84
KSaneIface::KSaneOptCombo::createWidget
void createWidget(QWidget *parent)
Definition: ksane_opt_combo.cpp:50
QString
KSaneIface::KSaneOptCombo::setValue
bool setValue(float val)
Definition: ksane_opt_combo.cpp:274
KSaneIface::KSaneOption
Definition: ksane_option.h:49
QStringList
KSaneIface::LabeledCombo::currentText
QString currentText()
This function is used to read the current string of the combobox.
Definition: labeled_combo.cpp:79
KSaneIface::KSaneOption::STATE_HIDDEN
Definition: ksane_option.h:69
KSaneIface::LabeledCombo::setIcon
bool setIcon(const QIcon &icon, const QString &str)
add an icon for a string in the combobox
Definition: labeled_combo.cpp:85
KSaneIface::KSaneOption::writeData
bool writeData(void *data)
Definition: ksane_option.cpp:126
KSaneIface::KSaneOption::readOption
virtual void readOption()
Definition: ksane_option.cpp:73
QString::toFloat
float toFloat(bool *ok) const
QVarLengthArray::data
T * data()
KSaneIface::KSaneOption::m_widget
KSaneOptionWidget * m_widget
Definition: ksane_option.h:117
QString::left
QString left(int n) const
KSaneIface::tmp_binary
static const char tmp_binary[]
Definition: ksane_opt_combo.cpp:43
QWidget::setToolTip
void setToolTip(const QString &)
KSaneIface::LabeledCombo
A label and a combobox.
Definition: labeled_combo.h:41
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KSaneIface::LabeledCombo::clear
void clear()
Remove all string entries.
Definition: labeled_combo.h:65
KSaneIface::KSaneOptCombo::valueChanged
void valueChanged()
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