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