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 "GuiFontMetrics.h"
20 #include "GuiApplication.h"
21 #include "qt_helpers.h"
27 #include "support/unicode.h"
35 QLPainter::QLPainter(QWidget * qwa)
38 //lyxerr << "QLPainter::start()" << endl;
39 QPainter::begin(qwa_);
40 setRenderHint(QPainter::TextAntialiasing);
41 // new QPainter has default QPen:
42 current_color_ = LColor::black;
43 current_ls_ = line_solid;
44 current_lw_ = line_thin;
48 QLPainter::~QLPainter()
51 //lyxerr << "QLPainter::end()" << endl;
55 void QLPainter::setQPainterPen(LColor_color col,
56 Painter::line_style ls, Painter::line_width lw)
58 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
65 QPen pen = QPainter::pen();
67 pen.setColor(guiApp->colorCache().get(col));
70 case line_solid: pen.setStyle(Qt::SolidLine); break;
71 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
75 case line_thin: pen.setWidth(0); break;
76 case line_thick: pen.setWidth(3); break;
83 void QLPainter::point(int x, int y, LColor_color col)
90 void QLPainter::line(int x1, int y1, int x2, int y2,
95 setQPainterPen(col, ls, lw);
96 drawLine(x1, y1, x2, y2);
100 void QLPainter::lines(int const * xp, int const * yp, int np,
107 // Must use new as np is not known at compile time.
108 boost::scoped_array<QPoint> points(new QPoint[np]);
110 for (int i = 0; i < np; ++i) {
111 points[i].setX(xp[i]);
112 points[i].setY(yp[i]);
115 setQPainterPen(col, ls, lw);
116 drawPolyline(points.get(), np);
120 void QLPainter::rectangle(int x, int y, int w, int h,
125 setQPainterPen(col, ls, lw);
126 drawRect(x, y, w, h);
130 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
132 fillRect(x, y, w, h, guiApp->colorCache().get(col));
136 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
137 int a1, int a2, LColor_color col)
139 // LyX usings 1/64ths degree, Qt usings 1/16th
141 drawArc(x, y, w, h, a1 / 4, a2 / 4);
145 void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
147 graphics::QLImage const & qlimage =
148 static_cast<graphics::QLImage const &>(i);
150 fillRectangle(x, y, w, h, LColor::graphicsbg);
152 drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
156 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
158 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
162 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
164 char_type s[2] = { c, char_type('\0') };
165 return text(x, y, s, 1, f);
169 int QLPainter::smallCapsText(int x, int y,
170 QString const & s, LyXFont const & f)
172 LyXFont smallfont(f);
173 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
175 QFont const & qfont = guiApp->guiFontLoader().get(f);
176 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
178 setQPainterPen(f.realColor());
180 size_t const ls = s.length();
181 for (unsigned int i = 0; i < ls; ++i) {
182 QChar const c = s[i].toUpper();
188 drawText(x + textwidth, y, c);
189 textwidth += fontMetrics().width(c);
195 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
199 Encoding const * encoding = f.language()->encoding();
200 if (f.isSymbolFont())
201 encoding = encodings.symbol_encoding();
205 ucs4_to_qstring(s, ls, str);
208 // HACK: QT3 refuses to show single compose characters
209 // Still needed with Qt4?
210 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
214 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
218 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
219 setQPainterPen(f.realColor());
220 if (font() != fi.font)
222 // We need to draw the text as LTR as we use our own bidi code.
223 setLayoutDirection(Qt::LeftToRight);
225 // Here we use the font width cache instead of
226 // textwidth = fontMetrics().width(str);
227 // because the above is awfully expensive on MacOSX
228 textwidth = fi.metrics->width(str);
230 textwidth = smallCapsText(x, y, str, f);
233 if (f.underbar() == LyXFont::ON) {
234 underline(f, x, y, textwidth);
241 } // namespace frontend