2 * This file is part of the KDE libraries
3 * Copyright (c) 2007 Alex Merry <alex.merry@kdemail.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "kprintpreview.h"
22 #include <QtCore/QFile>
23 #include <QtGui/QLabel>
24 #include <QtGui/QPrinter>
25 #include <QtGui/QShowEvent>
27 #include <kmimetypetrader.h>
28 #include <kparts/part.h>
29 #include <kpluginfactory.h>
30 #include <kpluginloader.h>
36 class KPrintPreviewPrivate
39 KPrintPreviewPrivate(KPrintPreview
*host
, QPrinter
* _printer
)
42 , mainWidget(new QWidget(host
))
46 if ( tempdir
.exists() ) {
47 filename
= tempdir
.name() + "print_preview.pdf";
50 kWarning() << "Failed to create temporary directory";
51 filename
= "/dev/null";
67 KParts::ReadOnlyPart
*previewPart
;
71 void KPrintPreviewPrivate::getPart()
74 kDebug(500) << "already got a part";
77 kDebug(500) << "querying trader for application/pdf service";
79 KPluginFactory
*factory(0);
80 const KService::List offers
=
81 KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart");
83 KService::List::ConstIterator it
= offers
.begin();
84 while (!factory
&& it
!= offers
.end()) {
85 KPluginLoader
loader(**it
);
86 factory
= loader
.factory();
88 kDebug(500) << "Loading failed:" << loader
.errorString();
93 kDebug(500) << "Trying to create a part";
94 previewPart
= factory
->create
<KParts::ReadOnlyPart
>(q
, (QVariantList() << "Print/Preview"));
96 kDebug(500) << "Part creation failed";
101 bool KPrintPreviewPrivate::doPreview()
103 if (!QFile::exists(filename
)) {
104 kWarning() << "Nothing was produced to be previewed";
111 kWarning() << "Could not find a PDF viewer for the preview dialog";
115 q
->setMainWidget(previewPart
->widget());
116 return previewPart
->openUrl(filename
);
120 void KPrintPreviewPrivate::fail()
123 failMessage
= new QLabel(i18n("Could not load print preview part"), q
);
125 q
->setMainWidget(failMessage
);
131 KPrintPreview::KPrintPreview(QPrinter
*printer
, QWidget
*parent
)
133 , d(new KPrintPreviewPrivate(this, printer
))
135 kDebug(500) << "kdeprint: creating preview dialog";
138 setCaption(i18n("Print Preview"));
139 setButtons(KDialog::Close
);
141 // Set up the printer
142 kDebug(500) << "Will print to" << d
->filename
;
143 printer
->setOutputFileName(d
->filename
);
146 KPrintPreview::~KPrintPreview()
151 void KPrintPreview::showEvent(QShowEvent
*event
)
153 if (!event
->spontaneous()) {
154 // being shown for the first time
155 if (!d
->doPreview()) {
160 KDialog::showEvent(event
);
163 #include "kprintpreview.moc"