1 /***************************************************************************
2 * Copyright (C) 2007 by John Layt <john@layt.net> *
4 * FilePrinterPreview based on KPrintPreview (originally LGPL) *
5 * Copyright (c) 2007 Alex Merry <huntedhacker@tiscali.co.uk> *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 ***************************************************************************/
13 #include "fileprinterpreview.h"
17 #include <QtCore/QFile>
18 #include <QtGui/QLabel>
19 #include <QtGui/QShowEvent>
21 #include <kmimetypetrader.h>
22 #include <kparts/part.h>
23 #include <kpluginfactory.h>
24 #include <kpluginloader.h>
28 using namespace Okular
;
30 // This code copied from KPrintPreview by Alex Merry, adapted to do PS files instead of PDF
32 class Okular::FilePrinterPreviewPrivate
35 FilePrinterPreviewPrivate( FilePrinterPreview
*host
, const QString
& _filename
)
37 , mainWidget(new QWidget(host
))
48 FilePrinterPreview
*q
;
54 KParts::ReadOnlyPart
*previewPart
;
58 void FilePrinterPreviewPrivate::getPart()
61 kDebug(500) << "already got a part";
64 kDebug(500) << "querying trader for application/ps service";
66 KPluginFactory
*factory(0);
67 KService::List offers
=
68 KMimeTypeTrader::self()->query("application/postscript", "KParts/ReadOnlyPart");
70 KService::List::ConstIterator it
= offers
.begin();
71 while (!factory
&& it
!= offers
.end()) {
72 KPluginLoader
loader(**it
);
73 factory
= loader
.factory();
75 kDebug(500) << "Loading failed:" << loader
.errorString();
80 kDebug(500) << "Trying to create a part";
81 previewPart
= factory
->create
<KParts::ReadOnlyPart
>(q
, (QVariantList() << "Print/Preview"));
83 kDebug(500) << "Part creation failed";
88 bool FilePrinterPreviewPrivate::doPreview()
90 if (!QFile::exists(filename
)) {
91 kWarning() << "Nothing was produced to be previewed";
98 kWarning() << "Could not find a PS viewer for the preview dialog";
102 q
->setMainWidget(previewPart
->widget());
103 return previewPart
->openUrl(filename
);
107 void FilePrinterPreviewPrivate::fail()
110 failMessage
= new QLabel(i18n("Could not load print preview part"), q
);
112 q
->setMainWidget(failMessage
);
118 FilePrinterPreview::FilePrinterPreview( const QString
&filename
, QWidget
*parent
)
120 , d( new FilePrinterPreviewPrivate( this, filename
) )
122 kDebug(500) << "kdeprint: creating preview dialog";
125 setCaption(i18n("Print Preview"));
126 setButtons(KDialog::Close
);
129 FilePrinterPreview::~FilePrinterPreview()
134 void FilePrinterPreview::showEvent(QShowEvent
*event
)
136 if (!event
->spontaneous()) {
137 // being shown for the first time
138 if (!d
->doPreview()) {
143 KDialog::showEvent(event
);
146 #include "fileprinterpreview.moc"