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