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"
20 #include "GuiApplication.h"
21 #include "qt_helpers.h"
27 #include "frontends/FontMetrics.h"
29 #include "support/unicode.h"
45 QLPainter::~QLPainter()
50 QLPainter::QLPainter(GuiWorkArea * qwa)
51 : Painter(), qwa_(qwa)
56 void QLPainter::start()
58 lyxerr << "QLPainter::start()" << endl;
59 qp_.reset(new QPainter(qwa_->paintDevice()));
60 qp_->setRenderHint(QPainter::TextAntialiasing);
61 // new QPainter has default QPen:
62 current_color_ = LColor::black;
63 current_ls_ = line_solid;
64 current_lw_ = line_thin;
71 lyxerr << "QLPainter::end()" << endl;
75 int QLPainter::paperWidth() const
77 return qwa_->viewport()->width();
81 int QLPainter::paperHeight() const
83 return qwa_->viewport()->height();
86 void QLPainter::setQPainterPen(LColor_color col,
87 Painter::line_style ls, Painter::line_width lw)
89 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
96 QPen pen = qp_.get()->pen();
98 pen.setColor(guiApp->colorCache().get(col));
101 case line_solid: pen.setStyle(Qt::SolidLine); break;
102 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
106 case line_thin: pen.setWidth(0); break;
107 case line_thick: pen.setWidth(3); break;
110 qp_.get()->setPen(pen);
114 void QLPainter::point(int x, int y, LColor_color col)
117 qp_->drawPoint(x, y);
121 void QLPainter::line(int x1, int y1, int x2, int y2,
126 setQPainterPen(col, ls, lw);
127 qp_->drawLine(x1, y1, x2, y2);
131 void QLPainter::lines(int const * xp, int const * yp, int np,
138 // Must use new as np is not known at compile time.
139 boost::scoped_array<QPoint> points(new QPoint[np]);
141 for (int i = 0; i < np; ++i) {
142 points[i].setX(xp[i]);
143 points[i].setY(yp[i]);
146 setQPainterPen(col, ls, lw);
147 qp_->drawPolyline(points.get(), np);
151 void QLPainter::rectangle(int x, int y, int w, int h,
156 setQPainterPen(col, ls, lw);
157 qp_->drawRect(x, y, w, h);
161 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
163 qp_->fillRect(x, y, w, h, guiApp->colorCache().get(col));
167 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
168 int a1, int a2, LColor_color col)
170 // LyX usings 1/64ths degree, Qt usings 1/16th
172 qp_->drawArc(x, y, w, h, a1 / 4, a2 / 4);
176 void QLPainter::image(int x, int y, int w, int h,
177 lyx::graphics::Image const & i)
179 lyx::graphics::QLImage const & qlimage =
180 static_cast<lyx::graphics::QLImage const &>(i);
182 fillRectangle(x, y, w, h, LColor::graphicsbg);
184 qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
188 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
190 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
194 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
196 char_type s[2] = { c, char_type('\0') };
197 return text(x, y, s, 1, f);
201 int QLPainter::smallCapsText(int x, int y,
202 QString const & s, LyXFont const & f)
204 LyXFont smallfont(f);
205 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
207 QFont const & qfont = guiApp->guiFontLoader().get(f);
208 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
210 setQPainterPen(f.realColor());
212 size_t ls = s.length();
213 for (unsigned int i = 0; i < ls; ++i) {
214 QChar const c = s[i].toUpper();
216 qp_->setFont(qsmallfont);
220 qp_->drawText(x + textwidth, y, c);
221 textwidth += qp_->fontMetrics().width(c);
227 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
231 Encoding const * encoding = f.language()->encoding();
232 if (f.isSymbolFont())
233 encoding = encodings.symbol_encoding();
237 ucs4_to_qstring(s, ls, str);
240 // HACK: QT3 refuses to show single compose characters
241 // Still needed with Qt4?
242 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
246 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
250 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
251 setQPainterPen(f.realColor());
252 if (qp_->font() != fi.font) {
253 qp_->setFont(fi.font);
255 // We need to draw the text as LTR as we use our own bidi code.
256 qp_->setLayoutDirection(Qt::LeftToRight);
257 qp_->drawText(x, y, str);
258 textwidth = qp_->fontMetrics().width(str);
260 textwidth = smallCapsText(x, y, str, f);
263 if (f.underbar() == LyXFont::ON) {
264 underline(f, x, y, textwidth);
271 void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
273 qp_->drawPixmap(x, y, pixmap);
277 void QLPainter::drawImage(int x, int y, QImage const & image)
279 qp_->drawImage(x, y, image);
282 } // namespace frontend