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