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(QPaintDevice * device)
36 : QPainter(device), Painter()
38 // new QPainter has default QPen:
39 current_color_ = LColor::black;
40 current_ls_ = line_solid;
41 current_lw_ = line_thin;
45 QLPainter::~QLPainter()
48 //lyxerr << "QLPainter::end()" << endl;
52 void QLPainter::setQPainterPen(LColor_color col,
53 Painter::line_style ls, Painter::line_width lw)
55 if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
62 QPen pen = QPainter::pen();
64 pen.setColor(guiApp->colorCache().get(col));
67 case line_solid: pen.setStyle(Qt::SolidLine); break;
68 case line_onoffdash: pen.setStyle(Qt::DotLine); break;
72 case line_thin: pen.setWidth(0); break;
73 case line_thick: pen.setWidth(3); break;
80 void QLPainter::point(int x, int y, LColor_color col)
82 if (!isDrawingEnabled())
90 void QLPainter::line(int x1, int y1, int x2, int y2,
95 if (!isDrawingEnabled())
98 setQPainterPen(col, ls, lw);
99 drawLine(x1, y1, x2, y2);
103 void QLPainter::lines(int const * xp, int const * yp, int np,
110 // Must use new as np is not known at compile time.
111 boost::scoped_array<QPoint> points(new QPoint[np]);
113 for (int i = 0; i < np; ++i) {
114 points[i].setX(xp[i]);
115 points[i].setY(yp[i]);
118 if (!isDrawingEnabled())
121 setQPainterPen(col, ls, lw);
122 drawPolyline(points.get(), np);
126 void QLPainter::rectangle(int x, int y, int w, int h,
131 if (!isDrawingEnabled())
134 setQPainterPen(col, ls, lw);
135 drawRect(x, y, w, h);
139 void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
141 fillRect(x, y, w, h, guiApp->colorCache().get(col));
145 void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
146 int a1, int a2, LColor_color col)
148 if (!isDrawingEnabled())
151 // LyX usings 1/64ths degree, Qt usings 1/16th
153 drawArc(x, y, w, h, a1 / 4, a2 / 4);
157 void QLPainter::image(int x, int y, int w, int h, graphics::Image const & i)
159 graphics::QLImage const & qlimage =
160 static_cast<graphics::QLImage const &>(i);
162 fillRectangle(x, y, w, h, LColor::graphicsbg);
164 if (!isDrawingEnabled())
167 drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
171 int QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
173 return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
177 int QLPainter::text(int x, int y, char_type c, LyXFont const & f)
179 char_type s[2] = { c, char_type('\0') };
180 return text(x, y, s, 1, f);
184 int QLPainter::smallCapsText(int x, int y,
185 QString const & s, LyXFont const & f)
187 LyXFont smallfont(f);
188 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
190 QFont const & qfont = guiApp->guiFontLoader().get(f);
191 QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont);
193 setQPainterPen(f.realColor());
195 size_t const ls = s.length();
196 for (unsigned int i = 0; i < ls; ++i) {
197 QChar const c = s[i].toUpper();
203 if (isDrawingEnabled())
204 drawText(x + textwidth, y, c);
205 textwidth += fontMetrics().width(c);
211 int QLPainter::text(int x, int y, char_type const * s, size_t ls,
214 // Caution: The following ucs4_to_qstring conversion works for
215 // symbol fonts only because it is no real conversion but a simple
219 ucs4_to_qstring(s, ls, str);
222 // HACK: QT3 refuses to show single compose characters
223 // Still needed with Qt4?
224 if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
228 QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f);
232 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
233 setQPainterPen(f.realColor());
234 if (font() != fi.font)
236 // We need to draw the text as LTR as we use our own bidi code.
237 setLayoutDirection(Qt::LeftToRight);
238 if (isDrawingEnabled()) {
239 if (lyxerr.debugging(Debug::PAINTING)) {
240 lyxerr[Debug::PAINTING] << "draw " << std::string(str.toUtf8())
241 << " at " << x << "," << y << std::endl;
245 // Here we use the font width cache instead of
246 // textwidth = fontMetrics().width(str);
247 // because the above is awfully expensive on MacOSX
248 textwidth = fi.metrics->width(str);
250 textwidth = smallCapsText(x, y, str, f);
253 if (f.underbar() == LyXFont::ON) {
254 underline(f, x, y, textwidth);
261 } // namespace frontend