Commit 8bf1a911 authored by Michael Weghorn's avatar Michael Weghorn Committed by Nate Graham

Add option to ignore print margins for non-PDF generators

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 2e97d587
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

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

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:
parent add7d391
......@@ -195,6 +195,7 @@ set(okularcore_SRCS
......@@ -229,6 +230,7 @@ install( FILES
......@@ -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;
......@@ -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)
* 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 <QComboBox>
#include <QFormLayout>
#include <KLocalizedString>
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<bool>();
* 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 <QWidget>
#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
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
explicit DefaultPrintOptionsWidget(QWidget *parent = nullptr);
bool ignorePrintMargins() const override;
QComboBox *m_ignorePrintMargins;
......@@ -44,6 +44,7 @@
#include <core/annotations.h>
#include <core/movie.h>
#include <core/pagetransition.h>
#include <core/printoptionswidget.h>
#include <core/sound.h>
#include <core/sourcereference.h>
#include <core/textpage.h>
......@@ -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
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 )
// If requested, scale to full page instead of the printable area
if ( scaleMode == PDFOptionsPage::FitToPage )
printer.setFullPage( true );
printer.setFullPage( pdfOptionsPage->ignorePrintMargins() );
QPainter painter;
......@@ -1996,7 +2000,7 @@ PDFGenerator::PrintError PDFGenerator::printError() const
return lastPrintError;
QWidget* PDFGenerator::printConfigurationWidget() const
Okular::PrintOptionsWidget* PDFGenerator::printConfigurationWidget() const
if ( !pdfOptionsPage )
......@@ -24,6 +24,7 @@
#include <core/document.h>
#include <core/generator.h>
#include <core/printoptionswidget.h>
#include <interfaces/configinterface.h>
#include <interfaces/printinterface.h>
#include <interfaces/saveinterface.h>
......@@ -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;
......@@ -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;
......@@ -117,6 +117,7 @@
#include "core/generator.h"
#include "core/page.h"
#include "core/fileprinter.h"
#include "core/printoptionswidget.h"
#include <memory>
......@@ -3302,6 +3303,10 @@ void Part::slotPrint()
printConfigWidget = m_document->printConfigurationWidget();
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;
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<PrintOptionsWidget *>(printConfigWidget);
if (optionWidget != nullptr)
printer.setFullPage( optionWidget->ignorePrintMargins() );
// 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 );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment