2013-07-09 12:37:53 +00:00
|
|
|
#include "printdialog.h"
|
2013-12-04 13:44:31 +00:00
|
|
|
#include "printoptions.h"
|
|
|
|
#include "printlayout.h"
|
2013-11-30 17:18:04 +00:00
|
|
|
#include "mainwindow.h"
|
2013-07-09 12:37:53 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QPushButton>
|
2013-12-04 11:41:19 +00:00
|
|
|
#include <QProgressBar>
|
2013-07-09 12:37:53 +00:00
|
|
|
#include <QVBoxLayout>
|
2013-12-04 11:41:19 +00:00
|
|
|
#include <QHBoxLayout>
|
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-08-04 16:32:13 +00:00
|
|
|
#include <QPrinterInfo>
|
|
|
|
#include <QMessageBox>
|
2014-11-13 22:57:42 +00:00
|
|
|
#include <QSettings>
|
2014-11-14 11:42:22 +00:00
|
|
|
|
|
|
|
#if QT_VERSION >= 0x050300
|
2014-11-13 22:57:42 +00:00
|
|
|
#include <QMarginsF>
|
2014-11-14 11:42:22 +00:00
|
|
|
#endif
|
2014-11-13 22:57:42 +00:00
|
|
|
|
|
|
|
#define SETTINGS_GROUP "PrintDialog"
|
2013-07-09 12:37:53 +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
|
|
|
{
|
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.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);
|
2014-11-14 11:42:22 +00:00
|
|
|
#if QT_VERSION >= 0x050300
|
2014-11-13 22:57:42 +00:00
|
|
|
QMarginsF margins;
|
|
|
|
margins.setLeft(printOptions.margins[0]);
|
2014-11-14 11:42:22 +00:00
|
|
|
margins.setTop(printOptions.margins[1]);
|
|
|
|
margins.setRight(printOptions.margins[2]);
|
2014-11-13 22:57:42 +00:00
|
|
|
margins.setBottom(printOptions.margins[3]);
|
|
|
|
printer.setPageMargins(margins, QPageLayout::Millimeter);
|
2014-11-14 11:42:22 +00:00
|
|
|
#else
|
|
|
|
printer.setPageMargins(
|
|
|
|
printOptions.margins[0],
|
|
|
|
printOptions.margins[1],
|
|
|
|
printOptions.margins[2],
|
|
|
|
printOptions.margins[3],
|
|
|
|
QPrinter::Millimeter
|
|
|
|
);
|
|
|
|
#endif
|
2014-11-13 22:57:42 +00:00
|
|
|
}
|
2013-07-10 12:34:57 +00:00
|
|
|
|
|
|
|
// create a print layout and pass the printer and options
|
|
|
|
printLayout = new PrintLayout(this, &printer, &printOptions);
|
|
|
|
|
2013-12-04 11:46:08 +00:00
|
|
|
// create a print options object and pass our options struct
|
2013-07-09 20:43:21 +00:00
|
|
|
optionsWidget = new PrintOptions(this, &printOptions);
|
|
|
|
|
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
|
|
|
connect(printLayout, SIGNAL(signalProgress(int)), progressBar, SLOT(setValue(int)));
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
// 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);
|
2014-11-14 11:42:22 +00:00
|
|
|
#if QT_VERSION >= 0x050300
|
2014-11-13 22:57:42 +00:00
|
|
|
s.setValue("landscape", (bool)printer.pageLayout().orientation());
|
|
|
|
QMarginsF margins = printer.pageLayout().margins(QPageLayout::Millimeter);
|
|
|
|
s.setValue("margin_left", margins.left());
|
2014-11-14 11:42:22 +00:00
|
|
|
s.setValue("margin_top", margins.top());
|
|
|
|
s.setValue("margin_right", margins.right());
|
2014-11-13 22:57:42 +00:00
|
|
|
s.setValue("margin_bottom", margins.bottom());
|
2014-11-14 11:42:22 +00:00
|
|
|
#else
|
|
|
|
s.setValue("landscape", (bool)printer.orientation());
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
printer.getPageMargins(&left, &top, &right, &bottom, QPrinter::Millimeter);
|
|
|
|
s.setValue("margin_left", (int)left);
|
|
|
|
s.setValue("margin_top", (int)top);
|
|
|
|
s.setValue("margin_right", (int)right);
|
|
|
|
s.setValue("margin_bottom", (int)bottom);
|
|
|
|
#endif
|
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
|
|
|
{
|
2014-10-30 14:44:21 +00:00
|
|
|
QPrintPreviewDialog previewDialog(&printer, this, Qt::Window
|
|
|
|
| Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint
|
|
|
|
| Qt::WindowTitleHint);
|
|
|
|
|
2014-02-08 23:13:58 +00:00
|
|
|
connect(&previewDialog, SIGNAL(paintRequested(QPrinter *)), this, SLOT(onPaintRequested(QPrinter *)));
|
2013-12-04 11:51:34 +00:00
|
|
|
previewDialog.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::printClicked(void)
|
|
|
|
{
|
|
|
|
QPrintDialog printDialog(&printer, this);
|
2014-07-18 00:25:57 +00:00
|
|
|
if (printDialog.exec() == QDialog::Accepted){
|
2013-12-04 11:51:34 +00:00
|
|
|
printLayout->print();
|
2014-07-18 00:25:57 +00:00
|
|
|
close();
|
|
|
|
}
|
2013-07-10 16:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDialog::onPaintRequested(QPrinter *printerPtr)
|
|
|
|
{
|
2013-07-10 12:34:57 +00:00
|
|
|
printLayout->print();
|
2013-07-09 12:37:53 +00:00
|
|
|
}
|