From 8bf1a911751beb4e65400223b896d42402476e9c Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Wed, 3 Apr 2019 07:58:21 -0600 Subject: [PATCH] Add option to ignore print margins for non-PDF generators Summary: This adds a combobox in the print dialog of the non-PDF generators to allow selecting whether or not to take print margins into account. For the PDF case and rasterized printing, new print otions have been implemented in commit 2e97d587508dff08aaf86ff149c8ed6b7658950d already, which adds an additional option to do no scaling at all. For consistency reasons, the same terms also used for the PDF case are used in the combobox (i.e. the two of the three that apply). This adds a new abstract class 'PrintOptionsWidget' with a 'ignorePrintMargins()' method to indicate whether print margins should be ignored or not, and a default implementation. The existing widget for the PDF generator now derives from this class. In order to avoid an ABI breakage, the return value of 'Document::printConfigurationWidget' is left as a 'QWidget *' and a dynamic_cast is done on use. FilePrinter is adapted to take into account the value set by 'QPrinter::setFullPage()' and the margin options are now passed accordingly (either the values set in the dialog or '0'). A big thanks to Albert Astals Cid for showing how to extend the initial implementation to cover more generators. Test Plan: 1) Open a PostScript file in Okular (using a document size that matches a paper size available on the printer used later makes it easier to see things behave as expected) 2) open print dialog, go to "Print options" and notice that there is a new "Scale mode" combobox whose value is set to "Fit to printable area" by default. 3) don't change any options, print to a printer that has hardware margins Expected result: the document is scaled to the printable area (e.g. scaled down so that the printer's hardware margins remain empty) as it has been without this change. 4) Set the value of the "Print Options" -> "Scale mode" combobox to "Fit to full page" and print again Expected result: The document is scaled to the full page size, i.e. ignoring the printer's hardware margins. 5) Try steps 1-4 with other document formats supported by Okular and observe that they behave the same (except for the PDF case, where there's a combobox with three options that has been implemented independent of this change). Reviewers: #okular, ngraham Reviewed By: ngraham Subscribers: fvogt, rkflx, arthurpeters, ltoscano, okular-devel, aacid, ngraham Tags: #okular Differential Revision: https://phabricator.kde.org/D10974 --- CMakeLists.txt | 2 ++ core/document.h | 4 +++ core/fileprinter.cpp | 6 ++-- core/printoptionswidget.cpp | 36 +++++++++++++++++++ core/printoptionswidget.h | 54 ++++++++++++++++++++++++++++ generators/poppler/generator_pdf.cpp | 12 ++++--- generators/poppler/generator_pdf.h | 3 +- interfaces/printinterface.h | 4 +++ part.cpp | 20 ++++++++++- 9 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 core/printoptionswidget.cpp create mode 100644 core/printoptionswidget.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ae4fa6af0..fe2525329 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,7 @@ set(okularcore_SRCS core/utils.cpp core/view.cpp core/fileprinter.cpp + core/printoptionswidget.cpp core/signatureutils.cpp core/script/event.cpp core/synctex/synctex_parser.c @@ -229,6 +230,7 @@ install( FILES core/tile.h core/utils.h core/fileprinter.h + core/printoptionswidget.h core/observer.h ${CMAKE_CURRENT_BINARY_DIR}/core/version.h ${CMAKE_CURRENT_BINARY_DIR}/core/okularcore_export.h diff --git a/core/document.h b/core/document.h index cf98aacbe..8485befc4 100644 --- a/core/document.h +++ b/core/document.h @@ -723,6 +723,10 @@ class OKULARCORE_EXPORT Document : public QObject /** * Returns a custom printer configuration page or 0 if no * custom printer configuration page is available. + * + * The returned object should be of a PrintOptionsWidget subclass + * (which is not officially enforced by the signature for binary + * compatibility reasons). */ QWidget* printConfigurationWidget() const; diff --git a/core/fileprinter.cpp b/core/fileprinter.cpp index cc476a2f2..5c9c57200 100644 --- a/core/fileprinter.cpp +++ b/core/fileprinter.cpp @@ -615,8 +615,10 @@ QStringList FilePrinter::optionPageMargins( QPrinter &printer ) if (printer.printEngine()->property(QPrintEngine::PPK_PageMargins).isNull()) { return QStringList(); } else { - qreal l, t, r, b; - printer.getPageMargins( &l, &t, &r, &b, QPrinter::Point ); + qreal l(0), t(0), r(0), b(0); + if (!printer.fullPage()) { + printer.getPageMargins( &l, &t, &r, &b, QPrinter::Point ); + } return QStringList(QStringLiteral("-o")) << QStringLiteral("page-left=%1").arg(l) << QStringLiteral("-o") << QStringLiteral("page-top=%1").arg(t) << QStringLiteral("-o") << QStringLiteral("page-right=%1").arg(r) diff --git a/core/printoptionswidget.cpp b/core/printoptionswidget.cpp new file mode 100644 index 000000000..989674b7d --- /dev/null +++ b/core/printoptionswidget.cpp @@ -0,0 +1,36 @@ +/*************************************************************************** + * Copyright (C) 2019 Michael Weghorn * + * * + * 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 "printoptionswidget.h" + +#include +#include + +#include + +namespace Okular { + +DefaultPrintOptionsWidget::DefaultPrintOptionsWidget(QWidget *parent) + : PrintOptionsWidget(parent) +{ + setWindowTitle( i18n( "Print Options" ) ); + QFormLayout *layout = new QFormLayout(this); + m_ignorePrintMargins = new QComboBox; + // value indicates whether full page is enabled (i.e. print margins ignored) + m_ignorePrintMargins->insertItem(0, i18n("Fit to printable area"), false); + m_ignorePrintMargins->insertItem(1, i18n("Fit to full page"), true); + layout->addRow(i18n("Scale mode:"), m_ignorePrintMargins); +} + +bool DefaultPrintOptionsWidget::ignorePrintMargins() const +{ + return m_ignorePrintMargins->currentData().value(); +} + +} diff --git a/core/printoptionswidget.h b/core/printoptionswidget.h new file mode 100644 index 000000000..d1a0deeb5 --- /dev/null +++ b/core/printoptionswidget.h @@ -0,0 +1,54 @@ +/*************************************************************************** + * Copyright (C) 2019 Michael Weghorn * + * * + * 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 PRINTOPTIONSWIDGET_H +#define PRINTOPTIONSWIDGET_H + +#include + +#include "okularcore_export.h" + +class QComboBox; +class QFormLayout; + +namespace Okular { + +/** + * @short Abstract base class for an extra print options widget in the print dialog. + */ +class OKULARCORE_EXPORT PrintOptionsWidget : public QWidget +{ + public: + explicit PrintOptionsWidget(QWidget * parent = nullptr) + : QWidget(parent) {} + virtual bool ignorePrintMargins() const = 0; +}; + +/** + * @short The default okular extra print options widget. + * + * It just implements the required method 'ignorePrintMargins()' from + * the base class 'PrintOptionsWidget'. + */ +class OKULARCORE_EXPORT DefaultPrintOptionsWidget : public PrintOptionsWidget +{ + Q_OBJECT + + public: + explicit DefaultPrintOptionsWidget(QWidget *parent = nullptr); + + bool ignorePrintMargins() const override; + + private: + QComboBox *m_ignorePrintMargins; +}; + +} + +#endif diff --git a/generators/poppler/generator_pdf.cpp b/generators/poppler/generator_pdf.cpp index 6bce88c8c..0cfd86d4d 100644 --- a/generators/poppler/generator_pdf.cpp +++ b/generators/poppler/generator_pdf.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -76,7 +77,7 @@ Q_DECLARE_METATYPE(const Poppler::LinkOCGState*) static const int defaultPageWidth = 595; static const int defaultPageHeight = 842; -class PDFOptionsPage : public QWidget +class PDFOptionsPage : public Okular::PrintOptionsWidget { Q_OBJECT public slots: @@ -137,6 +138,10 @@ public slots: setPrintAnnots( true ); // Default value } + bool ignorePrintMargins() const override { + return scaleMode() == FitToPage; + } + bool printAnnots() { return m_printAnnots->isChecked(); @@ -1369,8 +1374,7 @@ bool PDFGenerator::print( QPrinter& printer ) #endif // If requested, scale to full page instead of the printable area - if ( scaleMode == PDFOptionsPage::FitToPage ) - printer.setFullPage( true ); + printer.setFullPage( pdfOptionsPage->ignorePrintMargins() ); QPainter painter; painter.begin(&printer); @@ -1996,7 +2000,7 @@ PDFGenerator::PrintError PDFGenerator::printError() const return lastPrintError; } -QWidget* PDFGenerator::printConfigurationWidget() const +Okular::PrintOptionsWidget* PDFGenerator::printConfigurationWidget() const { if ( !pdfOptionsPage ) { diff --git a/generators/poppler/generator_pdf.h b/generators/poppler/generator_pdf.h index b168d045a..cc679d108 100644 --- a/generators/poppler/generator_pdf.h +++ b/generators/poppler/generator_pdf.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -95,7 +96,7 @@ class PDFGenerator : public Okular::Generator, public Okular::ConfigInterface, p bool exportTo( const QString &fileName, const Okular::ExportFormat &format ) override; // [INHERITED] print interface - QWidget* printConfigurationWidget() const override; + Okular::PrintOptionsWidget* printConfigurationWidget() const override; // [INHERITED] save interface bool supportsOption( SaveOption ) const override; diff --git a/interfaces/printinterface.h b/interfaces/printinterface.h index ac51272f8..334deda6e 100644 --- a/interfaces/printinterface.h +++ b/interfaces/printinterface.h @@ -49,6 +49,10 @@ class OKULARCORE_EXPORT PrintInterface * * @note don't keep a pointer to the new constructed widget, as it * will be handled elsewhere (in the Okular KPart) + * + * @note The returned object should be of a PrintOptionsWidget subclass + * (which is not officially enforced by the signature for binary + * compatibility reasons). */ virtual QWidget* printConfigurationWidget() const = 0; }; diff --git a/part.cpp b/part.cpp index 4ef3d0f43..869ee8a40 100644 --- a/part.cpp +++ b/part.cpp @@ -117,6 +117,7 @@ #include "core/generator.h" #include "core/page.h" #include "core/fileprinter.h" +#include "core/printoptionswidget.h" #include #ifdef OKULAR_KEEP_FILE_OPEN @@ -3302,6 +3303,10 @@ void Part::slotPrint() { printConfigWidget = m_document->printConfigurationWidget(); } + else + { + printConfigWidget = new DefaultPrintOptionsWidget(); + } printDialog = new QPrintDialog(&printer, widget()); printDialog->setWindowTitle(i18nc("@title:window", "Print")); @@ -3309,7 +3314,7 @@ void Part::slotPrint() if (printConfigWidget) { options << printConfigWidget; } - printDialog->setOptionTabs(options); + printDialog->setOptionTabs(options); if ( printDialog ) { @@ -3339,7 +3344,20 @@ void Part::slotPrint() bool success = true; if ( printDialog->exec() ) + { + // set option for margins if widget is of corresponding type that holds this information + PrintOptionsWidget *optionWidget = dynamic_cast(printConfigWidget); + if (optionWidget != nullptr) + printer.setFullPage( optionWidget->ignorePrintMargins() ); + else + { + // printConfigurationWidget() method should always return an object of type Okular::PrintOptionsWidget, + // (signature does not (yet) require it for ABI stability reasons), so emit a warning if the object is of another type + qWarning() << "printConfigurationWidget() method did not return an Okular::PrintOptionsWidget. This is strongly discouraged!"; + } + success = doPrint( printer ); + } delete printDialog; if ( m_cliPrintAndExit ) exit ( success ? EXIT_SUCCESS : EXIT_FAILURE );