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