~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to extra/libksane/libksane/options/ksane_opt_combo.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer, Rohan Garg, Philip Muškovac, Felix Geyer
  • Date: 2011-09-23 18:18:55 UTC
  • mfrom: (1.2.36 upstream)
  • Revision ID: package-import@ubuntu.com-20110923181855-ifs67wxkugshev9k
Tags: 2:2.1.1-0ubuntu1
[ Rohan Garg ]
* New upstream release (LP: #834190)
  - debian/control
    + Build with libqtwebkit-dev
 - debian/kipi-plugins-common
    + Install libkvkontakte required by kipi-plugins
 - debian/digikam
    + Install panoramagui

[ Philip Muškovac ]
* New upstream release
  - debian/control:
    + Add libcv-dev, libcvaux-dev, libhighgui-dev, libboost-graph1.46-dev,
      libksane-dev, libxml2-dev, libxslt-dev, libqt4-opengl-dev, libqjson-dev,
      libgpod-dev and libqca2-dev to build-deps
    + Add packages for kipi-plugins, libmediawiki, libkface, libkgeomap and
      libkvkontakte
  - debian/rules:
    + Don't build with gphoto2 since it doesn't build with it.
  - Add kubuntu_fix_test_linking.diff to fix linking of the dngconverter test
  - update install files
  - update kubuntu_01_mysqld_executable_name.diff for new cmake layout
    and rename to kubuntu_mysqld_executable_name.diff
* Fix typo in digikam-data description (LP: #804894)
* Fix Vcs links

[ Felix Geyer ]
* Move library data files to the new packages libkface-data, libkgeomap-data
  and libkvkontakte-data.
* Override version of the embedded library packages to 1.0~digikam<version>.
* Exclude the library packages from digikam-dbg to prevent file conflicts in
  the future.
* Call dh_install with --list-missing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    }
 
136
    return m_strList;
 
137
}
 
138
 
 
139
QString KSaneOptCombo::getSaneComboString(int ival)
 
140
{
 
141
    switch(m_optDesc->unit)
 
142
    {
 
143
        case SANE_UNIT_NONE:        break;
 
144
        case SANE_UNIT_PIXEL:       return i18np("%1 Pixel","%1 Pixels", ival);
 
145
        case SANE_UNIT_BIT:         return i18np("%1 Bit","%1 Bits", ival);
 
146
        case SANE_UNIT_MM:          return i18np("%1 mm","%1 mm", ival);
 
147
        case SANE_UNIT_DPI:         return i18np("%1 DPI","%1 DPI", ival);
 
148
        case SANE_UNIT_PERCENT:     return i18np("%1 %","%1 %", ival);
 
149
        case SANE_UNIT_MICROSECOND: return i18np("%1 µs","%1 µs", ival);
 
150
    }
 
151
    return QString::number(ival);
 
152
}
 
153
 
 
154
QString KSaneOptCombo::getSaneComboString(float fval)
 
155
{
 
156
    switch(m_optDesc->unit)
 
157
    {
 
158
        case SANE_UNIT_NONE:        break;
 
159
        case SANE_UNIT_PIXEL:       return i18ncp("Parameter and Unit","%1 Pixel", "%1 Pixels", fval);
 
160
        case SANE_UNIT_BIT:         return i18ncp("Parameter and Unit","%1 Bit","%1 Bits", fval);
 
161
        case SANE_UNIT_MM:          return i18nc("Parameter and Unit (Millimeter)","%1 mm", fval);
 
162
        case SANE_UNIT_DPI:         return i18nc("Parameter and Unit (Dots Per Inch)","%1 DPI", fval);
 
163
        case SANE_UNIT_PERCENT:     return i18nc("Parameter and Unit (Percentage)","%1 %", fval);
 
164
        case SANE_UNIT_MICROSECOND: return i18nc("Parameter and Unit (Microseconds)","%1 µs", fval);
 
165
    }
 
166
    return QString::number(fval, 'F', 4);
 
167
}
 
168
 
 
169
QString KSaneOptCombo::getSaneComboString(unsigned char *data)
 
