File indexing completed on 2024-03-24 15:48:02

0001 /************************************************************************
0002  *                                  *
0003  *  This file is part of Kooka, a scanning/OCR application using    *
0004  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.  *
0005  *                                  *
0006  *  Copyright (C) 1999-2016 Klaas Freitag <freitag@suse.de>     *
0007  *                          Jonathan Marten <jjm@keelhaul.me.uk>    *
0008  *                                  *
0009  *  Kooka is free software; you can redistribute it and/or modify it    *
0010  *  under the terms of the GNU Library General Public License as    *
0011  *  published by the Free Software Foundation and appearing in the  *
0012  *  file COPYING included in the packaging of this file;  either    *
0013  *  version 2 of the License, or (at your option) any later version.    *
0014  *                                  *
0015  *  As a special exception, permission is given to link this program    *
0016  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0017  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0018  *  executable without including the source code for KADMOS in the  *
0019  *  source distribution.                        *
0020  *                                  *
0021  *  This program is distributed in the hope that it will be useful, *
0022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0024  *  GNU General Public License for more details.            *
0025  *                                  *
0026  *  You should have received a copy of the GNU General Public       *
0027  *  License along with this program;  see the file COPYING.  If     *
0028  *  not, see <http://www.gnu.org/licenses/>.                *
0029  *                                  *
0030  ************************************************************************/
0031 
0032 #include "imgscaledialog.h"
0033 
0034 #include <qbuttongroup.h>
0035 #include <qlayout.h>
0036 #include <qradiobutton.h>
0037 #include <qlabel.h>
0038 #include <qlineedit.h>
0039 #include <qvalidator.h>
0040 
0041 #include <klocalizedstring.h>
0042 
0043 /* ############################################################################## */
0044 
0045 ImgScaleDialog::ImgScaleDialog(QWidget *parent, int curr_sel)
0046     : DialogBase(parent)
0047 {
0048     setObjectName("ImgScaleDialog");
0049 
0050     setButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0051     setWindowTitle(i18n("Image Zoom"));
0052     setModal(true);
0053 
0054     selected = curr_sel;
0055 
0056     QWidget *radios = new QWidget(this);
0057     setMainWidget(radios);
0058 
0059     QButtonGroup *radiosGroup = new QButtonGroup(radios);
0060 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
0061     connect(radiosGroup, &QButtonGroup::idClicked, this, &ImgScaleDialog::slotSetSelValue);
0062 #else
0063     connect(radiosGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), this, &ImgScaleDialog::slotSetSelValue);
0064 #endif
0065 
0066     QVBoxLayout *radiosLayout = new QVBoxLayout(this);
0067 
0068     // left column: smaller Image
0069     QHBoxLayout *hbox = new QHBoxLayout();
0070     QVBoxLayout *vbox = new QVBoxLayout();
0071 
0072     QRadioButton *rb25 = new QRadioButton(i18n("25 %"));
0073     if (curr_sel == 25) {
0074         rb25->setChecked(true);
0075     }
0076     vbox->addWidget(rb25);
0077     radiosGroup->addButton(rb25, 0);
0078 
0079     QRadioButton *rb50 = new QRadioButton(i18n("50 %"));
0080     if (curr_sel == 50) {
0081         rb50->setChecked(true);
0082     }
0083     vbox->addWidget(rb50);
0084     radiosGroup->addButton(rb50, 1);
0085 
0086     QRadioButton *rb75 = new QRadioButton(i18n("75 %"));
0087     if (curr_sel == 75) {
0088         rb75->setChecked(true);
0089     }
0090     vbox->addWidget(rb75);
0091     radiosGroup->addButton(rb75, 2);
0092 
0093     QRadioButton *rb100 = new QRadioButton(i18n("&100 %"));
0094     if (curr_sel == 100) {
0095         rb100->setChecked(true);
0096     }
0097     vbox->addWidget(rb100);
0098     radiosGroup->addButton(rb100, 3);
0099 
0100     hbox->addLayout(vbox);
0101 
0102     // right column: bigger image:
0103     vbox = new QVBoxLayout();
0104 
0105     QRadioButton *rb150 = new QRadioButton(i18n("150 &%"));
0106     if (curr_sel == 150) {
0107         rb150->setChecked(true);
0108     }
0109     vbox->addWidget(rb150);
0110     radiosGroup->addButton(rb150, 4);
0111 
0112     QRadioButton *rb200 = new QRadioButton(i18n("200 %"));
0113     if (curr_sel == 200) {
0114         rb200->setChecked(true);
0115     }
0116     vbox->addWidget(rb200);
0117     radiosGroup->addButton(rb200, 5);
0118 
0119     QRadioButton *rb300 = new QRadioButton(i18n("300 %"));
0120     if (curr_sel == 300) {
0121         rb300->setChecked(true);
0122     }
0123     vbox->addWidget(rb300);
0124     radiosGroup->addButton(rb300, 6);
0125 
0126     QRadioButton *rb400 = new QRadioButton(i18n("400 %"));
0127     if (curr_sel == 400) {
0128         rb400->setChecked(true);
0129     }
0130     vbox->addWidget(rb400);
0131     radiosGroup->addButton(rb400, 7);
0132 
0133     hbox->addLayout(vbox);
0134     radiosLayout->addLayout(hbox);
0135 
0136     radiosLayout->addSpacing(verticalSpacing());
0137 
0138     // Custom Scaler at the bottom:
0139     hbox = new QHBoxLayout();
0140 
0141     QRadioButton *rbCust = new QRadioButton(i18n("Custom:"));
0142     if (radiosGroup->checkedId() < 0) {
0143         rbCust->setChecked(true);
0144     }
0145     connect(rbCust, &QRadioButton::toggled, this, &ImgScaleDialog::slotEnableAndFocus);
0146 
0147     hbox->addWidget(rbCust);
0148     radiosGroup->addButton(rbCust, 8);
0149 
0150     leCust = new QLineEdit();
0151     QString sn;
0152     sn.setNum(curr_sel);
0153     leCust->setValidator(new QIntValidator(5, 1000, leCust));
0154     leCust->setText(sn);
0155     connect(leCust, &QLineEdit::textChanged, this, &ImgScaleDialog::slotCustomChanged);
0156     hbox->addWidget(leCust);
0157     hbox->setStretchFactor(leCust, 1);
0158 
0159     hbox->addWidget(new QLabel("%", this));
0160 
0161     radiosLayout->addLayout(hbox);
0162     radiosLayout->addStretch();
0163 
0164     radios->setLayout(radiosLayout);
0165 
0166     slotEnableAndFocus(rbCust->isChecked());
0167 }
0168 
0169 void ImgScaleDialog::slotCustomChanged(const QString &s)
0170 {
0171     bool ok;
0172     int okval = s.toInt(&ok);
0173     if (ok && okval >= 5 && okval <= 1000) {
0174         selected = okval;
0175         setButtonEnabled(QDialogButtonBox::Ok, true);
0176         emit customScaleChange(okval);
0177     } else {
0178         setButtonEnabled(QDialogButtonBox::Ok, false);
0179     }
0180 }
0181 
0182 // This slot is called, when the user changes the Scale-Selection
0183 // in the button group. The value val is the index of the active
0184 // button which is translated to the Scale-Size in percent.
0185 // If custom size is selected, the ScaleSize is read from the
0186 // QLineedit.
0187 //
0188 void ImgScaleDialog::slotSetSelValue(int val)
0189 {
0190     const int translator[] = { 25, 50, 75, 100, 150, 200, 300, 400, -1 };
0191     const int translator_size = sizeof(translator) / sizeof(int);
0192     int old_sel = selected;
0193 
0194     // Check if value is in Range
0195     if (val >= 0 && val < translator_size) {
0196         selected = translator[val];
0197 
0198         // Custom size selected
0199         if (selected == -1) {
0200             QString s = leCust->text();
0201 
0202             bool ok;
0203             int  okval = s.toInt(&ok);
0204             if (ok) {
0205                 selected = okval;
0206                 emit(customScaleChange(okval));
0207             } else {
0208                 selected = old_sel;
0209             }
0210         } // Selection is not custom
0211     }
0212 }
0213 
0214 int ImgScaleDialog::getSelected() const
0215 {
0216     return (selected);
0217 }
0218 
0219 void ImgScaleDialog::slotEnableAndFocus(bool b)
0220 {
0221     leCust->setEnabled(b);
0222     if (b) {
0223         leCust->setFocus();
0224     }
0225 }