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"
37 QLPainter::QLPainter(QWidget * qwa)
40 //lyxerr << "QLPainter::start()" << endl;
41 QPainter::begin(qwa_);
42 setRenderHint(QPainter::TextAntialiasing);
43 // new QPainter has default QPen:
44 current_color_ = LColor::black;
45 current_ls_ = line_solid;
46 current_lw_ = line_thin;
50 QLPainter::~QLPainter()
53 //lyxerr << "QLPainter::end()" << endl;
57 void QLPainter::setQPainterPen(LColor_color col,
58 Painter::line_style ls, Painter::line_width lw)
60 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
67 QPen pen = QPainter::pen();
69 pen.setColor(guiApp->colorCache().get(col));
72 case line_solid: pen.setStyle(Qt::SolidLine); break;
73 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
77 case line_thin: pen.setWidth(0); break;
78 case line_thick: pen.setWidth(3); break;
85 void QLPainter::point(int x, int y, LColor_color col)
92 void QLPainter::line(int x1, int y1, int x2, int y2,
97 setQPainterPen(col, ls, lw);
98 drawLine(x1, y1, x2, y2);
102 void QLPainter::lines(int const * xp, int const * yp, int np,
109 // Must use new as np is not known at compile time.
110 boost::scoped_array<QPoint> points(new QPoint[np]);
112 for (int i = 0; i < np; ++i) {
113 points[i].setX(xp[i]);
114 points[i].setY(yp[i]);
117 setQPainterPen(col, ls, lw);
118 drawPolyline(points.get(), np);
122 void QLPainter::rectangle(int x, int y, int w, int h,
127 setQPainterPen(col, ls, lw);
128 drawRect(x, y, w, h);
132 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
134 fillRect(x, y, w, h, guiApp->colorCache().get(col));
138 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
139 int a1, int a2, LColor_color col)
141 // LyX usings 1/64ths degree, Qt usings 1/16th
143 drawArc(x, y, w, h, a1 / 4, a2 / 4);
147 void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
149 graphics::QLImage const & qlimage =
150 static_cast<graphics::QLImage const &>(i);
152 fillRectangle(x, y, w, h, LColor::graphicsbg);
154 drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
158 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
160 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
164 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
166 char_type s[2] = { c, char_type('\0') };
167 return text(x, y, s, 1, f);
171 int QLPainter::smallCapsText(int x, int y,
172 QString const & s, LyXFont const & f)
174 LyXFont smallfont(f);
175 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
177 QFont const & qfont = guiApp->guiFontLoader().get(f);
178 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
180 setQPainterPen(f.realColor());
182 size_t const ls = s.length();
183 for (unsigned int i = 0; i < ls; ++i) {
184 QChar const c = s[i].toUpper();
190 drawText(x + textwidth, y, c);
191 textwidth += fontMetrics().width(c);
197 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
201 Encoding const * encoding = f.language()->encoding();
202 if (f.isSymbolFont())
203 encoding = encodings.symbol_encoding();
207 ucs4_to_qstring(s, ls, str);
210 // HACK: QT3 refuses to show single compose characters
211 // Still needed with Qt4?
212 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
216 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
220 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
221 setQPainterPen(f.realColor());
222 if (font() != fi.font)
224 // We need to draw the text as LTR as we use our own bidi code.
225 setLayoutDirection(Qt::LeftToRight);
227 // Here we use the font width cache instead of
228 // textwidth = fontMetrics().width(str);
229 // because the above is awfully expensive on MacOSX
230 textwidth = fi.metrics->width(str);
232 textwidth = smallCapsText(x, y, str, f);
235 if (f.underbar() == LyXFont::ON) {
236 underline(f, x, y, textwidth);
243 } // namespace frontend