File indexing completed on 2024-04-21 14:55:57

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (c) 2007 Alex Merry <alex.merry@kdemail.net>
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  **/
0020 
0021 #include "kprintpreview.h"
0022 
0023 #include <QDialogButtonBox>
0024 #include <QFile>
0025 #include <QLabel>
0026 #include <QShowEvent>
0027 #include <QVBoxLayout>
0028 #include <QTemporaryDir>
0029 #include <QDebug>
0030 
0031 #include <klocalizedstring.h>
0032 #include <kmimetypetrader.h>
0033 #include <kparts/readonlypart.h>
0034 #include <kpluginfactory.h>
0035 #include <kpluginloader.h>
0036 #include <kservice.h>
0037 
0038 class KPrintPreviewPrivate
0039 {
0040 public:
0041     KPrintPreviewPrivate(KPrintPreview *host, QPrinter *_printer)
0042         : q(host)
0043         , printer(_printer)
0044         , mainWidget(new QWidget(host))
0045         , previewPart(nullptr)
0046         , failMessage(nullptr)
0047     {
0048         mainWidget->setLayout(new QVBoxLayout);
0049 
0050         if (tempdir.isValid()) {
0051             filename = tempdir.path() + '/' + "print_preview.pdf";
0052         } else {
0053             qWarning() << "Failed to create temporary directory";
0054             filename = "/dev/null";
0055         }
0056     }
0057 
0058     void getPart();
0059     bool doPreview();
0060     void fail();
0061     void setCentralWidget(QWidget *widget);
0062 
0063     KPrintPreview *q;
0064 
0065     QPrinter *printer;
0066     QWidget *mainWidget;
0067 
0068     QTemporaryDir tempdir;
0069     QString filename;
0070 
0071     KParts::ReadOnlyPart *previewPart;
0072     QWidget *failMessage;
0073 };
0074 
0075 void KPrintPreviewPrivate::getPart()
0076 {
0077     if (previewPart) {
0078         return;
0079     }
0080 
0081     KPluginFactory *factory(nullptr);
0082     const KService::List offers =
0083         KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart");
0084 
0085     KService::List::ConstIterator it = offers.begin();
0086     while (!factory && it != offers.end()) {
0087         KPluginLoader loader(**it);
0088         factory = loader.factory();
0089         ++it;
0090     }
0091     if (factory) {
0092         previewPart = factory->create<KParts::ReadOnlyPart>(q, (QVariantList() << "Print/Preview"));
0093     }
0094 }
0095 
0096 bool KPrintPreviewPrivate::doPreview()
0097 {
0098     if (!QFile::exists(filename)) {
0099         qWarning() << "Nothing was produced to be previewed";
0100         return false;
0101     }
0102 
0103     getPart();
0104     if (!previewPart) {
0105         //TODO: error dialog
0106         qWarning() << "Could not find a PDF viewer for the preview dialog";
0107         fail();
0108         return false;
0109     } else {
0110         setCentralWidget(previewPart->widget());
0111         return previewPart->openUrl(QUrl::fromLocalFile(filename));
0112     }
0113 }
0114 
0115 void KPrintPreviewPrivate::fail()
0116 {
0117     if (!failMessage) {
0118         failMessage = new QLabel(i18n("Could not load print preview part"), q);
0119     }
0120     setCentralWidget(failMessage);
0121 }
0122 
0123 void KPrintPreviewPrivate::setCentralWidget(QWidget *widget)
0124 {
0125     mainWidget->layout()->addWidget(widget);
0126 }
0127 
0128 KPrintPreview::KPrintPreview(QPrinter *printer, QWidget *parent)
0129     : QDialog(parent)
0130     , d(new KPrintPreviewPrivate(this, printer))
0131 {
0132     //There is no printing on wince
0133 #ifndef _WIN32_WCE
0134     // Set up the dialog
0135     setWindowTitle(i18n("Print Preview"));
0136 
0137     // Set up the printer
0138     printer->setOutputFileName(d->filename);
0139 
0140     QVBoxLayout *layout = new QVBoxLayout;
0141     setLayout(layout);
0142 
0143     layout->addWidget(d->mainWidget, 1);
0144 
0145     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0146     buttonBox->setStandardButtons(QDialogButtonBox::Close);
0147     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0148     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0149     layout->addWidget(buttonBox);
0150 
0151     resize(QSize(600, 500));
0152     adjustSize();
0153 #endif
0154 }
0155 
0156 KPrintPreview::~KPrintPreview()
0157 {
0158     delete d;
0159 }
0160 
0161 void KPrintPreview::showEvent(QShowEvent *event)
0162 {
0163     if (!event->spontaneous()) {
0164         // being shown for the first time
0165         if (!d->doPreview()) {
0166             event->accept();
0167             return;
0168         }
0169     }
0170     QDialog::showEvent(event);
0171 }
0172 
0173 bool KPrintPreview::isAvailable()
0174 {
0175     return !KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart").isEmpty();
0176 }
0177 
0178 #include "moc_kprintpreview.cpp"