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