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