]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPainter.C
build fixes
[lyx.git] / src / frontends / qt2 / QLPainter.C
1 /**
2  * \file QLPainter.C
3  * Copyright 1998-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <iostream>
16 #include <boost/scoped_array.hpp>
17  
18 #include "font_metrics.h"
19 #include "support/lstrings.h" 
20 #include "lyxrc.h"
21 #include "debug.h"
22 #include "LyXView.h"
23  
24 #include "QWorkArea.h"
25 #include "qfont_loader.h"
26 #include "QLPainter.h"
27 #include "QLImage.h"
28  
29 #include <qpainter.h>
30 #include <qbrush.h> 
31 #include <qcolor.h>
32
33 using std::endl;
34
35 QLPainter::QLPainter(QWorkArea & qwa)
36         : Painter(), owner_(qwa), paint_check_(0)
37 {
38         qp_.reset(new QPainter());
39 }
40
41
42 void QLPainter::start()
43 {
44         if (++paint_check_ == 1)
45                 qp_->begin(owner_.getPixmap());
46 }
47
48  
49 void QLPainter::end()
50 {
51         if (paint_check_ == 0) {
52                 lyxerr << "ended painting whilst not painting ??" << endl;
53         } else if (--paint_check_ == 0) {
54                 qp_->end();
55         }
56 }
57
58  
59 int QLPainter::paperWidth() const
60 {
61         return owner_.workWidth();
62 }
63
64
65 int QLPainter::paperHeight() const
66 {
67         return owner_.workHeight();
68 }
69
70  
71 QPainter & QLPainter::setPen(LColor::color c, 
72         Painter::line_style ls, Painter::line_width lw)
73 {
74         QPen pen = qp_->pen();
75  
76         pen.setColor(lcolor.getX11Name(c).c_str());
77  
78         switch (ls) {
79                 case line_solid: pen.setStyle(QPen::SolidLine); break;
80                 case line_onoffdash: pen.setStyle(QPen::DashLine); break;
81         }
82  
83         switch (lw) {
84                 case line_thin: pen.setWidth(0); break;
85                 case line_thick: pen.setWidth(3); break;
86         }
87  
88         qp_->setPen(pen);
89         return *qp_;
90 }
91  
92  
93 Painter & QLPainter::point(int x, int y, LColor::color c)
94 {
95         setPen(c).drawPoint(x, y);
96         return *this;
97 }
98
99
100 Painter & QLPainter::line(int x1, int y1, 
101         int x2, int y2,
102         LColor::color col,
103         line_style ls,
104         line_width lw)
105 {
106         setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
107         return *this;
108 }
109
110
111 Painter & QLPainter::lines(int const * xp, int const * yp, 
112         int np,
113         LColor::color col,
114         line_style ls,
115         line_width lw)
116 {
117         // FIXME ?
118
119         // Must use new as np is not known at compile time.
120         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
121
122         int j = 0;
123  
124         for (int i = 0; i < np; ++i) {
125                 points[j++] = xp[i];
126                 points[j++] = yp[i];
127         }
128
129         setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
130
131         return *this;
132 }
133
134
135 Painter & QLPainter::rectangle(int x, int y, 
136         int w, int h,
137         LColor::color col,
138         line_style ls,
139         line_width lw)
140 {
141         setPen(col, ls, lw).drawRect(x, y, w, h);
142         return *this;
143 }
144
145
146 Painter & QLPainter::fillRectangle(int x, int y, 
147         int w, int h,
148         LColor::color col)
149 {
150         qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
151         return *this;
152 }
153
154
155 Painter & QLPainter::fillPolygon(int const * xp, int const * yp, 
156         int np, LColor::color col)
157 {
158         // FIXME ?
159  
160         // Must use new as np is not known at compile time.
161         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
162
163         int j = 0;
164  
165         for (int i = 0; i < np; ++i) {
166                 points[j++] = xp[i];
167                 points[j++] = yp[i];
168         }
169
170         setPen(col).drawPolygon(QPointArray(np, points.get()));
171
172         return *this;
173 }
174
175  
176 Painter & QLPainter::arc(int x, int y,
177         unsigned int w, unsigned int h,
178         int a1, int a2, LColor::color col)
179 {
180         // FIXME
181         setPen(col).drawArc(x, y, w, h, a1, a2);
182         return *this;
183 }
184
185  
186 Painter & QLPainter::image(int x, int y, 
187         int w, int h,
188         grfx::Image const & i)
189 {
190         qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
191         return *this;
192 }
193
194
195 Painter & QLPainter::text(int x, int y, 
196         string const & s, LyXFont const & f)
197 {
198         return text(x, y, s.data(), s.length(), f);
199 }
200
201
202 Painter & QLPainter::text(int x, int y, 
203         char c, LyXFont const & f)
204 {
205         char s[2] = { c, '\0' };
206         return text(x, y, s, 1, f);
207 }
208
209
210 void QLPainter::smallCapsText(int x, int y,
211         char const * s, size_t ls,
212         LyXFont const & f)
213 {
214         LyXFont smallfont(f);
215         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
216
217         QFont const & qfont = fontloader.get(f);
218         QFont const & qsmallfont = fontloader.get(smallfont);
219         QFontMetrics const & qfontm = QFontMetrics(qfont);
220         QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
221  
222         int tmpx = x;
223         for (size_t i = 0; i < ls; ++i) {
224                 char const c = uppercase(s[i]);
225                 if (c != s[i]) {
226                         qp_->setFont(qsmallfont);
227                         qp_->drawText(tmpx, y, &c, 1);
228                         tmpx += qsmallfontm.width(&c, 1);
229                 } else {
230                         qp_->setFont(qfont);
231                         qp_->drawText(tmpx, y, &c, 1);
232                         tmpx += qfontm.width(&c, 1);
233                 }
234         }
235 }
236
237  
238 Painter & QLPainter::text(int x, int y, 
239         char const * s, size_t ls,
240         LyXFont const & f)
241 {
242         setPen(f.color());
243
244         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
245                 qp_->setFont(fontloader.get(f));
246                 qp_->drawText(x, y, s, ls);
247         } else {
248                 smallCapsText(x, y, s, ls, f);
249         }
250
251         if (f.underbar() == LyXFont::ON) {
252                 underline(f, x, y, font_metrics::width(s, ls, f));
253         }
254         
255         return *this;
256 }