3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
18 #include <boost/scoped_array.hpp>
20 #include "font_metrics.h"
21 #include "support/lstrings.h"
26 #include "QWorkArea.h"
27 #include "qfont_loader.h"
28 #include "QLPainter.h"
37 QLPainter::QLPainter(QWorkArea & qwa)
38 : Painter(), owner_(qwa), paint_check_(0)
40 qp_.reset(new QPainter());
44 void QLPainter::start()
46 if (++paint_check_ == 1)
47 qp_->begin(owner_.getPixmap());
53 if (paint_check_ == 0) {
54 lyxerr << "ended painting whilst not painting ??" << endl;
55 } else if (--paint_check_ == 0) {
61 int QLPainter::paperWidth() const
63 return owner_.workWidth();
67 int QLPainter::paperHeight() const
69 return owner_.workHeight();
73 QPainter & QLPainter::setPen(LColor::color c,
74 Painter::line_style ls, Painter::line_width lw)
76 QPen pen = qp_->pen();
78 pen.setColor(lcolor.getX11Name(c).c_str());
81 case line_solid: pen.setStyle(QPen::SolidLine); break;
82 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
86 case line_thin: pen.setWidth(0); break;
87 case line_thick: pen.setWidth(3); break;
95 Painter & QLPainter::point(int x, int y, LColor::color c)
97 setPen(c).drawPoint(x, y);
102 Painter & QLPainter::line(int x1, int y1,
108 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
113 Painter & QLPainter::lines(int const * xp, int const * yp,
121 // Must use new as np is not known at compile time.
122 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
126 for (int i = 0; i < np; ++i) {
131 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
137 Painter & QLPainter::rectangle(int x, int y,
143 //lyxerr << "rectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
144 setPen(col, ls, lw).drawRect(x, y, w, h);
149 Painter & QLPainter::fillRectangle(int x, int y,
153 //lyxerr << "fillRectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
154 qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
159 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
160 int np, LColor::color col)
162 // Must use new as np is not known at compile time.
163 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
165 //if (1) return *this;
168 for (int i = 0; i < np; ++i) {
174 qp_->setBrush(lcolor.getX11Name(col).c_str());
175 qp_->drawPolygon(QPointArray(np, points.get()));
176 qp_->setBrush(Qt::NoBrush);
182 Painter & QLPainter::arc(int x, int y,
183 unsigned int w, unsigned int h,
184 int a1, int a2, LColor::color col)
186 lyxerr[Debug::GUI] << "arc: " << x<<","<<y
187 << " " << w<<","<<h << ", angles "
188 << a1 << " - " << a2 << endl;
189 // LyX usings 1/64ths degree, Qt usings 1/16th
190 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
195 Painter & QLPainter::image(int x, int y,
197 grfx::Image const & i)
199 qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
204 Painter & QLPainter::text(int x, int y,
205 string const & s, LyXFont const & f)
207 return text(x, y, s.data(), s.length(), f);
211 Painter & QLPainter::text(int x, int y,
212 char c, LyXFont const & f)
214 char s[2] = { c, '\0' };
215 return text(x, y, s, 1, f);
219 void QLPainter::smallCapsText(int x, int y,
220 char const * s, size_t ls,
223 LyXFont smallfont(f);
224 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
226 QFont const & qfont = fontloader.get(f);
227 QFont const & qsmallfont = fontloader.get(smallfont);
228 QFontMetrics const & qfontm = QFontMetrics(qfont);
229 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
232 for (size_t i = 0; i < ls; ++i) {
233 char const c = uppercase(s[i]);
235 qp_->setFont(qsmallfont);
236 qp_->drawText(tmpx, y, &c, 1);
237 tmpx += qsmallfontm.width(&c, 1);
240 qp_->drawText(tmpx, y, &c, 1);
241 tmpx += qfontm.width(&c, 1);
247 Painter & QLPainter::text(int x, int y,
248 char const * s, size_t ls,
253 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
254 qp_->setFont(fontloader.get(f));
255 qp_->drawText(x, y, s, ls);
257 smallCapsText(x, y, s, ls, f);
260 if (f.underbar() == LyXFont::ON) {
261 underline(f, x, y, font_metrics::width(s, ls, f));