From 622e9ba373f898a0d51fc009f3615d83ffd3a7fc Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 13 Feb 2021 18:58:14 +0100 Subject: [PATCH] printing: "fix" progress indicator In TemplateLayout, there was a progress indication, which reported the progress - not of the actual rendering - but of adding the dives to the "to render" list. Which is of course done in less than a ms, making the whole thing completely pointless. Instead, emit progress when actually looping over the dives or statistics. Nobody ever noticed the problem because even rendering is done in fractions of a second and indeed is accounted to only one fifth of the total progress. The real purpose of this "fix" is to get rid of the getTotalWork() function, which was just insane. Instead of asking the TemplateLayout how many dives it rendered, this number was extracted from global state. Simply store the number of dives in the TemplateLayout object instead. Moreover, fix two coding style issues: - "Page" variable identifier starting with a capital - The Printer::render() being defined (as opposed to declared) with a default parameter. This is not how C++'s default parameters work, sorry. Signed-off-by: Berthold Stoeger --- desktop-widgets/printer.cpp | 18 ++++++--------- desktop-widgets/templatelayout.cpp | 35 ++++++++++-------------------- desktop-widgets/templatelayout.h | 4 ++-- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/desktop-widgets/printer.cpp b/desktop-widgets/printer.cpp index f38b661c5..b3af96a75 100644 --- a/desktop-widgets/printer.cpp +++ b/desktop-widgets/printer.cpp @@ -122,7 +122,7 @@ void Printer::flowRender() painter.end(); } -void Printer::render(int Pages = 0) +void Printer::render(int pages) { // keep original preferences ProfileWidget2 *profile = MainWindow::instance()->graphics; @@ -155,7 +155,7 @@ void Printer::render(int Pages = 0) profile->setFontPrintScale(printFontScale); int elemNo = 0; - for (int i = 0; i < Pages; i++) { + for (int i = 0; i < pages; i++) { // render the base Html template webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer); @@ -173,8 +173,8 @@ void Printer::render(int Pages = 0) viewPort.adjust(0, pageSize.height(), 0, pageSize.height()); // rendering progress is 4/5 of total work - emit(progessUpdated(lrint((i * 80.0 / Pages) + done))); - if (i < Pages - 1 && printMode == Printer::PRINT) + emit(progessUpdated(lrint((i * 80.0 / pages) + done))); + if (i < pages - 1 && printMode == Printer::PRINT) static_cast(paintDevice)->newPage(); } painter.end(); @@ -220,7 +220,6 @@ void Printer::print() return; } - QPrinter *printerPtr; printerPtr = static_cast(paintDevice); @@ -253,13 +252,10 @@ void Printer::print() divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed //TODO: show warning } - int Pages; - if (divesPerPage == 0) { + if (divesPerPage == 0) flowRender(); - } else { - Pages = qCeil(getTotalWork(printOptions) / (float)divesPerPage); - render(Pages); - } + else + render((t.numDives - 1) / divesPerPage + 1); } void Printer::previewOnePage() diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp index f1454ac6e..ca7e6218a 100644 --- a/desktop-widgets/templatelayout.cpp +++ b/desktop-widgets/templatelayout.cpp @@ -15,16 +15,6 @@ QList grantlee_templates, grantlee_statistics_templates; -int getTotalWork(const print_options &printOptions) -{ - 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; - } - return dive_table.nr; -} - void find_all_templates() { const QLatin1String ext(".html"); @@ -101,36 +91,31 @@ void copy_bundled_templates(QString src, QString dst, QStringList *templateBacku } TemplateLayout::TemplateLayout(const print_options &printOptions, const template_options &templateOptions) : - printOptions(printOptions), templateOptions(templateOptions) + numDives(0), printOptions(printOptions), templateOptions(templateOptions) { } QString TemplateLayout::generate() { - int progress = 0; - int totalWork = getTotalWork(printOptions); - QString htmlContent; State state; - struct dive *dive; if (in_planner()) { state.dives.append(&displayed_dive); - emit progressUpdated(100.0); } 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); - progress++; - emit progressUpdated(lrint(progress * 100.0 / totalWork)); } } QString templateContents = readTemplate(printOptions.p_template); + numDives = state.dives.size(); QList tokens = lexer(templateContents); QString buffer; @@ -290,7 +275,7 @@ static QRegularExpression ifstatement(R"(forloop\.counter\|\s*divisibleby\:\s*(\ template void TemplateLayout::parser_for(QList tokenList, int from, int to, QTextStream &out, State &state, - const V &data, const T *&act) + const V &data, const T *&act, bool emitProgress) { const T *old = act; int i = 1; // Loop iterators start at one @@ -299,7 +284,11 @@ void TemplateLayout::parser_for(QList tokenList, int from, int to, QTextS act = &item; state.forloopiterator = i++; parser(tokenList, from, to, out, state); + if (emitProgress) + emit progressUpdated(state.forloopiterator * 100 / data.size()); } + if (data.empty()) + emit progressUpdated(100); act = old; state.forloopiterator = olditerator; } @@ -357,17 +346,17 @@ void TemplateLayout::parser(QList tokenList, int from, int to, QTextStrea break; } if (listname == "years") { - parser_for(tokenList, pos, loop_end, capture, state, state.years, state.currentYear); + parser_for(tokenList, pos, loop_end, capture, state, state.years, state.currentYear, true); } else if (listname == "dives") { - parser_for(tokenList, pos, loop_end, capture, state, state.dives, state.currentDive); + parser_for(tokenList, pos, loop_end, capture, state, state.dives, state.currentDive, true); } else if (listname == "cylinders") { if (state.currentDive) - parser_for(tokenList, pos, loop_end, capture, state, formatCylinders(*state.currentDive), state.currentCylinder); + parser_for(tokenList, pos, loop_end, capture, state, formatCylinders(*state.currentDive), state.currentCylinder, false); else qWarning("cylinders loop outside of dive"); } else if (listname == "cylinderObjects") { if (state.currentDive) - parser_for(tokenList, pos, loop_end, capture, state, cylinderList(*state.currentDive), state.currentCylinderObject); + parser_for(tokenList, pos, loop_end, capture, state, cylinderList(*state.currentDive), state.currentCylinderObject, false); else qWarning("cylinderObjects loop outside of dive"); } else { diff --git a/desktop-widgets/templatelayout.h b/desktop-widgets/templatelayout.h index 24dfdf278..3b5c3b9b5 100644 --- a/desktop-widgets/templatelayout.h +++ b/desktop-widgets/templatelayout.h @@ -10,7 +10,6 @@ struct print_options; struct template_options; class QTextStream; -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); @@ -32,6 +31,7 @@ public: QString generateStatistics(); static QString readTemplate(QString template_name); static void writeTemplate(QString template_name, QString grantlee_template); + int numDives; // valid after a call to generate() private: struct State { @@ -49,7 +49,7 @@ private: QList lexer(QString input); void parser(QList tokenList, int from, int to, QTextStream &out, State &state); template - void parser_for(QList tokenList, int from, int to, QTextStream &out, State &state, const V &data, const T *&act); + void parser_for(QList tokenList, int from, int to, QTextStream &out, State &state, const V &data, const T *&act, bool emitProgress); QVariant getValue(QString list, QString property, const State &state); QString translate(QString s, State &state);