]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QLPainter.C
54fa7fb6bc645abc472d2b0f99e280a26597d0ae
[features.git] / src / frontends / qt2 / QLPainter.C
1 /**
2  * \file QLPainter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "font_metrics.h"
15 #include "support/lstrings.h"
16 #include "lyxrc.h"
17 #include "debug.h"
18 #include "LyXView.h"
19 #include "encoding.h"
20 #include "language.h"
21
22 #include "QWorkArea.h"
23 #include "qfont_loader.h"
24 #include "QLPainter.h"
25 #include "QLImage.h"
26 #include "qt_helpers.h"
27 #include "lcolorcache.h"
28
29 #include <boost/scoped_array.hpp>
30
31 #include <qpainter.h>
32 #include <qbrush.h>
33 #include <qcolor.h>
34
35 #include <iostream>
36
37 using std::endl;
38
39
40 QLPainter::QLPainter(QWorkArea & qwa)
41         : Painter(), owner_(qwa), paint_check_(0)
42 {
43         qp_.reset(new QPainter);
44 }
45
46
47 void QLPainter::start()
48 {
49         if (++paint_check_ == 1)
50                 qp_->begin(owner_.getPixmap());
51 }
52
53
54 void QLPainter::end()
55 {
56         if (paint_check_ == 0) {
57                 lyxerr << "ended painting whilst not painting ??" << endl;
58         } else if (--paint_check_ == 0) {
59                 qp_->end();
60         }
61 }
62
63
64 int QLPainter::paperWidth() const
65 {
66         return owner_.workWidth();
67 }
68
69
70 int QLPainter::paperHeight() const
71 {
72         return owner_.workHeight();
73 }
74
75
76 QPainter & QLPainter::setPen(LColor::color c,
77         Painter::line_style ls, Painter::line_width lw)
78 {
79         QPen pen = qp_->pen();
80
81         pen.setColor(lcolorcache.get(c));
82
83         switch (ls) {
84                 case line_solid: pen.setStyle(QPen::SolidLine); break;
85                 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
86         }
87
88         switch (lw) {
89                 case line_thin: pen.setWidth(0); break;
90                 case line_thick: pen.setWidth(3); break;
91         }
92
93         qp_->setPen(pen);
94         return *qp_;
95 }
96
97
98 Painter & QLPainter::point(int x, int y, LColor::color c)
99 {
100         setPen(c).drawPoint(x, y);
101         return *this;
102 }
103
104
105 Painter & QLPainter::line(int x1, int y1,
106         int x2, int y2,
107         LColor::color col,
108         line_style ls,
109         line_width lw)
110 {
111         setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
112         return *this;
113 }
114
115
116 Painter & QLPainter::lines(int const * xp, int const * yp,
117         int np,
118         LColor::color col,
119         line_style ls,
120         line_width lw)
121 {
122         // FIXME ?
123
124         // Must use new as np is not known at compile time.
125         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
126
127         for (int i = 0, j = 0; i < np; ++i) {
128                 points[j++] = xp[i];
129                 points[j++] = yp[i];
130         }
131
132         setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
133
134         return *this;
135 }
136
137
138 Painter & QLPainter::rectangle(int x, int y,
139         int w, int h,
140         LColor::color col,
141         line_style ls,
142         line_width lw)
143 {
144         setPen(col, ls, lw).drawRect(x, y, w, h);
145         return *this;
146 }
147
148
149 Painter & QLPainter::fillRectangle(int x, int y,
150         int w, int h,
151         LColor::color col)
152 {
153         qp_->fillRect(x, y, w, h, lcolorcache.get(col));
154         return *this;
155 }
156
157
158 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
159         int np, LColor::color col)
160 {
161         // Must use new as np is not known at compile time.
162         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
163
164         //if (1) return *this;
165
166         for (int i = 0, j = 0; i < np; ++i) {
167                 points[j++] = xp[i];
168                 points[j++] = yp[i];
169         }
170
171         setPen(col);
172         qp_->setBrush(lcolorcache.get(col));
173         qp_->drawPolygon(QPointArray(np, points.get()));
174         qp_->setBrush(Qt::NoBrush);
175
176         return *this;
177 }
178
179
180 Painter & QLPainter::arc(int x, int y,
181         unsigned int w, unsigned int h,
182         int a1, int a2, LColor::color col)
183 {
184         // LyX usings 1/64ths degree, Qt usings 1/16th
185         setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
186         return *this;
187 }
188
189
190 Painter & QLPainter::image(int x, int y,
191         int w, int h,
192         grfx::Image const & i)
193 {
194         qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
195         return *this;
196 }
197
198
199 Painter & QLPainter::text(int x, int y,
200         string const & s, LyXFont const & f)
201 {
202         return text(x, y, s.data(), s.length(), f);
203 }
204
205
206 Painter & QLPainter::text(int x, int y,
207         char c, LyXFont const & f)
208 {
209         char s[2] = { c, '\0' };
210         return text(x, y, s, 1, f);
211 }
212
213
214 void QLPainter::smallCapsText(int x, int y,
215         QString const & s, LyXFont const & f)
216 {
217         LyXFont smallfont(f);
218         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
219
220         QFont const & qfont = fontloader.get(f);
221         QFont const & qsmallfont = fontloader.get(smallfont);
222         QFontMetrics const & qfontm = QFontMetrics(qfont);
223         QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
224
225         int tmpx = x;
226         size_t ls = s.length();
227         for (size_t i = 0; i < ls; ++i) {
228                 QChar const c = s[i].upper();
229                 if (c != s[i]) {
230                         qp_->setFont(qsmallfont);
231                         qp_->drawText(tmpx, y, c);
232                         tmpx += qsmallfontm.width(c);
233                 } else {
234                         qp_->setFont(qfont);
235                         qp_->drawText(tmpx, y, c);
236                         tmpx += qfontm.width(c);
237                 }
238         }
239 }
240
241
242 Painter & QLPainter::text(int x, int y,
243         char const * s, size_t ls,
244         LyXFont const & f)
245 {
246         setPen(f.realColor());
247
248         Encoding const * encoding = f.language()->encoding();
249         if (f.isSymbolFont())
250                 encoding = encodings.symbol_encoding();
251
252         QString str;
253 #if QT_VERSION >= 300
254         str.setLength(ls);
255         for (size_t i = 0; i < ls; ++i)
256                 str[i] = QChar(encoding->ucs(s[i]));
257         // HACK: QT3 refuses to show single compose characters
258         if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
259                 str = ' ' + str;
260 #else
261         for (size_t i = 0; i < ls; ++i)
262                 str += QChar(encoding->ucs(s[i]));
263 #endif
264
265         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
266                 qp_->setFont(fontloader.get(f));
267 #if QT_VERSION >= 300
268                 // We need to draw the text as LTR as we use our own bidi
269                 // code.
270                 qp_->drawText(x, y, str, -1, QPainter::LTR);
271 #else
272                 qp_->drawText(x, y, str);
273 #endif
274         } else {
275                 smallCapsText(x, y, str, f);
276         }
277
278         if (f.underbar() == LyXFont::ON) {
279                 underline(f, x, y, font_metrics::width(s, ls, f));
280         }
281
282         return *this;
283 }