2013-07-09 12:37:53 +00:00
|
|
|
#include "printdialog.h"
|
2013-12-04 13:44:31 +00:00
|
|
|
#include "printoptions.h"
|
2013-11-30 17:18:04 +00:00
|
|
|
#include "mainwindow.h"
|
2013-07-09 12:37:53 +00:00
|
|
|
|
2015-07-01 15:08:35 +00:00
|
|
|
#ifndef NO_PRINTING
|
2013-12-04 11:41:19 +00:00
|
|
|
#include <QProgressBar>
|
2013-07-10 16:27:10 +00:00
|
|
|
#include <QPrintPreviewDialog>
|
2013-12-04 11:51:34 +00:00
|
|
|
#include <QPrintDialog>
|
2014-04-25 17:27:44 +00:00
|
|
|
#include <QShortcut>
|
2014-11-13 22:57:42 +00:00
|
|
|
#include <QSettings>
|
2015-06-24 03:12:45 +00:00
|
|
|
#include <QMessageBox>
|
2014-11-14 11:42:22 +00:00
|
|
|
|
2014-11-13 22:57:42 +00:00
|
|
|
#define SETTINGS_GROUP "PrintDialog"
|
2013-07-09 12:37:53 +00:00
|
|
|
|
2015-07-12 04:42:50 +00:00
|
|
|
template_options::color_palette_struct almond_colors, custom_colors;
|
2015-07-12 03:00:03 +00:00
|
|
|
|
2014-02-12 14:26:25 +00:00
|
|
|
PrintDialog::PrintDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
|
2013-07-09 12:37:53 +00:00
|
|
|
{
|
2015-07-12 03:00:03 +00:00
|
|
|
// initialize const colors
|
|
|
|
almond_colors.color1 = QColor::fromRgb(243, 234, 207);
|
|
|
|
almond_colors.color2 = QColor::fromRgb(253, 204, 156);
|
|
|
|
almond_colors.color3 = QColor::fromRgb(136, 160, 150);
|
|
|
|
almond_colors.color4 = QColor::fromRgb(187, 171, 139);
|
|
|
|
almond_colors.color5 = QColor::fromRgb(239, 130, 117);
|
|
|
|
|
2014-11-13 22:57:42 +00:00
|
|
|
// 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.print_selected = true;
|
|
|
|
printOptions.color_selected = true;
|
|
|
|
printOptions.landscape = false;
|
2015-07-24 12:27:33 +00:00
|
|
|
printOptions.p_template = "one_dive.html";
|
2015-06-22 13:42:02 +00:00
|
|
|
printOptions.type = print_options::DIVELIST;
|
2015-06-29 01:24:29 +00:00
|
|
|
templateOptions.font_index = 0;
|
|
|
|
templateOptions.font_size = 9;
|
|
|
|
templateOptions.color_palette_index = 0;
|
|
|
|
templateOptions.line_spacing = 1;
|
2015-07-12 04:42:50 +00:00
|
|
|
custom_colors = almond_colors;
|
2014-11-13 22:57:42 +00:00
|
|
|
} 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.landscape = s.value("landscape").toBool();
|
2015-07-24 12:27:33 +00:00
|
|
|
printOptions.p_template = s.value("template_selected").toString();
|
2015-05-30 13:20:01 +00:00
|
|
|
qprinter.setOrientation((QPrinter::Orientation)printOptions.landscape);
|
2015-06-29 01:24:29 +00:00
|
|
|
templateOptions.font_index = s.value("font").toInt();
|
|
|
|
templateOptions.font_size = s.value("font_size").toDouble();
|
|
|
|
templateOptions.color_palette_index = s.value("color_palette").toInt();
|
|
|
|
templateOptions.line_spacing = s.value("line_spacing").toDouble();
|
2015-07-12 04:42:50 +00:00
|
|
|
custom_colors.color1 = QColor(s.value("custom_color_1").toString());
|
|
|
|
custom_colors.color2 = QColor(s.value("custom_color_2").toString());
|
|
|
|
custom_colors.color3 = QColor(s.value("custom_color_3").toString());
|
|
|
|
custom_colors.color4 = QColor(s.value("custom_color_4").toString());
|
|
|
|
custom_colors.color5 = QColor(s.value("custom_color_5").toString());
|
2014-11-13 22:57:42 +00:00
|
|
|
}
|
2013-07-10 12:34:57 +00:00
|
|
|
|
2015-07-24 14:23:50 +00:00
|
|
|
// handle cases from old QSettings group
|
|
|
|
if (templateOptions.font_size < 9) {
|
|
|
|
templateOptions.font_size = 9;
|
|
|
|
}
|
|
|
|
if (templateOptions.line_spacing < 1) {
|
|
|
|
templateOptions.line_spacing = 1;
|
|
|
|
}
|
|
|
|
|
2015-07-12 04:42:50 +00:00
|
|
|
switch (templateOptions.color_palette_index) {
|
|
|
|
case 0: // almond
|
|
|
|
templateOptions.color_palette = almond_colors;
|
|
|
|
break;
|
|
|
|
case 1: // custom
|
|
|
|
templateOptions.color_palette = custom_colors;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-12 03:00:03 +00:00
|
|
|
|
2013-12-04 11:46:08 +00:00
|
|
|
// create a print options object and pass our options struct
|
2015-06-29 01:16:19 +00:00
|
|
|
optionsWidget = new PrintOptions(this, &printOptions, &templateOptions);
|
2013-07-09 20:43:21 +00:00
|
|
|
|
2015-05-30 13:20:01 +00:00
|
|
|
// create a new printer object
|
2015-07-10 19:30:18 +00:00
|
|
|
printer = new Printer(&qprinter, &printOptions, &templateOptions, Printer::PRINT);
|
2015-05-30 13:20:01 +00:00
|
|
|
|
2013-07-09 12:37:53 +00:00
|
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
|
|
setLayout(layout);
|
|
|
|
|
2014-04-16 18:25:15 +00:00
|
|
|
layout->addWidget(optionsWidget);
|
2013-12-04 12:14:04 +00:00
|
|
|
|
2013-12-04 13:51:01 +00:00
|
|
|
progressBar = new QProgressBar();
|
2013-12-04 11:41:19 +00:00
|
|
|
progressBar->setMinimum(0);
|
|
|
|
progressBar->setMaximum(100);
|
2014-02-08 19:12:13 +00:00
|
|
|
progressBar->setValue(0);
|
2013-12-04 11:41:19 +00:00
|
|
|
progressBar->setTextVisible(false);
|
|
|
|
layout->addWidget(progressBar);
|
|
|
|
|
2014-04-16 18:25:15 +00:00
|
|
|
QHBoxLayout *hLayout = new QHBoxLayout();
|
|
|
|
layout->addLayout(hLayout);
|
|
|
|
|
|
|
|
QPushButton *printButton = new QPushButton(tr("P&rint"));
|
|
|
|
connect(printButton, SIGNAL(clicked(bool)), this, SLOT(printClicked()));
|
|
|
|
|
|
|
|
QPushButton *previewButton = new QPushButton(tr("&Preview"));
|
|
|
|
connect(previewButton, SIGNAL(clicked(bool)), this, SLOT(previewClicked()));
|
|
|
|
|
|
|
|
QDialogButtonBox *buttonBox = new QDialogButtonBox;
|
|
|
|
buttonBox->addButton(QDialogButtonBox::Cancel);
|
|
|
|
buttonBox->addButton(printButton, QDialogButtonBox::AcceptRole);
|
|
|
|
buttonBox->addButton(previewButton, QDialogButtonBox::ActionRole);
|
|
|
|
|
|
|
|
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
|
|
|
|
|
|
|
|
hLayout->addWidget(buttonBox);
|
2013-11-30 16:43:40 +00:00
|
|
|
|
2013-11-30 16:54:28 +00:00
|
|
|
setWindowTitle(tr("Print"));
|
|
|
|
setWindowIcon(QIcon(":subsurface-icon"));
|
2014-04-25 17:27:44 +00:00
|
|
|
|
|
|
|
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
|
|
|
|
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()));
|
2014-11-13 22:57:42 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(SETTINGS_GROUP);
|
2015-06-29 01:24:29 +00:00
|
|
|
|
|
|
|
// save print paper settings
|
2014-11-13 22:57:42 +00:00
|
|
|
s.setValue("type", printOptions.type);
|
|
|
|
s.setValue("print_selected", printOptions.print_selected);
|
|
|
|
s.setValue("color_selected", printOptions.color_selected);
|
2015-06-11 18:13:10 +00:00
|
|
|
s.setValue("template_selected", printOptions.p_template);
|
2015-06-29 01:24:29 +00:00
|
|
|
|
|
|
|
// save template settings
|
|
|
|
s.setValue("font", templateOptions.font_index);
|
|
|
|
s.setValue("font_size", templateOptions.font_size);
|
|
|
|
s.setValue("color_palette", templateOptions.color_palette_index);
|
|
|
|
s.setValue("line_spacing", templateOptions.line_spacing);
|
2015-07-12 04:42:50 +00:00
|
|
|
|
|
|
|
// save custom colors
|
|
|
|
s.setValue("custom_color_1", custom_colors.color1.name());
|
|
|
|
s.setValue("custom_color_2", custom_colors.color2.name());
|
|
|
|
s.setValue("custom_color_3", custom_colors.color3.name());
|
|
|
|
s.setValue("custom_color_4", custom_colors.color4.name());
|
|
|
|
s.setValue("custom_color_5", custom_colors.color5.name());
|
2013-07-09 12:37:53 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 11:51:34 +00:00
|
|
|
void PrintDialog::previewClicked(void)
|
2013-07-09 12:37:53 +00:00
|
|
|
{
|
2015-06-24 03:12:45 +00:00
|
|
|
if (printOptions.type == print_options::TABLE || printOptions.type == print_options::STATISTICS) {
|
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText("This feature is not implemented yet");
|
|
|
|
msgBox.exec();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-21 02:59:15 +00:00
|
|
|
QPrintPreviewDialog previewDialog(&qprinter, this, Qt::Window
|
|
|
|
| Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint
|
|
|
|
| Qt::WindowTitleHint);
|
|
|
|
connect(&previewDialog, SIGNAL(paintRequested(QPrinter *)), this, SLOT(onPaintRequested(QPrinter *)));
|
|
|
|
previewDialog.exec();
|
2013-12-04 11:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::printClicked(void)
|
|
|
|
{
|
2015-06-24 03:12:45 +00:00
|
|
|
if (printOptions.type == print_options::TABLE || printOptions.type == print_options::STATISTICS) {
|
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText("This feature is not implemented yet");
|
|
|
|
msgBox.exec();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:20:01 +00:00
|
|
|
QPrintDialog printDialog(&qprinter, this);
|
2015-05-30 08:53:34 +00:00
|
|
|
if (printDialog.exec() == QDialog::Accepted) {
|
2015-05-30 13:20:01 +00:00
|
|
|
switch (printOptions.type) {
|
|
|
|
case print_options::DIVELIST:
|
2015-05-30 13:32:15 +00:00
|
|
|
connect(printer, SIGNAL(progessUpdated(int)), progressBar, SLOT(setValue(int)));
|
2015-05-30 13:20:01 +00:00
|
|
|
printer->print();
|
|
|
|
break;
|
|
|
|
case print_options::TABLE:
|
|
|
|
break;
|
|
|
|
case print_options::STATISTICS:
|
|
|
|
break;
|
|
|
|
}
|
2014-07-18 00:25:57 +00:00
|
|
|
close();
|
|
|
|
}
|
2013-07-10 16:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::onPaintRequested(QPrinter *printerPtr)
|
|
|
|
{
|
2015-06-21 02:59:15 +00:00
|
|
|
connect(printer, SIGNAL(progessUpdated(int)), progressBar, SLOT(setValue(int)));
|
|
|
|
printer->print();
|
|
|
|
progressBar->setValue(0);
|
|
|
|
disconnect(printer, SIGNAL(progessUpdated(int)), progressBar, SLOT(setValue(int)));
|
2013-07-09 12:37:53 +00:00
|
|
|
}
|
2015-06-04 17:18:37 +00:00
|
|
|
#endif
|