KUtils
kprintpreview.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2007 Alex Merry <alex.merry@kdemail.net> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License version 2 as published by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 **/ 00019 00020 #include "kprintpreview.h" 00021 00022 #include <QtCore/QFile> 00023 #include <QtGui/QLabel> 00024 #include <QtGui/QPrinter> 00025 #include <QtGui/QShowEvent> 00026 00027 #include <kmimetypetrader.h> 00028 #include <kparts/part.h> 00029 #include <kpluginfactory.h> 00030 #include <kpluginloader.h> 00031 #include <kservice.h> 00032 #include <ktempdir.h> 00033 #include <kdebug.h> 00034 00035 00036 class KPrintPreviewPrivate 00037 { 00038 public: 00039 KPrintPreviewPrivate(KPrintPreview *host, QPrinter * _printer) 00040 : q(host) 00041 , printer(_printer) 00042 , mainWidget(new QWidget(host)) 00043 , previewPart(0) 00044 , failMessage(0) 00045 { 00046 if ( tempdir.exists() ) { 00047 filename = tempdir.name() + "print_preview.pdf"; 00048 } else { 00049 // XXX: not portable! 00050 kWarning() << "Failed to create temporary directory"; 00051 filename = "/dev/null"; 00052 } 00053 } 00054 00055 void getPart(); 00056 bool doPreview(); 00057 void fail(); 00058 00059 KPrintPreview *q; 00060 00061 QPrinter *printer; 00062 QWidget *mainWidget; 00063 00064 KTempDir tempdir; 00065 QString filename; 00066 00067 KParts::ReadOnlyPart *previewPart; 00068 QWidget *failMessage; 00069 }; 00070 00071 void KPrintPreviewPrivate::getPart() 00072 { 00073 if (previewPart) { 00074 kDebug(500) << "already got a part"; 00075 return; 00076 } 00077 kDebug(500) << "querying trader for application/pdf service"; 00078 00079 KPluginFactory *factory(0); 00080 const KService::List offers = 00081 KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart"); 00082 00083 KService::List::ConstIterator it = offers.begin(); 00084 while (!factory && it != offers.end()) { 00085 KPluginLoader loader(**it); 00086 factory = loader.factory(); 00087 if (!factory) { 00088 kDebug(500) << "Loading failed:" << loader.errorString(); 00089 } 00090 ++it; 00091 } 00092 if (factory) { 00093 kDebug(500) << "Trying to create a part"; 00094 previewPart = factory->create<KParts::ReadOnlyPart>(q, (QVariantList() << "Print/Preview")); 00095 if (!previewPart) { 00096 kDebug(500) << "Part creation failed"; 00097 } 00098 } 00099 } 00100 00101 bool KPrintPreviewPrivate::doPreview() 00102 { 00103 if (!QFile::exists(filename)) { 00104 kWarning() << "Nothing was produced to be previewed"; 00105 return false; 00106 } 00107 00108 getPart(); 00109 if (!previewPart) { 00110 //TODO: error dialog 00111 kWarning() << "Could not find a PDF viewer for the preview dialog"; 00112 fail(); 00113 return false; 00114 } else { 00115 q->setMainWidget(previewPart->widget()); 00116 return previewPart->openUrl(filename); 00117 } 00118 } 00119 00120 void KPrintPreviewPrivate::fail() 00121 { 00122 if (!failMessage) { 00123 failMessage = new QLabel(i18n("Could not load print preview part"), q); 00124 } 00125 q->setMainWidget(failMessage); 00126 } 00127 00128 00129 00130 00131 KPrintPreview::KPrintPreview(QPrinter *printer, QWidget *parent) 00132 : KDialog(parent) 00133 , d(new KPrintPreviewPrivate(this, printer)) 00134 { 00135 kDebug(500) << "kdeprint: creating preview dialog"; 00136 00137 //There is no printing on wince 00138 #ifndef _WIN32_WCE 00139 // Set up the dialog 00140 setCaption(i18n("Print Preview")); 00141 setButtons(KDialog::Close); 00142 00143 // Set up the printer 00144 kDebug(500) << "Will print to" << d->filename; 00145 printer->setOutputFileName(d->filename); 00146 00147 setInitialSize(QSize(600, 500)); 00148 #endif 00149 } 00150 00151 KPrintPreview::~KPrintPreview() 00152 { 00153 delete d; 00154 } 00155 00156 void KPrintPreview::showEvent(QShowEvent *event) 00157 { 00158 if (!event->spontaneous()) { 00159 // being shown for the first time 00160 if (!d->doPreview()) { 00161 event->accept(); 00162 return; 00163 } 00164 } 00165 KDialog::showEvent(event); 00166 } 00167 00168 bool KPrintPreview::isAvailable() 00169 { 00170 return !KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart").isEmpty(); 00171 } 00172 00173 #include "kprintpreview.moc" 00174 00175 00176
KDE 4.6 API Reference