--nextPart66152733.qHs8ObWW63 Content-Type: multipart/mixed; boundary="Boundary-01=_0N99GLe+HyKXsIJ" Content-Transfer-Encoding: 7bit Content-Disposition: inline --Boundary-01=_0N99GLe+HyKXsIJ Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I have here a working KPrintPreview class. It's a KDialog, and is used=20 as follows: QPrinter printer; KPrintPreview preview(&printer, this); // where "this" is a QWidget doSomePrinting(printer); preview.exec(); Basically, you use it almost identically to a QPrintDialog, except that=20 you exec() the dialog after you've done the printing rather than=20 before. It uses any available KPart that advertises that it can deal with=20 application/pdf (eg: Okular). Note that I had to delete the kghostview=20 desktop file to make this work, since kghostview is broken for me. It was quite straightforward to adapt kolourpaint to use QPrinter and=20 KPrintPreview, for testing. I temporarily named the library kdeprint2, so as not to interfere with=20 kdeprint. Attached are a patch for kdelibs and the modifications I made to get=20 kolourpaint to work. Obviously the class could be made better in various ways, but it=20 demonstrates a working print preview and an API for it. Alex PS: please keep discussions about the printing dialog in a separate=20 thread, unless they directly relate to print preview. =2D-=20 KDE: http://www.kde.org Ubuntu/Kubuntu: http://www.ubuntu.org http://www.kubuntu.org --Boundary-01=_0N99GLe+HyKXsIJ Content-Type: text/x-diff; charset="utf-8"; name="kolourpaint-kdeprint2.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="kolourpaint-kdeprint2.diff" Index: mainWindow/kpMainWindow_File.cpp =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- mainWindow/kpMainWindow_File.cpp (revision 716349) +++ mainWindow/kpMainWindow_File.cpp (working copy) @@ -37,6 +37,8 @@ #include #include #include +#include +#include =20 #include #include @@ -52,7 +54,7 @@ #include #include #include =2D#include +#include #include #include #include @@ -66,7 +68,9 @@ #include #include #include +#if 0 #include +#endif #include #include =20 @@ -1054,7 +1058,7 @@ bool kpMainWindow::slotReload () =20 =20 // private =2Dvoid kpMainWindow::sendFilenameToPrinter (KPrinter *printer) +void kpMainWindow::sendFilenameToPrinter (QPrinter &printer) { KUrl url =3D d->document->url (); if (!url.isEmpty ()) @@ -1069,22 +1073,19 @@ void kpMainWindow::sendFilenameToPrinter fileName.truncate (dot); =20 #if DEBUG_KP_MAIN_WINDOW =2D kDebug () << "kpMainWindow::sendFilenameToPrinter() fileName=3D" + kDebug () << "fileName=3D" << fileName << " dir=3D" << url.directory () << endl; #endif =2D printer->setDocName (fileName); =2D printer->setDocFileName (fileName); =2D printer->setDocDirectory (url.directory ()); + printer.setDocName (fileName); } } =20 =20 // private =2Dvoid kpMainWindow::sendImageToPrinter (KPrinter *printer, =2D bool showPrinterSetupDialog) +void kpMainWindow::sendImageToPrinter (QPrinter &printer) { // Get image to be printed. kpImage image =3D d->document->imageWithSelection (); @@ -1149,8 +1150,8 @@ void kpMainWindow::sendImageToPrinter (K // m m m =3D margin // m m // mmmmm =2D const int printerWidthMM =3D printer->widthMM (); =2D const int printerHeightMM =3D printer->heightMM (); + const int printerWidthMM =3D printer.widthMM (); + const int printerHeightMM =3D printer.heightMM (); #if DEBUG_KP_MAIN_WINDOW kDebug () << "\tprinter: widthMM=3D" << printerWidthMM << " heightMM=3D" << printerHeightMM @@ -1229,21 +1230,11 @@ void kpMainWindow::sendImageToPrinter (K Q_ASSERT (dpiX =3D=3D dpiY); =20 =20 =2D // QPrinter::setResolution() has to be called before QPrinter::setup= (). =2D printer->setResolution (qMax (1, qRound (dpiX))); + printer.setResolution (qMax (1, qRound (dpiX))); =20 =20 sendFilenameToPrinter (printer); =20 =2D if (showPrinterSetupDialog) =2D { =2D // The user can mutate margins at their own risk in this dialog. =2D // It doesn't seem to affect the size of the page as reported =2D // by QPaintDeviceMetrics::{width,height}MM(). COMPAT: fix comm= ent - no more QPaintDeviceMetrics =2D if (!printer->setup (this)) =2D return; =2D } =2D =20 double originX =3D 0, originY =3D 0; =20 @@ -1251,7 +1242,7 @@ void kpMainWindow::sendImageToPrinter (K // // Only call this method after the user has set the print options i.e. // after the print dialog ("printer->setup()") has been accepted. =2D if (kpPrintDialogPage::shouldPrintImageCenteredOnPage (printer)) + /*if (kpPrintDialogPage::shouldPrintImageCenteredOnPage (printer)) { originX =3D (printerWidthMM * dpiX / KP_MILLIMETERS_PER_INCH - image.width= ()) @@ -1259,7 +1250,7 @@ void kpMainWindow::sendImageToPrinter (K originY =3D (printerHeightMM * dpiY / KP_MILLIMETERS_PER_INCH - image.heig= ht ()) / 2; =2D } + }*/ =20 #if DEBUG_KP_MAIN_WINDOW kDebug () << "\torigin: x=3D" << originX << " y=3D" << originY; @@ -1268,7 +1259,7 @@ void kpMainWindow::sendImageToPrinter (K =20 // Send image to printer. QPainter painter; =2D painter.begin (printer); + painter.begin (&printer); painter.drawPixmap (qRound (originX), qRound (originY), image); painter.end (); } @@ -1279,10 +1270,12 @@ void kpMainWindow::slotPrint () { toolEndShape (); =20 =2D KPrinter printer; =2D printer.addDialogPage (new kpPrintDialogPage (this)); + QPrinter printer; + //printer.addDialogPage (new kpPrintDialogPage (this)); + QPrintDialog printDialog(&printer, this); + printDialog.exec(); =20 =2D sendImageToPrinter (&printer, true/*showPrinterSetupDialog*/); + sendImageToPrinter (printer); } =20 // private slot @@ -1293,12 +1286,13 @@ void kpMainWindow::slotPrintPreview () // Do not specify "false" to KPrinter::()'s "restore" arg, // since we want to use the same KPrinter::options(), set by // kpPrintDialogPage. =2D KPrinter printer; + QPrinter printer; + + KPrintPreview preview(&printer, this); =20 =2D // TODO: pass "this" as parent =2D printer.setPreviewOnly (true); + sendImageToPrinter (printer); =20 =2D sendImageToPrinter (&printer, false/*don't showPrinterSetupDialog*/); + preview.exec(); } =20 =20 Index: mainWindow/kpMainWindow.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- mainWindow/kpMainWindow.h (revision 716349) +++ mainWindow/kpMainWindow.h (working copy) @@ -362,8 +362,8 @@ private slots: bool slotReload (); =20 private: =2D void sendFilenameToPrinter (KPrinter *printer); =2D void sendImageToPrinter (KPrinter *printer, bool showPrinterSetupDia= log); + void sendFilenameToPrinter (QPrinter &printer); + void sendImageToPrinter (QPrinter &printer); =20 private slots: void slotPrint (); Index: CMakeLists.txt =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-- CMakeLists.txt (revision 716349) +++ CMakeLists.txt (working copy) @@ -155,7 +155,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/dialogs/imag ${CMAKE_CURRENT_SOURCE_DIR}/dialogs/imagelib/transforms/kpTransformSkewDia= log.cpp ${CMAKE_CURRENT_SOURCE_DIR}/dialogs/kpColorSimilarityDialog.cpp ${CMAKE_CURRENT_SOURCE_DIR}/dialogs/kpDocumentSaveOptionsPreviewDialog.cpp =2D${CMAKE_CURRENT_SOURCE_DIR}/dialogs/kpPrintDialogPage.cpp +#${CMAKE_CURRENT_SOURCE_DIR}/dialogs/kpPrintDialogPage.cpp ${CMAKE_CURRENT_SOURCE_DIR}/document/kpDocument.cpp ${CMAKE_CURRENT_SOURCE_DIR}/document/kpDocument_Open.cpp ${CMAKE_CURRENT_SOURCE_DIR}/document/kpDocument_Save.cpp @@ -332,7 +332,7 @@ set(kolourpaint_lgpl_SRCS =20 kde4_add_library(kolourpaint_lgpl SHARED ${kolourpaint_lgpl_SRCS}) =20 =2Dtarget_link_libraries(kolourpaint_lgpl ${KDE4_KDEPRINT_LIBS}) +target_link_libraries(kolourpaint_lgpl ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS}= kdeprint2) set_target_properties(kolourpaint_lgpl PROPERTIES VERSION ${GENERIC_LIB_VE= RSION} SOVERSION ${GENERIC_LIB_SOVERSION}) =20 install(TARGETS kolourpaint_lgpl DESTINATION ${LIB_INSTALL_DIR}) --Boundary-01=_0N99GLe+HyKXsIJ Content-Type: text/x-diff; charset="utf-8"; name="kdeprint2.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="kdeprint2.diff" Index: kdeprint2/kprintpreview.h =================================================================== --- kdeprint2/kprintpreview.h (revision 0) +++ kdeprint2/kprintpreview.h (revision 0) @@ -0,0 +1,45 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2007 Alex Merry + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#ifndef KPRINTPREVIEW_H +#define KPRINTPREVIEW_H + +#include + +#include + +class KPrintPreviewPrivate; + +class KDEPRINT2_EXPORT KPrintPreview : public KDialog +{ + Q_OBJECT + +public: + explicit KPrintPreview(QPrinter *printer, QWidget *parent = 0); + virtual ~KPrintPreview(); + + void showEvent(QShowEvent *event); + +private: + KPrintPreviewPrivate * const d; +}; + + +#endif // KPRINTPREVIEW_H + Index: kdeprint2/kdeprint2_export.h =================================================================== --- kdeprint2/kdeprint2_export.h (revision 0) +++ kdeprint2/kdeprint2_export.h (revision 0) @@ -0,0 +1,46 @@ +/* This file is part of the KDE project + Copyright (C) 2007 David Faure + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KDEPRINT2_EXPORT_H +#define KDEPRINT2_EXPORT_H + +/* needed for KDE_EXPORT and KDE_IMPORT macros */ +#include + +#ifndef KDEPRINT2_EXPORT +# if defined(MAKE_KDEPRINT2_LIB) +/* We are building this library */ +# define KDEPRINT2_EXPORT KDE_EXPORT +# else +/* We are using this library */ +# define KDEPRINT2_EXPORT KDE_IMPORT +# endif +#endif + +# ifndef KDEPRINT2_EXPORT_DEPRECATED +# define KDEPRINT2_EXPORT_DEPRECATED KDE_DEPRECATED KDEPRINT2_EXPORT +# endif +# ifndef KDEPRINT2_MANAGEMENT_EXPORT_DEPRECATED +# define KDEPRINT2_MANAGEMENT_EXPORT_DEPRECATED KDE_DEPRECATED KDEPRINT2_MANAGEMENT_EXPORT +# endif +# ifndef KDEPRINT2_MANAGEMENT_MODULE_EXPORT_DEPRECATED +# define KDEPRINT2_MANAGEMENT_MODULE_EXPORT_DEPRECATED KDE_DEPRECATED KDEPRINT2_MANAGEMENT_MODULE_EXPORT +# endif + +#endif Index: kdeprint2/kprintpreview.cpp =================================================================== --- kdeprint2/kprintpreview.cpp (revision 0) +++ kdeprint2/kprintpreview.cpp (revision 0) @@ -0,0 +1,165 @@ +/* + * This file is part of the KDE libraries + * Copyright (c) 2007 Alex Merry + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + **/ + +#include "kprintpreview.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + + +class KPrintPreviewPrivate +{ +public: + KPrintPreviewPrivate(KPrintPreview *host, QPrinter * _printer) + : q(host) + , printer(_printer) + , mainWidget(new QWidget(host)) + , previewPart(0) + , failMessage(0) + { + if ( tempdir.exists() ) { + filename = tempdir.name() + "print_preview.pdf"; + } else { + // XXX: not portable! + kWarning() << "Failed to create temporary directory"; + filename = "/dev/null"; + } + } + + void getPart(); + bool doPreview(); + void fail(); + + KPrintPreview *q; + + QPrinter *printer; + QWidget *mainWidget; + + KTempDir tempdir; + QString filename; + + KParts::ReadOnlyPart *previewPart; + QWidget *failMessage; +}; + +void KPrintPreviewPrivate::getPart() +{ + if (previewPart) { + kDebug(500) << "already got a part"; + return; + } + kDebug(500) << "querying trader for application/pdf service"; + + KPluginFactory *factory(0); + KService::List offers = + KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart"); + + KService::List::ConstIterator it = offers.begin(); + while (!factory && it != offers.end()) { + KPluginLoader loader(**it); + factory = loader.factory(); + if (!factory) { + kDebug(500) << "Loading failed:" << loader.errorString(); + } + ++it; + } + if (factory) { + kDebug(500) << "Trying to create a part"; + previewPart = factory->create(q, (QVariantList() << "Print/Preview")); + if (!previewPart) { + kDebug(500) << "Part creation failed"; + } + } +} + +bool KPrintPreviewPrivate::doPreview() +{ + if (!QFile::exists(filename)) { + kWarning() << "Nothing was produced to be previewed"; + return false; + } + + getPart(); + if (!previewPart) { + //TODO: error dialog + kWarning() << "Could not find a PDF viewer for the preview dialog"; + fail(); + return false; + } else { + q->setMainWidget(previewPart->widget()); + return previewPart->openUrl(filename); + } +} + +void KPrintPreviewPrivate::fail() +{ + if (!failMessage) { + failMessage = new QLabel(i18n("Could not load print preview part"), q); + } + q->setMainWidget(failMessage); +} + + + + +KPrintPreview::KPrintPreview(QPrinter *printer, QWidget *parent) + : KDialog(parent) + , d(new KPrintPreviewPrivate(this, printer)) +{ + kDebug(500) << "kdeprint: creating preview dialog"; + + // Set up the dialog + setCaption(i18n("Print Preview")); + setButtons(KDialog::Close); + + // Set up the printer + kDebug(500) << "Will print to" << d->filename; + printer->setOutputFileName(d->filename); +} + +KPrintPreview::~KPrintPreview() +{ + delete d; +} + +void KPrintPreview::showEvent(QShowEvent *event) +{ + if (!event->spontaneous()) { + // being shown for the first time + if (!d->doPreview()) { + event->accept(); + return; + } + } + KDialog::showEvent(event); +} + +#include "kprintpreview.moc" + + + Index: kdeprint2/CMakeLists.txt =================================================================== --- kdeprint2/CMakeLists.txt (revision 0) +++ kdeprint2/CMakeLists.txt (revision 0) @@ -0,0 +1,32 @@ +project(kdeprint2) + + +set(kdeprint2_LIB_SRCS + kprintpreview.cpp +) + +include_directories(${KDE4_KDEUI_INCLUDES} ${KDE4_KPARTS_INCLUDES}) + + +kde4_add_library(kdeprint2 SHARED ${kdeprint2_LIB_SRCS}) + +target_link_libraries(kdeprint2 ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} ) + +set_target_properties(kdeprint2 PROPERTIES VERSION ${KDE_NON_GENERIC_LIB_VERSION} SOVERSION ${KDE_NON_GENERIC_LIB_SOVERSION} ) +install(TARGETS kdeprint2 + RUNTIME DESTINATION ${BIN_INSTALL_DIR} + LIBRARY DESTINATION ${LIB_INSTALL_DIR} + ARCHIVE DESTINATION ${LIB_INSTALL_DIR} +) + +########### install files ############### + +install( FILES + kprintpreview.h + kdeprint2_export.h + DESTINATION ${INCLUDE_INSTALL_DIR}/kdeprint2 +) + + + + Index: CMakeLists.txt =================================================================== --- CMakeLists.txt (revision 716348) +++ CMakeLists.txt (working copy) @@ -180,6 +180,7 @@ add_subdirectory( mimetypes ) add_subdirectory( doc ) add_subdirectory( kinit ) add_subdirectory( kdeprint ) +add_subdirectory( kdeprint2 ) add_subdirectory( threadweaver ) add_subdirectory( sonnet ) add_subdirectory( khtml ) --Boundary-01=_0N99GLe+HyKXsIJ-- --nextPart66152733.qHs8ObWW63 Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBG99N0BRauKLutZ9ARAnUaAJ9gFL+hTuzpp8IlfI/+k9GjbJ7UMgCfXcvG b7njAvP0huQD08A4oLfJFog= =giTx -----END PGP SIGNATURE----- --nextPart66152733.qHs8ObWW63--