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