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 "GuiApplication.h"
17 #include "GuiWorkArea.h"
19 #include "FontLoader.h"
21 #include "GuiApplication.h"
22 #include "qt_helpers.h"
28 #include "frontends/FontMetrics.h"
30 #include "support/unicode.h"
46 QLPainter::~QLPainter()
51 QLPainter::QLPainter(GuiWorkArea * qwa)
52 : Painter(), qwa_(qwa)
57 void QLPainter::start()
59 qp_.reset(new QPainter(qwa_->paintDevice()));
60 // new QPainter has default QPen:
61 current_color_ = LColor::black;
62 current_ls_ = line_solid;
63 current_lw_ = line_thin;
73 int QLPainter::paperWidth() const
75 return qwa_->viewport()->width();
79 int QLPainter::paperHeight() const
81 return qwa_->viewport()->height();
84 void QLPainter::setQPainterPen(LColor_color col,
85 Painter::line_style ls, Painter::line_width lw)
87 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
94 QPen pen = qp_.get()->pen();
96 pen.setColor(guiApp->colorCache().get(col));
99 case line_solid: pen.setStyle(Qt::SolidLine); break;
100 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
104 case line_thin: pen.setWidth(0); break;
105 case line_thick: pen.setWidth(3); break;
108 qp_.get()->setPen(pen);
112 void QLPainter::point(int x, int y, LColor_color col)
115 qp_->drawPoint(x, y);
119 void QLPainter::line(int x1, int y1, int x2, int y2,
124 setQPainterPen(col, ls, lw);
125 qp_->drawLine(x1, y1, x2, y2);
129 void QLPainter::lines(int const * xp, int const * yp, int np,
136 // Must use new as np is not known at compile time.
137 boost::scoped_array<QPoint> points(new QPoint[np]);
139 for (int i = 0; i < np; ++i) {
140 points[i].setX(xp[i]);
141 points[i].setY(yp[i]);
144 setQPainterPen(col, ls, lw);
145 qp_->drawPolyline(points.get(), np);
149 void QLPainter::rectangle(int x, int y, int w, int h,
154 setQPainterPen(col, ls, lw);
155 qp_->drawRect(x, y, w, h);
159 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
161 qp_->fillRect(x, y, w, h, guiApp->colorCache().get(col));
165 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
166 int a1, int a2, LColor_color col)
168 // LyX usings 1/64ths degree, Qt usings 1/16th
170 qp_->drawArc(x, y, w, h, a1 / 4, a2 / 4);
174 void QLPainter::image(int x, int y, int w, int h,
175 lyx::graphics::Image const & i)
177 lyx::graphics::QLImage const & qlimage =
178 static_cast<lyx::graphics::QLImage const &>(i);
180 fillRectangle(x, y, w, h, LColor::graphicsbg);
182 qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
186 void QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
188 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
192 void QLPainter::text(int x, int y, char_type c, LyXFont const & f)
194 char_type s[2] = { c, char_type('\0') };
195 return text(x, y, s, 1, f);
199 int QLPainter::smallCapsText(int x, int y,
200 QString const & s, LyXFont const & f)
202 LyXFont smallfont(f);
203 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
205 QFont const & qfont = guiApp->guiFontLoader().get(f);
206 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
208 setQPainterPen(f.realColor());
210 size_t ls = s.length();
211 for (unsigned int i = 0; i < ls; ++i) {
212 QChar const c = s[i].toUpper();
214 qp_->setFont(qsmallfont);
218 qp_->drawText(x + textwidth, y, c);
219 textwidth += qp_->fontMetrics().width(c);
225 void QLPainter::text(int x, int y, char_type const * s, size_t ls,
229 Encoding const * encoding = f.language()->encoding();
230 if (f.isSymbolFont())
231 encoding = encodings.symbol_encoding();
235 ucs4_to_qstring(s, ls, str);
238 // HACK: QT3 refuses to show single compose characters
239 // Still needed with Qt4?
240 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
244 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
248 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
249 setQPainterPen(f.realColor());
250 qp_->setFont(fi.font);
251 // We need to draw the text as LTR as we use our own bidi code.
252 qp_->setLayoutDirection(Qt::LeftToRight);
253 qp_->drawText(x, y, str);
254 textwidth = qp_->fontMetrics().width(str);
256 textwidth = smallCapsText(x, y, str, f);
259 if (f.underbar() == LyXFont::ON) {
260 underline(f, x, y, textwidth);
266 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
268 qp_->drawPixmap(x, y, pixmap);
272 void QLPainter::drawImage(int x, int y, QImage const & image)
274 qp_->drawImage(x, y, image);
277 } // namespace frontend