[PATCH] Print: add the support to store margins and printer options

Lubomir I. Ivanov neolit123 at gmail.com
Thu Nov 13 14:57:42 PST 2014


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

All the print options will be stored after the user closes
or "cancels" the print dialog.

There seems to be no good way to store the last
selected page size, because print dialogs are different and
some just list them as strings - A4, A3, etc.

The patch also applies the following changes:
- renames display.h's 'struct options' to 'struct print_options'
as these were really just for the print dialog
- the print_options dialog now stores more options as 'bool'
- demote PrintDialog's 'printOptions' to 'private'

Fixes #653

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
i think this will break the Qt 4.x build so let's send
extra patches for that afterwards. i have no way to test
4.x ATM.

QPageLayout seems to have some differences between 4 and 5,
but perhaps there could be something more?

Not much can be done about the user comment in #653:
"only partly localization of print dialog"

we can eventually change the title at least, but things like
button and tooltips, not really i think.
---
 display.h              | 10 +++++----
 qt-ui/printdialog.cpp  | 56 +++++++++++++++++++++++++++++++++++++++++++++++---
 qt-ui/printdialog.h    |  3 ++-
 qt-ui/printlayout.cpp  | 10 ++++-----
 qt-ui/printlayout.h    |  4 ++--
 qt-ui/printoptions.cpp | 24 +++++++++++-----------
 qt-ui/printoptions.h   |  6 +++---
 7 files changed, 83 insertions(+), 30 deletions(-)

