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