summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/printtext.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/printtext.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/printtext.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/printtext.cpp b/pwmanager/pwmanager/printtext.cpp
new file mode 100644
index 0000000..7590114
--- a/dev/null
+++ b/pwmanager/pwmanager/printtext.cpp
@@ -0,0 +1,107 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2003 by Michael Buesch *
4 * email: mbuesch@freenet.de *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. *
9 * *
10 ***************************************************************************/
11
12#include "printtext.h"
13#include "pwmexception.h"
14
15#include <klocale.h>
16
17#include <qpen.h>
18
19
20PrintText::PrintText()
21{
22 curYPos = 0;
23 paint = new QPainter;
24 metrics = new QPaintDeviceMetrics(this);
25}
26
27PrintText::~PrintText()
28{
29 delete paint;
30 delete metrics;
31}
32
33void PrintText::beginPrint()
34{
35 PWM_ASSERT(paint && metrics);
36 curYPos = 0;
37 page = 1;
38 paint->begin(this);
39 ls = paint->fontMetrics().lineSpacing();
40
41 // set printing border
42 int border = (metrics->width() / metrics->widthMM()) * 15; // 15 mm border
43 topLeft.setX(border);
44 topLeft.setY(border);
45
46 // set body
47 body.setRect(topLeft.x(), ls * 2 + topLeft.y(),
48 metrics->width() - topLeft.x() * 2,
49 metrics->height() - ls * 2 - topLeft.y() * 2);
50 paint->setTabStops(paint->fontMetrics().width("M") * 8);
51
52 printHeader();
53}
54
55void PrintText::printLine(QString t)
56{
57 if (curYPos == 0)
58 curYPos = ls * 2 + topLeft.y(); // skip header
59 if (t == "")
60 t = " ";
61 QRect r = paint->boundingRect(topLeft.x(), curYPos, body.width(), body.height(),
62 QPainter::ExpandTabs | QPainter::WordBreak, t);
63 int height = r.height();
64 if (height + curYPos > metrics->height()) {
65 // next page
66 newPage();
67 ++page;
68 headerRight = "";
69 printHeader();
70 curYPos = ls * 2 + topLeft.y();
71 }
72 paint->drawText(topLeft.x(), curYPos, metrics->width(), metrics->height() - curYPos,
73 QPainter::ExpandTabs | QPainter::WordBreak, t);
74 curYPos += ls;
75}
76
77void PrintText::printHeader()
78{
79 if (headerRight == "")
80 headerRight = i18n("Page #") + QString::number(page);
81
82 paint->drawText(topLeft.x(), topLeft.y(), metrics->width() - topLeft.x() * 2,
83 ls, Qt::AlignLeft, headerLeft);
84 paint->drawText(topLeft.x(), topLeft.y(), metrics->width() - topLeft.x() * 2,
85 ls, Qt::AlignHCenter, headerMiddle);
86 paint->drawText(topLeft.x(), topLeft.y(), metrics->width() - topLeft.x() * 2,
87 ls, Qt::AlignRight, headerRight);
88
89 QPen pen;
90 pen.setWidth(3);
91 paint->setPen(pen);
92 paint->drawLine(topLeft.x(), (ls + ls / 2) + topLeft.y(),
93 metrics->width() - topLeft.x(),
94 (ls + ls / 2) + topLeft.y());
95}
96
97void PrintText::setHeader(const QString &left, const QString &middle)
98{
99 headerLeft = left;
100 headerMiddle = middle;
101}
102
103void PrintText::getHeader(QString *left, QString *middle)
104{
105 *left = headerLeft;
106 *middle = headerMiddle;
107}