diff --git a/display.h b/display.h
index 96a83c5..f010144 100644
--- a/display.h
+++ b/display.h
@@ -35,16 +35,18 @@ typedef enum {
 
 extern struct divecomputer *select_dc(struct dive *);
 
-struct options {
-	enum {
+struct print_options {
+	enum print_type {
 		PRETTY,
 		TABLE,
 		TWOPERPAGE,
 		ONEPERPAGE
 	} type;
-	int print_selected;
-	int color_selected;
+	bool print_selected;
+	bool color_selected;
 	bool notes_up;
+	bool landscape;
+	int margins[4]; // left, top, right, bottom
 };
 
 extern unsigned int dc_number;
diff --git a/qt-ui/printdialog.cpp b/qt-ui/printdialog.cpp
index a65078b..204f8a5 100644
--- a/qt-ui/printdialog.cpp
+++ b/qt-ui/printdialog.cpp
@@ -13,12 +13,42 @@
 #include <QShortcut>
 #include <QPrinterInfo>
 #include <QMessageBox>
+#include <QSettings>
+#include <QMarginsF>
+
+#define SETTINGS_GROUP "PrintDialog"
 
 PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
 {
-	// options template (are we storing these in the settings?)
-	struct options tempOptions = { options::PRETTY, 1, 2, false };
-	printOptions = tempOptions;
+	// check if the options were previously stored in the settings; if not use some defaults.
+	QSettings s;
+	bool stored = s.childGroups().contains(SETTINGS_GROUP);
+	if (!stored) {
+		printOptions.type = print_options::PRETTY;
+		printOptions.print_selected = true;
+		printOptions.color_selected = true;
+		printOptions.notes_up = false;
+		printOptions.landscape = false;
+		memset(printOptions.margins, 0, sizeof(printOptions.margins));
+	} else {
+		s.beginGroup(SETTINGS_GROUP);
+		printOptions.type = (print_options::print_type)s.value("type").toInt();
+		printOptions.print_selected = s.value("print_selected").toBool();
+		printOptions.color_selected = s.value("color_selected").toBool();
+		printOptions.notes_up = s.value("notes_up").toBool();
+		printOptions.landscape = s.value("landscape").toBool();
+		printOptions.margins[0] = s.value("margin_left").toInt();
+		printOptions.margins[1] = s.value("margin_top").toInt();
+		printOptions.margins[2] = s.value("margin_right").toInt();
+		printOptions.margins[3] = s.value("margin_bottom").toInt();
+		printer.setOrientation((QPrinter::Orientation)printOptions.landscape);
+		QMarginsF margins;
+		margins.setLeft(printOptions.margins[0]);
+		margins.setRight(printOptions.margins[1]);
+		margins.setTop(printOptions.margins[2]);
+		margins.setBottom(printOptions.margins[3]);
+		printer.setPageMargins(margins, QPageLayout::Millimeter);
+	}
 
 	// create a print layout and pass the printer and options
 	printLayout = new PrintLayout(this, &printer, &printOptions);
@@ -64,6 +94,26 @@ PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f
 	connect(close, SIGNAL(activated()), this, SLOT(close()));
 	QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
 	connect(quit, SIGNAL(activated()), parent, SLOT(close()));
+
+	// seems to be the most reliable way to track for all sorts of dialog disposal.
+	connect(this, SIGNAL(finished(int)), this, SLOT(onFinished()));
+}
+
+void PrintDialog::onFinished()
+{
+	// save the settings
+	QSettings s;
+	s.beginGroup(SETTINGS_GROUP);
+	s.setValue("type", printOptions.type);
+	s.setValue("print_selected", printOptions.print_selected);
+	s.setValue("color_selected", printOptions.color_selected);
+	s.setValue("notes_up", printOptions.notes_up);
+	s.setValue("landscape", (bool)printer.pageLayout().orientation());
+	QMarginsF margins = printer.pageLayout().margins(QPageLayout::Millimeter);
+	s.setValue("margin_left", margins.left());
+	s.setValue("margin_right", margins.top());
+	s.setValue("margin_top", margins.right());
+	s.setValue("margin_bottom", margins.bottom());
 }
 
 void PrintDialog::previewClicked(void)
diff --git a/qt-ui/printdialog.h b/qt-ui/printdialog.h
index 32069a2..b42102d 100644
--- a/qt-ui/printdialog.h
+++ b/qt-ui/printdialog.h
@@ -14,7 +14,6 @@ class PrintDialog : public QDialog {
 	Q_OBJECT
 
 public:
-	struct options printOptions;
 	explicit PrintDialog(QWidget *parent = 0, Qt::WindowFlags f = 0);
 
 private:
@@ -22,9 +21,11 @@ private:
 	PrintLayout *printLayout;
 	QProgressBar *progressBar;
 	QPrinter printer;
+	struct print_options printOptions;
 
 private
 slots:
+	void onFinished();
 	void previewClicked();
 	void printClicked();
 	void onPaintRequested(QPrinter *);
diff --git a/qt-ui/printlayout.cpp b/qt-ui/printlayout.cpp
index f9b71d9..63819ad 100644
--- a/qt-ui/printlayout.cpp
+++ b/qt-ui/printlayout.cpp
@@ -17,7 +17,7 @@
 #include "models.h"
 #include "modeldelegates.h"
 
-PrintLayout::PrintLayout(PrintDialog *dialogPtr, QPrinter *printerPtr, struct options *optionsPtr)
+PrintLayout::PrintLayout(PrintDialog *dialogPtr, QPrinter *printerPtr, struct print_options *optionsPtr)
 {
 	dialog = dialogPtr;
 	printer = printerPtr;
@@ -74,16 +74,16 @@ void PrintLayout::print()
 		return;
 	}
 	switch (printOptions->type) {
-	case options::PRETTY:
+	case print_options::PRETTY:
 		printProfileDives(3, 2);
 		break;
-	case options::ONEPERPAGE:
+	case print_options::ONEPERPAGE:
 		printProfileDives(1, 1);
 		break;
-	case options::TWOPERPAGE:
+	case print_options::TWOPERPAGE:
 		printProfileDives(2, 1);
 		break;
-	case options::TABLE:
+	case print_options::TABLE:
 		printTable();
 		break;
 	}
diff --git a/qt-ui/printlayout.h b/qt-ui/printlayout.h
index 9a816a3..d8c730a 100644
--- a/qt-ui/printlayout.h
+++ b/qt-ui/printlayout.h
@@ -17,13 +17,13 @@ class PrintLayout : public QObject {
 	Q_OBJECT
 
 public:
-	PrintLayout(PrintDialog *, QPrinter *, struct options *);
+	PrintLayout(PrintDialog *, QPrinter *, struct print_options *);
 	void print();
 
 private:
 	PrintDialog *dialog;
 	QPrinter *printer;
-	struct options *printOptions;
+	struct print_options *printOptions;
 
 	int screenDpiX, screenDpiY, printerDpi, pageW, pageH;
 	QRect pageRect;
diff --git a/qt-ui/printoptions.cpp b/qt-ui/printoptions.cpp
index 818e89c..21e2999 100644
--- a/qt-ui/printoptions.cpp
+++ b/qt-ui/printoptions.cpp
@@ -1,7 +1,7 @@
 #include "printoptions.h"
 #include "../display.h"
 
-PrintOptions::PrintOptions(QWidget *parent, struct options *printOpt)
+PrintOptions::PrintOptions(QWidget *parent, struct print_options *printOpt)
 {
 	hasSetupSlots = false;
 	ui.setupUi(this);
@@ -12,21 +12,21 @@ PrintOptions::PrintOptions(QWidget *parent, struct options *printOpt)
 	setup(printOpt);
 }
 
-void PrintOptions::setup(struct options *printOpt)
+void PrintOptions::setup(struct print_options *printOpt)
 {
 	printOptions = printOpt;
 	// print type radio buttons
 	switch (printOptions->type) {
-	case options::PRETTY:
+	case print_options::PRETTY:
 		ui.radioSixDives->setChecked(true);
 		break;
-	case options::TWOPERPAGE:
+	case print_options::TWOPERPAGE:
 		ui.radioTwoDives->setChecked(true);
 		break;
-	case options::ONEPERPAGE:
+	case print_options::ONEPERPAGE:
 		ui.radioOneDive->setChecked(true);
 		break;
-	case options::TABLE:
+	case print_options::TABLE:
 		ui.radioTablePrint->setChecked(true);
 		break;
 	}
@@ -61,33 +61,33 @@ void PrintOptions::setup(struct options *printOpt)
 // print type radio buttons
 void PrintOptions::radioSixDivesClicked(bool check)
 {
-	printOptions->type = options::PRETTY;
+	printOptions->type = print_options::PRETTY;
 }
 
 void PrintOptions::radioTwoDivesClicked(bool check)
 {
-	printOptions->type = options::TWOPERPAGE;
+	printOptions->type = print_options::TWOPERPAGE;
 }
 
 void PrintOptions::radioOneDiveClicked(bool check)
 {
-	printOptions->type = options::ONEPERPAGE;
+	printOptions->type = print_options::ONEPERPAGE;
 }
 
 void PrintOptions::radioTablePrintClicked(bool check)
 {
-	printOptions->type = options::TABLE;
+	printOptions->type = print_options::TABLE;
 }
 
 // general print option checkboxes
 void PrintOptions::printInColorClicked(bool check)
 {
-	printOptions->color_selected = (int)check;
+	printOptions->color_selected = check;
 }
 
 void PrintOptions::printSelectedClicked(bool check)
 {
-	printOptions->print_selected = (int)check;
+	printOptions->print_selected = check;
 }
 
 // ordering
diff --git a/qt-ui/printoptions.h b/qt-ui/printoptions.h
index e14ae46..658e7ec 100644
--- a/qt-ui/printoptions.h
+++ b/qt-ui/printoptions.h
@@ -10,12 +10,12 @@ class PrintOptions : public QWidget {
 	Q_OBJECT
 
 public:
-	explicit PrintOptions(QWidget *parent = 0, struct options *printOpt = 0);
-	void setup(struct options *printOpt);
+	explicit PrintOptions(QWidget *parent = 0, struct print_options *printOpt = 0);
+	void setup(struct print_options *printOpt);
 
 private:
 	Ui::PrintOptions ui;
-	struct options *printOptions;
+	struct print_options *printOptions;
 	bool hasSetupSlots;
 
 private
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list