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

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 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
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::setFull...
parent add7d391
......@@ -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
......
......@@ -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 <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>();
}
}
/***************************************************************************
* 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
......@@ -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 )
{
......
......@@ -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>
#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 );
......
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