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