2017-04-27 18:26:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-07-09 12:37:53 +00:00
|
|
|
#include "printdialog.h"
|
2020-12-13 12:32:40 +00:00
|
|
|
#include "printer.h"
|
|
|
|
#include "core/pref.h"
|
|
|
|
#include "core/dive.h" // for existing_filename
|
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>
|
2020-12-13 12:32:40 +00:00
|
|
|
#include <QPrinter>
|
2013-07-10 16:27:10 +00:00
|
|
|
#include <QPrintPreviewDialog>
|
2013-12-04 11:51:34 +00:00
|
|
|
#include <QPrintDialog>
|
2019-09-06 21:17:49 +00:00
|
|
|
#include <QFileDialog>
|
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>
|
2018-10-21 16:00:02 +00:00
|
|
|
#include <QDialogButtonBox>
|
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-28 17:48:29 +00:00
|
|
|
template_options::color_palette_struct ssrf_colors, almond_colors, blueshades_colors, custom_colors;
|
2015-07-12 03:00:03 +00:00
|
|
|
|
2022-11-06 19:37:53 +00:00
|
|
|
PrintDialog::PrintDialog(dive *singleDive, QWidget *parent) :
|
2020-10-25 21:27:39 +00:00
|
|
|
QDialog(parent, QFlag(0)),
|
2022-11-06 19:37:53 +00:00
|
|
|
singleDive(singleDive),
|
2016-01-07 21:33:58 +00:00
|
|
|
printer(NULL),
|
|
|
|
qprinter(NULL)
|
2013-07-09 12:37:53 +00:00
|
|
|
{
|
2015-07-12 03:00:03 +00:00
|
|
|
// initialize const colors
|
2015-08-07 07:54:09 +00:00
|
|
|
ssrf_colors.color1 = QColor::fromRgb(0xff, 0xff, 0xff);
|
2015-08-07 08:10:22 +00:00
|
|
|
ssrf_colors.color2 = QColor::fromRgb(0xa6, 0xbc, 0xd7);
|
|
|
|
ssrf_colors.color3 = QColor::fromRgb(0xef, 0xf7, 0xff);
|
2015-08-07 07:54:09 +00:00
|
|
|
ssrf_colors.color4 = QColor::fromRgb(0x34, 0x65, 0xa4);
|
|
|
|
ssrf_colors.color5 = QColor::fromRgb(0x20, 0x4a, 0x87);
|
|
|
|
ssrf_colors.color6 = QColor::fromRgb(0x17, 0x37, 0x64);
|
|
|
|
almond_colors.color1 = QColor::fromRgb(255, 255, 255);
|
2015-08-07 08:10:22 +00:00
|
|
|
almond_colors.color2 = QColor::fromRgb(253, 204, 156);
|
|
|
|
almond_colors.color3 = QColor::fromRgb(243, 234, 207);
|
2015-08-07 07:54:09 +00:00
|
|
|
almond_colors.color4 = QColor::fromRgb(136, 160, 150);
|
|
|
|
almond_colors.color5 = QColor::fromRgb(187, 171, 139);
|
2015-08-07 08:11:14 +00:00
|
|
|
almond_colors.color6 = QColor::fromRgb(0, 0, 0);
|
2015-08-07 07:54:09 +00:00
|
|
|
blueshades_colors.color1 = QColor::fromRgb(255, 255, 255);
|
2015-08-07 08:10:22 +00:00
|
|
|
blueshades_colors.color2 = QColor::fromRgb(142, 152, 166);
|
|
|
|
blueshades_colors.color3 = QColor::fromRgb(182, 192, 206);
|
2015-08-07 07:54:09 +00:00
|
|
|
blueshades_colors.color4 = QColor::fromRgb(31, 49, 75);
|
|
|
|
blueshades_colors.color5 = QColor::fromRgb(21, 45, 84);
|
2015-08-07 08:11:14 +00:00
|
|
|
blueshades_colors.color6 = QColor::fromRgb(0, 0, 0);
|
2015-07-12 03:00:03 +00:00
|
|
|
|
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;
|
2016-11-01 14:13:27 +00:00
|
|
|
s.beginGroup(SETTINGS_GROUP);
|
|
|
|
printOptions.type = (print_options::print_type)s.value("type", print_options::DIVELIST).toInt();
|
|
|
|
printOptions.print_selected = s.value("print_selected", true).toBool();
|
|
|
|
printOptions.color_selected = s.value("color_selected", true).toBool();
|
|
|
|
printOptions.landscape = s.value("landscape", false).toBool();
|
|
|
|
printOptions.p_template = s.value("template_selected", "one_dive.html").toString();
|
2020-11-12 18:27:11 +00:00
|
|
|
printOptions.resolution = s.value("resolution", 600).toInt();
|
2016-11-01 14:13:27 +00:00
|
|
|
templateOptions.font_index = s.value("font", 0).toInt();
|
|
|
|
templateOptions.font_size = s.value("font_size", 9).toDouble();
|
|
|
|
templateOptions.color_palette_index = s.value("color_palette", SSRF_COLORS).toInt();
|
|
|
|
templateOptions.line_spacing = s.value("line_spacing", 1).toDouble();
|
2017-03-06 16:41:54 +00:00
|
|
|
templateOptions.border_width = s.value("border_width", 1).toInt();
|
2016-11-01 14:13:27 +00:00
|
|
|
custom_colors.color1 = QColor(s.value("custom_color_1", ssrf_colors.color1).toString());
|
|
|
|
custom_colors.color2 = QColor(s.value("custom_color_2", ssrf_colors.color2).toString());
|
|
|
|
custom_colors.color3 = QColor(s.value("custom_color_3", ssrf_colors.color3).toString());
|
|
|
|
custom_colors.color4 = QColor(s.value("custom_color_4", ssrf_colors.color4).toString());
|
|
|
|
custom_colors.color5 = QColor(s.value("custom_color_5", ssrf_colors.color5).toString());
|
|
|
|
s.endGroup();
|
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) {
|
2015-07-28 17:48:29 +00:00
|
|
|
case SSRF_COLORS: // default Subsurface derived colors
|
|
|
|
templateOptions.color_palette = ssrf_colors;
|
|
|
|
break;
|
2015-07-26 17:57:57 +00:00
|
|
|
case ALMOND: // almond
|
2015-07-12 04:42:50 +00:00
|
|
|
templateOptions.color_palette = almond_colors;
|
|
|
|
break;
|
2015-07-26 17:57:57 +00:00
|
|
|
case BLUESHADES: // blueshades
|
2015-07-26 17:13:23 +00:00
|
|
|
templateOptions.color_palette = blueshades_colors;
|
|
|
|
break;
|
2015-07-26 17:57:57 +00:00
|
|
|
case CUSTOM: // custom
|
2015-07-12 04:42:50 +00:00
|
|
|
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
|
2020-12-12 12:28:36 +00:00
|
|
|
optionsWidget = new PrintOptions(this, printOptions, templateOptions);
|
2013-07-09 20:43:21 +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()));
|
|
|
|
|
2019-09-06 21:17:49 +00:00
|
|
|
QPushButton *exportHtmlButton = new QPushButton(tr("Export Html"));
|
|
|
|
connect(exportHtmlButton, SIGNAL(clicked(bool)), this, SLOT(exportHtmlClicked()));
|
|
|
|
|
2014-04-16 18:25:15 +00:00
|
|
|
QDialogButtonBox *buttonBox = new QDialogButtonBox;
|
|
|
|
buttonBox->addButton(QDialogButtonBox::Cancel);
|
|
|
|
buttonBox->addButton(printButton, QDialogButtonBox::AcceptRole);
|
|
|
|
buttonBox->addButton(previewButton, QDialogButtonBox::ActionRole);
|
2019-09-06 21:17:49 +00:00
|
|
|
buttonBox->addButton(exportHtmlButton, QDialogButtonBox::AcceptRole);
|
2014-04-16 18:25:15 +00:00
|
|
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:33:58 +00:00
|
|
|
PrintDialog::~PrintDialog()
|
|
|
|
{
|
|
|
|
delete qprinter;
|
|
|
|
delete printer;
|
|
|
|
}
|
|
|
|
|
2014-11-13 22:57:42 +00:00
|
|
|
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);
|
2020-11-12 18:27:11 +00:00
|
|
|
s.setValue("resolution", printOptions.resolution);
|
2023-03-18 17:23:33 +00:00
|
|
|
s.setValue("landscape", printOptions.landscape);
|
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);
|
2017-03-06 16:41:54 +00:00
|
|
|
s.setValue("border_width", templateOptions.border_width);
|
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
|
|
|
}
|
|
|
|
|
2016-01-07 21:33:58 +00:00
|
|
|
void PrintDialog::createPrinterObj()
|
|
|
|
{
|
|
|
|
// create a new printer object
|
|
|
|
if (!printer) {
|
2021-02-13 21:43:55 +00:00
|
|
|
qprinter = new QPrinter;
|
2020-08-22 12:36:41 +00:00
|
|
|
qprinter->setResolution(printOptions.resolution);
|
2016-01-07 21:33:58 +00:00
|
|
|
qprinter->setOrientation((QPrinter::Orientation)printOptions.landscape);
|
2022-11-06 19:37:53 +00:00
|
|
|
printer = new Printer(qprinter, printOptions, templateOptions, Printer::PRINT, singleDive);
|
2016-01-07 21:33:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 11:51:34 +00:00
|
|
|
void PrintDialog::previewClicked(void)
|
2013-07-09 12:37:53 +00:00
|
|
|
{
|
2016-01-07 21:33:58 +00:00
|
|
|
createPrinterObj();
|
|
|
|
QPrintPreviewDialog previewDialog(qprinter, this, Qt::Window
|
2015-06-21 02:59:15 +00:00
|
|
|
| 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
|
|
|
}
|
|
|
|
|
2019-09-06 21:17:49 +00:00
|
|
|
void PrintDialog::exportHtmlClicked(void)
|
|
|
|
{
|
|
|
|
createPrinterObj();
|
|
|
|
QString saveFileName = printOptions.p_template;
|
|
|
|
QString filename = existing_filename ?: prefs.default_filename;
|
|
|
|
QFileInfo fi(filename);
|
|
|
|
filename = fi.absolutePath().append(QDir::separator()).append(saveFileName);
|
|
|
|
QString htmlExportFilename = QFileDialog::getSaveFileName(this, tr("Filename to export html to"),
|
|
|
|
filename, tr("Html file") + " (*.html)");
|
|
|
|
if (!htmlExportFilename.isEmpty()) {
|
|
|
|
QFile file(htmlExportFilename);
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
connect(printer, SIGNAL(progessUpdated(int)), progressBar, SLOT(setValue(int)));
|
|
|
|
file.write(printer->exportHtml().toUtf8());
|
|
|
|
file.close();
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 11:51:34 +00:00
|
|
|
void PrintDialog::printClicked(void)
|
|
|
|
{
|
2016-01-07 21:33:58 +00:00
|
|
|
createPrinterObj();
|
|
|
|
QPrintDialog printDialog(qprinter, this);
|
2015-05-30 08:53:34 +00:00
|
|
|
if (printDialog.exec() == QDialog::Accepted) {
|
2015-08-21 17:12:33 +00:00
|
|
|
connect(printer, SIGNAL(progessUpdated(int)), progressBar, SLOT(setValue(int)));
|
|
|
|
printer->print();
|
2014-07-18 00:25:57 +00:00
|
|
|
close();
|
|
|
|
}
|
2013-07-10 16:27:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 16:09:09 +00:00
|
|
|
void PrintDialog::onPaintRequested(QPrinter*)
|
2013-07-10 16:27:10 +00:00
|
|
|
{
|
2016-01-07 21:33:58 +00:00
|
|
|
createPrinterObj();
|
2015-06-21 02:59:15 +00:00
|
|
|
connect(printer, SIGNAL(progessUpdated(int)), progressBar, SLOT(setValue(int)));
|
2015-08-21 17:12:33 +00:00
|
|
|
printer->print();
|
2015-06-21 02:59:15 +00:00
|
|
|
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
|