Debian Codegraph @

src/textprinter.cpp

raw symbols SCIP
1 ///////////////////////////////////////////////////////////////////////////////
2 // textprinter.h
3 // -------------------
4 // Copyright (c) 20072008 David Johnson <[email protected]>
5 // Please see the header file for copyright and license information.
6 ///////////////////////////////////////////////////////////////////////////////
7
8 #include "textprinter.h"
9
10 #include <QAbstractTextDocumentLayout>
11 #include <QDate>
12 #include <QFileDialog>
13 #include <QLocale>
14 #include <QPainter>
15 #include <QPrintDialog>
16 #include <QTextDocument>
17 #include <QTextFrame>
18
19 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
20 #include <QPrintPreviewDialog>
21 #endif
22
23 static inline double mmToInches(double mm) { return mm * 0.039370147; }
24
25 ///////////////////////////////////////////////////////////////////////////////
26 // TextPrinter()
27 ///////////////////////////////////////////////////////////////////////////////
28 /// Constructs a TextPrinter object.
29 /// Any related dialogs will be displayed centered over the parent, if it is
30 /// a QWidget object.
31 ///////////////////////////////////////////////////////////////////////////////
32
33 TextPrinter::TextPrinter(QObject *parent)
34 : QObject(parent), parent_(0),
35 printer_(new QPrinter(QPrinter::HighResolution)), tempdoc_(0),
36 leftmargin_(15.0), rightmargin_(15.0), topmargin_(15.0),
37 bottommargin_(15.0), spacing_(5.0),
38 headersize_(0.0), headerrule_(0.5), headertext_(QString()),
39 footersize_(0.0), footerrule_(0.5), footertext_(QString()),
40 dateformat_()
41 {
42 if (parent) parent_ = qobject_cast<QWidget*>(parent);
43
44 printer_->setFullPage(true);
45 printer_->setOrientation(QPrinter::Portrait);
46
47 // for convenience, default to US_Letter for C/US/Canada
48 // NOTE: bug in Qt, this value is not loaded by QPrintDialog
49 switch (QLocale::system().country()) {
50 case QLocale::AnyCountry:
51 case QLocale::Canada:
52 case QLocale::UnitedStates:
53 case QLocale::UnitedStatesMinorOutlyingIslands:
54 printer_->setPageSize(QPrinter::Letter); break;
55 default:
56 printer_->setPageSize(QPrinter::A4); break;
57 }
58 }
59
60 ///////////////////////////////////////////////////////////////////////////////
61 // ~TextPrinter()
62 ///////////////////////////////////////////////////////////////////////////////
63 /// Destroys the TextPrinter object.
64 ///////////////////////////////////////////////////////////////////////////////
65
66 TextPrinter::~TextPrinter()
67 {
68 delete printer_;
69 }
70
71 ///////////////////////////////////////////////////////////////////////////////
72 // pageSize()
73 ///////////////////////////////////////////////////////////////////////////////
74 /// Return the page size. This is defined by the QPrinter::PageSize
75 /// enumeration.
76 ///////////////////////////////////////////////////////////////////////////////
77
78 QPrinter::PageSize TextPrinter::pageSize() const
79 {
80 return printer_->pageSize();
81 }
82
83 ///////////////////////////////////////////////////////////////////////////////
84 // setPageSize()
85 ///////////////////////////////////////////////////////////////////////////////
86 /// Set the page size. Size is defined by the QPrinter::PageSize enumeration.
87 /// By default, this is QPrinter::Letter in US and Canada locales, and
88 /// QPrinter::A4 in other locales.
89 ///////////////////////////////////////////////////////////////////////////////
90
91 void TextPrinter::setPageSize(QPrinter::PageSize size)
92 {
93 printer_->setPageSize(size);
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////
97 // orientation()
98 ///////////////////////////////////////////////////////////////////////////////
99 /// Return the page orientation. This is defined by the QPrinterOrientation
100 /// enumeration.
101 ///////////////////////////////////////////////////////////////////////////////
102
103 QPrinter::Orientation TextPrinter::orientation() const
104 {
105 return printer_->orientation();
106 }
107
108 ///////////////////////////////////////////////////////////////////////////////
109 // setOrientation()
110 ///////////////////////////////////////////////////////////////////////////////
111 /// Set the page orientation. Orientation is defined by the
112 /// QPrinter::Orientation enumeration, and can be QPrinter::Portrait (the
113 /// default) or QPrinter::Landscape.
114 ///////////////////////////////////////////////////////////////////////////////
115
116 void TextPrinter::setOrientation(QPrinter::Orientation orientation)
117 {
118 printer_->setOrientation(orientation);
119 }
120
121 ///////////////////////////////////////////////////////////////////////////////
122 // leftMargin()
123 ///////////////////////////////////////////////////////////////////////////////
124 /// Return the left page margin width in millimeters.
125 ///////////////////////////////////////////////////////////////////////////////
126
127 double TextPrinter::leftMargin() const
128 {
129 return leftmargin_;
130 }
131
132 ///////////////////////////////////////////////////////////////////////////////
133 // setLeftMargin()
134 ///////////////////////////////////////////////////////////////////////////////
135 /// Set the left margin width in millimeters. The default value is 15mm.
136 ///////////////////////////////////////////////////////////////////////////////
137
138 void TextPrinter::setLeftMargin(double margin)
139 {
140 if ((margin > 0) && (margin < printer_->paperRect().width() / 2)) {
141 leftmargin_ = margin;
142 } else {
143 leftmargin_ = 0;
144 }
145 }
146
147 ///////////////////////////////////////////////////////////////////////////////
148 // rightMargin()
149 ///////////////////////////////////////////////////////////////////////////////
150 /// Return the right page margin width in millimeters.
151 ///////////////////////////////////////////////////////////////////////////////
152
153 double TextPrinter::rightMargin() const
154 {
155 return rightmargin_;
156 }
157
158 ///////////////////////////////////////////////////////////////////////////////
159 // setRightMargin()
160 ///////////////////////////////////////////////////////////////////////////////
161 /// Set the right margin width in millimeters. The default value is 15mm.
162 ///////////////////////////////////////////////////////////////////////////////
163
164 void TextPrinter::setRightMargin(double margin)
165 {
166 if ((margin > 0) && (margin < printer_->paperRect().width() / 2)) {
167 rightmargin_ = margin;
168 } else {
169 rightmargin_ = 0;
170 }
171 }
172
173 ///////////////////////////////////////////////////////////////////////////////
174 // topMargin()
175 ///////////////////////////////////////////////////////////////////////////////
176 /// Return the top page margin size in millimeters.
177 ///////////////////////////////////////////////////////////////////////////////
178
179 double TextPrinter::topMargin() const
180 {
181 return topmargin_;
182 }
183
184 ///////////////////////////////////////////////////////////////////////////////
185 // setTopMargin()
186 ///////////////////////////////////////////////////////////////////////////////
187 /// Set the top margin size in millimeters. The default value is 15mm.
188 ///////////////////////////////////////////////////////////////////////////////
189
190 void TextPrinter::setTopMargin(double margin)
191 {
192 if ((margin > 0) && (margin < printer_->paperRect().height() / 4)) {
193 topmargin_ = margin;
194 } else {
195 topmargin_ = 0;
196 }
197 }
198
199 ///////////////////////////////////////////////////////////////////////////////
200 // bottomMargin()
201 ///////////////////////////////////////////////////////////////////////////////
202 /// Return the bottom page margin size in millimeters.
203 ///////////////////////////////////////////////////////////////////////////////
204
205 double TextPrinter::bottomMargin() const
206 {
207 return bottommargin_;
208 }
209
210 ///////////////////////////////////////////////////////////////////////////////
211 // setBottomMargin()
212 ///////////////////////////////////////////////////////////////////////////////
213 /// Set the bottom margin size in millimeters. The default value is 15mm.
214 ///////////////////////////////////////////////////////////////////////////////
215
216 void TextPrinter::setBottomMargin(double margin)
217 {
218 if ((margin > 0) && (margin < printer_->paperRect().height() / 4)) {
219 bottommargin_ = margin;
220 } else {
221 bottommargin_ = 0;
222 }
223 }
224
225 ///////////////////////////////////////////////////////////////////////////////
226 // setMargins()
227 ///////////////////////////////////////////////////////////////////////////////
228 /// Set all margins to have the identical size. The default value is 15mm.
229 ///////////////////////////////////////////////////////////////////////////////
230
231 void TextPrinter::setMargins(double margin)
232 {
233 if ((margin > 0)
234 && (margin < printer_->paperRect().height() / 2)
235 && (margin < printer_->paperRect().width() / 2)) {
236 leftmargin_ = rightmargin_ = topmargin_ = bottommargin_ = margin;
237 } else {
238 leftmargin_ = rightmargin_ = topmargin_ = bottommargin_ = 0;
239 }
240 }
241
242 ///////////////////////////////////////////////////////////////////////////////
243 // spacing()
244 ///////////////////////////////////////////////////////////////////////////////
245 /// Return the spacing between the page content and the header and footer
246 /// blocks. This is defined in millimeters.
247 ///////////////////////////////////////////////////////////////////////////////
248
249 double TextPrinter::spacing() const
250 {
251 return spacing_;
252 }
253
254 ///////////////////////////////////////////////////////////////////////////////
255 // setSpacing()
256 ///////////////////////////////////////////////////////////////////////////////
257 /// Set the spacing between the page content and the header and footer blocks.
258 /// The default value is 5mm.
259 ///////////////////////////////////////////////////////////////////////////////
260
261 void TextPrinter::setSpacing(double spacing)
262 {
263 if ((spacing > 0) && (spacing <= printer_->paperRect().height() / 8)) {
264 spacing_ = spacing;
265 } else {
266 spacing_ = 0;
267 }
268 }
269
270 ///////////////////////////////////////////////////////////////////////////////
271 // headerSize()
272 ///////////////////////////////////////////////////////////////////////////////
273 /// Return the height of the header block in millimeters.
274 ///////////////////////////////////////////////////////////////////////////////
275
276 double TextPrinter::headerSize() const
277 {
278 return headersize_;
279 }
280
281 ///////////////////////////////////////////////////////////////////////////////
282 // setHeaderSize()
283 ///////////////////////////////////////////////////////////////////////////////
284 /// Set the height of the header in millimeters. The default is zero (no
285 /// header).
286 ///////////////////////////////////////////////////////////////////////////////
287
288 void TextPrinter::setHeaderSize(double size)
289 {
290 if ((size > 0) && (size <= printer_->paperRect().height() / 8)) {
291 headersize_ = size;
292 } else {
293 headersize_ = 0;
294 }
295 }
296
297 ///////////////////////////////////////////////////////////////////////////////
298 // headerRule()
299 ///////////////////////////////////////////////////////////////////////////////
300 /// Return the size of the header rule in points. One point is 1/72 inch.
301 ///////////////////////////////////////////////////////////////////////////////
302
303 double TextPrinter::headerRule() const
304 {
305 return headerrule_;
306 }
307
308 ///////////////////////////////////////////////////////////////////////////////
309 // setHeaderRule()
310 ///////////////////////////////////////////////////////////////////////////////
311 /// Set the header rule size in points. By default, the header rule is one half
312 /// point (1/144 inch). To turn off the rule, set the rule size to 0. The rule
313 /// will be drawn below the header area.
314 ///////////////////////////////////////////////////////////////////////////////
315
316 void TextPrinter::setHeaderRule(double pointsize)
317 {
318 headerrule_ = qMax(0.0, pointsize);
319 }
320
321 ///////////////////////////////////////////////////////////////////////////////
322 // headerText()
323 ///////////////////////////////////////////////////////////////////////////////
324 /// Return the text for the header.
325 ///////////////////////////////////////////////////////////////////////////////
326
327 const QString &TextPrinter::headerText() const
328 {
329 return headertext_;
330 }
331
332 ///////////////////////////////////////////////////////////////////////////////
333 // setHeaderText()
334 ///////////////////////////////////////////////////////////////////////////////
335 /// Set the text for the header. Rich text is supported. HTML tags may be used
336 /// to format the text and align elements. The following page variables may be
337 /// included in the text:
338 /// - \&page; - Insert current page number
339 /// - \&date; - Insert current date, using the format set with setPageFormat()
340 ///////////////////////////////////////////////////////////////////////////////
341
342 void TextPrinter::setHeaderText(const QString &text)
343 {
344 headertext_ = text;
345 }
346
347 ///////////////////////////////////////////////////////////////////////////////
348 // footerSize()
349 ///////////////////////////////////////////////////////////////////////////////
350 /// Return the height of the footer block in millimeters.
351 ///////////////////////////////////////////////////////////////////////////////
352
353 double TextPrinter::footerSize() const
354 {
355 return footersize_;
356 }
357
358 ///////////////////////////////////////////////////////////////////////////////
359 // setFooterSize()
360 ///////////////////////////////////////////////////////////////////////////////
361 /// Set the height of the footer in millimeters. The default is zero (no
362 /// header).
363 ///////////////////////////////////////////////////////////////////////////////
364
365 void TextPrinter::setFooterSize(double size)
366 {
367 if ((size > 0) && (size <= printer_->paperRect().height() / 8)) {
368 footersize_ = size;
369 } else {
370 footersize_ = 0;
371 }
372 }
373
374 ///////////////////////////////////////////////////////////////////////////////
375 // footerRule()
376 ///////////////////////////////////////////////////////////////////////////////
377 /// Return the size of the footer rule in points. One point is 1/72 inch.
378 ///////////////////////////////////////////////////////////////////////////////
379
380 double TextPrinter::footerRule() const
381 {
382 return footerrule_;
383 }
384
385 ///////////////////////////////////////////////////////////////////////////////
386 // setFooterRule()
387 ///////////////////////////////////////////////////////////////////////////////
388 /// Set the footer rule size in points. By default, the footer rule is one half
389 /// point (1/144 inch). To turn off the rule, set the rule size to 0. The rule
390 /// will be drawn just above the footer area.
391 ///////////////////////////////////////////////////////////////////////////////
392
393 void TextPrinter::setFooterRule(double pointsize)
394 {
395 footerrule_ = qMax(0.0, pointsize);
396 }
397
398 ///////////////////////////////////////////////////////////////////////////////
399 // footerText()
400 ///////////////////////////////////////////////////////////////////////////////
401 /// Return the text for the footer.
402 ///////////////////////////////////////////////////////////////////////////////
403
404 const QString &TextPrinter::footerText() const
405 {
406 return footertext_;
407 }
408
409 ///////////////////////////////////////////////////////////////////////////////
410 // setFooterText()
411 ///////////////////////////////////////////////////////////////////////////////
412 /// Set the text for the footer. Rich text is supported. HTML tags may be used
413 /// to format the text and align elements. The following page variables may be
414 /// included in the text:
415 /// - \&page; - Insert current page number
416 /// - \&date; - Insert current date, using the format set with setPageFormat()
417 ///////////////////////////////////////////////////////////////////////////////
418
419 void TextPrinter::setFooterText(const QString &text)
420 {
421 footertext_ = text;
422 }
423
424 ///////////////////////////////////////////////////////////////////////////////
425 // dateFormat()
426 ///////////////////////////////////////////////////////////////////////////////
427 /// Return the currently set date format string.
428 ///////////////////////////////////////////////////////////////////////////////
429
430 const QString &TextPrinter::dateFormat() const
431 {
432 return dateformat_;
433 }
434
435 ///////////////////////////////////////////////////////////////////////////////
436 // setDateFormat()
437 ///////////////////////////////////////////////////////////////////////////////
438 /// Set the date format to be used for the \&date; page variable. The format
439 /// is the same as that used by QDate::toString(). If no format is set, the
440 /// format defaults to Qt::TextDate.
441 ///////////////////////////////////////////////////////////////////////////////
442
443 void TextPrinter::setDateFormat(const QString &format)
444 {
445 dateformat_ = format;
446 }
447
448 ///////////////////////////////////////////////////////////////////////////////
449 // print()
450 ///////////////////////////////////////////////////////////////////////////////
451 /// Print a document. A standard print dialog will be displayed. If a QPrinter
452 /// is not provided, a temporary printer object will be used. If the caption
453 /// parameter is not empty, it will be used as the title for the print
454 /// dialog.
455 ///
456 /// \todo add overload taking html string
457 ///////////////////////////////////////////////////////////////////////////////
458
459 void TextPrinter::print(const QTextDocument *document,
460 const QString &caption)
461 {
462 if (!document) return;
463
464 // setup printer
465 printer_->setOutputFormat(QPrinter::NativeFormat);
466 printer_->setOutputFileName(QString());
467
468 // show print dialog
469 QPrintDialog dialog(printer_, parent_);
470 dialog.setWindowTitle(caption.isEmpty() ? "Print Document" : caption);
471 if (dialog.exec() == QDialog::Rejected) return;
472
473 // print it
474 tempdoc_ = document->clone();
475 print(printer_);
476
477 delete tempdoc_;
478 tempdoc_ = 0;
479 }
480
481 ///////////////////////////////////////////////////////////////////////////////
482 // exportPDF()
483 ///////////////////////////////////////////////////////////////////////////////
484 /// Export the document in PDF format. If caption is not empty, it will be used
485 /// as the title for the dialog. If filename is empty, a standard file
486 /// selection dialog will be displayed.
487 ///////////////////////////////////////////////////////////////////////////////
488
489 void TextPrinter::exportPdf(const QTextDocument *document,
490 const QString &caption,
491 const QString &filename)
492 {
493 if (!document) return;
494
495 // file save dialog
496 QString dialogcaption = caption.isEmpty() ? "Export PDF" : caption;
497 QString exportname;
498 if (!filename.isEmpty()) {
499 exportname = filename;
500 } else {
501 exportname = QFileDialog::getSaveFileName(parent_, dialogcaption,
502 filename, "*.pdf");
503 }
504 if (exportname.isEmpty()) return;
505 if (QFileInfo(exportname).suffix().isEmpty())
506 exportname.append(".pdf");
507
508 // setup printer
509 printer_->setOutputFormat(QPrinter::PdfFormat);
510 printer_->setOutputFileName(exportname);
511
512 // print it
513 tempdoc_ = document->clone();
514 print(printer_);
515
516 delete tempdoc_;
517 tempdoc_ = 0;
518 }
519
520 ///////////////////////////////////////////////////////////////////////////////
521 // preview()
522 ///////////////////////////////////////////////////////////////////////////////
523 /// Displays a print preview dialog. If caption is not empty, it will be used
524 /// as the title of the dialog.
525 ///////////////////////////////////////////////////////////////////////////////
526
527 void TextPrinter::preview(const QTextDocument *document,
528 const QString &caption)
529 {
530 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
531 if (!document) return;
532
533 QPrintPreviewDialog *dialog = new QPrintPreviewDialog(printer_, parent_);
534 dialog->setWindowTitle(caption.isEmpty() ? "Print Preview" : caption);
535
536 connect(dialog, SIGNAL(paintRequested(QPrinter*)),
537 this, SLOT(print(QPrinter*)));
538
539 // preview it
540 tempdoc_ = document->clone();
541 dialog->exec();
542
543 delete tempdoc_;
544 tempdoc_ = 0;
545
546 delete dialog;
547 #endif
548 }
549
550 ///////////////////////////////////////////////////////////////////////////////
551 // private methods
552 ///////////////////////////////////////////////////////////////////////////////
553
554 // paperRect() ////////////////////////////////////////////////////////////////
555 // Return the size of the paper, adjusted for DPI
556
557 QRectF TextPrinter::paperRect(QPaintDevice *device)
558 {
559 // calculate size of paper
560 QRectF rect = printer_->paperRect();
561 // adjust for DPI
562 rect.setWidth(rect.width() *
563 device->logicalDpiX() / printer_->logicalDpiX());
564 rect.setHeight(rect.height() *
565 device->logicalDpiY() / printer_->logicalDpiY());
566
567 return rect;
568 }
569
570 // contentRect() //////////////////////////////////////////////////////////////
571 // calculate the rect for the content block
572
573 QRectF TextPrinter::contentRect(QPaintDevice *device)
574 {
575 // calculate size of content (paper - margins)
576 QRectF rect = paperRect(device);
577
578 rect.adjust(mmToInches(leftmargin_) * device->logicalDpiX(),
579 mmToInches(topmargin_) * device->logicalDpiY(),
580 -mmToInches(rightmargin_) * device->logicalDpiX(),
581 -mmToInches(bottommargin_) * device->logicalDpiY());
582
583 // header
584 if (headersize_ > 0) {
585 rect.adjust(0, mmToInches(headersize_) * device->logicalDpiY(), 0, 0);
586 rect.adjust(0, mmToInches(spacing_) * device->logicalDpiY(), 0, 0);
587 }
588 // footer
589 if (footersize_ > 0) {
590 rect.adjust(0, 0, 0, -mmToInches(footersize_) * device->logicalDpiY());
591 rect.adjust(0, 0, 0, -mmToInches(spacing_) * device->logicalDpiY());
592 }
593
594 return rect;
595 }
596
597 // headerRect() //////////////////////////////////////////////////////////////
598 // calculate the rect for the header block
599
600 QRectF TextPrinter::headerRect(QPaintDevice *device)
601 {
602 QRectF rect = paperRect(device);
603 rect.adjust(mmToInches(leftmargin_) * device->logicalDpiX(),
604 mmToInches(topmargin_) * device->logicalDpiY(),
605 -mmToInches(rightmargin_) * device->logicalDpiX(), 0);
606
607 (headerrule_ / 144.0);
608
609 rect.setBottom(rect.top() +
610 mmToInches(headersize_) * device->logicalDpiY());
611
612 return rect;
613 };
614
615 // footerRect() ///////////////////////////////////////////////////////////////
616 // calculate the rect for the footer block
617
618 QRectF TextPrinter::footerRect(QPaintDevice *device)
619 {
620 QRectF rect = paperRect(device);
621 rect.adjust(mmToInches(leftmargin_) * device->logicalDpiX(), 0,
622 -mmToInches(rightmargin_) * device->logicalDpiX(),
623 -mmToInches(bottommargin_) * device->logicalDpiY());
624
625 rect.setTop(rect.bottom() -
626 mmToInches(footersize_) * device->logicalDpiY());
627
628 return rect;
629 };
630
631 // print() ////////////////////////////////////////////////////////////////////
632 // Common printing routine. Print tempdoc_ to given printer device.
633
634 void TextPrinter::print(QPrinter *printer)
635 {
636 if (!printer || !tempdoc_) return;
637
638 tempdoc_->setUseDesignMetrics(true);
639 tempdoc_->documentLayout()->setPaintDevice(printer);
640 tempdoc_->setPageSize(contentRect(printer).size());
641 // dump existing margin (if any)
642 QTextFrameFormat fmt = tempdoc_->rootFrame()->frameFormat();
643 fmt.setMargin(0);
644 tempdoc_->rootFrame()->setFrameFormat(fmt);
645
646 // to iterate through pages we have to worry about
647 // copies, collation, page range, and print order
648
649 // get num copies
650 int doccopies;
651 int pagecopies;
652 if (printer->collateCopies()) {
653 doccopies = 1;
654 pagecopies = printer->numCopies();
655 } else {
656 doccopies = printer->numCopies();
657 pagecopies = 1;
658 }
659
660 // get page range
661 int firstpage = printer->fromPage();
662 int lastpage = printer->toPage();
663 if (firstpage == 0 && lastpage == 0) { // all pages
664 firstpage = 1;
665 lastpage = tempdoc_->pageCount();
666 }
667
668 // print order
669 bool ascending = true;
670 if (printer->pageOrder() == QPrinter::LastPageFirst) {
671 int tmp = firstpage;
672 firstpage = lastpage;
673 lastpage = tmp;
674 ascending = false;
675 }
676
677 // loop through and print pages
678 QPainter painter(printer);
679 painter.setRenderHints(QPainter::Antialiasing |
680 QPainter::TextAntialiasing |
681 QPainter::SmoothPixmapTransform, true);
682 for (int dc=0; dc<doccopies; dc++) {
683 int pagenum = firstpage;
684 while (true) {
685 for (int pc=0; pc<pagecopies; pc++) {
686 if (printer->printerState() == QPrinter::Aborted ||
687 printer->printerState() == QPrinter::Error) {
688 return;
689 }
690 // print page
691 paintPage(&painter, tempdoc_, pagenum);
692 if (pc < pagecopies-1) printer->newPage();
693 }
694 if (pagenum == lastpage) break;
695 if (ascending) pagenum++;
696 else pagenum--;
697 printer->newPage();
698 }
699
700 if (dc < doccopies-1) printer->newPage();
701 }
702 }
703
704 // paintPage() ////////////////////////////////////////////////////////////////
705 // paint an individual page of the document to the painter
706
707 void TextPrinter::paintPage(QPainter *painter,
708 QTextDocument *document,
709 int pagenum)
710 {
711 QRectF rect;
712 double onepoint = painter->device()->logicalDpiY() / 72.0;
713
714 // header
715 if (headersize_ > 0) {
716 rect = headerRect(painter->device());
717 if (headerrule_ > 0.0) {
718 painter->save();
719 // allow space between rule and header
720 painter->translate(0, onepoint + (headerrule_ * onepoint / 2.0));
721 painter->setPen(QPen(Qt::black, headerrule_ * onepoint));
722 painter->drawLine(rect.bottomLeft(), rect.bottomRight());
723 painter->restore();
724 }
725
726 // replace page variables
727 QString header = headertext_;
728 header.replace("&page;", QString::number(pagenum));
729 if (dateformat_.isEmpty()) {
730 header.replace("&date;", QDate::currentDate().toString());
731 } else {
732 header.replace("&date;", QDate::currentDate().toString(dateformat_));
733 }
734
735 painter->save();
736 painter->translate(rect.left(), rect.top());
737 QRectF clip(0, 0, rect.width(), rect.height());
738 QTextDocument doc;
739 doc.setUseDesignMetrics(true);
740 doc.setHtml(header);
741 doc.documentLayout()->setPaintDevice(painter->device());
742 doc.setPageSize(rect.size());
743
744 // align text to bottom
745 double newtop = clip.bottom() - doc.size().height();
746 clip.setHeight(doc.size().height());
747 painter->translate(0, newtop);
748
749 doc.drawContents(painter, clip);
750 painter->restore();
751 }
752
753 // footer
754 if (footersize_ > 0) {
755 rect = footerRect(painter->device());
756 if (footerrule_ > 0.0) {
757 painter->save();
758 // allow space between rule and footer
759 painter->translate(0, -onepoint + (-footerrule_ * onepoint / 2.0));
760 painter->setPen(QPen(Qt::black, footerrule_ * onepoint));
761 painter->drawLine(rect.topLeft(), rect.topRight());
762 painter->restore();
763 }
764
765 // replace page variables
766 QString footer = footertext_;
767 footer.replace("&page;", QString::number(pagenum));
768 if (dateformat_.isEmpty()) {
769 footer.replace("&date;", QDate::currentDate().toString());
770 } else {
771 footer.replace("&date;", QDate::currentDate().toString(dateformat_));
772 }
773
774 painter->save();
775 painter->translate(rect.left(), rect.top());
776 QRectF clip(0, 0, rect.width(), rect.height());
777 QTextDocument doc;
778 doc.setUseDesignMetrics(true);
779 doc.setHtml(footer);
780 doc.documentLayout()->setPaintDevice(painter->device());
781 doc.setPageSize(rect.size());
782 doc.drawContents(painter, clip);
783 painter->restore();
784 }
785
786 // content
787 painter->save();
788
789 rect = contentRect(painter->device());
790 painter->translate(rect.left(), rect.top() - (pagenum-1) * rect.height());
791 QRectF clip(0, (pagenum-1) * rect.height(), rect.width(), rect.height());
792
793 document->drawContents(painter, clip);
794
795 painter->restore();
796 }