]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPainter.C
better selection and scrolling behaviour
[lyx.git] / src / frontends / qt2 / 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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "font_metrics.h"
18 #include "support/lstrings.h"
19 #include "lyxrc.h"
20 #include "debug.h"
21 #include "LyXView.h"
22 #include "encoding.h"
23 #include "language.h"
24
25 #include "QWorkArea.h"
26 #include "qfont_loader.h"
27 #include "QLPainter.h"
28 #include "QLImage.h"
29
30 #include <boost/scoped_array.hpp>
31
32 #include <qpainter.h>
33 #include <qbrush.h>
34 #include <qcolor.h>
35
36 #include <iostream>
37
38 using std::endl;
39
40
41 QLPainter::QLPainter(QWorkArea & qwa)
42         : Painter(), owner_(qwa), paint_check_(0)
43 {
44         qp_.reset(new QPainter);
45 }
46
47
48 void QLPainter::start()
49 {
50         if (++paint_check_ == 1)
51                 qp_->begin(owner_.getPixmap());
52 }
53
54
55 void QLPainter::end()
56 {
57         if (paint_check_ == 0) {
58                 lyxerr << "ended painting whilst not painting ??" << endl;
59         } else if (--paint_check_ == 0) {
60                 qp_->end();
61         }
62 }
63
64
65 int QLPainter::paperWidth() const
66 {
67         return owner_.workWidth();
68 }
69
70
71 int QLPainter::paperHeight() const
72 {
73         return owner_.workHeight();
74 }
75
76
77 QPainter & QLPainter::setPen(LColor::color c,
78         Painter::line_style ls, Painter::line_width lw)
79 {
80         QPen pen = qp_->pen();
81
82         pen.setColor(lcolor.getX11Name(c).c_str());
83
84         switch (ls) {
85                 case line_solid: pen.setStyle(QPen::SolidLine); break;
86                 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
87         }
88
89         switch (lw) {
90                 case line_thin: pen.setWidth(0); break;
91                 case line_thick: pen.setWidth(3); break;
92         }
93
94         qp_->setPen(pen);
95         return *qp_;
96 }
97
98
99 Painter & QLPainter::point(int x, int y, LColor::color c)
100 {
101         setPen(c).drawPoint(x, y);
102         return *this;
103 }
104
105
106 Painter & QLPainter::line(int x1, int y1,
107         int x2, int y2,
108         LColor::color col,
109         line_style ls,
110         line_width lw)
111 {
112         setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
113         return *this;
114 }
115
116
117 Painter & QLPainter::lines(int const * xp, int const * yp,
118         int np,
119         LColor::color col,
120         line_style ls,
121         line_width lw)
122 {
123         // FIXME ?
124
125         // Must use new as np is not known at compile time.
126         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
127
128         for (int i = 0, j = 0; i < np; ++i) {
129                 points[j++] = xp[i];
130                 points[j++] = yp[i];
131         }
132
133         setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
134
135         return *this;
136 }
137
138
139 Painter & QLPainter::rectangle(int x, int y,
140         int w, int h,
141         LColor::color col,
142         line_style ls,
143         line_width lw)
144 {
145         //lyxerr << "rectangle " << x << ',' << y << ' '
146         //       << w << ',' << h << endl;
147         setPen(col, ls, lw).drawRect(x, y, w, h);
148         return *this;
149 }
150
151
152 Painter & QLPainter::fillRectangle(int x, int y,
153         int w, int h,
154         LColor::color col)
155 {
156         //lyxerr << "fillRectangle " << x << ',' << y << ' '
157         //       << w << ',' << h << endl;
158         qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
159         return *this;
160 }
161
162
163 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
164         int np, LColor::color col)
165 {
166         // Must use new as np is not known at compile time.
167         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
168
169         //if (1) return *this;
170
171         for (int i = 0, j = 0; i < np; ++i) {
172                 points[j++] = xp[i];
173                 points[j++] = yp[i];
174         }
175
176         setPen(col);
177         qp_->setBrush(lcolor.getX11Name(col).c_str());
178         qp_->drawPolygon(QPointArray(np, points.get()));
179         qp_->setBrush(Qt::NoBrush);
180
181         return *this;
182 }
183
184
185 Painter & QLPainter::arc(int x, int y,
186         unsigned int w, unsigned int h,
187         int a1, int a2, LColor::color col)
188 {
189         lyxerr[Debug::GUI] << "arc: " << x << ',' << y
190                 << ' ' << w << ',' << h << ", angles "
191                 << a1 << " - " << a2 << endl;
192         // LyX usings 1/64ths degree, Qt usings 1/16th
193         setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
194         return *this;
195 }
196
197
198 Painter & QLPainter::image(int x, int y,
199         int w, int h,
200         grfx::Image const & i)
201 {
202         qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
203         return *this;
204 }
205
206
207 Painter & QLPainter::text(int x, int y,
208         string const & s, LyXFont const & f)
209 {
210         return text(x, y, s.data(), s.length(), f);
211 }
212
213
214 Painter & QLPainter::text(int x, int y,
215         char c, LyXFont const & f)
216 {
217         char s[2] = { c, '\0' };
218         return text(x, y, s, 1, f);
219 }
220
221
222 void QLPainter::smallCapsText(int x, int y,
223         QString const & s, LyXFont const & f)
224 {
225         LyXFont smallfont(f);
226         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
227
228         QFont const & qfont = fontloader.get(f);
229         QFont const & qsmallfont = fontloader.get(smallfont);
230         QFontMetrics const & qfontm = QFontMetrics(qfont);
231         QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
232
233         int tmpx = x;
234         size_t ls = s.length();
235         for (size_t i = 0; i < ls; ++i) {
236                 QChar const c = s[i].upper();
237                 if (c != s[i]) {
238                         qp_->setFont(qsmallfont);
239                         qp_->drawText(tmpx, y, c);
240                         tmpx += qsmallfontm.width(c);
241                 } else {
242                         qp_->setFont(qfont);
243                         qp_->drawText(tmpx, y, c);
244                         tmpx += qfontm.width(c);
245                 }
246         }
247 }
248
249
250 Painter & QLPainter::text(int x, int y,
251         char const * s, size_t ls,
252         LyXFont const & f)
253 {
254         setPen(f.realColor());
255
256         Encoding const * encoding = f.language()->encoding();
257         if (f.isSymbolFont())
258                 encoding = encodings.symbol_encoding();
259
260         QString str;
261 #if QT_VERSION >= 300
262         str.setLength(ls);
263         for (size_t i = 0; i < ls; ++i)
264                 str[i] = QChar(encoding->ucs(s[i]));
265         // HACK: QT3 refuses to show single compose characters
266         if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
267                 str = ' ' + str;
268 #else
269         for (size_t i = 0; i < ls; ++i)
270                 str += QChar(encoding->ucs(s[i]));
271 #endif
272
273         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
274                 qp_->setFont(fontloader.get(f));
275 #if QT_VERSION >= 300
276                 // We need to draw the text as LTR as we use our own bidi
277                 // code.
278                 qp_->drawText(x, y, str, -1, QPainter::LTR);
279 #else
280                 qp_->drawText(x, y, str);
281 #endif
282         } else {
283                 smallCapsText(x, y, str, f);
284         }
285
286         if (f.underbar() == LyXFont::ON) {
287                 underline(f, x, y, font_metrics::width(s, ls, f));
288         }
289
290         return *this;
291 }