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