Print: add the support to store margins and printer options

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@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2014-11-14 00:57:42 +02:00 committed by Dirk Hohndel
parent b2077dc9c0
commit 7ea728e95f
7 changed files with 83 additions and 30 deletions

View file

@ -35,16 +35,18 @@ typedef enum {
extern struct divecomputer *select_dc(struct dive *); extern struct divecomputer *select_dc(struct dive *);
struct options { struct print_options {
enum { enum print_type {
PRETTY, PRETTY,
TABLE, TABLE,
TWOPERPAGE, TWOPERPAGE,
ONEPERPAGE ONEPERPAGE
} type; } type;
int print_selected; bool print_selected;
int color_selected; bool color_selected;
bool notes_up; bool notes_up;
bool landscape;
int margins[4]; // left, top, right, bottom
}; };
extern unsigned int dc_number; extern unsigned int dc_number;

View file

@ -13,12 +13,42 @@
#include <QShortcut> #include <QShortcut>
#include <QPrinterInfo> #include <QPrinterInfo>
#include <QMessageBox> #include <QMessageBox>
#include <QSettings>
#include <QMarginsF>
#define SETTINGS_GROUP "PrintDialog"
PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
{ {
// options template (are we storing these in the settings?) // check if the options were previously stored in the settings; if not use some defaults.
struct options tempOptions = { options::PRETTY, 1, 2, false }; QSettings s;
printOptions = tempOptions; 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 // create a print layout and pass the printer and options
printLayout = new PrintLayout(this, &printer, &printOptions); 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())); connect(close, SIGNAL(activated()), this, SLOT(close()));
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this); QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
connect(quit, SIGNAL(activated()), parent, SLOT(close())); 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) void PrintDialog::previewClicked(void)

View file

@ -14,7 +14,6 @@ class PrintDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
struct options printOptions;
explicit PrintDialog(QWidget *parent = 0, Qt::WindowFlags f = 0); explicit PrintDialog(QWidget *parent = 0, Qt::WindowFlags f = 0);
private: private:
@ -22,9 +21,11 @@ private:
PrintLayout *printLayout; PrintLayout *printLayout;
QProgressBar *progressBar; QProgressBar *progressBar;
QPrinter printer; QPrinter printer;
struct print_options printOptions;
private private
slots: slots:
void onFinished();
void previewClicked(); void previewClicked();
void printClicked(); void printClicked();
void onPaintRequested(QPrinter *); void onPaintRequested(QPrinter *);

View file

@ -17,7 +17,7 @@
#include "models.h" #include "models.h"
#include "modeldelegates.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; dialog = dialogPtr;
printer = printerPtr; printer = printerPtr;
@ -74,16 +74,16 @@ void PrintLayout::print()
return; return;
} }
switch (printOptions->type) { switch (printOptions->type) {
case options::PRETTY: case print_options::PRETTY:
printProfileDives(3, 2); printProfileDives(3, 2);
break; break;
case options::ONEPERPAGE: case print_options::ONEPERPAGE:
printProfileDives(1, 1); printProfileDives(1, 1);
break; break;
case options::TWOPERPAGE: case print_options::TWOPERPAGE:
printProfileDives(2, 1); printProfileDives(2, 1);
break; break;
case options::TABLE: case print_options::TABLE:
printTable(); printTable();
break; break;
} }

View file

@ -17,13 +17,13 @@ class PrintLayout : public QObject {
Q_OBJECT Q_OBJECT
public: public:
PrintLayout(PrintDialog *, QPrinter *, struct options *); PrintLayout(PrintDialog *, QPrinter *, struct print_options *);
void print(); void print();
private: private:
PrintDialog *dialog; PrintDialog *dialog;
QPrinter *printer; QPrinter *printer;
struct options *printOptions; struct print_options *printOptions;
int screenDpiX, screenDpiY, printerDpi, pageW, pageH; int screenDpiX, screenDpiY, printerDpi, pageW, pageH;
QRect pageRect; QRect pageRect;

View file

@ -1,7 +1,7 @@
#include "printoptions.h" #include "printoptions.h"
#include "../display.h" #include "../display.h"
PrintOptions::PrintOptions(QWidget *parent, struct options *printOpt) PrintOptions::PrintOptions(QWidget *parent, struct print_options *printOpt)
{ {
hasSetupSlots = false; hasSetupSlots = false;
ui.setupUi(this); ui.setupUi(this);
@ -12,21 +12,21 @@ PrintOptions::PrintOptions(QWidget *parent, struct options *printOpt)
setup(printOpt); setup(printOpt);
} }
void PrintOptions::setup(struct options *printOpt) void PrintOptions::setup(struct print_options *printOpt)
{ {
printOptions = printOpt; printOptions = printOpt;
// print type radio buttons // print type radio buttons
switch (printOptions->type) { switch (printOptions->type) {
case options::PRETTY: case print_options::PRETTY:
ui.radioSixDives->setChecked(true); ui.radioSixDives->setChecked(true);
break; break;
case options::TWOPERPAGE: case print_options::TWOPERPAGE:
ui.radioTwoDives->setChecked(true); ui.radioTwoDives->setChecked(true);
break; break;
case options::ONEPERPAGE: case print_options::ONEPERPAGE:
ui.radioOneDive->setChecked(true); ui.radioOneDive->setChecked(true);
break; break;
case options::TABLE: case print_options::TABLE:
ui.radioTablePrint->setChecked(true); ui.radioTablePrint->setChecked(true);
break; break;
} }
@ -61,33 +61,33 @@ void PrintOptions::setup(struct options *printOpt)
// print type radio buttons // print type radio buttons
void PrintOptions::radioSixDivesClicked(bool check) void PrintOptions::radioSixDivesClicked(bool check)
{ {
printOptions->type = options::PRETTY; printOptions->type = print_options::PRETTY;
} }
void PrintOptions::radioTwoDivesClicked(bool check) void PrintOptions::radioTwoDivesClicked(bool check)
{ {
printOptions->type = options::TWOPERPAGE; printOptions->type = print_options::TWOPERPAGE;
} }
void PrintOptions::radioOneDiveClicked(bool check) void PrintOptions::radioOneDiveClicked(bool check)
{ {
printOptions->type = options::ONEPERPAGE; printOptions->type = print_options::ONEPERPAGE;
} }
void PrintOptions::radioTablePrintClicked(bool check) void PrintOptions::radioTablePrintClicked(bool check)
{ {
printOptions->type = options::TABLE; printOptions->type = print_options::TABLE;
} }
// general print option checkboxes // general print option checkboxes
void PrintOptions::printInColorClicked(bool check) void PrintOptions::printInColorClicked(bool check)
{ {
printOptions->color_selected = (int)check; printOptions->color_selected = check;
} }
void PrintOptions::printSelectedClicked(bool check) void PrintOptions::printSelectedClicked(bool check)
{ {
printOptions->print_selected = (int)check; printOptions->print_selected = check;
} }
// ordering // ordering

View file

@ -10,12 +10,12 @@ class PrintOptions : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit PrintOptions(QWidget *parent = 0, struct options *printOpt = 0); explicit PrintOptions(QWidget *parent = 0, struct print_options *printOpt = 0);
void setup(struct options *printOpt); void setup(struct print_options *printOpt);
private: private:
Ui::PrintOptions ui; Ui::PrintOptions ui;
struct options *printOptions; struct print_options *printOptions;
bool hasSetupSlots; bool hasSetupSlots;
private private