cleanup: make templateOptions and printOptions reference types

These two structs describe options used during printing.
They are passed through numerous classes as pointer. In this
case, reference semantics are preferred, as references:
 - can never be null
 - can not change during their lifetime
This not only helps the compiler, as it can optimize away null
checks, but also your fellow coder. Moreover, it prevents
unintentional creation of uninitialized references: one can't
create an instance of a class without initializing a reference
member. It does not prevent references from going dangling.
However, pointers have the same disadvantage.

Contains a few whitespace cleanups.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-12-12 13:28:36 +01:00 committed by Dirk Hohndel
parent 7bdd968e05
commit 0cbb448740
9 changed files with 97 additions and 111 deletions

View file

@ -87,7 +87,7 @@ PrintDialog::PrintDialog(QWidget *parent) :
} }
// create a print options object and pass our options struct // create a print options object and pass our options struct
optionsWidget = new PrintOptions(this, &printOptions, &templateOptions); optionsWidget = new PrintOptions(this, printOptions, templateOptions);
QVBoxLayout *layout = new QVBoxLayout(this); QVBoxLayout *layout = new QVBoxLayout(this);
setLayout(layout); setLayout(layout);
@ -175,7 +175,7 @@ void PrintDialog::createPrinterObj()
qprinter = new QPrinter(); qprinter = new QPrinter();
qprinter->setResolution(printOptions.resolution); qprinter->setResolution(printOptions.resolution);
qprinter->setOrientation((QPrinter::Orientation)printOptions.landscape); qprinter->setOrientation((QPrinter::Orientation)printOptions.landscape);
printer = new Printer(qprinter, &printOptions, &templateOptions, Printer::PRINT); printer = new Printer(qprinter, printOptions, templateOptions, Printer::PRINT);
} }
} }

View file

