1 /* This file is part of the KDE Project
2    Copyright (C) 2000 Klaas Freitag <freitag@suse.de>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 #include "scansourcedialog.h"
21 
22 #include <qlabel.h>
23 #include <qradiobutton.h>
24 #include <qgroupbox.h>
25 #include <qlayout.h>
26 
27 #include <KLocalizedString>
28 #include <QDebug>
29 #include <kvbox.h>
30 
31 #include "kscancontrols.h"
32 
33 extern "C" {
34 #include <sane/saneopts.h>
35 }
36 
37 #ifndef SANE_NAME_DOCUMENT_FEEDER
38 #define SANE_NAME_DOCUMENT_FEEDER "Automatic Document Feeder"
39 #endif
40 
41 /**
42   * Internal private class for ScanSourceDialog
43   */
44 class ScanSourceDialogPrivate
45 {
46 public:
47     KScanCombo       *sources;
48     QGroupBox        *bgroup;
49     QVBoxLayout      *bgroupLayout;
50     QRadioButton     *rbADFTillEnd, *rbADFOnce;
51     AdfBehaviour adf;
52     bool adf_enabled;
53 
54 public:
ScanSourceDialogPrivate()55     ScanSourceDialogPrivate()
56         : sources(0), bgroup(0), bgroupLayout(0), rbADFTillEnd(0), rbADFOnce(0),
57           adf(ADF_OFF), adf_enabled(false)
58     {
59     }
60 };
61 
ScanSourceDialog(QWidget * parent,const QList<QByteArray> list,AdfBehaviour adfBehave)62 ScanSourceDialog::ScanSourceDialog(QWidget *parent, const QList<QByteArray> list, AdfBehaviour adfBehave)
63     : KDialog(parent)
64 {
65     d = new ScanSourceDialogPrivate();
66     setObjectName("ScanSourceDialog");
67 
68     setButtons(KDialog::Ok | KDialog::Cancel);
69     setCaption(i18n("Custom Gamma Tables"));
70     setModal(true);
71     showButtonSeparator(true);
72 
73     KVBox *vbox = new KVBox(this);
74     setMainWidget(vbox);
75 
76     (void) new QLabel(i18n("<b>Source selection</b><br />"
77                            "Note that you may see more sources than actually exist"), vbox);
78 
79     /* Combo Box for sources */
80     d->sources = new KScanCombo(vbox, QString());
81     d->sources->setList(list);
82     connect(d->sources, SIGNAL(activated(int)), SLOT(slotChangeSource(int)));
83 
84     if (sourceAdfEntry() > -1) {
85         d->bgroup = new QGroupBox(i18n("Advanced ADF Options"));
86 
87         /* Two buttons inside */
88         d->bgroupLayout = new QVBoxLayout();
89 
90         d->rbADFTillEnd = new QRadioButton(i18n("Scan until ADF reports out of paper"));
91         connect(d->rbADFTillEnd, SIGNAL(toggled(bool)), this, SLOT(slotNotifyADF(bool)));
92         d->bgroupLayout->addWidget(d->rbADFTillEnd);
93 
94         d->rbADFOnce = new QRadioButton(i18n("Scan only one sheet of ADF per click"));
95         connect(d->rbADFOnce, SIGNAL(toggled(bool)), this, SLOT(slotNotifyADF(bool)));
96         d->bgroupLayout->addWidget(d->rbADFOnce);
97 
98         d->bgroup->setLayout(d->bgroupLayout);
99 
100         switch (adfBehave) {
101         case ADF_OFF:
102             d->rbADFOnce->setChecked(true);
103             enableBGroup(false);
104             break;
105         case ADF_SCAN_ONCE:
106             d->rbADFOnce->setChecked(true);
107             break;
108         case ADF_SCAN_ALONG:
109             d->rbADFTillEnd->setChecked(true);
110             break;
111         default:
112             //qDebug() << "Undefined Source!";
113             // Hmmm.
114             break;
115         }
116 
117         d->adf = adfBehave;
118     }
119 }
120 
~ScanSourceDialog()121 ScanSourceDialog::~ScanSourceDialog()
122 {
123     if (d) {
124         delete d;
125         d = nullptr;
126     }
127 }
128 
getText(void) const129 QString  ScanSourceDialog::getText(void) const
130 {
131     return (d->sources->text());
132 }
133 
getAdfBehave(void) const134 AdfBehaviour ScanSourceDialog::getAdfBehave(void) const
135 {
136     return (d->adf);
137 }
138 
slotNotifyADF(bool checked)139 void ScanSourceDialog::slotNotifyADF(bool checked)
140 {
141     if (checked) {
142         // check which one sent the event:
143         d->adf = ADF_OFF;
144         if (sender() == d->rbADFOnce) {
145             d->adf = ADF_SCAN_ONCE;
146         } else if (sender() == d->rbADFTillEnd) {
147             d->adf = ADF_SCAN_ALONG;
148         }
149     }
150 
151 #if 0
152     // debug( "reported adf-select %d", adf_group );
153     /* this seems to be broken, adf_text is a visible string?
154     *  needs rework if SANE 2 comes up which supports i18n */
155     QString adf_text = getText();
156 
157     adf = ADF_OFF;
158 
159     if (adf_text == "Automatic Document Feeder" ||
160             adf_text == "ADF") {
161         if (adf_group == 0) {
162             adf = ADF_SCAN_ALONG;
163         } else {
164             adf = ADF_SCAN_ONCE;
165         }
166     }
167 #endif
168 }
169 
slotChangeSource(int i)170 void ScanSourceDialog::slotChangeSource(int i)
171 {
172     if (d->bgroup == nullptr) {
173         return;
174     }
175 
176     if (i == sourceAdfEntry()) {
177         /* Adf was switched on */
178         enableBGroup(true);
179         d->rbADFOnce->setChecked(true);
180         d->adf = ADF_SCAN_ALONG;
181         d->adf_enabled = true;
182     } else {
183         enableBGroup(false);
184         // d->adf = ADF_OFF;
185         d->adf_enabled = false;
186     }
187 }
188 
sourceAdfEntry() const189 int ScanSourceDialog::sourceAdfEntry() const
190 {
191     if (d->sources == nullptr) {
192         return (-1);
193     }
194 
195     int cou = d->sources->count();
196 
197     for (int i = 0; i < cou; i++) {
198         QString q = d->sources->textAt(i);
199 
200 // TODO: this enables advanced ADF options, not implemented yet
201 #if 0
202         if (q == "ADF" || q == SANE_NAME_DOCUMENT_FEEDER) {
203             return (i);
204         }
205 #endif
206 
207     }
208     return (-1);
209 }
210 
slotSetSource(const QString & source)211 void ScanSourceDialog::slotSetSource(const QString &source)
212 {
213     if (d->sources == nullptr) {
214         return;
215     }
216 
217     //qDebug() << "Setting source to" << source;
218 
219     enableBGroup(false);
220     d->adf_enabled = false ;
221 
222     for (int i = 0; i < d->sources->count(); i++) {
223         if (d->sources->textAt(i) == source) {
224             d->sources->setValue(i);
225             if (source == QString::number(sourceAdfEntry())) {
226                 if (d->bgroup) {
227                     enableBGroup(true);
228                 }
229 
230                 d->adf_enabled = true;
231             }
232             break;
233         }
234     }
235 
236 }
237 
238 /**
239   QGroupBox doesn't have a way to enable / disable all contained radio buttons, so we need to implement a
240   dedicated function for it.
241   */
242 
enableBGroup(bool enable)243 void ScanSourceDialog::enableBGroup(bool enable)
244 {
245     if (d->bgroup) {
246         d->rbADFOnce->setEnabled(enable);
247         d->rbADFTillEnd->setEnabled(enable);
248     }
249 }
250