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