]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPainter.C
Lots and lots of little trivial bits.
[lyx.git] / src / frontends / qt2 / QLPainter.C
1 /**
2  * \file QLPainter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <iostream>
18 #include <boost/scoped_array.hpp>
19  
20 #include "font_metrics.h"
21 #include "support/lstrings.h" 
22 #include "lyxrc.h"
23 #include "debug.h"
24 #include "LyXView.h"
25  
26 #include "QWorkArea.h"
27 #include "qfont_loader.h"
28 #include "QLPainter.h"
29 #include "QLImage.h"
30  
31 #include <qpainter.h>
32 #include <qbrush.h> 
33 #include <qcolor.h>
34
35 using std::endl;
36
37 QLPainter::QLPainter(QWorkArea & qwa)
38         : Painter(), owner_(qwa), paint_check_(0)
39 {
40         qp_.reset(new QPainter());
41 }
42
43
44 void QLPainter::start()
45 {
46         if (++paint_check_ == 1)
47                 qp_->begin(owner_.getPixmap());
48 }
49
50  
51 void QLPainter::end()
52 {
53         if (paint_check_ == 0) {
54                 lyxerr << "ended painting whilst not painting ??" << endl;
55         } else if (--paint_check_ == 0) {
56                 qp_->end();
57         }
58 }
59
60  
61 int QLPainter::paperWidth() const
62 {
63         return owner_.workWidth();
64 }
65
66
67 int QLPainter::paperHeight() const
68 {
69         return owner_.workHeight();
70 }
71
72  
73 QPainter & QLPainter::setPen(LColor::color c, 
74         Painter::line_style ls, Painter::line_width lw)
75 {
76         QPen pen = qp_->pen();
77  
78         pen.setColor(lcolor.getX11Name(c).c_str());
79  
80         switch (ls) {
81                 case line_solid: pen.setStyle(QPen::SolidLine); break;
82                 case line_onoffdash: pen.setStyle(QPen::DotLine); break;
83         }
84  
85         switch (lw) {
86                 case line_thin: pen.setWidth(0); break;
87                 case line_thick: pen.setWidth(3); break;
88         }
89  
90         qp_->setPen(pen);
91         return *qp_;
92 }
93  
94  
95 Painter & QLPainter::point(int x, int y, LColor::color c)
96 {
97         setPen(c).drawPoint(x, y);
98         return *this;
99 }
100
101
102 Painter & QLPainter::line(int x1, int y1, 
103         int x2, int y2,
104         LColor::color col,
105         line_style ls,
106         line_width lw)
107 {
108         setPen(col, ls, lw).drawLine(x1, y1, x2, y2);
109         return *this;
110 }
111
112
113 Painter & QLPainter::lines(int const * xp, int const * yp, 
114         int np,
115         LColor::color col,
116         line_style ls,
117         line_width lw)
118 {
119         // FIXME ?
120
121         // Must use new as np is not known at compile time.
122         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
123
124         int j = 0;
125  
126         for (int i = 0; i < np; ++i) {
127                 points[j++] = xp[i];
128                 points[j++] = yp[i];
129         }
130
131         setPen(col, ls, lw).drawPolyline(QPointArray(np, points.get()));
132
133         return *this;
134 }
135
136
137 Painter & QLPainter::rectangle(int x, int y, 
138         int w, int h,
139         LColor::color col,
140         line_style ls,
141         line_width lw)
142 {
143         //lyxerr << "rectangle " << x<<","<<y << " " <<w<<","<<h<<endl; 
144         setPen(col, ls, lw).drawRect(x, y, w, h);
145         return *this;
146 }
147
148
149 Painter & QLPainter::fillRectangle(int x, int y, 
150         int w, int h,
151         LColor::color col)
152 {
153         //lyxerr << "fillRectangle " << x<<","<<y << " " <<w<<","<<h<<endl; 
154         qp_->fillRect(x, y, w, h, QColor(lcolor.getX11Name(col).c_str()));
155         return *this;
156 }
157
158
159 Painter & QLPainter::fillPolygon(int const * xp, int const * yp, 
160         int np, LColor::color col)
161 {
162         // Must use new as np is not known at compile time.
163         boost::scoped_array<QCOORD> points(new QCOORD[np * 2]);
164
165         //if (1) return *this; 
166         int j = 0;
167  
168         for (int i = 0; i < np; ++i) {
169                 points[j++] = xp[i];
170                 points[j++] = yp[i];
171         }
172
173         setPen(col);
174         qp_->setBrush(lcolor.getX11Name(col).c_str()); 
175         qp_->drawPolygon(QPointArray(np, points.get()));
176         qp_->setBrush(Qt::NoBrush);
177
178         return *this;
179 }
180
181  
182 Painter & QLPainter::arc(int x, int y,
183         unsigned int w, unsigned int h,
184         int a1, int a2, LColor::color col)
185 {
186         lyxerr[Debug::GUI] << "arc: " << x<<","<<y
187                 << " " << w<<","<<h << ", angles "
188                 << a1 << " - " << a2 << endl;
189         // LyX usings 1/64ths degree, Qt usings 1/16th 
190         setPen(col).drawArc(x, y, w, h, a1 / 4, a2 / 4);
191         return *this;
192 }
193
194  
195 Painter & QLPainter::image(int x, int y, 
196         int w, int h,
197         grfx::Image const & i)
198 {
199         qp_->drawPixmap(x, y, static_cast<grfx::QLImage const &>(i).qpixmap(), 0, 0, w, h);
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 }