170
{
 
171
    QString tmp;
 
172
    if (data == 0) return QString();
 
173
        
 
174
    switch (m_optDesc->type)
 
175
    {
 
176
        case SANE_TYPE_INT:
 
177
            return getSaneComboString((int)toSANE_Word(data));
 
178
        case SANE_TYPE_FIXED:
 
179
            return getSaneComboString((float)SANE_UNFIX(toSANE_Word(data)));
 
180
        case SANE_TYPE_STRING:
 
181
            tmp = i18n(reinterpret_cast<char*>(data));
 
182
            tmp = tmp.simplified();
 
183
            if (tmp.length() > 25) {
 
184
                tmp = tmp.left(22);
 
185
                tmp += "...";
 
186
            }
 
187
            return tmp;
 
188
        default :
 
189
            break;
 
190
    }
 
191
    return QString();
 
192
}
 
193
 
 
194
 
 
195
 
 
196
void KSaneOptCombo::comboboxChangedIndex(int i)
 
197
{
 
198
    if (m_combo && (m_combo->currentText() == m_currentText)) {
 
199
        return;
 
200
    }
 
201
    
 
202
    unsigned char data[4];
 
203
    void *dataPtr;
 
204
    
 
205
    switch (m_optDesc->type)
 
206
    {
 
207
        case SANE_TYPE_INT:
 
208
        case SANE_TYPE_FIXED:
 
209
            fromSANE_Word(data, m_optDesc->constraint.word_list[i+1]);
 
210
            dataPtr = data;
 
211
            break;
 
212
        case SANE_TYPE_STRING:
 
213
            dataPtr = (void *)m_optDesc->constraint.string_list[i];
 
214
            break;
 
215
        default:
 
216
            kDebug() << "can not handle type:" << m_optDesc->type;
 
217
            return;
 
218
    }
 
219
    writeData(dataPtr);
 
220
    readValue();
 
221
    emit valueChanged();
 
222
}
 
223
 
 
224
bool KSaneOptCombo::getMinValue(float &val)
 
225
{
 
226
    if (state() == STATE_HIDDEN) return false;
 
227
    switch (m_optDesc->type)
 
228
    {
 
229
        case SANE_TYPE_INT:
 
230
            val = (float)m_optDesc->constraint.word_list[1];
 
231
            for (int i=2; i<=m_optDesc->constraint.word_list[0]; i++) {
 
232
                val = qMin((float)m_optDesc->constraint.word_list[i], val);
 
233
            }
 
234
            break;
 
235
        case SANE_TYPE_FIXED:
 
236
            val = (float)SANE_UNFIX(m_optDesc->constraint.word_list[1]);
 
237
            for (int i=2; i<=m_optDesc->constraint.word_list[0]; i++) {
 
238
                val = qMin((float)SANE_UNFIX(m_optDesc->constraint.word_list[i]), val);
 
239
            }
 
240
            break;
 
241
        default:
 
242
            kDebug() << "can not handle type:" << m_optDesc->type;
 
243
            return false;
 
244
    }
 
245
    return true;
 
246
}
 
247
 
 
248
bool KSaneOptCombo::getValue(float &val)
 
