diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,7 @@ 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 @@ 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 --- a/core/document.h +++ b/core/document.h @@ -723,6 +723,10 @@ /** * 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/document.cpp b/core/document.cpp --- a/core/document.cpp +++ b/core/document.cpp @@ -80,6 +80,7 @@ #include "page.h" #include "page_p.h" #include "pagecontroller_p.h" +#include "printoptionswidget.h" #include "scripter.h" #include "script/event_p.h" #include "settings_core.h" diff --git a/core/fileprinter.cpp b/core/fileprinter.cpp --- a/core/fileprinter.cpp +++ b/core/fileprinter.cpp @@ -615,8 +615,10 @@ 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.h b/core/printoptionswidget.h new file mode 100644 --- /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); + + virtual bool ignorePrintMargins() const override; + + private: + QComboBox *m_ignorePrintMargins; +}; + +} + +#endif diff --git a/core/printoptionswidget.cpp b/core/printoptionswidget.cpp new file mode 100644 --- /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/generators/poppler/generator_pdf.h b/generators/poppler/generator_pdf.h --- a/generators/poppler/generator_pdf.h +++ b/generators/poppler/generator_pdf.h @@ -30,6 +30,7 @@ namespace Okular { class ObjectRect; +class PrintOptionsWidget; class SourceReference; class SignatureInfo; } @@ -95,7 +96,7 @@ 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/generators/poppler/generator_pdf.cpp b/generators/poppler/generator_pdf.cpp --- 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 @@ 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 @@ setPrintAnnots( true ); // Default value } + bool ignorePrintMargins() const override { + return scaleMode() == FitToPage; + } + bool printAnnots() { return m_printAnnots->isChecked(); @@ -1366,8 +1371,7 @@ #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); @@ -1993,7 +1997,7 @@ return lastPrintError; } -QWidget* PDFGenerator::printConfigurationWidget() const +Okular::PrintOptionsWidget* PDFGenerator::printConfigurationWidget() const { if ( !pdfOptionsPage ) { diff --git a/interfaces/printinterface.h b/interfaces/printinterface.h --- a/interfaces/printinterface.h +++ b/interfaces/printinterface.h @@ -14,10 +14,10 @@ #include -class QWidget; - namespace Okular { +class PrintOptionsWidget; + /** * @short Abstract interface for advanced printing control * @@ -50,7 +50,7 @@ * @note don't keep a pointer to the new constructed widget, as it * will be handled elsewhere (in the Okular KPart) */ - virtual QWidget* printConfigurationWidget() const = 0; + virtual PrintOptionsWidget* printConfigurationWidget() const = 0; }; } diff --git a/part.cpp b/part.cpp --- 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 @@ -3294,14 +3295,18 @@ { printConfigWidget = m_document->printConfigurationWidget(); } + else + { + printConfigWidget = new DefaultPrintOptionsWidget(); + } printDialog = new QPrintDialog(&printer, widget()); printDialog->setWindowTitle(i18nc("@title:window", "Print")); QList options; if (printConfigWidget) { options << printConfigWidget; } - printDialog->setOptionTabs(options); + printDialog->setOptionTabs(options); if ( printDialog ) { @@ -3331,7 +3336,14 @@ 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() ); + success = doPrint( printer ); + } delete printDialog; if ( m_cliPrintAndExit ) exit ( success ? EXIT_SUCCESS : EXIT_FAILURE );