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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-02-13 18:58:14 +01:00 committed by Dirk Hohndel
parent 6ef4f377f0
commit 622e9ba373
3 changed files with 21 additions and 36 deletions

View file

@ -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<QPrinter*>(paintDevice)->newPage();
}
painter.end();
@ -220,7 +220,6 @@ void Printer::print()
return;
}
QPrinter *printerPtr;
printerPtr = static_cast<QPrinter*>(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()

View file

@ -15,16 +15,6 @@
QList<QString> 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<token> tokens = lexer(templateContents);
QString buffer;
@ -290,7 +275,7 @@ static QRegularExpression ifstatement(R"(forloop\.counter\|\s*divisibleby\:\s*(\
template<typename V, typename T>
void TemplateLayout::parser_for(QList<token> 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<token> 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<token> 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 {

View file

@ -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<token> lexer(QString input);
void parser(QList<token> tokenList, int from, int to, QTextStream &out, State &state);
template<typename V, typename T>
void parser_for(QList<token> tokenList, int from, int to, QTextStream &out, State &state, const V &data, const T *&act);
void parser_for(QList<token> 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);