aboutsummaryrefslogtreecommitdiffstats
path: root/qt-ui/printlayout.cpp
blob: f33b372e12e9566b66ed75850718385de463cea6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <QDebug>
#include <QPainter>
#include <QDesktopWidget>
#include <QApplication>
#include <QTextDocument>
#include "mainwindow.h"
#include "printlayout.h"
#include "../dive.h"
#include "../display.h"
#include "models.h"

/*
struct options {
	enum { PRETTY, TABLE, TWOPERPAGE } type;
	int print_selected;
	int color_selected;
	bool notes_up;
	int profile_height, notes_height, tanks_height;
};
*/

#define TABLE_PRINT_COL 7

PrintLayout::PrintLayout(PrintDialog *dialogPtr, QPrinter *printerPtr, struct options *optionsPtr)
{
	dialog = dialogPtr;
	printer = printerPtr;
	printOptions = optionsPtr;
	// painter = new QPainter(printer);

	// 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");
}

void PrintLayout::print()
{
	// we call setup each time to check if the printer properties have changed
	setup();

	// temp / debug
	printTable();
	return;
	// ------------
	switch (printOptions->type) {
	case options::PRETTY:
		printSixDives();
		break;
	case options::TWOPERPAGE:
		printTwoDives();
		break;
	case options::TABLE:
		printTable();
		break;
	}
}

void PrintLayout::setup()
{
	QDesktopWidget *desktop = QApplication::desktop();
	screenDpiX = desktop->physicalDpiX();
	screenDpiY = desktop->physicalDpiX();

	printerDpi = printer->resolution();
	pageRect = printer->pageRect();

	scaleX = (qreal)printerDpi/(qreal)screenDpiX;
	scaleY = (qreal)printerDpi/(qreal)screenDpiY;
}

void PrintLayout::printSixDives()
{
	// nop
}

void PrintLayout::printTwoDives()
{
	// nop
}

void PrintLayout::printTable()
{
	QTextDocument doc;
	QSizeF pageSize;
	pageSize.setWidth(pageRect.width());
	pageSize.setHeight(pageRect.height());
	doc.setPageSize(pageSize);

	QString styleSheet(
		"<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;" \
		"}" \
		"</style>"
	);
	// setDefaultStyleSheet() doesn't work here?
	QString htmlText = styleSheet + "<table cellspacing='0' width='100%'>";
	QString htmlTextPrev;
	int pageCountNew = 1, pageCount;
	bool insertHeading = true;

	int i;
	struct dive *dive;
	for_each_dive(i, dive) {
		if (!dive->selected && printOptions->print_selected)
			continue;
		if (insertHeading) {
			htmlText += insertTableHeadingRow();
			insertHeading = false;
		}
		htmlTextPrev = htmlText;
		htmlText += insertTableDataRow(dive);
		doc.setHtml(htmlText);
		pageCount = pageCountNew;
		pageCountNew = doc.pageCount();
		/* if the page count increases after adding this row we 'revert'
		 * and add a heading instead. */
		if (pageCountNew > pageCount) {
			htmlText = htmlTextPrev;
			insertHeading = true;
			i--;
		}
	}
	htmlText += "</table>";
	doc.setHtml(htmlText);
	doc.print(printer);
}

QString PrintLayout::insertTableHeadingRow()
{
	int i;
	QString ret("<tr>");
	for (i = 0; i < TABLE_PRINT_COL; i++)
		ret += insertTableHeadingCol(i);
	ret += "</tr>";
	return ret;
}

QString PrintLayout::insertTableHeadingCol(int col)
{
	QString ret("<th align='left' width='");
	ret += tableColumnWidths.at(col);
	ret += "%'>";
	ret += tableColumnNames.at(col);
	ret += "</th>";
	return ret;
}

QString PrintLayout::insertTableDataRow(struct dive *dive)
{
	// use the DiveItem class
	struct DiveItem di;
	di.dive = dive;

	// fill row
	QString ret("<tr>");
	ret += insertTableDataCol(QString::number(dive->number));
	ret += insertTableDataCol(di.displayDate());
	ret += insertTableDataCol(di.displayDepth());
	ret += insertTableDataCol(di.displayDuration());
	ret += insertTableDataCol(dive->divemaster);
	ret += insertTableDataCol(dive->buddy);
	ret += insertTableDataCol(dive->location);
	ret += "</tr>";
	return ret;
}

QString PrintLayout::insertTableDataCol(QString data)
{
	return "<td>" + data + "</td>";
}