249
{
 
250
    if (state() == STATE_HIDDEN) return false;
 
251
 
 
252
    // read that current value
 
253
    QVarLengthArray<unsigned char> data(m_optDesc->size);
 
254
    SANE_Status status;
 
255
    SANE_Int res;
 
256
    status = sane_control_option (m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
 
257
    if (status != SANE_STATUS_GOOD) {
 
258
        kDebug() << m_optDesc->name << "sane_control_option returned" << status;
 
259
        return false;
 
260
    }
 
261
    
 
262
    switch (m_optDesc->type)
 
263
    {
 
264
        case SANE_TYPE_INT:
 
265
            val = (float)toSANE_Word(data.data());
 
266
            return true;
 
267
        case SANE_TYPE_FIXED:
 
268
            val = SANE_UNFIX(toSANE_Word(data.data()));
 
269
            return true;
 
270
        default:
 
271
            kDebug() << "Type" << m_optDesc->type << "not supported!";
 
272
    }
 
273
    return false;
 
274
}
 
275
 
 
276
bool KSaneOptCombo::setValue(float value)
 
277
{
 
278
    unsigned char data[4];
 
279
    float tmp;
 
280
    float minDiff;
 
281
    int i;
 
282
    int minIndex = 1;
 
283
    
 
284
    switch (m_optDesc->type)
 
285
    {
 
286
        case SANE_TYPE_INT:
 
287
            tmp = (float)m_optDesc->constraint.word_list[minIndex];
 
288
            minDiff = qAbs(value - tmp);
 
289
            for (i=2; i<=m_optDesc->constraint.word_list[0]; ++i) {
 
290
                tmp = (float)m_optDesc->constraint.word_list[i];
 
291
                if (qAbs(value - tmp) < minDiff) {
 
292
                    minDiff = qAbs(value - tmp);
 
293
                    minIndex = i;
 
294
                }
 
295
            }
 
296
            fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
 
297
            writeData(data);
 
298
            readValue();
 
299
            return (minDiff < 1.0);
 
300
        case SANE_TYPE_FIXED:
 
301
            tmp = (float)SANE_UNFIX(m_optDesc->constraint.word_list[minIndex]);
 
302
            minDiff = qAbs(value - tmp);
 
303
            for (i=2; i<=m_optDesc->constraint.word_list[0]; ++i) {
 
304
                tmp = (float)SANE_UNFIX(m_optDesc->constraint.word_list[i]);
 
305
                if (qAbs(value - tmp) < minDiff) {
 
306
                    minDiff = qAbs(value - tmp);
 
307
                    minIndex = i;
 
308
                }
 
309
            }
 
310
            fromSANE_Word(data, m_optDesc->constraint.word_list[minIndex]);
 
311
            writeData(data);
 
312
            readValue();
 
313
            return (minDiff < 1.0);
 
314
        default:
 
315
            kDebug() << "can not handle type:" << m_optDesc->type;
 
316
            break;
 
317
    }
 
318
    return false;
 
319
}
 
320
 
 
321
bool KSaneOptCombo::getValue(QString &val)
 
322
{
 
323
    if (state() == STATE_HIDDEN) return false;
 
324
    val = m_currentText;
 
325
    return true;
 
326
}
 
327
 
 
328
bool KSaneOptCombo::setValue(const QString &val)
 
329
{
 
330
    if (state() == STATE_HIDDEN) return false;
 
331
    if (val == m_currentText) return true;
 
332
    
 
333
    unsigned char data[4];
 
334
    void *data_ptr;
 
335
    SANE_Word fixed;
 
336
    int i;
 
337
    float f;
 
338
    bool ok;
 
339
    QString tmp;
 
340
    
 
341
    switch (m_optDesc->type)
 
342
    {
 
343
        case SANE_TYPE_INT:
 
344
            tmp = val.left(val.indexOf(' ')); // strip the unit
 
345
            // accept float formating of the string
 
346
            i = (int)(tmp.toFloat(&ok));
 
347
            if (ok == false) return false;
 
348
            fromSANE_Word(data, i);
 
349
            data_ptr = data;
 
350
            break;
 
351
        case SANE_TYPE_FIXED:
 
352
            tmp = val.left(val.indexOf(' ')); // strip the unit
 
353
            f = tmp.toFloat(&ok);
 
354
            if (ok == false) return false;
 
355
            fixed = SANE_FIX(f);
 
356
            fromSANE_Word(data, fixed);
 
357
            data_ptr = data;
 
358
            break;
 
359
        case SANE_TYPE_STRING:
 
360
            i = 0;
 
361
            while (m_optDesc->constraint.string_list[i] != 0) {
 
362
                tmp = getSaneComboString((unsigned char *)m_optDesc->constraint.string_list[i]);
 
363
                if (val == tmp) {
 
364
                    data_ptr = (void *)m_optDesc->constraint.string_list[i];
 
365
                    break;
 
366
                }
 
367
                i++;
 
368
            }
 
369
            if (m_optDesc->constraint.string_list[i] == 0) return false;
 
370
            break;
 
371
        default:
 
372
            kDebug() << "can only handle SANE_TYPE: INT, FIXED and STRING";
 
373
            return false;
 
374
    }
 
375
    writeData(data_ptr);
 
376
 
 
377
    readValue();
 
378
    return true;
 
379
}
 
380
 
 
381
 
 
382
}  // NameSpace KSaneIface