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