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