]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPainter.C
Use dots instead of dashes - it jives better with LyX's idea of what a dash is
[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::DotLine); 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         //lyxerr << "rectangle " << x<<","<<y << " " <<w<<","<<h<<endl; 
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         //lyxerr << "fillRectangle " << x<<","<<y << " " <<w<<","<<h<<endl; 
152         qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
153         return *this;
154 }
155
156
157 Painter & QLPainter::fillPolygon(int const * xp, int const * yp, 
158         int np, LColor::color col)
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         //if (1) return *this; 
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);
172         qp_->setBrush(lcolor.getX11Name(col).c_str()); 
173         qp_->drawPolygon(QPointArray(np, points.get()));
174         qp_->setBrush(Qt::NoBrush);
175
176         return *this;
177 }
178
179  
180 Painter & QLPainter::arc(int x, int y,
181         unsigned int w, unsigned int h,
182         int a1, int a2, LColor::color col)
183 {
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);
189         return *this;
190 }
191
192  
193 Painter & QLPainter::image(int x, int y, 
194         int w, int h,
195         grfx::Image const & i)
196 {
197         qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
198         return *this;
199 }
200
201
202 Painter & QLPainter::text(int x, int y, 
203         string const & s, LyXFont const & f)
204 {
205         return text(x, y, s.data(), s.length(), f);
206 }
207
208
209 Painter & QLPainter::text(int x, int y, 
210         char c, LyXFont const & f)
211 {
212         char s[2] = { c, '\0' };
213         return text(x, y, s, 1, f);
214 }
215
216
217 void QLPainter::smallCapsText(int x, int y,
218         char const * s, size_t ls,
219         LyXFont const & f)
220 {
221         LyXFont smallfont(f);
222         smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
223
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);
228  
229         int tmpx = x;
230         for (size_t i = 0; i < ls; ++i) {
231                 char const c = uppercase(s[i]);
232                 if (c != s[i]) {
233                         qp_->setFont(qsmallfont);
234                         qp_->drawText(tmpx, y, &c, 1);
235                         tmpx += qsmallfontm.width(&c, 1);
236                 } else {
237                         qp_->setFont(qfont);
238                         qp_->drawText(tmpx, y, &c, 1);
239                         tmpx += qfontm.width(&c, 1);
240                 }
241         }
242 }
243
244  
245 Painter & QLPainter::text(int x, int y, 
246         char const * s, size_t ls,
247         LyXFont const & f)
248 {
249         setPen(f.color());
250
251         if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
252                 qp_->setFont(fontloader.get(f));
253                 qp_->drawText(x, y, s, ls);
254         } else {
255                 smallCapsText(x, y, s, ls, f);
256         }
257
258         if (f.underbar() == LyXFont::ON) {
259                 underline(f, x, y, font_metrics::width(s, ls, f));
260         }
261         
262         return *this;
263 }