3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "QLPainter.h"
16 #include "GuiWorkArea.h"
19 #include "ColorCache.h"
20 #include "FontLoader.h"
25 #include "frontends/font_metrics.h"
38 QLPainter::~QLPainter()
43 QLPainter::QLPainter(GuiWorkArea * qwa)
44 : Painter(), qwa_(qwa)
49 void QLPainter::start()
51 qp_.reset(new QPainter(qwa_->paintDevice()));
52 // new QPainter has default QPen:
53 current_color_ = LColor::black;
54 current_ls_ = line_solid;
55 current_lw_ = line_thin;
65 int QLPainter::paperWidth() const
67 return qwa_->viewport()->width();
71 int QLPainter::paperHeight() const
73 return qwa_->viewport()->height();
76 void QLPainter::setQPainterPen(LColor_color col,
77 Painter::line_style ls, Painter::line_width lw)
79 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
86 QPen pen = qp_.get()->pen();
88 pen.setColor(lcolorcache.get(col));
91 case line_solid: pen.setStyle(Qt::SolidLine); break;
92 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
96 case line_thin: pen.setWidth(0); break;
97 case line_thick: pen.setWidth(3); break;
100 qp_.get()->setPen(pen);
104 void QLPainter::point(int x, int y, LColor_color col)
107 qp_->drawPoint(x, y);
111 void QLPainter::line(int x1, int y1, int x2, int y2,
116 setQPainterPen(col, ls, lw);
117 qp_->drawLine(x1, y1, x2, y2);
121 void QLPainter::lines(int const * xp, int const * yp, int np,
128 // Must use new as np is not known at compile time.
129 boost::scoped_array<QPoint> points(new QPoint[np]);
131 for (int i = 0; i < np; ++i) {
132 points[i].setX(xp[i]);
133 points[i].setY(yp[i]);
136 setQPainterPen(col, ls, lw);
137 qp_->drawPolyline(points.get(), np);
141 void QLPainter::rectangle(int x, int y, int w, int h,
146 setQPainterPen(col, ls, lw);
147 qp_->drawRect(x, y, w, h);
151 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
153 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
157 void QLPainter::fillPolygon(int const * xp, int const * yp,
158 int np, LColor_color col)
160 // Must use new as np is not known at compile time.
161 boost::scoped_array<QPoint> points(new QPoint[np]);
163 for (int i = 0; i < np; ++i) {
164 points[i].setX(xp[i]);
165 points[i].setY(yp[i]);
169 qp_->setBrush(lcolorcache.get(col));
170 qp_->drawPolygon(points.get(), np);
171 qp_->setBrush(Qt::NoBrush);
175 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
176 int a1, int a2, LColor_color col)
178 // LyX usings 1/64ths degree, Qt usings 1/16th
180 qp_->drawArc(x, y, w, h, a1 / 4, a2 / 4);
184 void QLPainter::image(int x, int y, int w, int h,
185 lyx::graphics::Image const & i)
187 lyx::graphics::QLImage const & qlimage =
188 static_cast<lyx::graphics::QLImage const &>(i);
190 fillRectangle(x, y, w, h, LColor::graphicsbg);
192 qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
196 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
198 return text(x, y, s.data(), s.length(), f);
202 void QLPainter::text(int x, int y, char c, LyXFont const & f)
204 char s[2] = { c, '\0' };
205 return text(x, y, s, 1, f);
209 void QLPainter::smallCapsText(int x, int y,
210 QString const & s, LyXFont const & f)
212 LyXFont smallfont(f);
213 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
215 QFont const & qfont = fontloader.get(f);
216 QFont const & qsmallfont = fontloader.get(smallfont);
217 QFontMetrics const & qfontm = QFontMetrics(qfont);
218 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
220 setQPainterPen(f.realColor());
222 size_t ls = s.length();
223 for (size_t i = 0; i < ls; ++i) {
224 QChar const c = s[i].upper();
226 qp_->setFont(qsmallfont);
227 qp_->drawText(tmpx, y, c);
228 tmpx += qsmallfontm.width(c);
231 qp_->drawText(tmpx, y, c);
232 tmpx += qfontm.width(c);
238 void QLPainter::text(int x, int y, char const * s, size_t ls,
241 Encoding const * encoding = f.language()->encoding();
242 if (f.isSymbolFont())
243 encoding = encodings.symbol_encoding();
247 for (size_t i = 0; i < ls; ++i)
248 str[i] = QChar(encoding->ucs(s[i]));
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)
255 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
256 setQPainterPen(f.realColor());
257 qp_->setFont(fontloader.get(f));
258 // We need to draw the text as LTR as we use our own bidi code.
259 qp_->setLayoutDirection(Qt::LeftToRight);
260 qp_->drawText(x, y, str);
262 smallCapsText(x, y, str, f);
265 if (f.underbar() == LyXFont::ON) {
266 underline(f, x, y, font_metrics::width(s, ls, f));
272 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
274 qp_->drawPixmap(x, y, pixmap);
278 void QLPainter::drawImage(int x, int y, QImage const & image)
280 qp_->drawImage(x, y, image);
283 } // namespace frontend