Содержимое файла | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
5 | diffor | 1 | /* |
2 | * Table View Print & Preview dialog |
||
3 | * Copyright (C) 2004-2008 by Gordos Kund / QnD Co Bt. |
||
4 | * |
||
5 | * This library is free software; you can redistribute it and/or |
||
6 | * modify it under the terms of the GNU Lesser General Public |
||
7 | * License as published by the Free Software Foundation; either |
||
8 | * version 2.1 of the License, or (at your option) any later version. |
||
9 | * |
||
10 | * This library is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
13 | * Lesser General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU Lesser General Public |
||
16 | * License along with this library; if not, write to the Free Software |
||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
18 | * Please contact gordos.kund@gmail.com with any questions on this license. |
||
19 | */ |
||
20 | |||
21 | #include "tdpreviewdialog.h" |
||
22 | #include <QtCore/QDebug> |
||
23 | #include <QtGui/QTextDocument> |
||
24 | #include <QtGui/QPageSetupDialog> |
||
25 | #include <QtGui/QGraphicsTextItem> |
||
26 | #include <QtGui/QHeaderView> |
||
27 | #include <QtGui/QPainter> |
||
28 | #include <QtCore/QTime> |
||
29 | #include <QtGui/QPrintDialog> |
||
30 | #include <QtCore/QRectF> |
||
31 | |||
32 | |||
33 | TDPreviewDialog::TDPreviewDialog(QTableView *p_tableView, QPrinter * p_printer, QWidget *parent) : QDialog(parent) |
||
34 | { |
||
35 | //variables |
||
36 | tableView=p_tableView; |
||
37 | leftMargin=80; |
||
38 | rightMargin=80; |
||
39 | topMargin=40; |
||
40 | bottomMargin=40; |
||
41 | spacing=4; |
||
42 | headerSize=60; |
||
43 | footerSize=60; |
||
44 | sceneZoomFactor=100; |
||
45 | columnZoomFactor=0.65; |
||
46 | gridMode=NoGrid; |
||
47 | |||
48 | model= tableView->model(); |
||
49 | |||
50 | //setup printer |
||
51 | printer=p_printer; |
||
52 | printer->setFullPage(true); |
||
53 | switch (QLocale::system().country()) { |
||
54 | case QLocale::AnyCountry: |
||
55 | case QLocale::Canada: |
||
56 | case QLocale::UnitedStates: |
||
57 | case QLocale::UnitedStatesMinorOutlyingIslands: |
||
58 | printer->setPageSize(QPrinter::Letter); |
||
59 | break; |
||
60 | default: |
||
61 | printer->setPageSize(QPrinter::A4); |
||
62 | break; |
||
63 | } |
||
64 | |||
65 | |||
66 | setHeaderText("Document"); |
||
67 | footerText=trUtf8("Page: "); |
||
68 | |||
69 | |||
70 | //get column widths |
||
71 | for (int i=0; i<model->columnCount(); i++) { |
||
72 | int colWidth=tableView->columnWidth(i); |
||
73 | colSizes.append(QTextLength(QTextLength::FixedLength,colWidth)); |
||
74 | } |
||
75 | |||
76 | //title font |
||
77 | titleFont=tableView->font(); |
||
78 | titleFont.setPointSize(24); |
||
79 | titleFont.setBold(false); |
||
80 | titleFont.setUnderline(false); |
||
81 | titleFmt =new QFontMetrics(titleFont); |
||
82 | |||
83 | |||
84 | //header font |
||
85 | headerFont=tableView->font(); |
||
86 | headerFont.setBold(true); |
||
87 | headerFont.setUnderline(true); |
||
88 | headerFont.setPointSize(9); |
||
89 | headerFmt =new QFontMetrics(headerFont); |
||
90 | |||
91 | //normal font |
||
92 | font=tableView->font(); |
||
93 | headerFont.setPointSize(9); |
||
94 | fmt =new QFontMetrics(font); |
||
95 | |||
96 | |||
97 | QString date=QDate::currentDate().toString(QLocale().dateFormat()); |
||
98 | QString time=QTime::currentTime().toString(QLocale().timeFormat(QLocale::ShortFormat)); |
||
99 | headerStdText = date+" "+time; |
||
100 | |||
101 | setupPage(); |
||
102 | columnMultiplier=pageScene.width()/tableView->width()*columnZoomFactor; |
||
103 | } |
||
104 | |||
105 | //================================================================= |
||
106 | |||
107 | int TDPreviewDialog::exec() |
||
108 | { |
||
109 | //GUI |
||
110 | ui.setupUi(this); |
||
111 | connect(ui.horizontalSlider, SIGNAL(valueChanged(int)), ui.pageSpinBox, SLOT(setValue(int))); |
||
112 | connect(ui.prevToolButton, SIGNAL(clicked()), ui.pageSpinBox, SLOT(stepDown())); |
||
113 | connect(ui.nextToolButton, SIGNAL(clicked()), ui.pageSpinBox, SLOT(stepUp())); |
||
114 | |||
115 | ui.graphicsView->setScene(&pageScene); |
||
116 | ui.graphicsView->ensureVisible(0,0,10,10); |
||
117 | setupSpinBox(); |
||
118 | on_pageSpinBox_valueChanged(1); |
||
119 | return QDialog::exec(); |
||
120 | } |
||
121 | |||
122 | //================================================================= |
||
123 | |||
124 | TDPreviewDialog::~TDPreviewDialog() |
||
125 | {} |
||
126 | |||
127 | //================================================================= |
||
128 | |||
129 | void TDPreviewDialog::on_setupToolButton_clicked() |
||
130 | { |
||
131 | QPageSetupDialog *dialog; |
||
132 | dialog = new QPageSetupDialog(printer, this); |
||
133 | if (dialog->exec() == QDialog::Rejected) { |
||
134 | return; |
||
135 | } |
||
136 | setupPage(); |
||
137 | on_pageSpinBox_valueChanged(1); |
||
138 | setupSpinBox(); |
||
139 | } |
||
140 | |||
141 | //================================================================= |
||
142 | |||
143 | void TDPreviewDialog::on_zoomInToolButton_clicked() |
||
144 | { |
||
145 | ui.graphicsView->scale(1.5,1.5); |
||
146 | } |
||
147 | |||
148 | //================================================================= |
||
149 | |||
150 | void TDPreviewDialog::on_zoomOutToolButton_clicked() |
||
151 | { |
||
152 | ui.graphicsView->scale(1 / 1.5, 1 / 1.5); |
||
153 | } |
||
154 | |||
155 | //================================================================= |
||
156 | |||
157 | void TDPreviewDialog::on_pageSpinBox_valueChanged(int value) |
||
158 | { |
||
159 | paintPage(value); |
||
160 | pageScene.addRect(0,0,pageScene.width(),pageScene.height(),QPen(Qt::black, 2.0)); |
||
161 | } |
||
162 | |||
163 | //================================================================= |
||
164 | |||
165 | void TDPreviewDialog::setupSpinBox() |
||
166 | { |
||
167 | ui.pageSpinBox->setPrefix(QString::number(pages)+" / "); |
||
168 | ui.pageSpinBox->setMaximum(pages); |
||
169 | ui.horizontalSlider->setMaximum(pages); |
||
170 | } |
||
171 | |||
172 | //================================================================= |
||
173 | |||
174 | void TDPreviewDialog::setupPage() |
||
175 | { |
||
176 | rowHeight=font.pointSize()+spacing; |
||
177 | QRectF rect=printer->paperRect(); |
||
178 | QRectF rectNew = QRectF(0,0,rect.width() / printer->logicalDpiX() * sceneZoomFactor, rect.height() / printer->logicalDpiY() * sceneZoomFactor); |
||
179 | pageScene.setSceneRect(rectNew); |
||
180 | |||
181 | //count lines |
||
182 | lines = ((pageScene.height()-topMargin-bottomMargin-headerSize-footerSize)/rowHeight)-3; |
||
183 | |||
184 | //count pages |
||
185 | QAbstractItemModel *model= tableView->model(); |
||
186 | int rowCount=model->rowCount(); |
||
187 | double div = rowCount / lines; |
||
188 | int modulo = rowCount % lines; |
||
189 | if (modulo == 0 ) { |
||
190 | pages = QVariant(div).toInt(); |
||
191 | } else { |
||
192 | div = div+1.0; |
||
193 | pages = QVariant(div).toInt(); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | //================================================================= |
||
198 | |||
199 | void TDPreviewDialog::setHeaderText(const QString &text) |
||
200 | { |
||
201 | headerText=text; |
||
202 | } |
||
203 | |||
204 | //================================================================= |
||
205 | |||
206 | void TDPreviewDialog::print() |
||
207 | { |
||
208 | //printDialog |
||
209 | printer->setFromTo(1,pages); |
||
210 | printer->setOutputFormat(QPrinter::NativeFormat); |
||
211 | printer->setOutputFileName(""); |
||
212 | QPrintDialog dialog(printer, this); |
||
213 | dialog.setWindowTitle("Print Document"); |
||
214 | if (dialog.exec() == QDialog::Rejected) { |
||
215 | return; |
||
216 | } |
||
217 | setupPage(); |
||
218 | |||
219 | |||
220 | // get num copies |
||
221 | int doccopies; |
||
222 | int pagecopies; |
||
223 | if (printer->collateCopies()) { |
||
224 | doccopies = 1; |
||
225 | pagecopies = printer->numCopies(); |
||
226 | } else { |
||
227 | doccopies = printer->numCopies(); |
||
228 | pagecopies = 1; |
||
229 | } |
||
230 | |||
231 | // get page range |
||
232 | int firstpage = printer->fromPage(); |
||
233 | int lastpage = printer->toPage(); |
||
234 | if (firstpage == 0 && lastpage == 0) { // all pages |
||
235 | firstpage = 1; |
||
236 | lastpage =pages; |
||
237 | } |
||
238 | |||
239 | // print order |
||
240 | bool ascending = true; |
||
241 | if (printer->pageOrder() == QPrinter::LastPageFirst) { |
||
242 | int tmp = firstpage; |
||
243 | firstpage = lastpage; |
||
244 | lastpage = tmp; |
||
245 | ascending = false; |
||
246 | } |
||
247 | |||
248 | // loop through and print pages |
||
249 | QPainter painter(printer); |
||
250 | for (int dc=0; dc<doccopies; dc++) { |
||
251 | int pagenum = firstpage; |
||
252 | while (true) { |
||
253 | for (int pc=0; pc<pagecopies; pc++) { |
||
254 | if (printer->printerState() == QPrinter::Aborted || |
||
255 | printer->printerState() == QPrinter::Error) { |
||
256 | break; |
||
257 | } |
||
258 | // print page |
||
259 | paintPage(pagenum); |
||
260 | pageScene.render(&painter); |
||
261 | if (pc < pagecopies-1) printer->newPage(); |
||
262 | } |
||
263 | if (pagenum == lastpage) break; |
||
264 | if (ascending) { |
||
265 | pagenum++; |
||
266 | } else { |
||
267 | pagenum--; |
||
268 | } |
||
269 | printer->newPage(); |
||
270 | } |
||
271 | |||
272 | if (dc < doccopies-1) printer->newPage(); |
||
273 | } |
||
274 | |||
275 | } |
||
276 | |||
277 | //================================================================= |
||
278 | |||
279 | void TDPreviewDialog::exportPdf(const QString &filename) |
||
280 | { |
||
281 | // file save dialog |
||
282 | QString dialogcaption = "Export PDF"; |
||
283 | QString exportname = QFileDialog::getSaveFileName(this, dialogcaption, filename, "*.pdf"); |
||
284 | if (exportname.isEmpty()) return; |
||
285 | if (QFileInfo(exportname).suffix().isEmpty()) |
||
286 | exportname.append(".pdf"); |
||
287 | |||
288 | // setup printer |
||
289 | printer->setOutputFormat(QPrinter::PdfFormat); |
||
290 | printer->setOutputFileName(exportname); |
||
291 | |||
292 | // print pdf |
||
293 | QPainter painter(printer); |
||
294 | for (int pagenum=1; pagenum<=pages; pagenum++) { |
||
295 | paintPage(pagenum); |
||
296 | pageScene.render(&painter); |
||
297 | if (pagenum < pages) { |
||
298 | printer->newPage(); |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | |||
303 | //================================================================= |
||
304 | |||
305 | void TDPreviewDialog::paintPage(int pagenum) |
||
306 | { |
||
307 | //Delete all |
||
308 | QList<QGraphicsItem*> L = pageScene.items(); |
||
309 | while ( ! L.empty() ) { |
||
310 | pageScene.removeItem( L.first() ); |
||
311 | delete L.first(); |
||
312 | L.removeFirst(); |
||
313 | } |
||
314 | |||
315 | //Table header |
||
316 | int csize=0; |
||
317 | for (int i=0; i<model->columnCount(); i++) { |
||
318 | int logicalIndex=tableView->horizontalHeader()->logicalIndex(i); |
||
319 | QString txt = model->headerData(logicalIndex,Qt::Horizontal,Qt::DisplayRole).toString(); |
||
320 | QGraphicsTextItem *item=new QGraphicsTextItem(); |
||
321 | item->setFont(headerFont); |
||
322 | txt=headerFmt->elidedText(txt,Qt::ElideRight,QVariant(colSizes[logicalIndex].rawValue()).toInt()-8); |
||
323 | item->setPlainText(txt); |
||
324 | //item->moveBy((csize*columnMultiplier)+leftMargin,topMargin+headerSize+(spacing*2)); |
||
325 | item->moveBy(csize+leftMargin,topMargin+headerSize+(spacing*2)); |
||
326 | csize=csize+QVariant(colSizes[logicalIndex].rawValue()).toInt(); |
||
327 | pageScene.addItem(item); |
||
328 | } |
||
329 | |||
330 | //Table rows |
||
331 | qreal dx,dy; |
||
332 | QPen pen(Qt::gray, 1); |
||
333 | QBrush brush(Qt::gray,Qt::SolidPattern); |
||
334 | int borderAdjust=rowHeight / 5; |
||
335 | |||
336 | for (int i=0; i<lines; i++) { |
||
337 | csize=0; |
||
338 | dy=(i*rowHeight)+topMargin+headerSize+(rowHeight*2); |
||
339 | int modelIdxY=(pagenum-1)*lines+i; |
||
340 | if (modelIdxY>=model->rowCount()) { |
||
341 | break; |
||
342 | } |
||
343 | |||
344 | for (int j=0; j<model->columnCount(); j++) { |
||
345 | int logicalIndex=tableView->horizontalHeader()->logicalIndex(j); |
||
346 | int actColSize=QVariant(colSizes[logicalIndex].rawValue()).toInt(); |
||
347 | QString txt = model->data(model->index(modelIdxY,logicalIndex)).toString(); |
||
348 | QGraphicsTextItem *item=new QGraphicsTextItem(); |
||
349 | item->setFont(font); |
||
350 | txt=fmt->elidedText(txt,Qt::ElideRight,actColSize-8); |
||
351 | item->setPlainText(txt); |
||
352 | //qreal dx=(csize*columnMultiplier)+leftMargin; |
||
353 | dx=csize+leftMargin; |
||
354 | item->moveBy(dx,dy); |
||
355 | |||
356 | //rectangle |
||
357 | if (gridMode==NormalGrid || gridMode==AlternateWithGrid) { |
||
358 | pageScene.addRect(dx,dy+borderAdjust,actColSize,rowHeight); |
||
359 | } |
||
360 | pageScene.addItem(item); |
||
361 | csize=csize+actColSize; |
||
362 | } |
||
363 | |||
364 | if (gridMode==AlternateColor || gridMode==AlternateWithGrid) { |
||
365 | |||
366 | int modulo=i % 2; |
||
367 | if (modulo==0) { |
||
368 | //rectangle grey |
||
369 | QGraphicsRectItem *rItem=pageScene.addRect(leftMargin,dy+borderAdjust,csize,rowHeight,pen,brush); |
||
370 | rItem->setZValue(-1); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | } |
||
375 | |||
376 | // Page header |
||
377 | if (headerSize > 0) { |
||
378 | //line |
||
379 | pageScene.addLine(leftMargin,headerSize+topMargin,pageScene.width()-rightMargin, headerSize+topMargin,QPen(Qt::black, 1.0)); |
||
380 | |||
381 | //tile |
||
382 | QGraphicsTextItem *titleItem=new QGraphicsTextItem(); |
||
383 | titleItem->setFont(titleFont); |
||
384 | titleItem->setPlainText(headerText); |
||
385 | int titleWidth=titleFmt->width(headerText); |
||
386 | double xpos=(pageScene.width()/2)-(titleWidth/2); |
||
387 | double ypos=(headerSize/2)-(titleFmt->height()/2); |
||
388 | titleItem->moveBy(xpos,ypos+topMargin); |
||
389 | pageScene.addItem(titleItem); |
||
390 | |||
391 | //std text |
||
392 | QGraphicsTextItem *item=new QGraphicsTextItem(); |
||
393 | item->setFont(font); |
||
394 | item->setPlainText(headerStdText); |
||
395 | item->moveBy(leftMargin,headerSize-rowHeight-spacing+topMargin); |
||
396 | pageScene.addItem(item); |
||
397 | } |
||
398 | |||
399 | // footer |
||
400 | if (footerSize > 0) { |
||
401 | pageScene.addLine(leftMargin,pageScene.height()-footerSize-bottomMargin,pageScene.width()-rightMargin, pageScene.height()-footerSize-bottomMargin,QPen(Qt::black, 1.0)); |
||
402 | QGraphicsTextItem *item=new QGraphicsTextItem(); |
||
403 | item->setFont(font); |
||
404 | item->setPlainText(footerText+QString::number(pages)+" / "+QString::number(pagenum)); |
||
405 | item->moveBy(leftMargin,pageScene.height()-footerSize-bottomMargin+spacing); |
||
406 | pageScene.addItem(item); |
||
407 | } |
||
408 | //pageScene.update(); |
||
409 | } |
||
410 | |||
411 | //================================================================= |
||
412 | |||
413 | void TDPreviewDialog::setGridMode(Grids _gridMode) |
||
414 | { |
||
415 | gridMode=_gridMode; |
||
416 | } |