2013-07-25 09:52:20 +00:00
|
|
|
#include <QtCore/qmath.h>
|
2013-07-10 16:32:15 +00:00
|
|
|
#include <QDebug>
|
2013-07-10 12:34:57 +00:00
|
|
|
#include <QPainter>
|
2013-07-10 13:04:00 +00:00
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QApplication>
|
2013-07-25 09:52:20 +00:00
|
|
|
#include <QTableView>
|
|
|
|
#include <QHeaderView>
|
2014-03-07 18:02:37 +00:00
|
|
|
#include <QPointer>
|
2014-07-11 23:22:19 +00:00
|
|
|
#include <QPicture>
|
2014-08-05 17:01:38 +00:00
|
|
|
#include <QMessageBox>
|
2014-07-11 23:22:19 +00:00
|
|
|
|
2013-07-10 12:34:57 +00:00
|
|
|
#include "mainwindow.h"
|
2013-07-10 19:55:40 +00:00
|
|
|
#include "../dive.h"
|
2013-07-10 21:45:29 +00:00
|
|
|
#include "../display.h"
|
2013-12-04 13:44:31 +00:00
|
|
|
#include "printdialog.h"
|
|
|
|
#include "printlayout.h"
|
2013-07-11 09:45:41 +00:00
|
|
|
#include "models.h"
|
2013-10-03 14:50:40 +00:00
|
|
|
#include "modeldelegates.h"
|
2013-07-10 12:34:57 +00:00
|
|
|
|
2014-11-13 22:57:42 +00:00
|
|
|
PrintLayout::PrintLayout(PrintDialog *dialogPtr, QPrinter *printerPtr, struct print_options *optionsPtr)
|
2013-07-10 12:34:57 +00:00
|
|
|
{
|
|
|
|
dialog = dialogPtr;
|
|
|
|
printer = printerPtr;
|
|
|
|
printOptions = optionsPtr;
|
2013-07-10 21:48:38 +00:00
|
|
|
|
|
|
|
// table print settings
|
2013-07-25 09:52:20 +00:00
|
|
|
tablePrintHeadingBackground = 0xffeeeeee;
|
|
|
|
tablePrintColumnNames.append(tr("Dive#"));
|
|
|
|
tablePrintColumnNames.append(tr("Date"));
|
|
|
|
tablePrintColumnNames.append(tr("Depth"));
|
|
|
|
tablePrintColumnNames.append(tr("Duration"));
|
|
|
|
tablePrintColumnNames.append(tr("Master"));
|
|
|
|
tablePrintColumnNames.append(tr("Buddy"));
|
|
|
|
tablePrintColumnNames.append(tr("Location"));
|
|
|
|
tablePrintColumnWidths.append(7);
|
2014-07-12 04:51:44 +00:00
|
|
|
tablePrintColumnWidths.append(14);
|
|
|
|
tablePrintColumnWidths.append(8);
|
|
|
|
tablePrintColumnWidths.append(8);
|
2013-07-25 09:52:20 +00:00
|
|
|
tablePrintColumnWidths.append(15);
|
|
|
|
tablePrintColumnWidths.append(15);
|
|
|
|
tablePrintColumnWidths.append(33);
|
2013-10-03 14:50:40 +00:00
|
|
|
// profile print settings
|
2013-11-30 12:38:54 +00:00
|
|
|
const int dw = 20; // base percentage
|
2013-10-03 14:50:40 +00:00
|
|
|
profilePrintColumnWidths.append(dw);
|
|
|
|
profilePrintColumnWidths.append(dw);
|
2014-07-12 04:39:44 +00:00
|
|
|
profilePrintColumnWidths.append(dw + 8);
|
|
|
|
profilePrintColumnWidths.append(dw - 4);
|
|
|
|
profilePrintColumnWidths.append(dw - 4); // fit to 100%
|
2014-07-25 00:50:39 +00:00
|
|
|
const int sr = 12; // smallest row height in pixels
|
2013-11-30 15:37:27 +00:00
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr + 4);
|
2013-10-03 14:50:40 +00:00
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
2013-11-30 12:38:54 +00:00
|
|
|
profilePrintRowHeights.append(sr);
|
|
|
|
profilePrintRowHeights.append(sr);
|
2013-07-10 12:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintLayout::print()
|
|
|
|
{
|
2013-07-10 13:04:00 +00:00
|
|
|
// we call setup each time to check if the printer properties have changed
|
|
|
|
setup();
|
2014-08-05 17:01:38 +00:00
|
|
|
if (pageW == 0 || pageH == 0) {
|
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setIcon(QMessageBox::Critical);
|
|
|
|
msgBox.setText(tr("Subsurface cannot find a usable printer on this system!"));
|
|
|
|
msgBox.setWindowIcon(QIcon(":subsurface-icon"));
|
|
|
|
msgBox.exec();
|
|
|
|
return;
|
|
|
|
}
|
2013-07-10 12:34:57 +00:00
|
|
|
switch (printOptions->type) {
|
2014-11-13 22:57:42 +00:00
|
|
|
case print_options::PRETTY:
|
2013-09-27 16:06:45 +00:00
|
|
|
printProfileDives(3, 2);
|
2013-07-10 12:34:57 +00:00
|
|
|
break;
|
2014-11-13 22:57:42 +00:00
|
|
|
case print_options::ONEPERPAGE:
|
2014-07-24 17:56:39 +00:00
|
|
|
printProfileDives(1, 1);
|
|
|
|
break;
|
2014-11-13 22:57:42 +00:00
|
|
|
case print_options::TWOPERPAGE:
|
2013-09-27 16:06:45 +00:00
|
|
|
printProfileDives(2, 1);
|
2013-07-10 12:34:57 +00:00
|
|
|
break;
|
2014-11-13 22:57:42 +00:00
|
|
|
case print_options::TABLE:
|
2013-07-10 12:34:57 +00:00
|
|
|
printTable();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 13:04:00 +00:00
|
|
|
void PrintLayout::setup()
|
|
|
|
{
|
|
|
|
QDesktopWidget *desktop = QApplication::desktop();
|
|
|
|
screenDpiX = desktop->physicalDpiX();
|
2013-07-16 15:48:37 +00:00
|
|
|
screenDpiY = desktop->physicalDpiY();
|
2013-07-10 13:04:00 +00:00
|
|
|
|
|
|
|
printerDpi = printer->resolution();
|
|
|
|
pageRect = printer->pageRect();
|
|
|
|
|
2014-07-25 00:50:40 +00:00
|
|
|
// a printer page in pixels
|
|
|
|
pageW = pageRect.width();
|
|
|
|
pageH = pageRect.height();
|
2013-07-10 13:04:00 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 13:13:40 +00:00
|
|
|
// go trought the dive table and find how many dives we are a going to print
|
2014-04-17 02:56:42 +00:00
|
|
|
// TODO: C function: 'count_selected_dives' or something
|
2013-12-04 23:48:38 +00:00
|
|
|
int PrintLayout::estimateTotalDives() const
|
2013-12-04 13:13:40 +00:00
|
|
|
{
|
2013-12-04 23:48:38 +00:00
|
|
|
int total = 0, i = 0;
|
|
|
|
struct dive *dive;
|
2014-05-22 18:40:22 +00:00
|
|
|
for_each_dive (i, dive) {
|
2013-12-04 13:13:40 +00:00
|
|
|
if (!dive->selected && printOptions->print_selected)
|
|
|
|
continue;
|
2013-12-04 23:48:38 +00:00
|
|
|
total++;
|
2013-12-04 13:13:40 +00:00
|
|
|
}
|
2013-12-04 23:48:38 +00:00
|
|
|
return total;
|
2013-12-04 13:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 14:50:40 +00:00
|
|
|
/* the used formula here is:
|
|
|
|
* s = (S - (n - 1) * p) / n
|
|
|
|
* where:
|
|
|
|
* s is the length of a single element (unknown)
|
|
|
|
* S is the total available length
|
|
|
|
* n is the number of elements to fit
|
|
|
|
* p is the padding between elements
|
|
|
|
*/
|
|
|
|
#define ESTIMATE_DIVE_DIM(S, n, p) \
|
2014-03-05 20:19:45 +00:00
|
|
|
((S) - ((n) - 1) * (p)) / (n);
|
2013-10-03 14:50:40 +00:00
|
|
|
|
2013-09-27 16:06:45 +00:00
|
|
|
void PrintLayout::printProfileDives(int divesPerRow, int divesPerColumn)
|
2013-07-10 12:34:57 +00:00
|
|
|
{
|
2013-12-04 23:48:38 +00:00
|
|
|
int i, row = 0, col = 0, printed = 0, total = estimateTotalDives();
|
2014-07-21 22:10:31 +00:00
|
|
|
int animationOriginal = prefs.animation_speed;
|
2014-07-11 21:55:33 +00:00
|
|
|
|
2013-12-04 13:16:20 +00:00
|
|
|
struct dive *dive;
|
|
|
|
if (!total)
|
|
|
|
return;
|
|
|
|
|
2014-07-11 21:55:33 +00:00
|
|
|
// disable animations on the profile:
|
2014-07-21 22:10:31 +00:00
|
|
|
prefs.animation_speed = 0;
|
2014-07-11 21:55:33 +00:00
|
|
|
|
2013-09-27 16:06:45 +00:00
|
|
|
// setup a painter
|
2013-10-03 14:50:40 +00:00
|
|
|
QPainter painter;
|
|
|
|
painter.begin(printer);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
2013-07-12 16:23:47 +00:00
|
|
|
|
2013-09-27 16:06:45 +00:00
|
|
|
// setup the profile widget
|
2014-03-25 21:34:11 +00:00
|
|
|
QPointer<ProfileWidget2> profile = MainWindow::instance()->graphics();
|
|
|
|
const int profileFrameStyle = profile->frameStyle();
|
|
|
|
profile->setFrameStyle(QFrame::NoFrame);
|
|
|
|
profile->setPrintMode(true, !printOptions->color_selected);
|
2014-07-25 00:50:43 +00:00
|
|
|
profile->setFontPrintScale(divesPerRow * divesPerColumn > 3 ? 0.6 : 1.0);
|
2014-03-25 21:34:11 +00:00
|
|
|
QSize originalSize = profile->size();
|
2013-09-27 16:06:45 +00:00
|
|
|
// swap rows/col for landscape
|
|
|
|
if (printer->orientation() == QPrinter::Landscape) {
|
|
|
|
int swap = divesPerColumn;
|
|
|
|
divesPerColumn = divesPerRow;
|
|
|
|
divesPerRow = swap;
|
|
|
|
}
|
2014-03-07 15:42:13 +00:00
|
|
|
|
2013-10-03 14:50:40 +00:00
|
|
|
// padding in pixels between two dives. no padding if only one dive per page.
|
|
|
|
const int padDef = 20;
|
|
|
|
const int padW = (divesPerColumn < 2) ? 0 : padDef;
|
|
|
|
const int padH = (divesPerRow < 2) ? 0 : padDef;
|
|
|
|
// estimate dimensions for a single dive
|
2014-07-25 00:50:40 +00:00
|
|
|
const int scaledW = ESTIMATE_DIVE_DIM(pageW, divesPerColumn, padW);
|
|
|
|
const int scaledH = ESTIMATE_DIVE_DIM(pageH, divesPerRow, padH);
|
2013-10-03 14:50:40 +00:00
|
|
|
// padding in pixels between profile and table
|
2013-10-15 09:54:17 +00:00
|
|
|
const int padPT = 5;
|
2013-10-03 14:50:40 +00:00
|
|
|
// create a model and table
|
|
|
|
ProfilePrintModel model;
|
2014-07-25 00:50:41 +00:00
|
|
|
model.setFontsize(7); // if this is changed we also need to change 'const int sr' in the constructor
|
2014-07-24 17:56:39 +00:00
|
|
|
// if there is only one dive per page row we pass fitNotesToHeight to be almost half the page height
|
|
|
|
QPointer<QTableView> table(createProfileTable(&model, scaledW, (divesPerRow == 1) ? scaledH * 0.45 : 0.0));
|
2013-10-03 14:50:40 +00:00
|
|
|
// profilePrintTableMaxH updates after the table is created
|
|
|
|
const int tableH = profilePrintTableMaxH;
|
|
|
|
// resize the profile widget
|
2014-03-25 21:34:11 +00:00
|
|
|
profile->resize(scaledW, scaledH - tableH - padPT);
|
2013-10-05 10:31:08 +00:00
|
|
|
// offset table or profile on top
|
|
|
|
int yOffsetProfile = 0, yOffsetTable = 0;
|
|
|
|
if (printOptions->notes_up)
|
|
|
|
yOffsetProfile = tableH + padPT;
|
|
|
|
else
|
|
|
|
yOffsetTable = scaledH - tableH;
|
2013-10-03 14:50:40 +00:00
|
|
|
|
|
|
|
// plot the dives at specific rows and columns on the page
|
2014-05-22 18:40:22 +00:00
|
|
|
for_each_dive (i, dive) {
|
2013-07-12 16:23:47 +00:00
|
|
|
if (!dive->selected && printOptions->print_selected)
|
|
|
|
continue;
|
2013-09-27 16:06:45 +00:00
|
|
|
if (col == divesPerColumn) {
|
|
|
|
col = 0;
|
|
|
|
row++;
|
|
|
|
if (row == divesPerRow) {
|
|
|
|
row = 0;
|
|
|
|
printer->newPage();
|
|
|
|
}
|
|
|
|
}
|
2013-10-03 14:50:40 +00:00
|
|
|
// draw a profile
|
2014-07-25 00:50:42 +00:00
|
|
|
QTransform origTransform = painter.transform();
|
2013-10-16 08:39:15 +00:00
|
|
|
painter.translate((scaledW + padW) * col, (scaledH + padH) * row + yOffsetProfile);
|
2014-07-09 18:19:08 +00:00
|
|
|
profile->plotDive(dive, true); // make sure the profile is actually redrawn
|
2014-07-25 00:50:42 +00:00
|
|
|
#ifdef Q_OS_LINUX // on Linux there is a vector line bug (big lines in PDF), which forces us to render to QImage
|
|
|
|
QImage image(scaledW, scaledH - tableH - padPT, QImage::Format_ARGB32);
|
|
|
|
QPainter imgPainter(&image);
|
|
|
|
imgPainter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
imgPainter.setRenderHint(QPainter::SmoothPixmapTransform);
|
2014-07-18 01:00:24 +00:00
|
|
|
profile->render(&imgPainter, QRect(0, 0, scaledW, scaledH - tableH - padPT));
|
|
|
|
imgPainter.end();
|
|
|
|
painter.drawImage(image.rect(),image);
|
2014-07-25 00:50:42 +00:00
|
|
|
#else // for other OS we can try rendering the profile as vector
|
|
|
|
profile->render(&painter, QRect(0, 0, scaledW, scaledH - tableH - padPT));
|
|
|
|
#endif
|
2013-10-16 08:39:15 +00:00
|
|
|
painter.setTransform(origTransform);
|
|
|
|
|
2013-10-03 14:50:40 +00:00
|
|
|
// draw a table
|
2014-07-25 00:50:44 +00:00
|
|
|
QPicture pic;
|
|
|
|
QPainter picPainter;
|
2013-10-16 08:39:15 +00:00
|
|
|
painter.translate((scaledW + padW) * col, (scaledH + padH) * row + yOffsetTable);
|
2013-10-03 14:50:40 +00:00
|
|
|
model.setDive(dive);
|
2014-07-11 23:22:19 +00:00
|
|
|
picPainter.begin(&pic);
|
|
|
|
table->render(&picPainter);
|
|
|
|
picPainter.end();
|
|
|
|
painter.drawPicture(QPoint(0,0), pic);
|
2013-10-16 08:39:15 +00:00
|
|
|
painter.setTransform(origTransform);
|
2013-09-27 16:06:45 +00:00
|
|
|
col++;
|
2013-12-04 13:16:20 +00:00
|
|
|
printed++;
|
|
|
|
emit signalProgress((printed * 100) / total);
|
2013-07-12 16:23:47 +00:00
|
|
|
}
|
2014-03-25 21:34:11 +00:00
|
|
|
// cleanup
|
|
|
|
painter.end();
|
|
|
|
profile->setFrameStyle(profileFrameStyle);
|
|
|
|
profile->setPrintMode(false);
|
|
|
|
profile->resize(originalSize);
|
2014-07-09 18:19:08 +00:00
|
|
|
// we need to force a redraw of the profile so it switches back from print mode
|
|
|
|
profile->plotDive(0, true);
|
2014-07-11 21:55:33 +00:00
|
|
|
// re-enable animations
|
2014-07-21 22:10:31 +00:00
|
|
|
prefs.animation_speed = animationOriginal;
|
2013-07-10 12:34:57 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 14:50:40 +00:00
|
|
|
/* we create a table that has a fixed height, but can stretch to fit certain width */
|
2014-07-24 17:56:39 +00:00
|
|
|
QTableView *PrintLayout::createProfileTable(ProfilePrintModel *model, const int tableW, const qreal fitNotesToHeight)
|
2013-10-03 14:50:40 +00:00
|
|
|
{
|
|
|
|
// setup a new table
|
|
|
|
QTableView *table = new QTableView();
|
|
|
|
QHeaderView *vHeader = table->verticalHeader();
|
|
|
|
QHeaderView *hHeader = table->horizontalHeader();
|
|
|
|
table->setAttribute(Qt::WA_DontShowOnScreen);
|
|
|
|
table->setSelectionMode(QAbstractItemView::NoSelection);
|
|
|
|
table->setFocusPolicy(Qt::NoFocus);
|
|
|
|
table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
hHeader->setVisible(false);
|
|
|
|
vHeader->setVisible(false);
|
2014-02-28 04:09:57 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2014-01-15 08:30:37 +00:00
|
|
|
hHeader->setResizeMode(QHeaderView::Fixed);
|
2013-10-03 14:50:40 +00:00
|
|
|
vHeader->setResizeMode(QHeaderView::Fixed);
|
2014-01-15 08:30:37 +00:00
|
|
|
#else
|
|
|
|
hHeader->setSectionResizeMode(QHeaderView::Fixed);
|
|
|
|
vHeader->setSectionResizeMode(QHeaderView::Fixed);
|
|
|
|
#endif
|
2013-10-03 14:50:40 +00:00
|
|
|
// set the model
|
|
|
|
table->setModel(model);
|
|
|
|
|
|
|
|
/* setup cell span for the table using QTableView::setSpan().
|
|
|
|
* changes made here reflect on ProfilePrintModel::data(). */
|
|
|
|
const int cols = model->columnCount();
|
|
|
|
const int rows = model->rowCount();
|
2013-11-30 12:38:54 +00:00
|
|
|
// info on top
|
2014-07-12 04:40:22 +00:00
|
|
|
table->setSpan(0, 0, 1, 3);
|
|
|
|
table->setSpan(1, 0, 1, 3);
|
|
|
|
table->setSpan(0, 3, 1, 2);
|
|
|
|
table->setSpan(1, 3, 1, 2);
|
2013-11-30 12:38:54 +00:00
|
|
|
// gas used
|
|
|
|
table->setSpan(2, 0, 1, 2);
|
|
|
|
table->setSpan(3, 0, 1, 2);
|
|
|
|
// notes
|
|
|
|
table->setSpan(6, 0, 1, 5);
|
|
|
|
table->setSpan(7, 0, 5, 5);
|
2013-10-03 14:50:40 +00:00
|
|
|
/* resize row heights to the 'profilePrintRowHeights' indexes.
|
2014-07-24 17:56:39 +00:00
|
|
|
* profilePrintTableMaxH will then hold the table height.
|
|
|
|
* what fitNotesToHeight does it to expand the notes section to fit a special height */
|
2013-10-03 14:50:40 +00:00
|
|
|
int i;
|
|
|
|
profilePrintTableMaxH = 0;
|
|
|
|
for (i = 0; i < rows; i++) {
|
2014-07-24 17:56:39 +00:00
|
|
|
int h = (i == rows - 1 && fitNotesToHeight != 0.0) ? fitNotesToHeight : profilePrintRowHeights.at(i);
|
2013-10-03 14:50:40 +00:00
|
|
|
profilePrintTableMaxH += h;
|
|
|
|
vHeader->resizeSection(i, h);
|
|
|
|
}
|
2014-07-24 17:56:39 +00:00
|
|
|
|
2013-10-03 14:50:40 +00:00
|
|
|
// resize columns. columns widths are percentages from the table width.
|
|
|
|
int accW = 0;
|
|
|
|
for (i = 0; i < cols; i++) {
|
|
|
|
int pw = qCeil((qreal)(profilePrintColumnWidths.at(i) * tableW) / 100.0);
|
|
|
|
accW += pw;
|
|
|
|
if (i == cols - 1 && accW > tableW) /* adjust last column */
|
|
|
|
pw -= accW - tableW;
|
|
|
|
hHeader->resizeSection(i, pw);
|
|
|
|
}
|
|
|
|
// resize
|
|
|
|
table->resize(tableW, profilePrintTableMaxH);
|
|
|
|
// hide the grid and set a stylesheet
|
2014-07-17 23:34:11 +00:00
|
|
|
table->setItemDelegate(new ProfilePrintDelegate(table));
|
2014-07-18 00:06:12 +00:00
|
|
|
table->setItemDelegateForRow(7, new HTMLDelegate(table));
|
|
|
|
|
2013-10-03 14:50:40 +00:00
|
|
|
table->setShowGrid(false);
|
|
|
|
table->setStyleSheet(
|
|
|
|
"QTableView { border: none }"
|
2014-02-28 04:09:57 +00:00
|
|
|
"QTableView::item { border: 0px; padding-left: 2px; padding-right: 2px; }");
|
2013-10-03 14:50:40 +00:00
|
|
|
// return
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2013-07-25 09:52:20 +00:00
|
|
|
void PrintLayout::printTable()
|
2013-07-10 12:34:57 +00:00
|
|
|
{
|
2013-12-04 13:18:56 +00:00
|
|
|
struct dive *dive;
|
2013-12-06 16:24:28 +00:00
|
|
|
int done = 0; // percents done
|
2013-12-04 23:48:38 +00:00
|
|
|
int i, row = 0, progress, total = estimateTotalDives();
|
2013-12-04 13:18:56 +00:00
|
|
|
if (!total)
|
|
|
|
return;
|
|
|
|
|
2013-07-25 09:52:20 +00:00
|
|
|
// create and setup a table
|
|
|
|
QTableView table;
|
|
|
|
table.setAttribute(Qt::WA_DontShowOnScreen);
|
|
|
|
table.setSelectionMode(QAbstractItemView::NoSelection);
|
|
|
|
table.setFocusPolicy(Qt::NoFocus);
|
|
|
|
table.horizontalHeader()->setVisible(false);
|
|
|
|
table.verticalHeader()->setVisible(false);
|
2014-02-28 04:09:57 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2014-01-15 08:30:37 +00:00
|
|
|
table.horizontalHeader()->setResizeMode(QHeaderView::Fixed);
|
2013-07-25 09:52:20 +00:00
|
|
|
table.verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
|
2014-01-15 08:30:37 +00:00
|
|
|
#else
|
|
|
|
table.horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
|
|
|
|
table.verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
#endif
|
2013-07-25 09:52:20 +00:00
|
|
|
table.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
table.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
// fit table to one page initially
|
2014-07-25 00:50:40 +00:00
|
|
|
table.resize(pageW, pageH);
|
2013-07-25 09:52:20 +00:00
|
|
|
|
2013-10-03 14:50:41 +00:00
|
|
|
// don't show border
|
|
|
|
table.setStyleSheet(
|
2014-02-28 04:09:57 +00:00
|
|
|
"QTableView { border: none }");
|
2013-10-03 14:50:41 +00:00
|
|
|
|
2013-07-25 09:52:20 +00:00
|
|
|
// create and fill a table model
|
|
|
|
TablePrintModel model;
|
|
|
|
addTablePrintHeadingRow(&model, row); // add one heading row
|
|
|
|
row++;
|
2013-12-04 13:18:56 +00:00
|
|
|
progress = 0;
|
2014-05-22 18:40:22 +00:00
|
|
|
for_each_dive (i, dive) {
|
2013-07-10 20:03:01 +00:00
|
|
|
if (!dive->selected && printOptions->print_selected)
|
2013-07-10 19:55:40 +00:00
|
|
|
continue;
|
2013-07-25 09:52:20 +00:00
|
|
|
addTablePrintDataRow(&model, row, dive);
|
|
|
|
row++;
|
2013-12-04 13:18:56 +00:00
|
|
|
progress++;
|
2013-12-06 16:24:28 +00:00
|
|
|
emit signalProgress((progress * 10) / total);
|
2013-07-25 09:52:20 +00:00
|
|
|
}
|
2013-12-06 16:24:28 +00:00
|
|
|
done = 10;
|
2013-07-25 09:52:20 +00:00
|
|
|
table.setModel(&model); // set model to table
|
|
|
|
// resize columns to percentages from page width
|
2013-10-03 14:50:41 +00:00
|
|
|
int accW = 0;
|
|
|
|
int cols = model.columns;
|
|
|
|
int tableW = table.width();
|
2014-02-12 21:24:40 +00:00
|
|
|
for (i = 0; i < model.columns; i++) {
|
2013-10-03 14:50:41 +00:00
|
|
|
int pw = qCeil((qreal)(tablePrintColumnWidths.at(i) * table.width()) / 100.0);
|
|
|
|
accW += pw;
|
|
|
|
if (i == cols - 1 && accW > tableW) /* adjust last column */
|
|
|
|
pw -= accW - tableW;
|
2013-07-25 09:52:20 +00:00
|
|
|
table.horizontalHeader()->resizeSection(i, pw);
|
|
|
|
}
|
|
|
|
// reset the model at this point
|
|
|
|
model.callReset();
|
|
|
|
|
|
|
|
// a list of vertical offsets where pages begin and some helpers
|
|
|
|
QList<unsigned int> pageIndexes;
|
|
|
|
pageIndexes.append(0);
|
|
|
|
|
2013-12-06 16:24:28 +00:00
|
|
|
/* the algorithm bellow processes the table rows in multiple passes,
|
|
|
|
* compensating for loss of space due to moving rows on a new page instead
|
|
|
|
* of truncating them.
|
|
|
|
* there is a 'passes' array defining how much percents of the total
|
|
|
|
* progress each will take. given, the first and last stage of this function
|
|
|
|
* use 10% each, then the sum of passes[] here should be 80%.
|
|
|
|
* two should be enough! */
|
|
|
|
const int passes[] = { 70, 10 };
|
2014-08-05 17:45:53 +00:00
|
|
|
int tableHeight = 0, lastAccIndex = 0, rowH, accH, headings, headingRowHeightD2, headingRowHeight;
|
2014-08-05 17:47:25 +00:00
|
|
|
bool newHeading = false;
|
2013-12-06 16:24:28 +00:00
|
|
|
|
2013-12-07 22:54:16 +00:00
|
|
|
for (unsigned int pass = 0; pass < sizeof(passes) / sizeof(passes[0]); pass++) {
|
2013-12-06 16:24:28 +00:00
|
|
|
progress = headings = accH = 0;
|
|
|
|
total = model.rows - lastAccIndex;
|
2014-02-12 21:24:40 +00:00
|
|
|
for (i = lastAccIndex; i < model.rows; i++) {
|
2013-12-06 16:24:28 +00:00
|
|
|
rowH = table.rowHeight(i);
|
2014-08-05 17:45:53 +00:00
|
|
|
if (i == 0) { // first row is always a heading. it's height is constant.
|
|
|
|
headingRowHeight = rowH;
|
|
|
|
headingRowHeightD2 = rowH / 2;
|
|
|
|
}
|
|
|
|
if (rowH > pageH - headingRowHeight) // skip huge rows. we don't support row spanning on multiple pages.
|
|
|
|
continue;
|
2013-12-06 16:24:28 +00:00
|
|
|
accH += rowH;
|
2014-08-05 17:47:25 +00:00
|
|
|
if (newHeading) {
|
2013-12-06 16:24:28 +00:00
|
|
|
headings += rowH;
|
2014-08-05 17:47:25 +00:00
|
|
|
newHeading = false;
|
2013-12-06 16:24:28 +00:00
|
|
|
}
|
2014-07-25 00:50:40 +00:00
|
|
|
if (accH > pageH) {
|
2013-12-06 16:24:28 +00:00
|
|
|
lastAccIndex = i;
|
|
|
|
pageIndexes.append(pageIndexes.last() + (accH - rowH));
|
|
|
|
addTablePrintHeadingRow(&model, i);
|
2014-08-05 17:47:25 +00:00
|
|
|
newHeading = true;
|
2013-12-06 16:24:28 +00:00
|
|
|
accH = 0;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
tableHeight += table.rowHeight(i);
|
|
|
|
progress++;
|
|
|
|
emit signalProgress(done + (progress * passes[pass]) / total);
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
2013-12-06 16:24:28 +00:00
|
|
|
done += passes[pass];
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
2013-12-06 16:24:28 +00:00
|
|
|
done = 90;
|
2013-12-05 12:08:33 +00:00
|
|
|
pageIndexes.append(pageIndexes.last() + accH + headings);
|
2014-07-25 00:50:40 +00:00
|
|
|
table.resize(pageW, tableHeight);
|
2013-07-10 16:32:15 +00:00
|
|
|
|
2014-07-25 12:59:12 +00:00
|
|
|
/* attach a painter and render pages by using pageIndexes
|
|
|
|
* there is a weird QPicture dependency here; we need to offset a page
|
|
|
|
* by headingRowHeightD2, which is half the heading height. the same doesn't
|
|
|
|
* make sense if we are rendering the table widget directly to the printer-painter. */
|
2013-07-25 09:52:20 +00:00
|
|
|
QPainter painter(printer);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
2013-12-04 13:18:56 +00:00
|
|
|
total = pageIndexes.size() - 1;
|
|
|
|
progress = 0;
|
2014-02-12 21:24:40 +00:00
|
|
|
for (i = 0; i < total; i++) {
|
2013-07-25 09:52:20 +00:00
|
|
|
if (i > 0)
|
|
|
|
printer->newPage();
|
2014-08-01 22:08:19 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
(void)headingRowHeightD2;
|
|
|
|
QRegion region(0, pageIndexes.at(i) - 1,
|
2014-08-05 17:01:38 +00:00
|
|
|
table.width(),
|
|
|
|
pageIndexes.at(i + 1) - pageIndexes.at(i) + 1);
|
2014-08-01 22:08:19 +00:00
|
|
|
table.render(&painter, QPoint(0, 0), region);
|
|
|
|
#else
|
2014-07-25 12:59:12 +00:00
|
|
|
QRegion region(0, pageIndexes.at(i) + headingRowHeightD2 - 1,
|
2014-08-05 17:01:38 +00:00
|
|
|
table.width(),
|
|
|
|
pageIndexes.at(i + 1) - (pageIndexes.at(i) + headingRowHeightD2) + 1);
|
2014-07-25 00:50:45 +00:00
|
|
|
// vectorize the table first by using QPicture
|
|
|
|
QPicture pic;
|
|
|
|
QPainter picPainter;
|
|
|
|
picPainter.begin(&pic);
|
|
|
|
table.render(&picPainter, QPoint(0, 0), region);
|
|
|
|
picPainter.end();
|
2014-07-25 12:59:12 +00:00
|
|
|
painter.drawPicture(QPoint(0, headingRowHeightD2), pic);
|
2014-08-01 22:08:19 +00:00
|
|
|
#endif
|
2013-12-04 13:18:56 +00:00
|
|
|
progress++;
|
2013-12-06 16:24:28 +00:00
|
|
|
emit signalProgress(done + (progress * 10) / total);
|
2013-07-25 09:52:20 +00:00
|
|
|
}
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 09:52:20 +00:00
|
|
|
void PrintLayout::addTablePrintDataRow(TablePrintModel *model, int row, struct dive *dive) const
|
2013-07-10 16:32:15 +00:00
|
|
|
{
|
2013-07-11 09:45:41 +00:00
|
|
|
struct DiveItem di;
|
2014-01-07 03:57:20 +00:00
|
|
|
di.diveId = dive->id;
|
2013-07-25 09:52:20 +00:00
|
|
|
model->insertRow();
|
|
|
|
model->setData(model->index(row, 0), QString::number(dive->number), Qt::DisplayRole);
|
|
|
|
model->setData(model->index(row, 1), di.displayDate(), Qt::DisplayRole);
|
2014-07-12 04:59:21 +00:00
|
|
|
model->setData(model->index(row, 2), di.displayDepthWithUnit(), Qt::DisplayRole);
|
2013-07-25 09:52:20 +00:00
|
|
|
model->setData(model->index(row, 3), di.displayDuration(), Qt::DisplayRole);
|
|
|
|
model->setData(model->index(row, 4), dive->divemaster, Qt::DisplayRole);
|
|
|
|
model->setData(model->index(row, 5), dive->buddy, Qt::DisplayRole);
|
|
|
|
model->setData(model->index(row, 6), dive->location, Qt::DisplayRole);
|
2013-07-10 21:48:38 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 09:52:20 +00:00
|
|
|
void PrintLayout::addTablePrintHeadingRow(TablePrintModel *model, int row) const
|
2013-07-10 21:48:38 +00:00
|
|
|
{
|
2013-07-25 09:52:20 +00:00
|
|
|
model->insertRow(row);
|
|
|
|
for (int i = 0; i < model->columns; i++) {
|
|
|
|
model->setData(model->index(row, i), tablePrintColumnNames.at(i), Qt::DisplayRole);
|
|
|
|
model->setData(model->index(row, i), tablePrintHeadingBackground, Qt::BackgroundRole);
|
|
|
|
}
|
2013-07-10 12:34:57 +00:00
|
|
|
}
|