3 * Copyright 1998-2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
12 #pragma implementation
16 #include <boost/scoped_array.hpp>
18 #include "font_metrics.h"
19 #include "support/lstrings.h"
24 #include "QWorkArea.h"
25 #include "qfont_loader.h"
26 #include "QLPainter.h"
35 QLPainter::QLPainter(QWorkArea & qwa)
36 : Painter(), owner_(qwa), paint_check_(0)
38 qp_.reset(new QPainter());
42 void QLPainter::start()
44 if (++paint_check_ == 1)
45 qp_->begin(owner_.getPixmap());
51 if (paint_check_ == 0) {
52 lyxerr << "ended painting whilst not painting ??" << endl;
53 } else if (--paint_check_ == 0) {
59 int QLPainter::paperWidth() const
61 return owner_.workWidth();
65 int QLPainter::paperHeight() const
67 return owner_.workHeight();
71 QPainter & QLPainter::setPen(LColor::color c,
72 Painter::line_style ls, Painter::line_width lw)
74 QPen pen = qp_->pen();
76 pen.setColor(lcolor.getX11Name(c).c_str());
79 case line_solid: pen.setStyle(QPen::SolidLine); break;
80 case line_onoffdash: pen.setStyle(QPen::DashLine); break;
84 case line_thin: pen.setWidth(0); break;
85 case line_thick: pen.setWidth(3); break;
93 Painter & QLPainter::point(int x, int y, LColor::color c)
95 setPen(c).drawPoint(x, y);
100 Painter & QLPainter::line(int x1, int y1,
106 setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
111 Painter & QLPainter::lines(int const * xp, int const * yp,
119 // Must use new as np is not known at compile time.
120 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
124 for (int i = 0; i < np; ++i) {
129 setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
135 Painter & QLPainter::rectangle(int x, int y,
141 //lyxerr << "rectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
142 setPen(col, ls, lw).drawRect(x, y, w, h);
147 Painter & QLPainter::fillRectangle(int x, int y,
151 //lyxerr << "fillRectangle " << x<<","<<y << " " <<w<<","<<h<<endl;
152 qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
157 Painter & QLPainter::fillPolygon(int const * xp, int const * yp,
158 int np, LColor::color col)
160 // Must use new as np is not known at compile time.
161 boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
163 //if (1) return *this;
166 for (int i = 0; i < np; ++i) {
172 qp_->setBrush(lcolor.getX11Name(col).c_str());
173 qp_->drawPolygon(QPointArray(np, points.get()));
174 qp_->setBrush(Qt::NoBrush);
180 Painter & QLPainter::arc(int x, int y,
181 unsigned int w, unsigned int h,
182 int a1, int a2, LColor::color col)
184 lyxerr[Debug::GUI] << "arc: " << x<<","<<y
185 << " " << w<<","<<h << ", angles "
186 << a1 << " - " << a2 << endl;
187 // LyX usings 1/64ths degree, Qt usings 1/16th
188 setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
193 Painter & QLPainter::image(int x, int y,
195 grfx::Image const & i)
197 qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
202 Painter & QLPainter::text(int x, int y,
203 string const & s, LyXFont const & f)
205 return text(x, y, s.data(), s.length(), f);
209 Painter & QLPainter::text(int x, int y,
210 char c, LyXFont const & f)
212 char s[2] = { c, '\0' };
213 return text(x, y, s, 1, f);
217 void QLPainter::smallCapsText(int x, int y,
218 char const * s, size_t ls,
221 LyXFont smallfont(f);
222 smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
224 QFont const & qfont = fontloader.get(f);
225 QFont const & qsmallfont = fontloader.get(smallfont);
226 QFontMetrics const & qfontm = QFontMetrics(qfont);
227 QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
230 for (size_t i = 0; i < ls; ++i) {
231 char const c = uppercase(s[i]);
233 qp_->setFont(qsmallfont);
234 qp_->drawText(tmpx, y, &c, 1);
235 tmpx += qsmallfontm.width(&c, 1);
238 qp_->drawText(tmpx, y, &c, 1);
239 tmpx += qfontm.width(&c, 1);
245 Painter & QLPainter::text(int x, int y,
246 char const * s, size_t ls,
251 if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
252 qp_->setFont(fontloader.get(f));
253 qp_->drawText(x, y, s, ls);
255 smallCapsText(x, y, s, ls, f);
258 if (f.underbar() == LyXFont::ON) {
259 underline(f, x, y, font_metrics::width(s, ls, f));