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)
87 if (!isDrawingEnabled())
95 void QLPainter::line(int x1, int y1, int x2, int y2,
100 if (!isDrawingEnabled())
103 setQPainterPen(col, ls, lw);
104 drawLine(x1, y1, x2, y2);
108 void QLPainter::lines(int const * xp, int const * yp, int np,
115 // Must use new as np is not known at compile time.
116 boost::scoped_array<QPoint> points(new QPoint[np]);
118 for (int i = 0; i < np; ++i) {
119 points[i].setX(xp[i]);
120 points[i].setY(yp[i]);
123 if (!isDrawingEnabled())
126 setQPainterPen(col, ls, lw);
127 drawPolyline(points.get(), np);
131 void QLPainter::rectangle(int x, int y, int w, int h,
136 if (!isDrawingEnabled())
139 setQPainterPen(col, ls, lw);
140 drawRect(x, y, w, h);
144 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
146 fillRect(x, y, w, h, guiApp->colorCache().get(col));
150 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
151 int a1, int a2, LColor_color col)
153 if (!isDrawingEnabled())
156 // LyX usings 1/64ths degree, Qt usings 1/16th
158 drawArc(x, y, w, h, a1 / 4, a2 / 4);
162 void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
164 graphics::QLImage const & qlimage =
165 static_cast<graphics::QLImage const &>(i);
167 fillRectangle(x, y, w, h, LColor::graphicsbg);
169 if (!isDrawingEnabled())
172 drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
176 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
178 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
182 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
184 char_type s[2] = { c, char_type('\0') };
185 return text(x, y, s, 1, f);
189 int QLPainter::smallCapsText(int x, int y,
190 QString const & s, LyXFont const & f)
192 LyXFont smallfont(f);
193 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
195 QFont const & qfont = guiApp->guiFontLoader().get(f);
196 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
198 setQPainterPen(f.realColor());
200 size_t const ls = s.length();
201 for (unsigned int i = 0; i < ls; ++i) {
202 QChar const c = s[i].toUpper();
208 if (isDrawingEnabled())
209 drawText(x + textwidth, y, c);
210 textwidth += fontMetrics().width(c);
216 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
220 Encoding const * encoding = f.language()->encoding();
221 if (f.isSymbolFont())
222 encoding = encodings.symbol_encoding();
226 ucs4_to_qstring(s, ls, str);
229 // HACK: QT3 refuses to show single compose characters
230 // Still needed with Qt4?
231 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
235 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
239 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
240 setQPainterPen(f.realColor());
241 if (font() != fi.font)
243 // We need to draw the text as LTR as we use our own bidi code.
244 setLayoutDirection(Qt::LeftToRight);
245 if (isDrawingEnabled())
247 // Here we use the font width cache instead of
248 // textwidth = fontMetrics().width(str);
249 // because the above is awfully expensive on MacOSX
250 textwidth = fi.metrics->width(str);
252 textwidth = smallCapsText(x, y, str, f);
255 if (f.underbar() == LyXFont::ON) {
256 underline(f, x, y, textwidth);
263 } // namespace frontend