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
|
/***************************************************************************
* *
* copyright (C) 2003 by Michael Buesch *
* email: mbuesch@freenet.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 *
* as published by the Free Software Foundation. *
* *
***************************************************************************/
#include "printtext.h"
#include "pwmexception.h"
#include <klocale.h>
#include <qpen.h>
PrintText::PrintText()
{
curYPos = 0;
paint = new QPainter;
metrics = new QPaintDeviceMetrics(this);
}
PrintText::~PrintText()
{
delete paint;
delete metrics;
}
void PrintText::beginPrint()
{
PWM_ASSERT(paint && metrics);
curYPos = 0;
page = 1;
paint->begin(this);
ls = paint->fontMetrics().lineSpacing();
// set printing border
int border = (metrics->width() / metrics->widthMM()) * 15; // 15 mm border
topLeft.setX(border);
topLeft.setY(border);
// set body
body.setRect(topLeft.x(), ls * 2 + topLeft.y(),
metrics->width() - topLeft.x() * 2,
metrics->height() - ls * 2 - topLeft.y() * 2);
paint->setTabStops(paint->fontMetrics().width("M") * 8);
printHeader();
}
void PrintText::printLine(QString t)
{
if (curYPos == 0)
curYPos = ls * 2 + topLeft.y(); // skip header
if (t == "")
t = " ";
QRect r = paint->boundingRect(topLeft.x(), curYPos, body.width(), body.height(),
QPainter::ExpandTabs | QPainter::WordBreak, t);
int height = r.height();
if (height + curYPos > metrics->height()) {
// next page
newPage();
++page;
headerRight = "";
printHeader();
curYPos = ls * 2 + topLeft.y();
}
paint->drawText(topLeft.x(), curYPos, metrics->width(), metrics->height() - curYPos,
QPainter::ExpandTabs | QPainter::WordBreak, t);
curYPos += ls;
}
void PrintText::printHeader()
{
if (headerRight == "")
headerRight = i18n("Page #") + QString::number(page);
paint->drawText(topLeft.x(), topLeft.y(), metrics->width() - topLeft.x() * 2,
ls, Qt::AlignLeft, headerLeft);
paint->drawText(topLeft.x(), topLeft.y(), metrics->width() - topLeft.x() * 2,
ls, Qt::AlignHCenter, headerMiddle);
paint->drawText(topLeft.x(), topLeft.y(), metrics->width() - topLeft.x() * 2,
ls, Qt::AlignRight, headerRight);
QPen pen;
pen.setWidth(3);
paint->setPen(pen);
paint->drawLine(topLeft.x(), (ls + ls / 2) + topLeft.y(),
metrics->width() - topLeft.x(),
(ls + ls / 2) + topLeft.y());
}
void PrintText::setHeader(const QString &left, const QString &middle)
{
headerLeft = left;
headerMiddle = middle;
}
void PrintText::getHeader(QString *left, QString *middle)
{
*left = headerLeft;
*middle = headerMiddle;
}
|