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 2e97d58750
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 <aacid@kde.org> 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
This commit is contained in:
Michael Weghorn 2019-04-03 07:58:21 -06:00 committed by Nate Graham
parent add7d39179
commit 8bf1a91175
9 changed files with 133 additions and 8 deletions

View file

@ -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

View file

@ -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;

View file

@ -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)

View file

@ -0,0 +1,36 @@
/***************************************************************************
* Copyright (C) 2019 Michael Weghorn <m.weghorn@posteo.de> *
* *
* 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>();
}
}

54
core/printoptionswidget.h Normal file
View file

@ -0,0 +1,54 @@
/***************************************************************************
* Copyright (C) 2019 Michael Weghorn <m.weghorn@posteo.de> *
* *
* 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 <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
{
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

View file

@ -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
{
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 )
{

View file

@ -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;

View file

@ -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;
};

View file

@ -117,6 +117,7 @@
#include "core/generator.h"
#include "core/page.h"
#include "core/fileprinter.h"
#include "core/printoptionswidget.h"
#include <memory>
#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<PrintOptionsWidget *>(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 );