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

@ -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();