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-10 16:32:15 +00:00
|
|
|
#include <QTextDocument>
|
2013-07-12 10:23:02 +00:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
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;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2013-07-10 21:48:38 +00:00
|
|
|
#define TABLE_PRINT_COL 7
|
|
|
|
|
2013-07-10 12:34:57 +00:00
|
|
|
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
|
|
|
|
tableColumnNames.append(tr("Dive#"));
|
|
|
|
tableColumnNames.append(tr("Date"));
|
|
|
|
tableColumnNames.append(tr("Depth"));
|
|
|
|
tableColumnNames.append(tr("Duration"));
|
|
|
|
tableColumnNames.append(tr("Master"));
|
|
|
|
tableColumnNames.append(tr("Buddy"));
|
|
|
|
tableColumnNames.append(tr("Location"));
|
|
|
|
tableColumnWidths.append("7");
|
|
|
|
tableColumnWidths.append("10");
|
|
|
|
tableColumnWidths.append("10");
|
|
|
|
tableColumnWidths.append("10");
|
|
|
|
tableColumnWidths.append("15");
|
|
|
|
tableColumnWidths.append("15");
|
|
|
|
tableColumnWidths.append("100");
|
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-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();
|
|
|
|
profile->setPrintMode(true);
|
|
|
|
QSize originalSize = profile->size();
|
|
|
|
profile->resize(pageRect.height()/scaleY, pageRect.width()/scaleX);
|
|
|
|
|
|
|
|
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
|
|
|
if (!printOptions->color_selected)
|
|
|
|
pm = convertPixmapToGrayscale(pm);
|
|
|
|
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-11 09:55:48 +00:00
|
|
|
void PrintLayout::printTable() const
|
2013-07-10 12:34:57 +00:00
|
|
|
{
|
2013-07-10 16:32:15 +00:00
|
|
|
QTextDocument doc;
|
|
|
|
QSizeF pageSize;
|
2013-07-10 20:03:01 +00:00
|
|
|
pageSize.setWidth(pageRect.width());
|
|
|
|
pageSize.setHeight(pageRect.height());
|
2013-07-12 10:23:02 +00:00
|
|
|
doc.documentLayout()->setPaintDevice(printer);
|
2013-07-10 20:03:01 +00:00
|
|
|
doc.setPageSize(pageSize);
|
2013-07-10 16:32:15 +00:00
|
|
|
|
2013-07-10 20:03:01 +00:00
|
|
|
QString styleSheet(
|
2013-07-16 15:49:31 +00:00
|
|
|
"<style type='text/css'>"
|
|
|
|
"table {"
|
|
|
|
" border-width: 1px;"
|
|
|
|
" border-style: solid;"
|
|
|
|
" border-color: #999999;"
|
|
|
|
"}"
|
|
|
|
"th {"
|
|
|
|
" background-color: #eeeeee;"
|
|
|
|
" font-size: small;"
|
|
|
|
" padding: 3px 5px 3px 5px;"
|
|
|
|
"}"
|
|
|
|
"td {"
|
|
|
|
" font-size: small;"
|
|
|
|
" padding: 3px 5px 3px 5px;"
|
|
|
|
"}"
|
2013-07-10 20:03:01 +00:00
|
|
|
"</style>"
|
|
|
|
);
|
2013-07-10 16:32:15 +00:00
|
|
|
// setDefaultStyleSheet() doesn't work here?
|
2013-07-12 11:08:25 +00:00
|
|
|
const QString heading(insertTableHeadingRow());
|
|
|
|
const QString lineBreak("<br>");
|
2013-07-10 16:32:15 +00:00
|
|
|
QString htmlText = styleSheet + "<table cellspacing='0' width='100%'>";
|
|
|
|
QString htmlTextPrev;
|
2013-07-12 11:08:25 +00:00
|
|
|
int pageCountNew = 1, pageCount = 0, lastPageWithHeading = 0;
|
2013-07-10 16:32:15 +00:00
|
|
|
bool insertHeading = true;
|
|
|
|
|
2013-07-10 19:55:40 +00:00
|
|
|
int i;
|
|
|
|
struct dive *dive;
|
|
|
|
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-10 16:32:15 +00:00
|
|
|
if (insertHeading) {
|
2013-07-12 11:08:25 +00:00
|
|
|
htmlTextPrev = htmlText;
|
|
|
|
htmlText += heading;
|
|
|
|
doc.setHtml(htmlText);
|
|
|
|
pageCount = doc.pageCount();
|
|
|
|
// prevent adding two headings on the same page
|
|
|
|
if (pageCount == lastPageWithHeading) {
|
|
|
|
htmlText = htmlTextPrev;
|
|
|
|
// add line breaks until a new page is reached
|
|
|
|
while (pageCount == lastPageWithHeading) {
|
|
|
|
htmlTextPrev = htmlText;
|
|
|
|
htmlText += lineBreak;
|
|
|
|
doc.setHtml(htmlText);
|
|
|
|
pageCount = doc.pageCount();
|
|
|
|
}
|
|
|
|
// revert last line break from the new page and add heading
|
|
|
|
htmlText = htmlTextPrev;
|
|
|
|
htmlText += heading;
|
|
|
|
}
|
2013-07-10 16:32:15 +00:00
|
|
|
insertHeading = false;
|
2013-07-12 11:08:25 +00:00
|
|
|
lastPageWithHeading = pageCount;
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
|
|
|
htmlTextPrev = htmlText;
|
2013-07-10 19:55:40 +00:00
|
|
|
htmlText += insertTableDataRow(dive);
|
2013-07-10 16:32:15 +00:00
|
|
|
doc.setHtml(htmlText);
|
2013-07-10 20:03:01 +00:00
|
|
|
pageCount = pageCountNew;
|
2013-07-10 16:32:15 +00:00
|
|
|
pageCountNew = doc.pageCount();
|
2013-07-12 11:08:25 +00:00
|
|
|
// if the page count increases revert and add heading instead
|
2013-07-10 16:32:15 +00:00
|
|
|
if (pageCountNew > pageCount) {
|
|
|
|
htmlText = htmlTextPrev;
|
|
|
|
insertHeading = true;
|
2013-07-10 19:55:40 +00:00
|
|
|
i--;
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
htmlText += "</table>";
|
|
|
|
doc.setHtml(htmlText);
|
|
|
|
doc.print(printer);
|
|
|
|
}
|
|
|
|
|
2013-07-11 09:55:48 +00:00
|
|
|
QString PrintLayout::insertTableHeadingRow() const
|
2013-07-10 16:32:15 +00:00
|
|
|
{
|
2013-07-10 21:48:38 +00:00
|
|
|
int i;
|
|
|
|
QString ret("<tr>");
|
|
|
|
for (i = 0; i < TABLE_PRINT_COL; i++)
|
|
|
|
ret += insertTableHeadingCol(i);
|
|
|
|
ret += "</tr>";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-11 09:55:48 +00:00
|
|
|
QString PrintLayout::insertTableHeadingCol(int col) const
|
2013-07-10 21:48:38 +00:00
|
|
|
{
|
|
|
|
QString ret("<th align='left' width='");
|
|
|
|
ret += tableColumnWidths.at(col);
|
|
|
|
ret += "%'>";
|
|
|
|
ret += tableColumnNames.at(col);
|
|
|
|
ret += "</th>";
|
|
|
|
return ret;
|
2013-07-10 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 09:55:48 +00:00
|
|
|
QString PrintLayout::insertTableDataRow(struct dive *dive) const
|
2013-07-10 16:32:15 +00:00
|
|
|
{
|
2013-07-11 09:45:41 +00:00
|
|
|
// use the DiveItem class
|
|
|
|
struct DiveItem di;
|
|
|
|
di.dive = dive;
|
|
|
|
|
|
|
|
// fill row
|
2013-07-10 21:48:38 +00:00
|
|
|
QString ret("<tr>");
|
|
|
|
ret += insertTableDataCol(QString::number(dive->number));
|
2013-07-11 09:45:41 +00:00
|
|
|
ret += insertTableDataCol(di.displayDate());
|
|
|
|
ret += insertTableDataCol(di.displayDepth());
|
|
|
|
ret += insertTableDataCol(di.displayDuration());
|
|
|
|
ret += insertTableDataCol(dive->divemaster);
|
|
|
|
ret += insertTableDataCol(dive->buddy);
|
|
|
|
ret += insertTableDataCol(dive->location);
|
2013-07-10 21:48:38 +00:00
|
|
|
ret += "</tr>";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-11 09:55:48 +00:00
|
|
|
QString PrintLayout::insertTableDataCol(QString data) const
|
2013-07-10 21:48:38 +00:00
|
|
|
{
|
|
|
|
return "<td>" + data + "</td>";
|
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);
|
|
|
|
}
|