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