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>
|
2013-07-10 12:34:57 +00:00
|
|
|
#include "mainwindow.h"
|
2013-07-12 16:23:47 +00:00
|
|
|
#include "profilegraphics.h"
|
2013-07-10 12:34:57 +00:00
|
|
|
#include "printlayout.h"
|
2013-07-10 19:55:40 +00:00
|
|
|
#include "../dive.h"
|
2013-07-10 21:45:29 +00:00
|
|
|
#include "../display.h"
|
2013-07-11 09:45:41 +00:00
|
|
|
#include "models.h"
|
2013-07-10 12:34:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
struct options {
|
|
|
|
enum { PRETTY, TABLE, TWOPERPAGE } type;
|
|
|
|
int print_selected;
|
|
|
|
int color_selected;
|
|
|
|
bool notes_up;
|
|
|
|
int profile_height, notes_height, tanks_height;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
PrintLayout::PrintLayout(PrintDialog *dialogPtr, QPrinter *printerPtr, struct options *optionsPtr)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
tablePrintColumnWidths.append(10);
|
|
|
|
tablePrintColumnWidths.append(10);
|
|
|
|
tablePrintColumnWidths.append(10);
|
|
|
|
tablePrintColumnWidths.append(15);
|
|
|
|
tablePrintColumnWidths.append(15);
|
|
|
|
tablePrintColumnWidths.append(33);
|
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();
|
2013-07-10 12:34:57 +00:00
|
|
|
switch (printOptions->type) {
|
|
|
|
case options::PRETTY:
|
|
|
|
printSixDives();
|
|
|
|
break;
|
|
|
|
case options::TWOPERPAGE:
|
|
|
|
printTwoDives();
|
|
|
|
break;
|
|
|
|
case options::TABLE:
|
|
|
|
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();
|
|
|
|
|
|
|
|
scaleX = (qreal)printerDpi/(qreal)screenDpiX;
|
|
|
|
scaleY = (qreal)printerDpi/(qreal)screenDpiY;
|
2013-07-25 09:52:20 +00:00
|
|
|
|
|
|
|
// a printer page scalled to screen DPI
|
|
|
|
scaledPageW = pageRect.width() / scaleX;
|
|
|
|
scaledPageH = pageRect.height() / scaleY;
|
2013-07-10 13:04:00 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 16:23:47 +00:00
|
|
|
// experimental
|
2013-07-11 09:55:48 +00:00
|
|
|
void PrintLayout::printSixDives() const
|
2013-07-10 12:34:57 +00:00
|
|
|
{
|
2013-07-12 16:23:47 +00:00
|
|
|
ProfileGraphicsView *profile = mainWindow()->graphics();
|
|
|
|
QPainter painter;
|
|
|
|
painter.begin(printer);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
// painter.setRenderHint(QPainter::HighQualityAntialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter.scale(scaleX, scaleY);
|
|
|
|
|
|
|
|
profile->clear();
|
2013-07-13 15:09:22 +00:00
|
|
|
profile->setPrintMode(true, !printOptions->color_selected);
|
2013-07-12 16:23:47 +00:00
|
|
|
QSize originalSize = profile->size();
|
2013-07-25 09:52:20 +00:00
|
|
|
profile->resize(scaledPageW, scaledPageH);
|
2013-07-12 16:23:47 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
struct dive *dive;
|
|
|
|
bool firstPage = true;
|
|
|
|
for_each_dive(i, dive) {
|
|
|
|
if (!dive->selected && printOptions->print_selected)
|
|
|
|
continue;
|
|
|
|
// don't create a new page if still on first page
|
|
|
|
if (!firstPage)
|
|
|
|
printer->newPage();
|
|
|
|
else
|
|
|
|
firstPage = false;
|
|
|
|
profile->plot(dive, true);
|
|
|
|
QPixmap pm = QPixmap::grabWidget(profile);
|
|
|
|
QTransform transform;
|
|
|
|
transform.rotate(270);
|
|
|
|
pm = QPixmap(pm.transformed(transform));
|
2013-07-13 13:12:32 +00:00
|
|
|
painter.drawPixmap(0, 0, pm);
|
2013-07-12 16:23:47 +00:00
|
|
|
}
|
|
|
|
painter.end();
|
|
|
|
profile->setPrintMode(false);
|
|
|
|
profile->resize(originalSize);
|
|
|
|
profile->clear();
|
|
|
|
profile->plot(current_dive, true);
|
2013-07-10 12:34:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 09:55:48 +00:00
|
|
|
void PrintLayout::printTwoDives() const
|
2013-07-10 12:34:57 +00:00
|
|
|
{
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
2013-07-25 09:52:20 +00:00
|
|
|
void PrintLayout::printTable()
|
2013-07-10 12:34:57 +00:00
|
|
|
{
|
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.horizontalHeader()->setResizeMode(QHeaderView::Fixed);
|
|
|
|
table.verticalHeader()->setVisible(false);
|
|
|
|
table.verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
table.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
table.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
// fit table to one page initially
|
|
|
|
table.resize(scaledPageW, scaledPageH);
|
|
|
|
|
|
|
|
// create and fill a table model
|
|
|
|
TablePrintModel model;
|
2013-07-10 19:55:40 +00:00
|
|
|
struct dive *dive;
|
2013-07-25 09:52:20 +00:00
|
|
|
int i, row = 0;
|
|
|
|
addTablePrintHeadingRow(&model, row); // add one heading row
|
|
|
|
row++;
|
2013-07-10 19:55:40 +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++;
|
|
|
|
}
|
|
|
|
table.setModel(&model); // set model to table
|
|
|
|
// resize columns to percentages from page width
|
|
|
|
for (int i = 0; i < model.columns; i++) {
|
|
|
|
int pw = qCeil((qreal)(tablePrintColumnWidths.at(i) * table.width()) / 100);
|
|
|
|
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);
|
|
|
|
int tableHeight = 0, rowH = 0, accH = 0;
|
|
|
|
|
|
|
|
// process all rows
|
|
|
|
for (int i = 0; i < model.rows; i++) {
|
|
|
|
rowH = table.rowHeight(i);
|
|
|
|
accH += rowH;
|
|
|
|
if (accH > scaledPageH) { // push a new page index and add a heading
|
|
|
|
pageIndexes.append(pageIndexes.last() + (accH - rowH));
|
|
|
|
addTablePrintHeadingRow(&model, i);
|
|
|
|
accH = 0;
|
2013-07-10 19:55:40 +00:00
|
|
|
i--;
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
2013-07-25 09:52:20 +00:00
|
|
|
tableHeight += rowH;
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
2013-07-25 09:52:20 +00:00
|
|
|
pageIndexes.append(pageIndexes.last() + accH);
|
|
|
|
// resize the whole widget so that it can be rendered
|
|
|
|
table.resize(scaledPageW, tableHeight);
|
2013-07-10 16:32:15 +00:00
|
|
|
|
2013-07-25 09:52:20 +00:00
|
|
|
// attach a painter and render pages by using pageIndexes
|
|
|
|
QPainter painter(printer);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter.scale(scaleX, scaleY);
|
|
|
|
for (int i = 0; i < pageIndexes.size() - 1; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
printer->newPage();
|
|
|
|
QRegion region(0, pageIndexes.at(i) - 1,
|
|
|
|
table.width(),
|
|
|
|
pageIndexes.at(i + 1) - pageIndexes.at(i) + 2);
|
|
|
|
table.render(&painter, QPoint(0, 0), region);
|
|
|
|
}
|
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;
|
|
|
|
di.dive = dive;
|
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);
|
|
|
|
model->setData(model->index(row, 2), di.displayDepth(), Qt::DisplayRole);
|
|
|
|
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
|
|
|
}
|
2013-07-13 13:12:32 +00:00
|
|
|
|
|
|
|
// experimental
|
|
|
|
QPixmap PrintLayout::convertPixmapToGrayscale(QPixmap pixmap) const
|
|
|
|
{
|
|
|
|
QImage image = pixmap.toImage();
|
|
|
|
int gray, width = pixmap.width(), height = pixmap.height();
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
gray = qGray(image.pixel(i, j));
|
|
|
|
image.setPixel(i, j, qRgb(gray, gray, gray));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pixmap.fromImage(image);
|
|
|
|
}
|