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