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 "autoselectbar.h"
0032 
0033 #include <qtoolbutton.h>
0034 #include <qlayout.h>
0035 #include <qlabel.h>
0036 #include <qslider.h>
0037 #include <qicon.h>
0038 #include <qframe.h>
0039 #include <qpalette.h>
0040 
0041 #include <klocalizedstring.h>
0042 
0043 #include "autoselectdialog.h"
0044 #include "kscancontrols.h"
0045 #include "scansettings.h"
0046 #include "dialogbase.h"
0047 
0048 
0049 AutoSelectBar::AutoSelectBar(int initialValue, QWidget *parent)
0050     : QWidget(parent)
0051 {
0052     setObjectName("AutoSelectBar");
0053 
0054     QHBoxLayout *hbl = new QHBoxLayout;
0055 
0056     QLabel *l = new QLabel(xi18nc("@info", "<subtitle>Auto Select</subtitle>"));
0057     hbl->addWidget(l);
0058 
0059     hbl->addSpacing(2*DialogBase::horizontalSpacing());
0060 
0061     // Threshold setting label
0062     const KConfigSkeletonItem *item = ScanSettings::self()->previewAutoselThresholdItem();
0063     l = new QLabel(item->label());
0064     hbl->addWidget(l);
0065 
0066     // Threshold setting slider/spinbox
0067     int maxThresh = item->maxValue().toInt();
0068     mThresholdSlider = new KScanSlider(nullptr, QString());
0069     mThresholdSlider->setRange(0, maxThresh, -1, initialValue);
0070     mThresholdSlider->setToolTip(item->toolTip());
0071     l->setBuddy(mThresholdSlider);
0072 
0073     connect(mThresholdSlider, QOverload<int>::of(&KScanSlider::settingChanged), this, &AutoSelectBar::slotThresholdChanged);
0074     hbl->addWidget(mThresholdSlider);
0075     hbl->setStretchFactor(mThresholdSlider, 1);
0076 
0077     mColourPatch = new QFrame(this);
0078     // from kdelibs4support/src/kdeui/kcolordialog.cpp
0079     mColourPatch->setFrameStyle(QFrame::StyledPanel|QFrame::Sunken);
0080     mColourPatch->setMinimumWidth(32);
0081     mColourPatch->setAutoFillBackground(true);
0082     mColourPatch->setToolTip(i18nc("@info:tooltip", "This is the grayscale value of the selected threshold"));
0083     hbl->addWidget(mColourPatch);
0084 
0085     hbl->addSpacing(DialogBase::horizontalSpacing());
0086 
0087     // Refresh/recalculate button
0088     QToolButton *but = new QToolButton;
0089     but->setIcon(QIcon::fromTheme("view-refresh"));
0090     but->setToolTip(i18nc("@info:tooltip", "Perform the auto-detection again"));
0091     connect(but, &QAbstractButton::clicked, this, &AutoSelectBar::performSelection);
0092     hbl->addWidget(but);
0093 
0094     // Advanced settings button
0095     but = new QToolButton;
0096     but->setIcon(QIcon::fromTheme("configure"));
0097     but->setToolTip(i18nc("@info:tooltip", "Advanced settings for auto-detection"));
0098     connect(but, &QAbstractButton::clicked, this, &AutoSelectBar::slotShowSettings);
0099     hbl->addWidget(but);
0100 
0101     setLayout(hbl);
0102 
0103     slotThresholdChanged(mThresholdSlider->value());    // update the colour patch
0104 }
0105 
0106 
0107 void AutoSelectBar::slotThresholdChanged(int value)
0108 {
0109     int colValue = value;
0110     if (mBgIsWhite) {
0111         colValue = 255 - colValue;
0112     }
0113 
0114     QPalette pal = mColourPatch->palette();
0115     pal.setColor(QPalette::Normal, QPalette::Window, qRgb(colValue, colValue, colValue));
0116     mColourPatch->setPalette(pal);
0117     emit thresholdChanged(value);
0118 }
0119 
0120 void AutoSelectBar::slotShowSettings()
0121 {
0122     AutoSelectDialog *d = new AutoSelectDialog(this);
0123     d->setSettings(mMargin, mBgIsWhite, mDustsize);
0124     connect(d, &AutoSelectDialog::settingsChanged, this, &AutoSelectBar::setAdvancedSettings);
0125     connect(d, &AutoSelectDialog::settingsChanged, this, &AutoSelectBar::advancedSettingsChanged);
0126     d->show();
0127 }
0128 
0129 void AutoSelectBar::setThreshold(int thresh)
0130 {
0131     mThresholdSlider->setValue(thresh);
0132 }
0133 
0134 void AutoSelectBar::setAdvancedSettings(int margin, bool bgIsWhite, int dustsize)
0135 {
0136     // just retained for dialogue
0137     mMargin = margin;
0138     mBgIsWhite = bgIsWhite;
0139     mDustsize = dustsize;
0140 }