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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-11-06 20:37:53 +01:00 committed by bstoeger
parent 80df790efc
commit d51589b9a7
8 changed files with 44 additions and 29 deletions

View file

@ -548,8 +548,10 @@ void MainWindow::updateLastUsedDir(const QString &dir)
void MainWindow::on_actionPrint_triggered() void MainWindow::on_actionPrint_triggered()
{ {
#ifndef NO_PRINTING #ifndef NO_PRINTING
bool in_planner = inPlanner(); // When in planner, only print the planned dive.
PrintDialog dlg(in_planner, this); dive *singleDive = appState == ApplicationState::PlanDive ? &displayed_dive
: nullptr;
PrintDialog dlg(singleDive, this);
dlg.exec(); dlg.exec();
#endif #endif

View file

@ -19,9 +19,9 @@
template_options::color_palette_struct ssrf_colors, almond_colors, blueshades_colors, custom_colors; 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)), QDialog(parent, QFlag(0)),
inPlanner(inPlanner), singleDive(singleDive),
printer(NULL), printer(NULL),
qprinter(NULL) qprinter(NULL)
{ {
@ -179,7 +179,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, inPlanner); printer = new Printer(qprinter, printOptions, templateOptions, Printer::PRINT, singleDive);
} }
} }

View file

@ -7,6 +7,7 @@
#include "templateedit.h" #include "templateedit.h"
#include "printoptions.h" #include "printoptions.h"
struct dive;
class Printer; class Printer;
class QPrinter; class QPrinter;
class QProgressBar; class QProgressBar;
@ -18,11 +19,12 @@ class PrintDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: 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(); ~PrintDialog();
private: private:
bool inPlanner; dive *singleDive;
PrintOptions *optionsWidget; PrintOptions *optionsWidget;
QProgressBar *progressBar; QProgressBar *progressBar;
Printer *printer; Printer *printer;

View file

@ -2,6 +2,7 @@
#include "printer.h" #include "printer.h"
#include "templatelayout.h" #include "templatelayout.h"
#include "core/dive.h" // for get_dive_by_uniq_id() #include "core/dive.h" // for get_dive_by_uniq_id()
#include "core/selection.h"
#include "core/statistics.h" #include "core/statistics.h"
#include "core/qthelper.h" #include "core/qthelper.h"
#include "profile-widget/profilescene.h" #include "profile-widget/profilescene.h"
@ -14,13 +15,13 @@
#include <QWebElementCollection> #include <QWebElementCollection>
#include <QWebElement> #include <QWebElement>
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), paintDevice(paintDevice),
webView(new QWebView), webView(new QWebView),
printOptions(printOptions), printOptions(printOptions),
templateOptions(templateOptions), templateOptions(templateOptions),
printMode(printMode), printMode(printMode),
inPlanner(inPlanner), singleDive(singleDive),
done(0) done(0)
{ {
} }
@ -151,6 +152,22 @@ void Printer::templateProgessUpdated(int value)
emit progessUpdated(done); emit progessUpdated(done);
} }
std::vector<dive *> Printer::getDives() const
{
if (singleDive) {
return { singleDive };
} else if (printOptions.print_selected) {
return getDiveSelection();
} else {
std::vector<dive *> res;
int i;
struct dive *dive;
for_each_dive (i, dive)
res.push_back(dive);
return res;
}
}
QString Printer::exportHtml() QString Printer::exportHtml()
{ {
TemplateLayout t(printOptions, templateOptions); TemplateLayout t(printOptions, templateOptions);
@ -158,7 +175,7 @@ QString Printer::exportHtml()
QString html; QString html;
if (printOptions.type == print_options::DIVELIST) if (printOptions.type == print_options::DIVELIST)
html = t.generate(inPlanner); html = t.generate(getDives());
else if (printOptions.type == print_options::STATISTICS ) else if (printOptions.type == print_options::STATISTICS )
html = t.generateStatistics(); html = t.generateStatistics();
@ -188,7 +205,7 @@ void Printer::print()
// 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(inPlanner)); webView->setHtml(t.generate(getDives()));
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())
@ -222,7 +239,7 @@ void Printer::previewOnePage()
// 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(inPlanner)); webView->setHtml(t.generate(getDives()));
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;

View file

@ -5,6 +5,7 @@
#include "printoptions.h" #include "printoptions.h"
#include "templateedit.h" #include "templateedit.h"
struct dive;
class ProfileScene; class ProfileScene;
class QPainter; class QPainter;
class QPaintDevice; class QPaintDevice;
@ -27,10 +28,11 @@ private:
const template_options &templateOptions; const template_options &templateOptions;
QSize pageSize; QSize pageSize;
PrintMode printMode; PrintMode printMode;
bool inPlanner; struct dive *singleDive;
int done; int done;
void render(int Pages); void render(int Pages);
void flowRender(); void flowRender();
std::vector<dive *> getDives() const;
void putProfileImage(const QRect &box, const QRect &viewPort, QPainter *painter, void putProfileImage(const QRect &box, const QRect &viewPort, QPainter *painter,
struct dive *dive, ProfileScene *profile); struct dive *dive, ProfileScene *profile);
@ -38,7 +40,9 @@ private slots:
void templateProgessUpdated(int value); void templateProgessUpdated(int value);
public: 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(); ~Printer();
void print(); void print();
void previewOnePage(); void previewOnePage();

View file

@ -59,7 +59,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, false); Printer printer(&map, printOptions, newTemplateOptions, Printer::PREVIEW, nullptr);
printer.previewOnePage(); printer.previewOnePage();
ui->label->setPixmap(map.scaled(width, height, Qt::IgnoreAspectRatio)); ui->label->setPixmap(map.scaled(width, height, Qt::IgnoreAspectRatio));

View file

@ -95,24 +95,14 @@ TemplateLayout::TemplateLayout(const print_options &printOptions, const template
{ {
} }
QString TemplateLayout::generate(bool in_planner) QString TemplateLayout::generate(const std::vector<dive *> &dives)
{ {
QString htmlContent; QString htmlContent;
State state; State state;
if (in_planner) { for (dive *d: dives)
state.dives.append(&displayed_dive); state.dives.append(d);
} 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);
}
}
QString templateContents = readTemplate(printOptions.p_template); QString templateContents = readTemplate(printOptions.p_template);
numDives = state.dives.size(); numDives = state.dives.size();

View file

@ -27,7 +27,7 @@ class TemplateLayout : public QObject {
Q_OBJECT Q_OBJECT
public: public:
TemplateLayout(const print_options &printOptions, const template_options &templateOptions); TemplateLayout(const print_options &printOptions, const template_options &templateOptions);
QString generate(bool in_planner); QString generate(const std::vector<dive *> &dives);
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);