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()
{
#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

View file

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

View file

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

View file

@ -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 <QWebElementCollection>
#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),
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<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()
{
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;

View file

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

View file

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

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

View file

@ -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<dive *> &dives);
QString generateStatistics();
static QString readTemplate(QString template_name);
static void writeTemplate(QString template_name, QString grantlee_template);