File indexing completed on 2024-05-12 15:55:06

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) 2000-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 "ocrgocrdialog.h"
0033 
0034 #include <qlabel.h>
0035 #include <qregexp.h>
0036 #include <qgridlayout.h>
0037 #include <qprogressbar.h>
0038 #include <qspinbox.h>
0039 
0040 #include <klocalizedstring.h>
0041 #include <kprocess.h>
0042 #include <kconfigskeleton.h>
0043 
0044 #include "scanimage.h"
0045 #include "kookapref.h"
0046 #include "kookasettings.h"
0047 
0048 #include "ocrgocrengine.h"
0049 #include "kscancontrols.h"
0050 
0051 
0052 OcrGocrDialog::OcrGocrDialog(AbstractOcrEngine *plugin, QWidget *pnt)
0053     : AbstractOcrDialogue(plugin, pnt),
0054       m_ocrCmd(QString())
0055 {
0056 }
0057 
0058 
0059 bool OcrGocrDialog::setupGui()
0060 {
0061     AbstractOcrDialogue::setupGui();
0062 
0063     QWidget *w = addExtraSetupWidget();
0064     QGridLayout *gl = new QGridLayout(w);
0065 
0066     KConfigSkeletonItem *ski = KookaSettings::self()->ocrGocrGrayLevelItem();
0067     Q_ASSERT(ski!=nullptr);
0068     QLabel *l = new QLabel(ski->label(), w);
0069     gl->addWidget(l, 0, 0);
0070 
0071     sliderGrayLevel = new KScanSlider(w, QString(), true);
0072     sliderGrayLevel->setRange(0, 254, -1, 160);
0073     sliderGrayLevel->setValue(KookaSettings::ocrGocrGrayLevel());
0074     sliderGrayLevel->setToolTip(ski->toolTip());
0075     sliderGrayLevel->spinBox()->setSpecialValueText(i18n("Automatic"));
0076     l->setBuddy(sliderGrayLevel);
0077     gl->addWidget(sliderGrayLevel, 0, 1);
0078 
0079     ski = KookaSettings::self()->ocrGocrDustSizeItem();
0080     Q_ASSERT(ski!=nullptr);
0081     l = new QLabel(ski->label(), w);
0082     gl->addWidget(l, 1, 0);
0083     sliderDustSize = new KScanSlider(w, QString(), true);
0084     sliderDustSize->setRange(-1, 20, -1, 10);
0085     sliderDustSize->setValue(KookaSettings::ocrGocrDustSize());
0086     sliderDustSize->setToolTip(ski->toolTip());
0087     sliderDustSize->spinBox()->setSpecialValueText(i18n("Automatic"));
0088     l->setBuddy(sliderDustSize);
0089     gl->addWidget(sliderDustSize, 1, 1);
0090 
0091     ski = KookaSettings::self()->ocrGocrSpaceWidthItem();
0092     Q_ASSERT(ski!=nullptr);
0093     l = new QLabel(ski->label(), w);
0094     gl->addWidget(l, 2, 0);
0095     sliderSpace = new KScanSlider(w, QString(), true);
0096     sliderSpace->setRange(0, 60, -1, 0);
0097     sliderSpace->setValue(KookaSettings::ocrGocrSpaceWidth());
0098     sliderSpace->setToolTip(ski->toolTip());
0099     sliderSpace->spinBox()->setSpecialValueText(i18n("Automatic"));
0100     l->setBuddy(sliderSpace);
0101     gl->addWidget(sliderSpace, 2, 1);
0102 
0103     ski = KookaSettings::self()->ocrGocrCertaintyItem();
0104     Q_ASSERT(ski!=nullptr);
0105     l = new QLabel(ski->label(), w);
0106     gl->addWidget(l, 3, 0);
0107     sliderCertainty = new KScanSlider(w, QString(), true);
0108     sliderCertainty->setRange(5, 100, -1, 95);
0109     sliderCertainty->setValue(KookaSettings::ocrGocrCertainty());
0110     sliderCertainty->setToolTip(ski->toolTip());
0111     l->setBuddy(sliderCertainty);
0112     gl->addWidget(sliderCertainty, 3, 1);
0113 
0114     gl->setRowStretch(4, 1);                // for top alignment
0115 
0116     /* find the GOCR binary */
0117     m_ocrCmd = engine()->findExecutable(&KookaSettings::ocrGocrBinary, KookaSettings::self()->ocrGocrBinaryItem());
0118     if (m_ocrCmd.isEmpty())             // found, get its version
0119     {
0120         engine()->setErrorText(i18n("The GOCR executable is not configured or is not available."));
0121     }
0122 
0123     ocrShowInfo(m_ocrCmd, version());           // the show binary and version
0124     progressBar()->setMaximum(0);           // progress animation only
0125 
0126     m_setupWidget = w;
0127     return (!m_ocrCmd.isEmpty());
0128 }
0129 
0130 
0131 void OcrGocrDialog::introduceImage(ScanImage::Ptr img)
0132 {
0133     AbstractOcrDialogue::introduceImage(img);
0134     if (img==nullptr || img->isNull()) return;
0135 
0136     // See if the image is black-and-white, where the GreyLevel slider is not needed.
0137     // This was originally (as a member variable) called 'm_isBW', but the logic was
0138     // the other way round!
0139     if (sliderGrayLevel!=nullptr) sliderGrayLevel->setEnabled(!engine()->isBW());
0140 }
0141 
0142 
0143 void OcrGocrDialog::slotWriteConfig()
0144 {
0145     AbstractOcrDialogue::slotWriteConfig();
0146 
0147     KookaSettings::setOcrGocrBinary(getOCRCmd());
0148 
0149     KookaSettings::setOcrGocrGrayLevel(getGraylevel());
0150     KookaSettings::setOcrGocrDustSize(getDustsize());
0151     KookaSettings::setOcrGocrSpaceWidth(getSpaceWidth());
0152     KookaSettings::setOcrGocrCertainty(getCertainty());
0153     KookaSettings::self()->save();
0154 }
0155 
0156 void OcrGocrDialog::enableFields(bool enable)
0157 {
0158     m_setupWidget->setEnabled(enable);
0159 }
0160 
0161 
0162 int OcrGocrDialog::getGraylevel() const
0163 {
0164     return (sliderGrayLevel->value());
0165 }
0166 
0167 
0168 int OcrGocrDialog::getDustsize() const
0169 {
0170     return (sliderDustSize->value());
0171 }
0172 
0173 
0174 int OcrGocrDialog::getSpaceWidth() const
0175 {
0176     return (sliderSpace->value());
0177 }
0178 
0179 
0180 int OcrGocrDialog::getCertainty() const
0181 {
0182     return (sliderCertainty->value());
0183 }
0184 
0185 
0186 QString OcrGocrDialog::version()
0187 {
0188     QString vers;
0189 
0190     KProcess proc;
0191     proc.setOutputChannelMode(KProcess::MergedChannels);
0192     proc << m_ocrCmd << "-h";
0193 
0194     int status = proc.execute(5000);
0195     if (status == 0) {
0196         QByteArray output = proc.readAllStandardOutput();
0197         QRegExp rx("-- gocr ([\\d\\.\\s]+)");
0198         if (rx.indexIn(output) > -1) {
0199             vers = rx.cap(1);
0200         } else {
0201             vers = i18n("Unknown");
0202         }
0203     } else {
0204         vers = i18n("Error");
0205     }
0206 
0207     return (vers);
0208 }