From bd294e1de62b60d1ae33fc9513de63927215bfbe Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Mon, 3 Dec 2007 00:17:12 +0000 Subject: [PATCH] move the FilePrinterPreview to the kpart (no need for it to be in the core library) svn path=/trunk/KDE/kdegraphics/okular/; revision=744225 --- CMakeLists.txt | 5 +- core/fileprinter.cpp | 126 -------------------------------- core/fileprinter.h | 30 -------- part.cpp | 1 + ui/fileprinterpreview.cpp | 146 ++++++++++++++++++++++++++++++++++++++ ui/fileprinterpreview.h | 47 ++++++++++++ 6 files changed, 197 insertions(+), 158 deletions(-) create mode 100644 ui/fileprinterpreview.cpp create mode 100644 ui/fileprinterpreview.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b0da77dc5..069e51dbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,7 @@ IF(APPLE) SET(OKULAR_IOKIT "-framework IOKit" CACHE STRING "Apple IOKit framework") ENDIF(APPLE) -target_link_libraries(okularcore ${OKULAR_IOKIT} ${KDE4_KIO_LIBS} ${KDE4_KUTILS_LIBS} ${KDE4_PHONON_LIBRARY} ${MATH_LIB} ${KDE4_THREADWEAVER_LIBRARY} ) +target_link_libraries(okularcore ${OKULAR_IOKIT} ${KDE4_KIO_LIBS} ${KDE4_PHONON_LIBRARY} ${MATH_LIB} ${KDE4_THREADWEAVER_LIBRARY} ) set_target_properties(okularcore PROPERTIES VERSION 1.0.0 SOVERSION 1 ) @@ -118,6 +118,7 @@ set(okularpart_SRCS ui/annotationtools.cpp ui/annotationwidgets.cpp ui/bookmarklist.cpp + ui/fileprinterpreview.cpp ui/findbar.cpp ui/formwidgets.cpp ui/ktreeviewsearchline.cpp @@ -152,7 +153,7 @@ kde4_add_ui_files(okularpart_SRCS kde4_add_plugin(okularpart ${okularpart_SRCS}) -target_link_libraries(okularpart okularcore ${KDE4_KPARTS_LIBS} ${KDE4_KNEWSTUFF2_LIBS} ${MATH_LIB} ${BLITZ_LIBRARIES}) +target_link_libraries(okularpart okularcore ${KDE4_KPARTS_LIBS} ${KDE4_KUTILS_LIBS} ${KDE4_KNEWSTUFF2_LIBS} ${MATH_LIB} ${BLITZ_LIBRARIES}) install(TARGETS okularpart DESTINATION ${PLUGIN_INSTALL_DIR}) diff --git a/core/fileprinter.cpp b/core/fileprinter.cpp index 7c11bda39..52334da1a 100644 --- a/core/fileprinter.cpp +++ b/core/fileprinter.cpp @@ -25,11 +25,6 @@ #include #include #include -#include -#include -#include -#include -#include #include #include @@ -538,125 +533,4 @@ QStringList FilePrinter::optionCollateCopies( QPrinter &printer ) return QStringList("-o") << "Collate=False"; } -// This code copied from KPrintPreview by Alex Merry, adapted to do PS files instead of PDF - -class Okular::FilePrinterPreviewPrivate -{ -public: - FilePrinterPreviewPrivate( FilePrinterPreview *host, const QString & _filename ) - : q(host) - , mainWidget(new QWidget(host)) - , previewPart(0) - , failMessage(0) - { - filename = _filename; - } - - void getPart(); - bool doPreview(); - void fail(); - - FilePrinterPreview *q; - - QWidget *mainWidget; - - QString filename; - - KParts::ReadOnlyPart *previewPart; - QWidget *failMessage; -}; - -void FilePrinterPreviewPrivate::getPart() -{ - if (previewPart) { - kDebug(500) << "already got a part"; - return; - } - kDebug(500) << "querying trader for application/ps service"; - - KPluginFactory *factory(0); - KService::List offers = - KMimeTypeTrader::self()->query("application/postscript", "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 FilePrinterPreviewPrivate::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 PS viewer for the preview dialog"; - fail(); - return false; - } else { - q->setMainWidget(previewPart->widget()); - return previewPart->openUrl(filename); - } -} - -void FilePrinterPreviewPrivate::fail() -{ - if (!failMessage) { - failMessage = new QLabel(i18n("Could not load print preview part"), q); - } - q->setMainWidget(failMessage); -} - - - - -FilePrinterPreview::FilePrinterPreview( const QString &filename, QWidget *parent ) - : KDialog( parent ) - , d( new FilePrinterPreviewPrivate( this, filename ) ) -{ - kDebug(500) << "kdeprint: creating preview dialog"; - - // Set up the dialog - setCaption(i18n("Print Preview")); - setButtons(KDialog::Close); -} - -FilePrinterPreview::~FilePrinterPreview() -{ - delete d; -} - -void FilePrinterPreview::showEvent(QShowEvent *event) -{ - if (!event->spontaneous()) { - // being shown for the first time - if (!d->doPreview()) { - event->accept(); - return; - } - } - KDialog::showEvent(event); -} - #include "fileprinter.moc" - - - - - diff --git a/core/fileprinter.h b/core/fileprinter.h index 66060db25..b7ab1f2b7 100644 --- a/core/fileprinter.h +++ b/core/fileprinter.h @@ -1,9 +1,6 @@ /*************************************************************************** * Copyright (C) 2007 by John Layt * * * - * FilePrinterPreview based on KPrintPreview (originally LGPL) * - * Copyright (c) 2007 Alex Merry * - * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * @@ -20,8 +17,6 @@ #include #include -#include - #include class QPrinter; @@ -168,31 +163,6 @@ protected: QStringList optionCollateCopies( QPrinter &printer ); }; -// This code copied from KPrintPreview by Alex Merry, adapted to do PS files instead of PDF - -class FilePrinterPreviewPrivate; - -class OKULAR_EXPORT FilePrinterPreview : public KDialog -{ - Q_OBJECT - -public: - /** - * Create a Print Preview dialog for a given file. - * - * @param printer file to print preview - * @param parent pointer to the parent widget for the dialog - */ - explicit FilePrinterPreview( const QString &filename, QWidget *parent = 0 ); - virtual ~FilePrinterPreview(); - -protected: - void showEvent( QShowEvent *event ); - -private: - FilePrinterPreviewPrivate * const d; -}; - } #endif // FILEPRINTER_H diff --git a/part.cpp b/part.cpp index dedb2cadc..fca8b873b 100644 --- a/part.cpp +++ b/part.cpp @@ -73,6 +73,7 @@ #include "ui/bookmarklist.h" #include "ui/findbar.h" #include "ui/sidebar.h" +#include "ui/fileprinterpreview.h" #include "conf/preferencesdialog.h" #include "settings.h" #include "core/bookmarkmanager.h" diff --git a/ui/fileprinterpreview.cpp b/ui/fileprinterpreview.cpp new file mode 100644 index 000000000..174ec4828 --- /dev/null +++ b/ui/fileprinterpreview.cpp @@ -0,0 +1,146 @@ +/*************************************************************************** + * Copyright (C) 2007 by John Layt * + * * + * FilePrinterPreview based on KPrintPreview (originally LGPL) * + * Copyright (c) 2007 Alex Merry * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#include "fileprinterpreview.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace Okular; + +// This code copied from KPrintPreview by Alex Merry, adapted to do PS files instead of PDF + +class Okular::FilePrinterPreviewPrivate +{ +public: + FilePrinterPreviewPrivate( FilePrinterPreview *host, const QString & _filename ) + : q(host) + , mainWidget(new QWidget(host)) + , previewPart(0) + , failMessage(0) + { + filename = _filename; + } + + void getPart(); + bool doPreview(); + void fail(); + + FilePrinterPreview *q; + + QWidget *mainWidget; + + QString filename; + + KParts::ReadOnlyPart *previewPart; + QWidget *failMessage; +}; + +void FilePrinterPreviewPrivate::getPart() +{ + if (previewPart) { + kDebug(500) << "already got a part"; + return; + } + kDebug(500) << "querying trader for application/ps service"; + + KPluginFactory *factory(0); + KService::List offers = + KMimeTypeTrader::self()->query("application/postscript", "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 FilePrinterPreviewPrivate::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 PS viewer for the preview dialog"; + fail(); + return false; + } else { + q->setMainWidget(previewPart->widget()); + return previewPart->openUrl(filename); + } +} + +void FilePrinterPreviewPrivate::fail() +{ + if (!failMessage) { + failMessage = new QLabel(i18n("Could not load print preview part"), q); + } + q->setMainWidget(failMessage); +} + + + + +FilePrinterPreview::FilePrinterPreview( const QString &filename, QWidget *parent ) + : KDialog( parent ) + , d( new FilePrinterPreviewPrivate( this, filename ) ) +{ + kDebug(500) << "kdeprint: creating preview dialog"; + + // Set up the dialog + setCaption(i18n("Print Preview")); + setButtons(KDialog::Close); +} + +FilePrinterPreview::~FilePrinterPreview() +{ + delete d; +} + +void FilePrinterPreview::showEvent(QShowEvent *event) +{ + if (!event->spontaneous()) { + // being shown for the first time + if (!d->doPreview()) { + event->accept(); + return; + } + } + KDialog::showEvent(event); +} + +#include "fileprinterpreview.moc" diff --git a/ui/fileprinterpreview.h b/ui/fileprinterpreview.h new file mode 100644 index 000000000..27fdbe80e --- /dev/null +++ b/ui/fileprinterpreview.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * Copyright (C) 2007 by John Layt * + * * + * FilePrinterPreview based on KPrintPreview (originally LGPL) * + * Copyright (c) 2007 Alex Merry * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef FILEPRINTERPREVIEW_H +#define FILEPRINTERPREVIEW_H + +#include + +namespace Okular { + +// This code copied from KPrintPreview by Alex Merry, adapted to do PS files instead of PDF + +class FilePrinterPreviewPrivate; + +class FilePrinterPreview : public KDialog +{ + Q_OBJECT + +public: + /** + * Create a Print Preview dialog for a given file. + * + * @param printer file to print preview + * @param parent pointer to the parent widget for the dialog + */ + explicit FilePrinterPreview( const QString &filename, QWidget *parent = 0 ); + virtual ~FilePrinterPreview(); + +protected: + void showEvent( QShowEvent *event ); + +private: + FilePrinterPreviewPrivate * const d; +}; + +} + +#endif // FILEPRINTER_H