1 /************************************************************************
2  *									*
3  *  This file is part of Kooka, a scanning/OCR application using	*
4  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.	*
5  *									*
6  *  Copyright (C) 1999-2016 Klaas Freitag <freitag@suse.de>		*
7  *                          Jonathan Marten <jjm@keelhaul.me.uk>	*
8  *									*
9  *  Kooka is free software; you can redistribute it and/or modify it	*
10  *  under the terms of the GNU Library General Public License as	*
11  *  published by the Free Software Foundation and appearing in the	*
12  *  file COPYING included in the packaging of this file;  either	*
13  *  version 2 of the License, or (at your option) any later version.	*
14  *									*
15  *  As a special exception, permission is given to link this program	*
16  *  with any version of the KADMOS OCR/ICR engine (a product of		*
17  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting	*
18  *  executable without including the source code for KADMOS in the	*
19  *  source distribution.						*
20  *									*
21  *  This program is distributed in the hope that it will be useful,	*
22  *  but WITHOUT ANY WARRANTY; without even the implied warranty of	*
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	*
24  *  GNU General Public License for more details.			*
25  *									*
26  *  You should have received a copy of the GNU General Public		*
27  *  License along with this program;  see the file COPYING.  If		*
28  *  not, see <http://www.gnu.org/licenses/>.				*
29  *									*
30  ************************************************************************/
31 
32 #include "imgscaledialog.h"
33 
34 #include <qbuttongroup.h>
35 #include <qlayout.h>
36 #include <qradiobutton.h>
37 #include <qlabel.h>
38 #include <qlineedit.h>
39 #include <qvalidator.h>
40 
41 #include <klocalizedstring.h>
42 
43 /* ############################################################################## */
44 
ImgScaleDialog(QWidget * parent,int curr_sel)45 ImgScaleDialog::ImgScaleDialog(QWidget *parent, int curr_sel)
46     : DialogBase(parent)
47 {
48     setObjectName("ImgScaleDialog");
49 
50     setButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
51     setWindowTitle(i18n("Image Zoom"));
52     setModal(true);
53 
54     selected = curr_sel;
55 
56     QWidget *radios = new QWidget(this);
57     setMainWidget(radios);
58 
59     QButtonGroup *radiosGroup = new QButtonGroup(radios);
60     connect(radiosGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), this, &ImgScaleDialog::slotSetSelValue);
61 
62     QVBoxLayout *radiosLayout = new QVBoxLayout(this);
63 
64     // left column: smaller Image
65     QHBoxLayout *hbox = new QHBoxLayout();
66     QVBoxLayout *vbox = new QVBoxLayout();
67 
68     QRadioButton *rb25 = new QRadioButton(i18n("25 %"));
69     if (curr_sel == 25) {
70         rb25->setChecked(true);
71     }
72     vbox->addWidget(rb25);
73     radiosGroup->addButton(rb25, 0);
74 
75     QRadioButton *rb50 = new QRadioButton(i18n("50 %"));
76     if (curr_sel == 50) {
77         rb50->setChecked(true);
78     }
79     vbox->addWidget(rb50);
80     radiosGroup->addButton(rb50, 1);
81 
82     QRadioButton *rb75 = new QRadioButton(i18n("75 %"));
83     if (curr_sel == 75) {
84         rb75->setChecked(true);
85     }
86     vbox->addWidget(rb75);
87     radiosGroup->addButton(rb75, 2);
88 
89     QRadioButton *rb100 = new QRadioButton(i18n("&100 %"));
90     if (curr_sel == 100) {
91         rb100->setChecked(true);
92     }
93     vbox->addWidget(rb100);
94     radiosGroup->addButton(rb100, 3);
95 
96     hbox->addLayout(vbox);
97 
98     // right column: bigger image:
99     vbox = new QVBoxLayout();
100 
101     QRadioButton *rb150 = new QRadioButton(i18n("150 &%"));
102     if (curr_sel == 150) {
103         rb150->setChecked(true);
104     }
105     vbox->addWidget(rb150);
106     radiosGroup->addButton(rb150, 4);
107 
108     QRadioButton *rb200 = new QRadioButton(i18n("200 %"));
109     if (curr_sel == 200) {
110         rb200->setChecked(true);
111     }
112     vbox->addWidget(rb200);
113     radiosGroup->addButton(rb200, 5);
114 
115     QRadioButton *rb300 = new QRadioButton(i18n("300 %"));
116     if (curr_sel == 300) {
117         rb300->setChecked(true);
118     }
119     vbox->addWidget(rb300);
120     radiosGroup->addButton(rb300, 6);
121 
122     QRadioButton *rb400 = new QRadioButton(i18n("400 %"));
123     if (curr_sel == 400) {
124         rb400->setChecked(true);
125     }
126     vbox->addWidget(rb400);
127     radiosGroup->addButton(rb400, 7);
128 
129     hbox->addLayout(vbox);
130     radiosLayout->addLayout(hbox);
131 
132     radiosLayout->addSpacing(verticalSpacing());
133 
134     // Custom Scaler at the bottom:
135     hbox = new QHBoxLayout();
136 
137     QRadioButton *rbCust = new QRadioButton(i18n("Custom:"));
138     if (radiosGroup->checkedId() < 0) {
139         rbCust->setChecked(true);
140     }
141     connect(rbCust, &QRadioButton::toggled, this, &ImgScaleDialog::slotEnableAndFocus);
142 
143     hbox->addWidget(rbCust);
144     radiosGroup->addButton(rbCust, 8);
145 
146     leCust = new QLineEdit();
147     QString sn;
148     sn.setNum(curr_sel);
149     leCust->setValidator(new QIntValidator(5, 1000, leCust));
150     leCust->setText(sn);
151     connect(leCust, &QLineEdit::textChanged, this, &ImgScaleDialog::slotCustomChanged);
152     hbox->addWidget(leCust);
153     hbox->setStretchFactor(leCust, 1);
154 
155     hbox->addWidget(new QLabel("%", this));
156 
157     radiosLayout->addLayout(hbox);
158     radiosLayout->addStretch();
159 
160     radios->setLayout(radiosLayout);
161 
162     slotEnableAndFocus(rbCust->isChecked());
163 }
164 
slotCustomChanged(const QString & s)165 void ImgScaleDialog::slotCustomChanged(const QString &s)
166 {
167     bool ok;
168     int okval = s.toInt(&ok);
169     if (ok && okval >= 5 && okval <= 1000) {
170         selected = okval;
171         setButtonEnabled(QDialogButtonBox::Ok, true);
172         emit customScaleChange(okval);
173     } else {
174         setButtonEnabled(QDialogButtonBox::Ok, false);
175     }
176 }
177 
178 // This slot is called, when the user changes the Scale-Selection
179 // in the button group. The value val is the index of the active
180 // button which is translated to the Scale-Size in percent.
181 // If custom size is selected, the ScaleSize is read from the
182 // QLineedit.
183 //
slotSetSelValue(int val)184 void ImgScaleDialog::slotSetSelValue(int val)
185 {
186     const int translator[] = { 25, 50, 75, 100, 150, 200, 300, 400, -1 };
187     const int translator_size = sizeof(translator) / sizeof(int);
188     int old_sel = selected;
189 
190     // Check if value is in Range
191     if (val >= 0 && val < translator_size) {
192         selected = translator[val];
193 
194         // Custom size selected
195         if (selected == -1) {
196             QString s = leCust->text();
197 
198             bool ok;
199             int  okval = s.toInt(&ok);
200             if (ok) {
201                 selected = okval;
202                 emit(customScaleChange(okval));
203             } else {
204                 selected = old_sel;
205             }
206         } // Selection is not custom
207     } else {
208         //qDebug() << "Error: Invalid size selected!" << val;
209     }
210 }
211 
getSelected() const212 int ImgScaleDialog::getSelected() const
213 {
214     return (selected);
215 }
216 
slotEnableAndFocus(bool b)217 void ImgScaleDialog::slotEnableAndFocus(bool b)
218 {
219     leCust->setEnabled(b);
220     if (b) {
221         leCust->setFocus();
222     }
223 }
224