Print: fix issues when printing a lot of dives in table print

This patch improves the algorithm when estimating where
to put the new page header in the table and how we move
larger dive rows on a new page. It now performs a couple of
'passes', where the first one processes the table and the
second one is used to compensate for the lost space.

Fixes #326

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2013-12-06 18:24:28 +02:00 committed by Dirk Hohndel
parent c7a4bb185c
commit 5506fa4b38

View file

@ -273,7 +273,7 @@ QTableView *PrintLayout::createProfileTable(ProfilePrintModel *model, const int
void PrintLayout::printTable() void PrintLayout::printTable()
{ {
struct dive *dive; struct dive *dive;
const int stage = 33; // there are 3 stages in this routine: 100% / 3 ~= 33% int done = 0; // percents done
int i, row = 0, progress, total = estimateTotalDives(); int i, row = 0, progress, total = estimateTotalDives();
if (!total) if (!total)
return; return;
@ -308,8 +308,9 @@ void PrintLayout::printTable()
addTablePrintDataRow(&model, row, dive); addTablePrintDataRow(&model, row, dive);
row++; row++;
progress++; progress++;
emit signalProgress((progress * stage) / total); emit signalProgress((progress * 10) / total);
} }
done = 10;
table.setModel(&model); // set model to table table.setModel(&model); // set model to table
// resize columns to percentages from page width // resize columns to percentages from page width
int accW = 0; int accW = 0;
@ -328,27 +329,44 @@ void PrintLayout::printTable()
// a list of vertical offsets where pages begin and some helpers // a list of vertical offsets where pages begin and some helpers
QList<unsigned int> pageIndexes; QList<unsigned int> pageIndexes;
pageIndexes.append(0); pageIndexes.append(0);
int tableHeight = 0, rowH = 0, accH = 0, headings = 0;
// process all rows /* the algorithm bellow processes the table rows in multiple passes,
progress = 0; * compensating for loss of space due to moving rows on a new page instead
total = model.rows; * of truncating them.
for (int i = 0; i < total; i++) { * there is a 'passes' array defining how much percents of the total
rowH = table.rowHeight(i); * progress each will take. given, the first and last stage of this function
accH += rowH; * use 10% each, then the sum of passes[] here should be 80%.
if (accH > scaledPageH) { // push a new page index and add a heading * two should be enough! */
pageIndexes.append(pageIndexes.last() + (accH - rowH)); const int passes[] = { 70, 10 };
addTablePrintHeadingRow(&model, i); int tableHeight = 0, lastAccIndex = 0, rowH, accH, headings;
headings += rowH; // last row was moved to a new page; compensate! bool isHeading = false;
accH = 0;
i--; for (int pass = 0; pass < sizeof(passes); pass++) {
progress = headings = accH = 0;
total = model.rows - lastAccIndex;
for (int i = lastAccIndex; i < model.rows; i++) {
rowH = table.rowHeight(i);
accH += rowH;
if (isHeading) {
headings += rowH;
isHeading = false;
}
if (accH > scaledPageH) {
lastAccIndex = i;
pageIndexes.append(pageIndexes.last() + (accH - rowH));
addTablePrintHeadingRow(&model, i);
isHeading = true;
accH = 0;
i--;
}
tableHeight += table.rowHeight(i);
progress++;
emit signalProgress(done + (progress * passes[pass]) / total);
} }
tableHeight += rowH; done += passes[pass];
progress++;
emit signalProgress(stage + (progress * stage) / total);
} }
done = 90;
pageIndexes.append(pageIndexes.last() + accH + headings); pageIndexes.append(pageIndexes.last() + accH + headings);
// resize the whole widget so that it can be rendered
table.resize(scaledPageW, tableHeight); table.resize(scaledPageW, tableHeight);
// attach a painter and render pages by using pageIndexes // attach a painter and render pages by using pageIndexes
@ -366,7 +384,7 @@ void PrintLayout::printTable()
pageIndexes.at(i + 1) - pageIndexes.at(i) + 1); pageIndexes.at(i + 1) - pageIndexes.at(i) + 1);
table.render(&painter, QPoint(0, 0), region); table.render(&painter, QPoint(0, 0), region);
progress++; progress++;
emit signalProgress((stage << 1) + (progress * (stage + 1)) / total); emit signalProgress(done + (progress * 10) / total);
} }
} }