• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KUtils

  • sources
  • kde-4.12
  • kdelibs
  • kutils
kprintpreview.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2007 Alex Merry <alex.merry@kdemail.net>
4  *
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.
8  *
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.
13  *
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.
18  **/
19 
20 #include "kprintpreview.h"
21 
22 #include <QtCore/QFile>
23 #include <QtGui/QLabel>
24 #include <QtGui/QPrinter>
25 #include <QtGui/QShowEvent>
26 
27 #include <kmimetypetrader.h>
28 #include <kparts/part.h>
29 #include <kpluginfactory.h>
30 #include <kpluginloader.h>
31 #include <kservice.h>
32 #include <ktempdir.h>
33 #include <kdebug.h>
34 
35 
36 class KPrintPreviewPrivate
37 {
38 public:
39  KPrintPreviewPrivate(KPrintPreview *host, QPrinter * _printer)
40  : q(host)
41  , printer(_printer)
42  , mainWidget(new QWidget(host))
43  , previewPart(0)
44  , failMessage(0)
45  {
46  if ( tempdir.exists() ) {
47  filename = tempdir.name() + "print_preview.pdf";
48  } else {
49  // XXX: not portable!
50  kWarning() << "Failed to create temporary directory";
51  filename = "/dev/null";
52  }
53  }
54 
55  void getPart();
56  bool doPreview();
57  void fail();
58 
59  KPrintPreview *q;
60 
61  QPrinter *printer;
62  QWidget *mainWidget;
63 
64  KTempDir tempdir;
65  QString filename;
66 
67  KParts::ReadOnlyPart *previewPart;
68  QWidget *failMessage;
69 };
70 
71 void KPrintPreviewPrivate::getPart()
72 {
73  if (previewPart) {
74  kDebug(500) << "already got a part";
75  return;
76  }
77  kDebug(500) << "querying trader for application/pdf service";
78 
79  KPluginFactory *factory(0);
80  const KService::List offers =
81  KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart");
82 
83  KService::List::ConstIterator it = offers.begin();
84  while (!factory && it != offers.end()) {
85  KPluginLoader loader(**it);
86  factory = loader.factory();
87  if (!factory) {
88  kDebug(500) << "Loading failed:" << loader.errorString();
89  }
90  ++it;
91  }
92  if (factory) {
93  kDebug(500) << "Trying to create a part";
94  previewPart = factory->create<KParts::ReadOnlyPart>(q, (QVariantList() << "Print/Preview"));
95  if (!previewPart) {
96  kDebug(500) << "Part creation failed";
97  }
98  }
99 }
100 
101 bool KPrintPreviewPrivate::doPreview()
102 {
103  if (!QFile::exists(filename)) {
104  kWarning() << "Nothing was produced to be previewed";
105  return false;
106  }
107 
108  getPart();
109  if (!previewPart) {
110  //TODO: error dialog
111  kWarning() << "Could not find a PDF viewer for the preview dialog";
112  fail();
113  return false;
114  } else {
115  q->setMainWidget(previewPart->widget());
116  return previewPart->openUrl(filename);
117  }
118 }
119 
120 void KPrintPreviewPrivate::fail()
121 {
122  if (!failMessage) {
123  failMessage = new QLabel(i18n("Could not load print preview part"), q);
124  }
125  q->setMainWidget(failMessage);
126 }
127 
128 
129 
130 
131 KPrintPreview::KPrintPreview(QPrinter *printer, QWidget *parent)
132  : KDialog(parent)
133  , d(new KPrintPreviewPrivate(this, printer))
134 {
135  kDebug(500) << "kdeprint: creating preview dialog";
136 
137  //There is no printing on wince
138 #ifndef _WIN32_WCE
139  // Set up the dialog
140  setCaption(i18n("Print Preview"));
141  setButtons(KDialog::Close);
142 
143  // Set up the printer
144  kDebug(500) << "Will print to" << d->filename;
145  printer->setOutputFileName(d->filename);
146 
147  setInitialSize(QSize(600, 500));
148 #endif
149 }
150 
151 KPrintPreview::~KPrintPreview()
152 {
153  delete d;
154 }
155 
156 void KPrintPreview::showEvent(QShowEvent *event)
157 {
158  if (!event->spontaneous()) {
159  // being shown for the first time
160  if (!d->doPreview()) {
161  event->accept();
162  return;
163  }
164  }
165  KDialog::showEvent(event);
166 }
167 
168 bool KPrintPreview::isAvailable()
169 {
170  return !KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart").isEmpty();
171 }
172 
173 #include "kprintpreview.moc"
174 
175 
176 
i18n
QString i18n(const char *text)
kprintpreview.h
kdebug.h
KTempDir
KPrintPreview
KPrintPreview provides a print preview dialog.
Definition: kprintpreview.h:41
QWidget
KPrintPreview::isAvailable
static bool isAvailable()
Returns true if the print preview system is available.
Definition: kprintpreview.cpp:168
KDialog
QString
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KPrintPreview::KPrintPreview
KPrintPreview(QPrinter *printer, QWidget *parent=0)
Create a KPrintPreview object.
Definition: kprintpreview.cpp:131
ktempdir.h
KPluginLoader
kpluginloader.h
KMimeTypeTrader::query
KService::List query(const QString &mimeType, const QString &genericServiceType=QString::fromLatin1("Application"), const QString &constraint=QString()) const
kmimetypetrader.h
KMimeTypeTrader::self
static KMimeTypeTrader * self()
KPrintPreview::showEvent
void showEvent(QShowEvent *event)
Definition: kprintpreview.cpp:156
KPrintPreview::~KPrintPreview
virtual ~KPrintPreview()
Definition: kprintpreview.cpp:151
kservice.h
kpluginfactory.h
QLabel
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QSize
KPluginFactory
QList
Definition: dialog.h:29
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:34 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal