]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLPainter.C
a8d5e30aa78e416e61e7db76b7ff2784c5688d0a
[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::arc(int x, int y, unsigned int w, unsigned int h,
160         int a1, int a2, LColor_color col)
161 {
162         // LyX usings 1/64ths degree, Qt usings 1/16th
163         setQPainterPen(col);
164         qp_->drawArc(x, y, w, h, a1 / 4, a2 / 4);
165 }
166
167
168 void QLPainter::image(int x, int y, int w, int h,
169         lyx::graphics::Image const & i)
170 {
171         lyx::graphics::QLImage const & qlimage =
172                 static_cast<lyx::graphics::QLImage const &>(i);
173
174         fillRectangle(x, y, w, h, LColor::graphicsbg);
175
176         qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
177 }
178
179
180 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
181 {
182         return text(x, y, s.data(), s.length(), f);
183 }
184
185
186 void QLPainter::text(int x, int y, char c, LyXFont const & f)
187 {
188         char s[2] = { c, '\0' };
189         return text(x, y, s, 1, f);
190 }
191
192
193 void QLPainter::smallCapsText(int x, int y,
194         QString const & s, LyXFont const & f)
195 {
196         LyXFont smallfont(f);
197         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
198
199         QFont const & qfont = theApp->fontLoader().get(f);
200         QFont const & qsmallfont = theApp->fontLoader().get(smallfont);
201         QFontMetrics const & qfontm = QFontMetrics(qfont);
202         QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
203
204         setQPainterPen(f.realColor());
205         int tmpx = x;
206         size_t ls = s.length();
207         for (unsigned int i = 0; i < ls; ++i) {
208                 QChar const c = s[i].upper();
209                 if (c != s.at(i)) {
210                         qp_->setFont(qsmallfont);
211                         qp_->drawText(tmpx, y, c);
212                         tmpx += qsmallfontm.width(c);
213                 } else {
214                         qp_->setFont(qfont);
215                         qp_->drawText(tmpx, y, c);
216                         tmpx += qfontm.width(c);
217                 }
218         }
219 }
220
221
222 void QLPainter::text(int x, int y, char const * s, size_t ls,
223         LyXFont const & f)
224 {
225         Encoding const * encoding = f.language()->encoding();
226         if (f.isSymbolFont())
227                 encoding = encodings.symbol_encoding();
228
229         QString str;
230         str.setLength(ls);
231         for (unsigned int i = 0; i < ls; ++i)
232                 str[i] = QChar(encoding->ucs(s[i]));
233
234         // HACK: QT3 refuses to show single compose characters
235         //       Still needed with Qt4?
236         if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
237                 str = ' ' + str;
238
239         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
240                 setQPainterPen(f.realColor());
241                 qp_->setFont(theApp->fontLoader().get(f));
242                 // We need to draw the text as LTR as we use our own bidi code.
243                 qp_->setLayoutDirection(Qt::LeftToRight);
244                 qp_->drawText(x, y, str);
245         } else {
246                 smallCapsText(x, y, str, f);
247         }
248
249         if (f.underbar() == LyXFont::ON) {
250                 underline(f, x, y, font_metrics::width(s, ls, f));
251         }
252
253 }
254
255
256 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
257 {
258         qp_->drawPixmap(x, y, pixmap);
259 }
260
261
262 void QLPainter::drawImage(int x, int y, QImage const & image)
263 {
264         qp_->drawImage(x, y, image);
265 }
266
267 } // namespace frontend
268 } // namespace lyx