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

View file

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

View file

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

View file

@ -64,14 +64,14 @@ class PrintOptions : public QWidget {
Q_OBJECT
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();
QString getSelectedTemplate();
private:
Ui::PrintOptions ui;
struct print_options *printOptions = nullptr;
struct template_options *templateOptions = nullptr;
print_options &printOptions;
template_options &templateOptions;
bool hasSetupSlots;
QString lastImportExportTemplate;
void setupTemplates();

View file

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

View file

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

View file

@ -9,9 +9,9 @@
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
// but don't return 0 as we might divide by this number
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()
@ -117,7 +116,7 @@ QString TemplateLayout::generate()
int i;
for_each_dive (i, dive) {
//TODO check for exporting selected dives only
if (!dive->selected && printOptions->print_selected)
if (!dive->selected && printOptions.print_selected)
continue;
diveList.append(QVariant::fromValue(DiveObjectHelperGrantlee(dive)));
progress++;
@ -125,11 +124,11 @@ QString TemplateLayout::generate()
}
}
QString templateContents = readTemplate(printOptions->p_template);
QString templateContents = readTemplate(printOptions.p_template);
QHash<QString, QVariant> options;
options["print_options"] = QVariant::fromValue(*printOptions);
options["template_options"] = QVariant::fromValue(*templateOptions);
options["print_options"] = QVariant::fromValue(printOptions);
options["template_options"] = QVariant::fromValue(templateOptions);
options["dives"] = QVariant::fromValue(diveList);
QList<token> tokens = lexer(templateContents);
QString buffer;
@ -154,12 +153,12 @@ QString TemplateLayout::generateStatistics()
i++;
}
QString templateFile = QString("statistics") + QDir::separator() + printOptions->p_template;
QString templateFile = QString("statistics") + QDir::separator() + printOptions.p_template;
QString templateContents = readTemplate(templateFile);
QHash<QString, QVariant> options;
options["print_options"] = QVariant::fromValue(*printOptions);
options["template_options"] = QVariant::fromValue(*templateOptions);
options["print_options"] = QVariant::fromValue(printOptions);
options["template_options"] = QVariant::fromValue(templateOptions);
options["years"] = QVariant::fromValue(years);
QList<token> tokens = lexer(templateContents);
QString buffer;

View file

@ -10,7 +10,7 @@
#include "core/subsurface-qt/diveobjecthelper.h"
#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 set_bundled_templates_as_read_only();
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 {
Q_OBJECT
public:
TemplateLayout(print_options *printOptions, template_options *templateOptions);
TemplateLayout(const print_options &printOptions, const template_options &templateOptions);
QString generate();
QString generateStatistics();
static QString readTemplate(QString template_name);
static void writeTemplate(QString template_name, QString grantlee_template);
private:
print_options *printOptions;
template_options *templateOptions;
const print_options &printOptions;
const template_options &templateOptions;
QList<token> lexer(QString input);
void parser(QList<token> tokenList, int &pos, QTextStream &out, QHash<QString, QVariant> options);
QVariant getValue(QString list, QString property, QVariant option);
QString translate(QString s, QHash<QString, QVariant> options);
signals:
void progressUpdated(int value);
};