Mercurial > hg > syntilista
comparison src/printing.cpp @ 157:325e7590f93e
Move most of the printing related code to printing.cpp
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 12 Sep 2017 15:56:40 +0300 |
parents | |
children | f5c1861b48aa |
comparison
equal
deleted
inserted
replaced
156:afadc1380fb1 | 157:325e7590f93e |
---|---|
1 // | |
2 // Syntilista - debt list/management database program | |
3 // Programmed and designed by Matti Hämäläinen <ccr@tnsp.org> | |
4 // (C) Copyright 2017 Tecnic Software productions (TNSP) | |
5 // | |
6 // Distributed under 3-clause BSD style license, refer to | |
7 // included file "COPYING" for exact terms. | |
8 // | |
9 #include <QPrintDialog> | |
10 #include <QPrintPreviewDialog> | |
11 #include "main.h" | |
12 | |
13 | |
14 void SyntilistaMainWindow::on_button_Print_clicked() | |
15 { | |
16 // Create a printer object and force some basic settings | |
17 QPrinter printer(QPrinter::HighResolution); | |
18 printer.setPageSize(QPageSize(QPageSize::A4)); | |
19 printer.setColorMode(QPrinter::GrayScale); | |
20 printer.setResolution(300); | |
21 | |
22 // We need to get the page count here, and also need it again in | |
23 // printDocument(), but there is no sane way to pass that there, | |
24 // so some code duplication is unfortunately necessary | |
25 SLPageInfo pinfo; | |
26 pinfo.npages = 0; | |
27 pinfo.nlinesPerPage = 0; | |
28 | |
29 QPixmap tmpPixmap(1000, 1300); | |
30 QPainter tmpPainter; | |
31 tmpPainter.begin(&tmpPixmap); | |
32 bool ret = printDocumentPage(pinfo, true, -1, &tmpPainter, &printer); | |
33 tmpPainter.end(); | |
34 | |
35 if (!ret) | |
36 { | |
37 // Some kind of error occured | |
38 return; | |
39 } | |
40 | |
41 | |
42 // Set available pages | |
43 printer.setFromTo(1, pinfo.npages); | |
44 | |
45 // Create print preview dialog and show it | |
46 QPrintPreviewDialog preview(&printer, this); | |
47 preview.setWindowModality(Qt::ApplicationModal); | |
48 preview.setSizeGripEnabled(true); | |
49 | |
50 connect( | |
51 &preview, | |
52 SIGNAL(paintRequested(QPrinter *)), | |
53 this, | |
54 SLOT(printDocument(QPrinter *))); | |
55 | |
56 preview.exec(); | |
57 } | |
58 | |
59 | |
60 void SyntilistaMainWindow::printDocument(QPrinter *printer) | |
61 { | |
62 // Create progress dialog | |
63 QProgressDialog progress( | |
64 tr("Tulostetaan ..."), | |
65 tr("Peruuta"), | |
66 0, | |
67 1, | |
68 this); | |
69 | |
70 progress.setWindowModality(Qt::ApplicationModal); | |
71 | |
72 // Again, get the page info here .. we need the number of lines per page | |
73 SLPageInfo pinfo; | |
74 pinfo.npages = 0; | |
75 pinfo.nlinesPerPage = 0; | |
76 | |
77 QPixmap tmpPixmap(1000, 1300); | |
78 QPainter tmpPainter; | |
79 tmpPainter.begin(&tmpPixmap); | |
80 bool ret = printDocumentPage(pinfo, true, -1, &tmpPainter, printer); | |
81 tmpPainter.end(); | |
82 | |
83 if (!ret) | |
84 return; | |
85 | |
86 // If from and to are 0, we are supposed to print all pages | |
87 if (printer->fromPage() == 0 && printer->toPage() == 0) | |
88 printer->setFromTo(1, pinfo.npages); | |
89 | |
90 // Setup rest of the progress dialog here | |
91 progress.setMinimum(printer->fromPage() - 1); | |
92 progress.setMaximum(printer->toPage()); | |
93 | |
94 // Begin painting to the printer (or preview) | |
95 QPainter painter; | |
96 painter.begin(printer); | |
97 | |
98 bool firstPage = true; | |
99 for (int page = printer->fromPage(); page <= printer->toPage(); page++) | |
100 { | |
101 if (!firstPage) | |
102 printer->newPage(); | |
103 | |
104 qApp->processEvents(); | |
105 if (progress.wasCanceled()) | |
106 break; | |
107 | |
108 printDocumentPage(pinfo, false, page, &painter, printer); | |
109 progress.setValue(page); | |
110 firstPage = false; | |
111 } | |
112 | |
113 painter.end(); | |
114 } | |
115 | |
116 | |
117 bool SyntilistaMainWindow::printDocumentPage(SLPageInfo &pinfo, const bool getPageInfo, const int npage, QPainter *pt, QPrinter *printer) | |
118 { | |
119 // Form the SQL query for list of users | |
120 QString querystr = QStringLiteral( | |
121 "SELECT id,first_name,last_name,extra_info,added,updated, " | |
122 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance " | |
123 "FROM people ORDER BY last_name ASC,first_name ASC"); | |
124 | |
125 // If we are fetching page info, we need to process all entries | |
126 if (!getPageInfo) | |
127 { | |
128 // Otherwise we can limit to given page number | |
129 querystr += QStringLiteral(" LIMIT %1 OFFSET %2"). | |
130 arg(pinfo.nlinesPerPage). | |
131 arg((npage - 1) * pinfo.nlinesPerPage); | |
132 } | |
133 | |
134 QSqlQuery query; | |
135 query.prepare(querystr); | |
136 query.setForwardOnly(true); | |
137 query.exec(); | |
138 | |
139 if (!slCheckAndReportSQLError("printDocumentPage()", query.lastError())) | |
140 { | |
141 slErrorMsg( | |
142 tr("SQL-tietokantavirhe"), | |
143 tr("Tietokantaa selattaessa tapahtui virhe.")); | |
144 | |
145 return false; | |
146 } | |
147 | |
148 pt->save(); | |
149 if (!getPageInfo) | |
150 { | |
151 pt->scale( | |
152 printer->pageRect().width() / 1000.0f, | |
153 printer->pageRect().height() / 1300.0f); | |
154 } | |
155 | |
156 QFont font1("Arial", 5); | |
157 SLDrawContext ctx(pt); | |
158 ctx.setFont(font1); | |
159 | |
160 int nline = 0; | |
161 while (query.next()) | |
162 { | |
163 PersonInfo info; | |
164 slGetPersonInfoRec(query, info); | |
165 | |
166 // Check for end of page | |
167 // KLUDGE for now | |
168 if (getPageInfo && ctx.lfq(10) >= 1300.0f) | |
169 { | |
170 if (nline > pinfo.nlinesPerPage) | |
171 pinfo.nlinesPerPage = nline; | |
172 | |
173 pinfo.npages++; | |
174 nline = 0; | |
175 } | |
176 | |
177 if (nline == 0) | |
178 { | |
179 // If we are at the start of the page, we shall draw a header | |
180 pt->setBrush(QBrush(Qt::black)); | |
181 pt->setPen(QPen(Qt::black, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); | |
182 | |
183 ctx.setPos(0, 0); | |
184 ctx.drawText( 5, 180, tr("Etunimi")); | |
185 ctx.drawText( 200, 230, tr("Sukunimi")); | |
186 ctx.drawText( 450, 190, tr("Lisätty")); | |
187 ctx.drawText( 650, 190, tr("Päivitetty")); | |
188 ctx.drawText( 870, 120, tr("Tase")); | |
189 ctx.lf(); | |
190 | |
191 pt->drawLine(0, ctx.m_pos.y(), 1000, ctx.m_pos.y()); | |
192 | |
193 ctx.move(0, 5); | |
194 } | |
195 | |
196 // Draw a gray bar under every second line | |
197 if (nline % 2 == 0) | |
198 { | |
199 pt->fillRect( | |
200 0, | |
201 ctx.m_pos.y() - 1, | |
202 1000, | |
203 ctx.boundRect().height() + 4, | |
204 QColor(0, 0, 0, 40)); | |
205 } | |
206 | |
207 ctx.drawText( 5, 180, info.firstName); | |
208 ctx.drawText( 200, 230, info.lastName); | |
209 ctx.drawText( 450, 190, slDateTimeToStr(info.added)); | |
210 ctx.drawText( 650, 190, slDateTimeToStr(info.updated)); | |
211 ctx.drawText( 870, 120, slMoneyValueToStr(info.balance), Qt::AlignRight); | |
212 | |
213 ctx.lf(10); | |
214 nline++; | |
215 } | |
216 | |
217 query.finish(); | |
218 | |
219 if (getPageInfo) | |
220 { | |
221 if (nline > pinfo.nlinesPerPage) | |
222 pinfo.nlinesPerPage = nline; | |
223 | |
224 pinfo.npages++; | |
225 } | |
226 else | |
227 { | |
228 ctx.setPos(0, 1240); | |
229 ctx.drawText(0, 1000, | |
230 tr("Sivu %1 / %2 (%3 / %4)"). | |
231 arg(npage - printer->fromPage() + 1). | |
232 arg(printer->toPage() - printer->fromPage() + 1). | |
233 arg(npage). | |
234 arg(printer->toPage()), | |
235 Qt::AlignHCenter); | |
236 } | |
237 | |
238 pt->restore(); | |
239 return true; | |
240 } | |
241 | |
242 |