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"
18 #include "ColorCache.h"
19 #include "FontLoader.h"
21 #include "Application.h"
27 #include "frontends/font_metrics.h"
40 QLPainter::~QLPainter()
45 QLPainter::QLPainter(GuiWorkArea * qwa)
46 : Painter(), qwa_(qwa)
51 void QLPainter::start()
53 qp_.reset(new QPainter(qwa_->paintDevice()));
54 // new QPainter has default QPen:
55 current_color_ = LColor::black;
56 current_ls_ = line_solid;
57 current_lw_ = line_thin;
67 int QLPainter::paperWidth() const
69 return qwa_->viewport()->width();
73 int QLPainter::paperHeight() const
75 return qwa_->viewport()->height();
78 void QLPainter::setQPainterPen(LColor_color col,
79 Painter::line_style ls, Painter::line_width lw)
81 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
88 QPen pen = qp_.get()->pen();
90 pen.setColor(lcolorcache.get(col));
93 case line_solid: pen.setStyle(Qt::SolidLine); break;
94 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
98 case line_thin: pen.setWidth(0); break;
99 case line_thick: pen.setWidth(3); break;
102 qp_.get()->setPen(pen);
106 void QLPainter::point(int x, int y, LColor_color col)
109 qp_->drawPoint(x, y);
113 void QLPainter::line(int x1, int y1, int x2, int y2,
118 setQPainterPen(col, ls, lw);
119 qp_->drawLine(x1, y1, x2, y2);
123 void QLPainter::lines(int const * xp, int const * yp, int np,
130 // Must use new as np is not known at compile time.
131 boost::scoped_array<QPoint> points(new QPoint[np]);
133 for (int i = 0; i < np; ++i) {
134 points[i].setX(xp[i]);
135 points[i].setY(yp[i]);
138 setQPainterPen(col, ls, lw);
139 qp_->drawPolyline(points.get(), np);
143 void QLPainter::rectangle(int x, int y, int w, int h,
148 setQPainterPen(col, ls, lw);
149 qp_->drawRect(x, y, w, h);
153 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
155 qp_->fillRect(x, y, w, h, lcolorcache.get(col));
159 void QLPainter::fillPolygon(int const * xp, int const * yp,
160 int np, LColor_color col)
162 // Must use new as np is not known at compile time.
163 boost::scoped_array<QPoint> points(new QPoint[np]);
165 for (int i = 0; i < np; ++i) {
166 points[i].setX(xp[i]);
167 points[i].setY(yp[i]);
171 qp_->setBrush(lcolorcache.get(col));
172 qp_->drawPolygon(points.get(), np);
173 qp_->setBrush(Qt::NoBrush);
177 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
178 int a1, int a2, LColor_color col)
180 // LyX usings 1/64ths degree, Qt usings 1/16th
182 qp_->drawArc(x, y, w, h, a1 / 4, a2 / 4);
186 void QLPainter::image(int x, int y, int w, int h,
187 lyx::graphics::Image const & i)
189 lyx::graphics::QLImage const & qlimage =
190 static_cast<lyx::graphics::QLImage const &>(i);
192 fillRectangle(x, y, w, h, LColor::graphicsbg);
194 qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
198 void QLPainter::text(int x, int y, string const & s, LyXFont const & f)
200 return text(x, y, s.data(), s.length(), f);
204 void QLPainter::text(int x, int y, char c, LyXFont const & f)
206 char s[2] = { c, '\0' };
207 return text(x, y, s, 1, f);
211 void QLPainter::smallCapsText(int x, int y,
212 QString const & s, LyXFont const & f)
214 LyXFont smallfont(f);
215 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
217 QFont const & qfont = theApp->fontLoader().get(f);
218 QFont const & qsmallfont = theApp->fontLoader().get(smallfont);
219 QFontMetrics const & qfontm = QFontMetrics(qfont);
220 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
222 setQPainterPen(f.realColor());
224 size_t ls = s.length();
225 for (unsigned int i = 0; i < ls; ++i) {
226 QChar const c = s[i].upper();
228 qp_->setFont(qsmallfont);
229 qp_->drawText(tmpx, y, c);
230 tmpx += qsmallfontm.width(c);
233 qp_->drawText(tmpx, y, c);
234 tmpx += qfontm.width(c);
240 void QLPainter::text(int x, int y, char const * s, size_t ls,
243 Encoding const * encoding = f.language()->encoding();
244 if (f.isSymbolFont())
245 encoding = encodings.symbol_encoding();
249 for (unsigned int i = 0; i < ls; ++i)
250 str[i] = QChar(encoding->ucs(s[i]));
252 // HACK: QT3 refuses to show single compose characters
253 // Still needed with Qt4?
254 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
257 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
258 setQPainterPen(f.realColor());
259 qp_->setFont(theApp->fontLoader().get(f));
260 // We need to draw the text as LTR as we use our own bidi code.
261 qp_->setLayoutDirection(Qt::LeftToRight);
262 qp_->drawText(x, y, str);
264 smallCapsText(x, y, str, f);
267 if (f.underbar() == LyXFont::ON) {
268 underline(f, x, y, font_metrics::width(s, ls, f));
274 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
276 qp_->drawPixmap(x, y, pixmap);
280 void QLPainter::drawImage(int x, int y, QImage const & image)
282 qp_->drawImage(x, y, image);
285 } // namespace frontend