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