@ -12,15 +12,15 @@
#include <QWebElement> #include <QWebElement>
#include "profile-widget/profilewidget2.h" #include "profile-widget/profilewidget2.h"
Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode) Printer::Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode) :
paintDevice(paintDevice),
webView(new QWebView),
printOptions(printOptions),
templateOptions(templateOptions),
printMode(printMode),
done(0),
dpi(0)
{ {
this->paintDevice = paintDevice;
this->printOptions = printOptions;
this->templateOptions = templateOptions;
this->printMode = printMode;
dpi = 0;
done = 0;
webView = new QWebView();
} }
Printer::~Printer() Printer::~Printer()
@ -36,7 +36,7 @@ void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter
QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height()); QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height());
profile->plotDive(dive, true, true); profile->plotDive(dive, true, true);
if (!printOptions->color_selected) { if (!printOptions.color_selected) {
QImage image(pos.width(), pos.height(), QImage::Format_ARGB32); QImage image(pos.width(), pos.height(), QImage::Format_ARGB32);
QPainter imgPainter(&image); QPainter imgPainter(&image);
imgPainter.setRenderHint(QPainter::Antialiasing); imgPainter.setRenderHint(QPainter::Antialiasing);
@ -85,7 +85,7 @@ void Printer::flowRender()
} else { } else {
// fill the page with background color // fill the page with background color
QRect fullPage(0, 0, pageSize.width(), pageSize.height()); QRect fullPage(0, 0, pageSize.width(), pageSize.height());
QBrush fillBrush(templateOptions->color_palette.color1); QBrush fillBrush(templateOptions.color_palette.color1);
painter.fillRect(fullPage, fillBrush); painter.fillRect(fullPage, fillBrush);
QRegion reigon(0, 0, pageSize.width(), end - start); QRegion reigon(0, 0, pageSize.width(), end - start);
viewPort.setRect(0, start, pageSize.width(), end - start); viewPort.setRect(0, start, pageSize.width(), end - start);
@ -111,7 +111,7 @@ void Printer::flowRender()
} }
// render the remianing page // render the remianing page
QRect fullPage(0, 0, pageSize.width(), pageSize.height()); QRect fullPage(0, 0, pageSize.width(), pageSize.height());
QBrush fillBrush(templateOptions->color_palette.color1); QBrush fillBrush(templateOptions.color_palette.color1);
painter.fillRect(fullPage, fillBrush); painter.fillRect(fullPage, fillBrush);
QRegion reigon(0, 0, pageSize.width(), end - start); QRegion reigon(0, 0, pageSize.width(), end - start);
webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon);
@ -130,7 +130,7 @@ void Printer::render(int Pages = 0)
// apply printing settings to profile // apply printing settings to profile
profile->setFrameStyle(QFrame::NoFrame); profile->setFrameStyle(QFrame::NoFrame);
profile->setPrintMode(true, !printOptions->color_selected); profile->setPrintMode(true, !printOptions.color_selected);
profile->setToolTipVisibile(false); profile->setToolTipVisibile(false);
qPrefDisplay::set_animation_speed(0); qPrefDisplay::set_animation_speed(0);
@ -195,16 +195,16 @@ void Printer::templateProgessUpdated(int value)
emit progessUpdated(done); emit progessUpdated(done);
} }
QString Printer::exportHtml() { QString Printer::exportHtml()
{
TemplateLayout t(printOptions, templateOptions); TemplateLayout t(printOptions, templateOptions);
connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int))); connect(&t, SIGNAL(progressUpdated(int)), this, SLOT(templateProgessUpdated(int)));
QString html; QString html;
if (printOptions->type == print_options::DIVELIST) { if (printOptions.type == print_options::DIVELIST)
html = t.generate(); html = t.generate();
} else if (printOptions->type == print_options::STATISTICS ) { else if (printOptions.type == print_options::STATISTICS )
html = t.generateStatistics(); html = t.generateStatistics();
}
// TODO: write html to file // TODO: write html to file
return html; return html;
@ -231,17 +231,15 @@ void Printer::print()
webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
// export border width with at least 1 pixel // export border width with at least 1 pixel
// templateOptions->borderwidth = std::max(1, pageSize.width() / 1000); // templateOptions.borderwidth = std::max(1, pageSize.width() / 1000);
if (printOptions->type == print_options::DIVELIST) { if (printOptions.type == print_options::DIVELIST)
webView->setHtml(t.generate()); webView->setHtml(t.generate());
} else if (printOptions->type == print_options::STATISTICS ) { else if (printOptions.type == print_options::STATISTICS )
webView->setHtml(t.generateStatistics()); webView->setHtml(t.generateStatistics());
} if (printOptions.color_selected && printerPtr->colorMode())
if (printOptions->color_selected && printerPtr->colorMode()) {
printerPtr->setColorMode(QPrinter::Color); printerPtr->setColorMode(QPrinter::Color);
} else { else
printerPtr->setColorMode(QPrinter::GrayScale); printerPtr->setColorMode(QPrinter::GrayScale);
}
// apply user settings // apply user settings
int divesPerPage; int divesPerPage;
@ -270,12 +268,11 @@ void Printer::previewOnePage()
pageSize.setWidth(paintDevice->width()); pageSize.setWidth(paintDevice->width());
webView->page()->setViewportSize(pageSize); webView->page()->setViewportSize(pageSize);
// initialize the border settings // initialize the border settings
// templateOptions->border_width = std::max(1, pageSize.width() / 1000); // templateOptions.border_width = std::max(1, pageSize.width() / 1000);
if (printOptions->type == print_options::DIVELIST) { if (printOptions.type == print_options::DIVELIST)
webView->setHtml(t.generate()); webView->setHtml(t.generate());
} else if (printOptions->type == print_options::STATISTICS ) { else if (printOptions.type == print_options::STATISTICS )
webView->setHtml(t.generateStatistics()); webView->setHtml(t.generateStatistics());
}
bool ok; bool ok;
int divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); int divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok);
if (!ok) { if (!ok) {

View file

@ -22,8 +22,8 @@ public:
private: private:
QPaintDevice *paintDevice; QPaintDevice *paintDevice;
QWebView *webView; QWebView *webView;
print_options *printOptions; const print_options &printOptions;
template_options *templateOptions; const template_options &templateOptions;
QSize pageSize; QSize pageSize;
PrintMode printMode; PrintMode printMode;
int done; int done;
@ -36,7 +36,7 @@ private slots:
void templateProgessUpdated(int value); void templateProgessUpdated(int value);
public: public:
Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode); Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode);
~Printer(); ~Printer();
void print(); void print();
void previewOnePage(); void previewOnePage();

