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

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) 2013-2016 Jonathan Marten <jjm@keelhaul.me.uk>    *
0007  *                                  *
0008  *  Kooka is free software; you can redistribute it and/or modify it    *
0009  *  under the terms of the GNU Library General Public License as    *
0010  *  published by the Free Software Foundation and appearing in the  *
0011  *  file COPYING included in the packaging of this file;  either    *
0012  *  version 2 of the License, or (at your option) any later version.    *
0013  *                                  *
0014  *  As a special exception, permission is given to link this program    *
0015  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0016  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0017  *  executable without including the source code for KADMOS in the  *
0018  *  source distribution.                        *
0019  *                                  *
0020  *  This program is distributed in the hope that it will be useful, *
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0023  *  GNU General Public License for more details.            *
0024  *                                  *
0025  *  You should have received a copy of the GNU General Public       *
0026  *  License along with this program;  see the file COPYING.  If     *
0027  *  not, see <http://www.gnu.org/licenses/>.                *
0028  *                                  *
0029  ************************************************************************/
0030 
0031 #include "autoselectdialog.h"
0032 
0033 #include <qcombobox.h>
0034 #include <qformlayout.h>
0035 #include <qlabel.h>
0036 #include <qpushbutton.h>
0037 
0038 #include <klocalizedstring.h>
0039 
0040 #include "kscancontrols.h"
0041 #include "kscandevice.h"
0042 #include "scansettings.h"
0043 
0044 
0045 // Combo box indexes
0046 #define INDEX_BLACK     0
0047 #define INDEX_WHITE     1
0048 
0049 
0050 AutoSelectDialog::AutoSelectDialog(QWidget *parent)
0051     : DialogBase(parent)
0052 {
0053     setObjectName("AutoSelectDialog");
0054 
0055     setModal(true);
0056     setWindowTitle(i18nc("@title:window", "Autoselect Settings"));
0057     setButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Apply);
0058 
0059     QWidget *w = new QWidget(this);
0060     QFormLayout *fl = new QFormLayout(w);       // looks better with combo expanded
0061     fl->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
0062 
0063     // slider for add/subtract margin
0064     const KConfigSkeletonItem *item = ScanSettings::self()->previewAutoselMarginItem();
0065     Q_ASSERT(item!=nullptr);
0066     int defaultVal = KScanDevice::getDefault<int>(item);
0067     int maxVal = item->maxValue().toInt();
0068     int minVal = item->minValue().toInt();
0069 
0070     mMarginSlider = new KScanSlider(nullptr, QString(), true);
0071     mMarginSlider->setRange(minVal, maxVal, -1, defaultVal);
0072     mMarginSlider->setToolTip(item->toolTip());
0073     connect(mMarginSlider, QOverload<int>::of(&KScanSlider::settingChanged), this, &AutoSelectDialog::slotControlChanged);
0074     fl->addRow(item->label(), mMarginSlider);
0075 
0076     fl->addItem(new QSpacerItem(1, verticalSpacing()));
0077 
0078     // combobox to select whether black or white background
0079     item = ScanSettings::self()->previewAutoselBackgroundItem();
0080     Q_ASSERT(item!=nullptr);
0081     mBackgroundCombo = new QComboBox;
0082     mBackgroundCombo->insertItem(INDEX_BLACK, i18n("Black"));
0083     mBackgroundCombo->insertItem(INDEX_WHITE, i18n("White"));
0084     mBackgroundCombo->setToolTip(item->toolTip());
0085     connect(mBackgroundCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &AutoSelectDialog::slotControlChanged);
0086     fl->addRow(item->label(), mBackgroundCombo);
0087 
0088     // slider for dust size - apparently not really much impact on the result
0089     item = ScanSettings::self()->previewAutoselDustsizeItem();
0090     Q_ASSERT(item!=nullptr);
0091     defaultVal = KScanDevice::getDefault<int>(item);
0092     maxVal = item->maxValue().toInt();
0093     minVal = item->minValue().toInt();
0094 
0095     mDustsizeSlider = new KScanSlider(nullptr, QString(), true);
0096     mDustsizeSlider->setRange(minVal, maxVal, -1, defaultVal);
0097     mDustsizeSlider->setToolTip(item->toolTip());
0098     connect(mDustsizeSlider, QOverload<int>::of(&KScanSlider::settingChanged), this, &AutoSelectDialog::slotControlChanged);
0099     fl->addRow(item->label(), mDustsizeSlider);
0100 
0101     setMainWidget(w);
0102 
0103     connect(buttonBox()->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &AutoSelectDialog::slotApplySettings);
0104     connect(buttonBox()->button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, &AutoSelectDialog::slotApplySettings);
0105     buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
0106 }
0107 
0108 void AutoSelectDialog::slotControlChanged()
0109 {
0110     buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(true);
0111 }
0112 
0113 void AutoSelectDialog::slotApplySettings()
0114 {
0115     const int margin = mMarginSlider->value();
0116     const bool bgIsWhite = (mBackgroundCombo->currentIndex()==INDEX_WHITE);
0117     const int dustsize = mDustsizeSlider->value();
0118     emit settingsChanged(margin, bgIsWhite, dustsize);
0119     buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
0120 }
0121 
0122 void AutoSelectDialog::setSettings(int margin, bool bgIsWhite, int dustsize)
0123 {
0124     mMarginSlider->setValue(margin);
0125     mBackgroundCombo->setCurrentIndex(bgIsWhite ? INDEX_WHITE : INDEX_BLACK);
0126     mDustsizeSlider->setValue(dustsize);
0127     buttonBox()->button(QDialogButtonBox::Apply)->setEnabled(false);
0128 }