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