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