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) 2020      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 "destinationprint.h"
0032 
0033 #include <qcheckbox.h>
0034 #include <qprintdialog.h>
0035 
0036 #include <kpluginfactory.h>
0037 #include <klocalizedstring.h>
0038 #include <kmessagebox.h>
0039 
0040 #include "scanparamspage.h"
0041 #include "kookaprint.h"
0042 #include "imgprintdialog.h"
0043 #include "destination_logging.h"
0044 
0045 
0046 K_PLUGIN_FACTORY_WITH_JSON(DestinationPrintFactory, "kookadestination-print.json", registerPlugin<DestinationPrint>();)
0047 #include "destinationprint.moc"
0048 
0049 
0050 DestinationPrint::DestinationPrint(QObject *pnt, const QVariantList &args)
0051     : AbstractDestination(pnt, "DestinationPrint")
0052 {
0053     mImmediateCheck = nullptr;
0054     mPrinter = nullptr;
0055 }
0056 
0057 
0058 DestinationPrint::~DestinationPrint()
0059 {
0060     delete mPrinter;
0061 }
0062 
0063 
0064 void DestinationPrint::imageScanned(ScanImage::Ptr img)
0065 {
0066     qCDebug(DESTINATION_LOG) << "received image size" << img->size();
0067 
0068     if (mPrinter==nullptr)              // create KookaPrint if needed
0069     {
0070         mPrinter = new KookaPrint;
0071         mPrinter->setCopyMode(true);
0072     }
0073     mPrinter->setImage(img.data());         // pass image to printer
0074 
0075     if (!mImmediateCheck->isChecked())          // want the print dialogue
0076     {
0077         QPrintDialog d(mPrinter, parentWidget());
0078         d.setWindowTitle(i18nc("@title:window", "Print Image"));
0079         d.setOptions(QAbstractPrintDialog::PrintToFile|QAbstractPrintDialog::PrintShowPageSize);
0080 
0081         ImgPrintDialog imgTab(img, mPrinter);       // create tab for our options
0082         d.setOptionTabs(QList<QWidget *>() << &imgTab); // add tab to print dialogue
0083 
0084         if (!d.exec()) return;              // open the dialogue
0085         QString msg = imgTab.checkValid();      // check that settings are valid
0086         if (!msg.isEmpty())             // if not, display error message
0087         {
0088             KMessageBox::error(parentWidget(),
0089                                i18nc("@info", "Invalid print options were specified:\n\n%1", msg),
0090                                i18nc("@title:window", "Cannot Print"));
0091             return;
0092         }
0093 
0094         imgTab.updatePrintParameters();         // set final printer options
0095     }
0096 
0097     qDebug() << "printing to" << mPrinter->printerName();
0098     mPrinter->printImage();             // print the image
0099 
0100     mImmediateCheck->setEnabled(true);          // now can skip dialogue next time
0101 }
0102 
0103 
0104 void DestinationPrint::createGUI(ScanParamsPage *page)
0105 {
0106     mImmediateCheck = new QCheckBox(i18n("Print immediately with same settings"));
0107     // The option is turned off and not available until
0108     // one scan/print cycle has been done.
0109     mImmediateCheck->setEnabled(false);
0110     page->addRow(nullptr, mImmediateCheck);
0111 }
0112 
0113 
0114 KLocalizedString DestinationPrint::scanDestinationString()
0115 {
0116     return (ki18n("Printing image"));
0117 }
0118 
0119 
0120 void DestinationPrint::saveSettings() const
0121 {
0122     // TODO: save settings from mPrinter
0123 }