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