2007-12-03 00:17:12 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2007 by John Layt <john@layt.net> *
|
|
|
|
* *
|
|
|
|
* FilePrinterPreview based on KPrintPreview (originally LGPL) *
|
|
|
|
* Copyright (c) 2007 Alex Merry <huntedhacker@tiscali.co.uk> *
|
|
|
|
* *
|
|
|
|
* 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 <QFile>
|
|
|
|
#include <QSize>
|
2016-07-17 00:28:57 +00:00
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QShowEvent>
|
|
|
|
#include <QVBoxLayout>
|
2007-12-03 00:17:12 +00:00
|
|
|
|
2016-07-17 00:28:57 +00:00
|
|
|
#include <KWindowConfig>
|
2014-08-10 00:04:40 +00:00
|
|
|
#include <klocalizedstring.h>
|
2007-12-03 00:17:12 +00:00
|
|
|
#include <kmimetypetrader.h>
|
2014-08-09 19:08:17 +00:00
|
|
|
#include <kparts/readonlypart.h>
|
2007-12-03 00:17:12 +00:00
|
|
|
#include <kpluginfactory.h>
|
|
|
|
#include <kpluginloader.h>
|
2014-08-09 19:08:17 +00:00
|
|
|
#include <ksharedconfig.h>
|
2018-08-17 18:05:01 +00:00
|
|
|
#include <qloggingcategory.h>
|
2014-09-11 19:12:27 +00:00
|
|
|
|
|
|
|
#include "debug_ui.h"
|
2007-12-03 00:17:12 +00:00
|
|
|
|
|
|
|
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))
|
2017-09-05 21:27:18 +00:00
|
|
|
, previewPart(nullptr)
|
|
|
|
, failMessage(nullptr)
|
2015-10-29 12:37:11 +00:00
|
|
|
, config(KSharedConfig::openConfig(QStringLiteral("okularrc")))
|
2009-03-26 22:44:16 +00:00
|
|
|
|
2007-12-03 00:17:12 +00:00
|
|
|
{
|
2016-07-17 00:28:57 +00:00
|
|
|
mainlayout = new QVBoxLayout(q);
|
|
|
|
buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, q);
|
|
|
|
mainlayout->addWidget(buttonBox);
|
2007-12-03 00:17:12 +00:00
|
|
|
filename = _filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void getPart();
|
|
|
|
bool doPreview();
|
|
|
|
void fail();
|
|
|
|
|
|
|
|
FilePrinterPreview *q;
|
|
|
|
|
|
|
|
QWidget *mainWidget;
|
|
|
|
|
2016-07-17 00:28:57 +00:00
|
|
|
QDialogButtonBox *buttonBox;
|
|
|
|
|
|
|
|
QVBoxLayout *mainlayout;
|
|
|
|
|
2007-12-03 00:17:12 +00:00
|
|
|
QString filename;
|
|
|
|
|
|
|
|
KParts::ReadOnlyPart *previewPart;
|
|
|
|
QWidget *failMessage;
|
2009-03-26 22:44:16 +00:00
|
|
|
|
|
|
|
KSharedConfig::Ptr config;
|
2007-12-03 00:17:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void FilePrinterPreviewPrivate::getPart()
|
|
|
|
{
|
|
|
|
if (previewPart) {
|
2014-09-11 19:12:27 +00:00
|
|
|
qCDebug(OkularUiDebug) << "already got a part";
|
2007-12-03 00:17:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-11 19:12:27 +00:00
|
|
|
qCDebug(OkularUiDebug) << "querying trader for application/ps service";
|
2007-12-03 00:17:12 +00:00
|
|
|
|
2017-09-05 21:27:18 +00:00
|
|
|
KPluginFactory *factory(nullptr);
|
2016-08-13 20:29:50 +00:00
|
|
|
KService::List offers;
|
|
|
|
if (filename.endsWith(QStringLiteral(".ps"))) {
|
|
|
|
/* Explicitly look for the Okular/Ghostview part: no other PostScript
|
|
|
|
parts are available now; other parts which handles text are not
|
|
|
|
suitable here (PostScript source code) */
|
|
|
|
offers =
|
|
|
|
KMimeTypeTrader::self()->query(QStringLiteral("application/postscript"),
|
|
|
|
QStringLiteral("KParts/ReadOnlyPart"),
|
|
|
|
QStringLiteral("[DesktopEntryName] == 'okularghostview'"));
|
|
|
|
} else {
|
|
|
|
offers = KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart");
|
|
|
|
}
|
2007-12-03 00:17:12 +00:00
|
|
|
|
2008-11-11 18:48:40 +00:00
|
|
|
KService::List::ConstIterator it = offers.constBegin();
|
|
|
|
while (!factory && it != offers.constEnd()) {
|
2007-12-03 00:17:12 +00:00
|
|
|
KPluginLoader loader(**it);
|
|
|
|
factory = loader.factory();
|
|
|
|
if (!factory) {
|
2014-09-11 19:12:27 +00:00
|
|
|
qCDebug(OkularUiDebug) << "Loading failed:" << loader.errorString();
|
2007-12-03 00:17:12 +00:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
if (factory) {
|
2014-09-11 19:12:27 +00:00
|
|
|
qCDebug(OkularUiDebug) << "Trying to create a part";
|
2016-07-11 20:05:18 +00:00
|
|
|
previewPart = factory->create<KParts::ReadOnlyPart>(q, (QVariantList() << QStringLiteral("Print/Preview")));
|
2007-12-03 00:17:12 +00:00
|
|
|
if (!previewPart) {
|
2014-09-11 19:12:27 +00:00
|
|
|
qCDebug(OkularUiDebug) << "Part creation failed";
|
2007-12-03 00:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FilePrinterPreviewPrivate::doPreview()
|
|
|
|
{
|
|
|
|
if (!QFile::exists(filename)) {
|
2014-09-11 19:12:27 +00:00
|
|
|
qCWarning(OkularUiDebug) << "Nothing was produced to be previewed";
|
2007-12-03 00:17:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPart();
|
|
|
|
if (!previewPart) {
|
|
|
|
//TODO: error dialog
|
2014-09-11 19:12:27 +00:00
|
|
|
qCWarning(OkularUiDebug) << "Could not find a PS viewer for the preview dialog";
|
2007-12-03 00:17:12 +00:00
|
|
|
fail();
|
|
|
|
return false;
|
|
|
|
} else {
|
2016-07-17 00:28:57 +00:00
|
|
|
mainlayout->insertWidget(0, previewPart->widget());
|
2014-08-10 18:36:41 +00:00
|
|
|
return previewPart->openUrl(QUrl::fromLocalFile(filename));
|
2007-12-03 00:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilePrinterPreviewPrivate::fail()
|
|
|
|
{
|
|
|
|
if (!failMessage) {
|
|
|
|
failMessage = new QLabel(i18n("Could not load print preview part"), q);
|
|
|
|
}
|
2016-07-17 00:28:57 +00:00
|
|
|
mainlayout->insertWidget(0, failMessage);
|
2007-12-03 00:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FilePrinterPreview::FilePrinterPreview( const QString &filename, QWidget *parent )
|
2016-07-17 00:28:57 +00:00
|
|
|
: QDialog( parent )
|
2007-12-03 00:17:12 +00:00
|
|
|
, d( new FilePrinterPreviewPrivate( this, filename ) )
|
|
|
|
{
|
2014-09-11 19:12:27 +00:00
|
|
|
qCDebug(OkularUiDebug) << "kdeprint: creating preview dialog";
|
2007-12-03 00:17:12 +00:00
|
|
|
|
|
|
|
// Set up the dialog
|
2016-07-17 00:28:57 +00:00
|
|
|
setWindowTitle(i18n("Print Preview"));
|
|
|
|
|
|
|
|
connect(d->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
2009-03-26 22:44:16 +00:00
|
|
|
|
2016-07-17 00:28:57 +00:00
|
|
|
KWindowConfig::restoreWindowSize(windowHandle(), d->config->group("Print Preview"));
|
2007-12-03 00:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FilePrinterPreview::~FilePrinterPreview()
|
|
|
|
{
|
2009-03-26 22:44:16 +00:00
|
|
|
KConfigGroup group(d->config->group("Print Preview"));
|
2016-07-17 00:28:57 +00:00
|
|
|
KWindowConfig::saveWindowSize(windowHandle(), group);
|
2009-03-26 22:44:16 +00:00
|
|
|
|
2007-12-03 00:17:12 +00:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-03-26 22:44:16 +00:00
|
|
|
QSize FilePrinterPreview::sizeHint() const
|
|
|
|
{
|
|
|
|
// return a more or less useful window size, if not saved already
|
|
|
|
return QSize(600, 500);
|
|
|
|
}
|
|
|
|
|
2007-12-03 00:17:12 +00:00
|
|
|
void FilePrinterPreview::showEvent(QShowEvent *event)
|
|
|
|
{
|
|
|
|
if (!event->spontaneous()) {
|
|
|
|
// being shown for the first time
|
|
|
|
if (!d->doPreview()) {
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-07-17 00:28:57 +00:00
|
|
|
QDialog::showEvent(event);
|
2007-12-03 00:17:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 22:00:07 +00:00
|
|
|
#include "moc_fileprinterpreview.cpp"
|