From d51589b9a7b26b79932eab2d218bc5005c963d4d Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 6 Nov 2022 20:37:53 +0100 Subject: [PATCH] printing: don't access displayed_dive in printing code To phase out this global variable, avoid access of displayed_dive in the printing code. This is used when printing a plan. Instead, when in plan-mode, pass the planned dive to the printing code as a single dive to be printed. Signed-off-by: Berthold Stoeger --- desktop-widgets/mainwindow.cpp | 6 ++++-- desktop-widgets/printdialog.cpp | 6 +++--- desktop-widgets/printdialog.h | 6 ++++-- desktop-widgets/printer.cpp | 27 ++++++++++++++++++++++----- desktop-widgets/printer.h | 8 ++++++-- desktop-widgets/templateedit.cpp | 2 +- desktop-widgets/templatelayout.cpp | 16 +++------------- desktop-widgets/templatelayout.h | 2 +- 8 files changed, 44 insertions(+), 29 deletions(-) diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index a513133d0..92ffa229b 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -548,8 +548,10 @@ void MainWindow::updateLastUsedDir(const QString &dir) void MainWindow::on_actionPrint_triggered() { #ifndef NO_PRINTING - bool in_planner = inPlanner(); - PrintDialog dlg(in_planner, this); + // When in planner, only print the planned dive. + dive *singleDive = appState == ApplicationState::PlanDive ? &displayed_dive + : nullptr; + PrintDialog dlg(singleDive, this); dlg.exec(); #endif diff --git a/desktop-widgets/printdialog.cpp b/desktop-widgets/printdialog.cpp index 25183beaf..12ac3790c 100644 --- a/desktop-widgets/printdialog.cpp +++ b/desktop-widgets/printdialog.cpp @@ -19,9 +19,9 @@ template_options::color_palette_struct ssrf_colors, almond_colors, blueshades_colors, custom_colors; -PrintDialog::PrintDialog(bool inPlanner, QWidget *parent) : +PrintDialog::PrintDialog(dive *singleDive, QWidget *parent) : QDialog(parent, QFlag(0)), - inPlanner(inPlanner), + singleDive(singleDive), printer(NULL), qprinter(NULL) { @@ -179,7 +179,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, inPlanner); + printer = new Printer(qprinter, printOptions, templateOptions, Printer::PRINT, singleDive); } } diff --git a/desktop-widgets/printdialog.h b/desktop-widgets/printdialog.h index 7e98f22f6..073ad6a72 100644 --- a/desktop-widgets/printdialog.h +++ b/desktop-widgets/printdialog.h @@ -7,6 +7,7 @@ #include "templateedit.h" #include "printoptions.h" +struct dive; class Printer; class QPrinter; class QProgressBar; @@ -18,11 +19,12 @@ class PrintDialog : public QDialog { Q_OBJECT public: - explicit PrintDialog(bool inPlanner, QWidget *parent = 0); + // If singleDive is non-null, print only that single dive + explicit PrintDialog(dive *singleDive, QWidget *parent = 0); ~PrintDialog(); private: - bool inPlanner; + dive *singleDive; PrintOptions *optionsWidget; QProgressBar *progressBar; Printer *printer; diff --git a/desktop-widgets/printer.cpp b/desktop-widgets/printer.cpp index 529491fe4..1491caed1 100644 --- a/desktop-widgets/printer.cpp +++ b/desktop-widgets/printer.cpp @@ -2,6 +2,7 @@ #include "printer.h" #include "templatelayout.h" #include "core/dive.h" // for get_dive_by_uniq_id() +#include "core/selection.h" #include "core/statistics.h" #include "core/qthelper.h" #include "profile-widget/profilescene.h" @@ -14,13 +15,13 @@ #include #include -Printer::Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner) : +Printer::Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, dive *singleDive) : paintDevice(paintDevice), webView(new QWebView), printOptions(printOptions), templateOptions(templateOptions), printMode(printMode), - inPlanner(inPlanner), + singleDive(singleDive), done(0) { } @@ -151,6 +152,22 @@ void Printer::templateProgessUpdated(int value) emit progessUpdated(done); } +std::vector Printer::getDives() const +{ + if (singleDive) { + return { singleDive }; + } else if (printOptions.print_selected) { + return getDiveSelection(); + } else { + std::vector res; + int i; + struct dive *dive; + for_each_dive (i, dive) + res.push_back(dive); + return res; + } +} + QString Printer::exportHtml() { TemplateLayout t(printOptions, templateOptions); @@ -158,7 +175,7 @@ QString Printer::exportHtml() QString html; if (printOptions.type == print_options::DIVELIST) - html = t.generate(inPlanner); + html = t.generate(getDives()); else if (printOptions.type == print_options::STATISTICS ) html = t.generateStatistics(); @@ -188,7 +205,7 @@ void Printer::print() // export border width with at least 1 pixel // templateOptions.borderwidth = std::max(1, pageSize.width() / 1000); if (printOptions.type == print_options::DIVELIST) - webView->setHtml(t.generate(inPlanner)); + webView->setHtml(t.generate(getDives())); else if (printOptions.type == print_options::STATISTICS ) webView->setHtml(t.generateStatistics()); if (printOptions.color_selected && printerPtr->colorMode()) @@ -222,7 +239,7 @@ void Printer::previewOnePage() // initialize the border settings // templateOptions.border_width = std::max(1, pageSize.width() / 1000); if (printOptions.type == print_options::DIVELIST) - webView->setHtml(t.generate(inPlanner)); + webView->setHtml(t.generate(getDives())); else if (printOptions.type == print_options::STATISTICS ) webView->setHtml(t.generateStatistics()); bool ok; diff --git a/desktop-widgets/printer.h b/desktop-widgets/printer.h index 3d16a3d9a..28838f4a4 100644 --- a/desktop-widgets/printer.h +++ b/desktop-widgets/printer.h @@ -5,6 +5,7 @@ #include "printoptions.h" #include "templateedit.h" +struct dive; class ProfileScene; class QPainter; class QPaintDevice; @@ -27,10 +28,11 @@ private: const template_options &templateOptions; QSize pageSize; PrintMode printMode; - bool inPlanner; + struct dive *singleDive; int done; void render(int Pages); void flowRender(); + std::vector getDives() const; void putProfileImage(const QRect &box, const QRect &viewPort, QPainter *painter, struct dive *dive, ProfileScene *profile); @@ -38,7 +40,9 @@ private slots: void templateProgessUpdated(int value); public: - Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, PrintMode printMode, bool inPlanner); + // If singleDive is non-null, then only print this particular dive. + Printer(QPaintDevice *paintDevice, const print_options &printOptions, const template_options &templateOptions, + PrintMode printMode, dive *singleDive); ~Printer(); void print(); void previewOnePage(); diff --git a/desktop-widgets/templateedit.cpp b/desktop-widgets/templateedit.cpp index db8a92007..fe41d3ce9 100644 --- a/desktop-widgets/templateedit.cpp +++ b/desktop-widgets/templateedit.cpp @@ -59,7 +59,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, false); + Printer printer(&map, printOptions, newTemplateOptions, Printer::PREVIEW, nullptr); printer.previewOnePage(); ui->label->setPixmap(map.scaled(width, height, Qt::IgnoreAspectRatio)); diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp index c79a4c205..81ee337be 100644 --- a/desktop-widgets/templatelayout.cpp +++ b/desktop-widgets/templatelayout.cpp @@ -95,24 +95,14 @@ TemplateLayout::TemplateLayout(const print_options &printOptions, const template { } -QString TemplateLayout::generate(bool in_planner) +QString TemplateLayout::generate(const std::vector &dives) { QString htmlContent; State state; - if (in_planner) { - state.dives.append(&displayed_dive); - } else { - int i; - struct dive *dive; - for_each_dive (i, dive) { - //TODO check for exporting selected dives only - if (!dive->selected && printOptions.print_selected) - continue; - state.dives.append(dive); - } - } + for (dive *d: dives) + state.dives.append(d); QString templateContents = readTemplate(printOptions.p_template); numDives = state.dives.size(); diff --git a/desktop-widgets/templatelayout.h b/desktop-widgets/templatelayout.h index c25c9d4ea..724b43b59 100644 --- a/desktop-widgets/templatelayout.h +++ b/desktop-widgets/templatelayout.h @@ -27,7 +27,7 @@ class TemplateLayout : public QObject { Q_OBJECT public: TemplateLayout(const print_options &printOptions, const template_options &templateOptions); - QString generate(bool in_planner); + QString generate(const std::vector &dives); QString generateStatistics(); static QString readTemplate(QString template_name); static void writeTemplate(QString template_name, QString grantlee_template);