View file

@ -7,23 +7,21 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
PrintOptions::PrintOptions(QWidget *parent, struct print_options *printOpt, struct template_options *templateOpt) PrintOptions::PrintOptions(QWidget *parent, print_options &printOpt, template_options &templateOpt) :
printOptions(printOpt),
templateOptions(templateOpt)
{ {
hasSetupSlots = false; hasSetupSlots = false;
ui.setupUi(this); ui.setupUi(this);
if (parent) if (parent)
setParent(parent); setParent(parent);
if (!printOpt || !templateOpt)
return;
templateOptions = templateOpt;
printOptions = printOpt;
setup(); setup();
} }
void PrintOptions::setup() void PrintOptions::setup()
{ {
// print type radio buttons // print type radio buttons
switch (printOptions->type) { switch (printOptions.type) {
case print_options::DIVELIST: case print_options::DIVELIST:
ui.radioDiveListPrint->setChecked(true); ui.radioDiveListPrint->setChecked(true);
break; break;
@ -35,11 +33,11 @@ void PrintOptions::setup()
setupTemplates(); setupTemplates();
// general print option checkboxes // general print option checkboxes
ui.printInColor->setChecked(printOptions->color_selected); ui.printInColor->setChecked(printOptions.color_selected);
ui.printSelected->setChecked(printOptions->print_selected); ui.printSelected->setChecked(printOptions.print_selected);
// resolution // resolution
ui.resolution->setValue(printOptions->resolution); ui.resolution->setValue(printOptions.resolution);
// connect slots only once // connect slots only once
if (hasSetupSlots) if (hasSetupSlots)
@ -48,19 +46,19 @@ void PrintOptions::setup()
connect(ui.printInColor, SIGNAL(clicked(bool)), this, SLOT(printInColorClicked(bool))); connect(ui.printInColor, SIGNAL(clicked(bool)), this, SLOT(printInColorClicked(bool)));
connect(ui.printSelected, SIGNAL(clicked(bool)), this, SLOT(printSelectedClicked(bool))); connect(ui.printSelected, SIGNAL(clicked(bool)), this, SLOT(printSelectedClicked(bool)));
connect(ui.resolution, QOverload<int>::of(&QSpinBox::valueChanged), [this](int value) { connect(ui.resolution, QOverload<int>::of(&QSpinBox::valueChanged), [this](int value) {
printOptions->resolution = value; printOptions.resolution = value;
}); });
hasSetupSlots = true; hasSetupSlots = true;
} }
void PrintOptions::setupTemplates() void PrintOptions::setupTemplates()
{ {
QStringList currList = printOptions->type == print_options::DIVELIST ? QStringList currList = printOptions.type == print_options::DIVELIST ?
grantlee_templates : grantlee_statistics_templates; grantlee_templates : grantlee_statistics_templates;
// temp. store the template from options, as addItem() updates it via: // temp. store the template from options, as addItem() updates it via:
// on_printTemplate_currentIndexChanged() // on_printTemplate_currentIndexChanged()
QString storedTemplate = printOptions->p_template; QString storedTemplate = printOptions.p_template;
currList.sort(); currList.sort();
int current_index = 0; int current_index = 0;
ui.printTemplate->clear(); ui.printTemplate->clear();
@ -78,7 +76,7 @@ void PrintOptions::setupTemplates()
void PrintOptions::on_radioDiveListPrint_toggled(bool check) void PrintOptions::on_radioDiveListPrint_toggled(bool check)
{ {
if (check) { if (check) {
printOptions->type = print_options::DIVELIST; printOptions.type = print_options::DIVELIST;
// print options // print options
ui.printSelected->setEnabled(true); ui.printSelected->setEnabled(true);
@ -95,7 +93,7 @@ void PrintOptions::on_radioDiveListPrint_toggled(bool check)
void PrintOptions::on_radioStatisticsPrint_toggled(bool check) void PrintOptions::on_radioStatisticsPrint_toggled(bool check)
{ {
if (check) { if (check) {
printOptions->type = print_options::STATISTICS; printOptions.type = print_options::STATISTICS;
// print options // print options
ui.printSelected->setEnabled(false); ui.printSelected->setEnabled(false);
@ -112,24 +110,24 @@ void PrintOptions::on_radioStatisticsPrint_toggled(bool check)
// general print option checkboxes // general print option checkboxes
void PrintOptions::printInColorClicked(bool check) void PrintOptions::printInColorClicked(bool check)
{ {
printOptions->color_selected = check; printOptions.color_selected = check;
} }
void PrintOptions::printSelectedClicked(bool check) void PrintOptions::printSelectedClicked(bool check)
{ {
printOptions->print_selected = check; printOptions.print_selected = check;
} }
void PrintOptions::on_printTemplate_currentIndexChanged(int index) void PrintOptions::on_printTemplate_currentIndexChanged(int index)
{ {
printOptions->p_template = ui.printTemplate->itemData(index).toString(); printOptions.p_template = ui.printTemplate->itemData(index).toString();
} }
void PrintOptions::on_editButton_clicked() void PrintOptions::on_editButton_clicked()
{ {
QString templateName = getSelectedTemplate(); QString templateName = getSelectedTemplate();
QString prefix = (printOptions->type == print_options::STATISTICS) ? "statistics/" : ""; QString prefix = (printOptions.type == print_options::STATISTICS) ? "statistics/" : "";
QFile f(getPrintingTemplatePathUser() + QDir::separator() + prefix + templateName); QFile f(getPrintingTemplatePathUser() + QDir::separator() + prefix + templateName);
if (!f.open(QFile::ReadWrite | QFile::Text)) { if (!f.open(QFile::ReadWrite | QFile::Text)) {
QMessageBox msgBox(this); QMessageBox msgBox(this);
@ -172,7 +170,7 @@ void PrintOptions::on_importButton_clicked()
} }
QFile::copy(filename, dest); QFile::copy(filename, dest);
printOptions->p_template = fileInfo.fileName(); printOptions.p_template = fileInfo.fileName();
lastImportExportTemplate = fileInfo.fileName(); lastImportExportTemplate = fileInfo.fileName();
find_all_templates(); find_all_templates();
setup(); setup();

View file

@ -64,14 +64,14 @@ class PrintOptions : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
explicit PrintOptions(QWidget *parent, struct print_options *printOpt, struct template_options *templateOpt); explicit PrintOptions(QWidget *parent, print_options &printOpt, template_options &templateOpt);
void setup(); void setup();
QString getSelectedTemplate(); QString getSelectedTemplate();
private: private:
Ui::PrintOptions ui; Ui::PrintOptions ui;
struct print_options *printOptions = nullptr; print_options &printOptions;
struct template_options *templateOptions = nullptr; template_options &templateOptions;
bool hasSetupSlots; bool hasSetupSlots;
QString lastImportExportTemplate; QString lastImportExportTemplate;
void setupTemplates(); void setupTemplates();

View file

@ -8,27 +8,27 @@
#include <QButtonGroup> #include <QButtonGroup>
#include <QColorDialog> #include <QColorDialog>
TemplateEdit::TemplateEdit(QWidget *parent, struct print_options *printOptions, struct template_options *templateOptions) : TemplateEdit::TemplateEdit(QWidget *parent, const print_options &printOptions, template_options &templateOptions) :
QDialog(parent), QDialog(parent),
ui(new Ui::TemplateEdit) ui(new Ui::TemplateEdit),
printOptions(printOptions),
templateOptions(templateOptions)
{ {
ui->setupUi(this); ui->setupUi(this);
this->templateOptions = templateOptions; newTemplateOptions = templateOptions;
newTemplateOptions = *templateOptions;
this->printOptions = printOptions;
// restore the settings and init the UI // restore the settings and init the UI
ui->fontSelection->setCurrentIndex(templateOptions->font_index); ui->fontSelection->setCurrentIndex(templateOptions.font_index);
ui->fontsize->setValue(lrint(templateOptions->font_size)); ui->fontsize->setValue(lrint(templateOptions.font_size));
ui->colorpalette->setCurrentIndex(templateOptions->color_palette_index); ui->colorpalette->setCurrentIndex(templateOptions.color_palette_index);
ui->linespacing->setValue(templateOptions->line_spacing); ui->linespacing->setValue(templateOptions.line_spacing);
ui->borderwidth->setValue(templateOptions->border_width); ui->borderwidth->setValue(templateOptions.border_width);
grantlee_template = TemplateLayout::readTemplate(printOptions->p_template); grantlee_template = TemplateLayout::readTemplate(printOptions.p_template);
if (printOptions->type == print_options::DIVELIST) if (printOptions.type == print_options::DIVELIST)
grantlee_template = TemplateLayout::readTemplate(printOptions->p_template); grantlee_template = TemplateLayout::readTemplate(printOptions.p_template);
else if (printOptions->type == print_options::STATISTICS) else if (printOptions.type == print_options::STATISTICS)
grantlee_template = TemplateLayout::readTemplate(QString::fromUtf8("statistics") + QDir::separator() + printOptions->p_template); grantlee_template = TemplateLayout::readTemplate(QString::fromUtf8("statistics") + QDir::separator() + printOptions.p_template);
// gui // gui
btnGroup = new QButtonGroup; btnGroup = new QButtonGroup;
@ -58,7 +58,7 @@ void TemplateEdit::updatePreview()
int height = ui->label->height(); int height = ui->label->height();
QPixmap map(width * 2, height * 2); QPixmap map(width * 2, height * 2);
map.fill(QColor::fromRgb(255, 255, 255)); map.fill(QColor::fromRgb(255, 255, 255));
Printer printer(&map, printOptions, &newTemplateOptions, Printer::PREVIEW); Printer printer(&map, printOptions, newTemplateOptions, Printer::PREVIEW);
printer.previewOnePage(); printer.previewOnePage();
ui->label->setPixmap(map.scaled(width, height, Qt::IgnoreAspectRatio)); ui->label->setPixmap(map.scaled(width, height, Qt::IgnoreAspectRatio));
@ -81,11 +81,11 @@ void TemplateEdit::updatePreview()
ui->colorpalette->setCurrentIndex(newTemplateOptions.color_palette_index); ui->colorpalette->setCurrentIndex(newTemplateOptions.color_palette_index);
// update grantlee template string // update grantlee template string
grantlee_template = TemplateLayout::readTemplate(printOptions->p_template); grantlee_template = TemplateLayout::readTemplate(printOptions.p_template);
if (printOptions->type == print_options::DIVELIST) if (printOptions.type == print_options::DIVELIST)
grantlee_template = TemplateLayout::readTemplate(printOptions->p_template); grantlee_template = TemplateLayout::readTemplate(printOptions.p_template);
else if (printOptions->type == print_options::STATISTICS) else if (printOptions.type == print_options::STATISTICS)
grantlee_template = TemplateLayout::readTemplate(QString::fromUtf8("statistics") + QDir::separator() + printOptions->p_template); grantlee_template = TemplateLayout::readTemplate(QString::fromUtf8("statistics") + QDir::separator() + printOptions.p_template);
} }
void TemplateEdit::on_fontsize_valueChanged(int font_size) void TemplateEdit::on_fontsize_valueChanged(int font_size)
@ -137,7 +137,7 @@ void TemplateEdit::on_colorpalette_currentIndexChanged(int index)
void TemplateEdit::saveSettings() void TemplateEdit::saveSettings()
{ {
if ((*templateOptions) != newTemplateOptions || grantlee_template.compare(ui->plainTextEdit->toPlainText())) { if (templateOptions != newTemplateOptions || grantlee_template.compare(ui->plainTextEdit->toPlainText())) {
QMessageBox msgBox(this); QMessageBox msgBox(this);
QString message = tr("Do you want to save your changes?"); QString message = tr("Do you want to save your changes?");
bool templateChanged = false; bool templateChanged = false;
@ -147,16 +147,16 @@ void TemplateEdit::saveSettings()
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Cancel);
if (msgBox.exec() == QMessageBox::Save) { if (msgBox.exec() == QMessageBox::Save) {
*templateOptions = newTemplateOptions; templateOptions = newTemplateOptions;
if (templateChanged) { if (templateChanged) {
TemplateLayout::writeTemplate(printOptions->p_template, ui->plainTextEdit->toPlainText()); TemplateLayout::writeTemplate(printOptions.p_template, ui->plainTextEdit->toPlainText());
if (printOptions->type == print_options::DIVELIST) if (printOptions.type == print_options::DIVELIST)
TemplateLayout::writeTemplate(printOptions->p_template, ui->plainTextEdit->toPlainText()); TemplateLayout::writeTemplate(printOptions.p_template, ui->plainTextEdit->toPlainText());
else if (printOptions->type == print_options::STATISTICS) else if (printOptions.type == print_options::STATISTICS)
TemplateLayout::writeTemplate(QString::fromUtf8("statistics") + QDir::separator() + printOptions->p_template, ui->plainTextEdit->toPlainText()); TemplateLayout::writeTemplate(QString::fromUtf8("statistics") + QDir::separator() + printOptions.p_template, ui->plainTextEdit->toPlainText());
} }
if (templateOptions->color_palette_index == CUSTOM) if (templateOptions.color_palette_index == CUSTOM)
custom_colors = templateOptions->color_palette; custom_colors = templateOptions.color_palette;
} }
} }
} }

View file

@ -14,30 +14,24 @@ class TemplateEdit : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit TemplateEdit(QWidget *parent, struct print_options *printOptions, struct template_options *templateOptions); explicit TemplateEdit(QWidget *parent, const print_options &printOptions, template_options &templateOptions);
~TemplateEdit(); ~TemplateEdit();
private slots: private slots:
void on_fontsize_valueChanged(int font_size); void on_fontsize_valueChanged(int font_size);
void on_linespacing_valueChanged(double line_spacing); void on_linespacing_valueChanged(double line_spacing);
void on_borderwidth_valueChanged(double border_width); void on_borderwidth_valueChanged(double border_width);
void on_fontSelection_currentIndexChanged(int index); void on_fontSelection_currentIndexChanged(int index);
void on_colorpalette_currentIndexChanged(int index); void on_colorpalette_currentIndexChanged(int index);
void on_buttonBox_clicked(QAbstractButton *button); void on_buttonBox_clicked(QAbstractButton *button);
void colorSelect(QAbstractButton *button); void colorSelect(QAbstractButton *button);
private: private:
Ui::TemplateEdit *ui; Ui::TemplateEdit *ui;
QButtonGroup *btnGroup; QButtonGroup *btnGroup;
bool editingCustomColors; bool editingCustomColors;
struct template_options *templateOptions; const print_options &printOptions;
template_options &templateOptions;
struct template_options newTemplateOptions; struct template_options newTemplateOptions;
struct print_options *printOptions;
QString grantlee_template; QString grantlee_template;
void saveSettings(); void saveSettings();
void updatePreview(); void updatePreview();

View file

@ -9,9 +9,9 @@
QList<QString> grantlee_templates, grantlee_statistics_templates; QList<QString> grantlee_templates, grantlee_statistics_templates;
int getTotalWork(print_options *printOptions) int getTotalWork(const print_options &printOptions)
{ {
if (printOptions->print_selected) { if (printOptions.print_selected) {
// return the correct number depending on all/selected dives // return the correct number depending on all/selected dives
// but don't return 0 as we might divide by this number // but don't return 0 as we might divide by this number
return amount_selected && !in_planner() ? amount_selected : 1; return amount_selected && !in_planner() ? amount_selected : 1;
@ -94,10 +94,9 @@ void copy_bundled_templates(QString src, QString dst, QStringList *templateBacku
} }
} }
TemplateLayout::TemplateLayout(print_options *printOptions, template_options *templateOptions) TemplateLayout::TemplateLayout(const print_options &printOptions, const template_options &templateOptions) :
printOptions(printOptions), templateOptions(templateOptions)
{ {
this->printOptions = printOptions;
this->templateOptions = templateOptions;
} }
QString TemplateLayout::generate() QString TemplateLayout::generate()
@ -117,7 +116,7 @@ QString TemplateLayout::generate()
int i; int i;
for_each_dive (i, dive) { for_each_dive (i, dive) {
//TODO check for exporting selected dives only //TODO check for exporting selected dives only
if (!dive->selected && printOptions->print_selected) if (!dive->selected && printOptions.print_selected)
continue; continue;
diveList.append(QVariant::fromValue(DiveObjectHelperGrantlee(dive))); diveList.append(QVariant::fromValue(DiveObjectHelperGrantlee(dive)));
progress++; progress++;
@ -125,11 +124,11 @@ QString TemplateLayout::generate()
} }
} }
QString templateContents = readTemplate(printOptions->p_template); QString templateContents = readTemplate(printOptions.p_template);
QHash<QString, QVariant> options; QHash<QString, QVariant> options;
options["print_options"] = QVariant::fromValue(*printOptions); options["print_options"] = QVariant::fromValue(printOptions);
options["template_options"] = QVariant::fromValue(*templateOptions); options["template_options"] = QVariant::fromValue(templateOptions);
options["dives"] = QVariant::fromValue(diveList); options["dives"] = QVariant::fromValue(diveList);
QList<token> tokens = lexer(templateContents); QList<token> tokens = lexer(templateContents);
QString buffer; QString buffer;
@ -154,12 +153,12 @@ QString TemplateLayout::generateStatistics()
i++; i++;
} }
QString templateFile = QString("statistics") + QDir::separator() + printOptions->p_template; QString templateFile = QString("statistics") + QDir::separator() + printOptions.p_template;
QString templateContents = readTemplate(templateFile); QString templateContents = readTemplate(templateFile);
QHash<QString, QVariant> options; QHash<QString, QVariant> options;
options["print_options"] = QVariant::fromValue(*printOptions); options["print_options"] = QVariant::fromValue(printOptions);
options["template_options"] = QVariant::fromValue(*templateOptions); options["template_options"] = QVariant::fromValue(templateOptions);
options["years"] = QVariant::fromValue(years); options["years"] = QVariant::fromValue(years);
QList<token> tokens = lexer(templateContents); QList<token> tokens = lexer(templateContents);
QString buffer; QString buffer;

View file

@ -10,7 +10,7 @@
#include "core/subsurface-qt/diveobjecthelper.h" #include "core/subsurface-qt/diveobjecthelper.h"
#include "core/subsurface-qt/cylinderobjecthelper.h" // TODO: remove once grantlee supports Q_GADGET objects #include "core/subsurface-qt/cylinderobjecthelper.h" // TODO: remove once grantlee supports Q_GADGET objects
int getTotalWork(print_options *printOptions); int getTotalWork(const print_options &printOptions);
void find_all_templates(); void find_all_templates();
void set_bundled_templates_as_read_only(); void set_bundled_templates_as_read_only();
void copy_bundled_templates(QString src, QString dst, QStringList *templateBackupList); void copy_bundled_templates(QString src, QString dst, QStringList *templateBackupList);
@ -27,22 +27,20 @@ extern QList<QString> grantlee_templates, grantlee_statistics_templates;
class TemplateLayout : public QObject { class TemplateLayout : public QObject {
Q_OBJECT Q_OBJECT
public: public:
TemplateLayout(print_options *printOptions, template_options *templateOptions); TemplateLayout(const print_options &printOptions, const template_options &templateOptions);
QString generate(); QString generate();
QString generateStatistics(); QString generateStatistics();
static QString readTemplate(QString template_name); static QString readTemplate(QString template_name);
static void writeTemplate(QString template_name, QString grantlee_template); static void writeTemplate(QString template_name, QString grantlee_template);
private: private:
print_options *printOptions; const print_options &printOptions;
template_options *templateOptions; const template_options &templateOptions;
QList<token> lexer(QString input); QList<token> lexer(QString input);
void parser(QList<token> tokenList, int &pos, QTextStream &out, QHash<QString, QVariant> options); void parser(QList<token> tokenList, int &pos, QTextStream &out, QHash<QString, QVariant> options);
QVariant getValue(QString list, QString property, QVariant option); QVariant getValue(QString list, QString property, QVariant option);
QString translate(QString s, QHash<QString, QVariant> options); QString translate(QString s, QHash<QString, QVariant> options);
signals: signals:
void progressUpdated(int value); void progressUpdated(int value);
}; };