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