~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.2/src/qwt_symbol.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-04-12 23:25:58 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090412232558-3bl06x785yr8xm8u
Tags: 5.1.2-1
* New upstream release.
* Bump compat/debhelper to 7.
* Bump Standards-Version to 3.8.1. No changes needed.
* Invert Maintainers and Uploaders field.
* Fix lintian warnings:
  - dh_clean _k deprecated.
  - missing dependency on libc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2002   Uwe Rathmann
 
5
 * 
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
#include <qpainter.h>
 
11
#include <qapplication.h>
 
12
#include "qwt_painter.h"
 
13
#include "qwt_polygon.h"
 
14
#include "qwt_symbol.h"
 
15
 
 
16
/*!
 
17
  Default Constructor
 
18
 
 
19
  The symbol is constructed with gray interior,
 
20
  black outline with zero width, no size and style 'NoSymbol'.
 
21
*/
 
22
QwtSymbol::QwtSymbol(): 
 
23
    d_brush(Qt::gray), 
 
24
    d_pen(Qt::black), 
 
25
    d_size(0,0),
 
26
    d_style(QwtSymbol::NoSymbol)
 
27
{
 
28
}
 
29
 
 
30
/*!
 
31
  \brief Constructor
 
32
  \param style Symbol Style
 
33
  \param brush brush to fill the interior
 
34
  \param pen outline pen 
 
35
  \param size size
 
36
*/
 
37
QwtSymbol::QwtSymbol(QwtSymbol::Style style, const QBrush &brush, 
 
38
        const QPen &pen, const QSize &size): 
 
39
    d_brush(brush), 
 
40
    d_pen(pen), 
 
41
    d_size(size),
 
42
    d_style(style)
 
43
{
 
44
}
 
45
 
 
46
//! Destructor
 
47
QwtSymbol::~QwtSymbol()
 
48
{
 
49
}
 
50
 
 
51
QwtSymbol *QwtSymbol::clone() const
 
52
{
 
53
    QwtSymbol *other = new QwtSymbol;
 
54
    *other = *this;
 
55
 
 
56
    return other;
 
57
}
 
58
 
 
59
/*!
 
60
  \brief Specify the symbol's size
 
61
 
 
62
  If the 'h' parameter is left out or less than 0,
 
63
  and the 'w' parameter is greater than or equal to 0,
 
64
  the symbol size will be set to (w,w).
 
65
  \param w width
 
66
  \param h height (defaults to -1)
 
67
*/
 
68
void QwtSymbol::setSize(int w, int h)
 
69
{
 
70
    if ((w >= 0) && (h < 0)) 
 
71
        h = w;
 
72
    d_size = QSize(w,h);
 
73
}
 
74
 
 
75
//! Set the symbol's size
 
76
void QwtSymbol::setSize(const QSize &s)
 
77
{
 
78
    if (s.isValid()) 
 
79
        d_size = s;
 
80
}
 
81
 
 
82
/*!
 
83
  \brief Assign a brush
 
84
 
 
85
  The brush is used to draw the interior of the symbol.
 
86
  \param br brush
 
87
*/
 
88
void QwtSymbol::setBrush(const QBrush &br)
 
89
{
 
90
    d_brush = br;
 
91
}
 
92
 
 
93
/*!
 
94
  \brief Assign a pen
 
95
 
 
96
  The pen is used to draw the symbol's outline.
 
97
 
 
98
  \param pn pen
 
99
*/
 
100
void QwtSymbol::setPen(const QPen &pn)
 
101
{
 
102
    d_pen = pn;
 
103
}
 
104
 
 
105
/*!
 
106
  \brief Draw the symbol at a point (x,y).
 
107
*/
 
108
void QwtSymbol::draw(QPainter *painter, int x, int y) const
 
109
{
 
110
    draw(painter, QPoint(x, y));
 
111
}
 
112
 
 
113
 
 
114
/*!
 
115
  \brief Draw the symbol into a bounding rectangle.
 
116
 
 
117
  This function assumes that the painter has been initialized with
 
118
  brush and pen before. This allows a much more performant implementation
 
119
  when painting many symbols with the same brush and pen like in curves.
 
120
 
 
121
  \param painter Painter
 
122
  \param r Bounding rectangle
 
123
*/
 
124
void QwtSymbol::draw(QPainter *painter, const QRect& r) const
 
125
{
 
126
    switch(d_style)
 
127
    {
 
128
        case QwtSymbol::Ellipse:
 
129
            QwtPainter::drawEllipse(painter, r);
 
130
            break;
 
131
        case QwtSymbol::Rect:
 
132
            QwtPainter::drawRect(painter, r);
 
133
            break;
 
134
        case QwtSymbol::Diamond:
 
135
        {
 
136
            const int w2 = r.width() / 2;
 
137
            const int h2 = r.height() / 2;
 
138
 
 
139
            QwtPolygon pa(4);
 
140
            pa.setPoint(0, r.x() + w2, r.y());
 
141
            pa.setPoint(1, r.right(), r.y() + h2);
 
142
            pa.setPoint(2, r.x() + w2, r.bottom());
 
143
            pa.setPoint(3, r.x(), r.y() + h2);
 
144
            QwtPainter::drawPolygon(painter, pa);
 
145
            break;
 
146
        }
 
147
        case QwtSymbol::Cross:
 
148
        {
 
149
            const int w2 = r.width() / 2;
 
150
            const int h2 = r.height() / 2;
 
151
 
 
152
            QwtPainter::drawLine(painter, r.x() + w2, r.y(), 
 
153
                r.x() + w2, r.bottom());
 
154
            QwtPainter::drawLine(painter, r.x(), r.y() + h2, 
 
155
                r.right(), r.y() + h2);
 
156
            break;
 
157
        }
 
158
        case QwtSymbol::XCross:
 
159
        {
 
160
            QwtPainter::drawLine(painter, r.left(), r.top(), 
 
161
                r.right(), r.bottom());
 
162
            QwtPainter::drawLine(painter, r.left(), r.bottom(), 
 
163
                r.right(), r.top());
 
164
            break;
 
165
        }
 
166
        case QwtSymbol::Triangle:
 
167
        case QwtSymbol::UTriangle:
 
168
        {
 
169
            const int w2 = r.width() / 2;
 
170
 
 
171
            QwtPolygon pa(3);
 
172
            pa.setPoint(0, r.x() + w2, r.y());
 
173
            pa.setPoint(1, r.right(), r.bottom());
 
174
            pa.setPoint(2, r.x(), r.bottom());
 
175
            QwtPainter::drawPolygon(painter, pa);
 
176
            break;
 
177
        }
 
178
        case QwtSymbol::DTriangle:
 
179
        {
 
180
            const int w2 = r.width() / 2;
 
181
 
 
182
            QwtPolygon pa(3);
 
183
            pa.setPoint(0, r.x(), r.y());
 
184
            pa.setPoint(1, r.right(), r.y());
 
185
            pa.setPoint(2, r.x() + w2, r.bottom());
 
186
            QwtPainter::drawPolygon(painter, pa);
 
187
            break;
 
188
        }
 
189
        case QwtSymbol::RTriangle:
 
190
        {
 
191
            const int h2 = r.height() / 2;
 
192
 
 
193
            QwtPolygon pa(3);
 
194
            pa.setPoint(0, r.x(), r.y());
 
195
            pa.setPoint(1, r.right(), r.y() + h2);
 
196
            pa.setPoint(2, r.x(), r.bottom());
 
197
            QwtPainter::drawPolygon(painter, pa);
 
198
            break;
 
199
        }
 
200
        case QwtSymbol::LTriangle:
 
201
        {
 
202
            const int h2 = r.height() / 2;
 
203
 
 
204
            QwtPolygon pa(3);
 
205
            pa.setPoint(0, r.right(), r.y());
 
206
            pa.setPoint(1, r.x(), r.y() + h2);
 
207
            pa.setPoint(2, r.right(), r.bottom());
 
208
            QwtPainter::drawPolygon(painter, pa);
 
209
            break;
 
210
        }
 
211
        case QwtSymbol::HLine:
 
212
        {
 
213
            const int h2 = r.height() / 2;
 
214
            QwtPainter::drawLine(painter, r.left(), r.top() + h2,
 
215
                    r.right(), r.top() + h2);
 
216
            break;
 
217
        }
 
218
        case QwtSymbol::VLine:
 
219
        {
 
220
            const int w2 = r.width() / 2;
 
221
            QwtPainter::drawLine(painter, r.left() + w2, r.top(),
 
222
                    r.left() + w2, r.bottom());
 
223
            break;
 
224
        }
 
225
        case QwtSymbol::Star1:
 
226
        {
 
227
            const double sqrt1_2 = 0.70710678118654752440; /* 1/sqrt(2) */
 
228
 
 
229
            const int w2 = r.width() / 2;
 
230
            const int h2 = r.height() / 2;
 
231
            const int d1  = (int)( (double)w2 * (1.0 - sqrt1_2) );
 
232
 
 
233
            QwtPainter::drawLine(painter, r.left() + d1, r.top() + d1,
 
234
                    r.right() - d1, r.bottom() - d1);
 
235
            QwtPainter::drawLine(painter, r.left() + d1, r.bottom() - d1,
 
236
                    r.right() - d1, r.top() + d1);
 
237
            QwtPainter::drawLine(painter, r.left() + w2, r.top(),
 
238
                    r.left() + w2, r.bottom());
 
239
            QwtPainter::drawLine(painter, r.left(), r.top() + h2,
 
240
                    r.right(), r.top() + h2);
 
241
            break;
 
242
        }
 
243
        case QwtSymbol::Star2:
 
244
        {
 
245
            const int w = r.width();
 
246
            const int side = (int)(((double)r.width() * (1.0 - 0.866025)) /
 
247
                2.0);  // 0.866025 = cos(30°)
 
248
            const int h4 = r.height() / 4;
 
249
            const int h2 = r.height() / 2;
 
250
            const int h34 = (r.height() * 3) / 4;
 
251
 
 
252
            QwtPolygon pa(12);
 
253
            pa.setPoint(0, r.left() + (w / 2), r.top());
 
254
            pa.setPoint(1, r.right() - (side + (w - 2 * side) / 3),
 
255
                r.top() + h4 );
 
256
            pa.setPoint(2, r.right() - side, r.top() + h4);
 
257
            pa.setPoint(3, r.right() - (side + (w / 2 - side) / 3),
 
258
                r.top() + h2 );
 
259
            pa.setPoint(4, r.right() - side, r.top() + h34);
 
260
            pa.setPoint(5, r.right() - (side + (w - 2 * side) / 3),
 
261
                r.top() + h34 );
 
262
            pa.setPoint(6, r.left() + (w / 2), r.bottom());
 
263
            pa.setPoint(7, r.left() + (side + (w - 2 * side) / 3),
 
264
                r.top() + h34 );
 
265
            pa.setPoint(8, r.left() + side, r.top() + h34);
 
266
            pa.setPoint(9, r.left() + (side + (w / 2 - side) / 3),
 
267
                r.top() + h2 );
 
268
            pa.setPoint(10, r.left() + side, r.top() + h4);
 
269
            pa.setPoint(11, r.left() + (side + (w - 2 * side) / 3),
 
270
                r.top() + h4 );
 
271
            QwtPainter::drawPolygon(painter, pa);
 
272
            break;
 
273
        }
 
274
        case QwtSymbol::Hexagon:
 
275
        {
 
276
            const int w2 = r.width() / 2;
 
277
            const int side = (int)(((double)r.width() * (1.0 - 0.866025)) /
 
278
                2.0);  // 0.866025 = cos(30°)
 
279
            const int h4 = r.height() / 4;
 
280
            const int h34 = (r.height() * 3) / 4;
 
281
 
 
282
            QwtPolygon pa(6);
 
283
            pa.setPoint(0, r.left() + w2, r.top());
 
284
            pa.setPoint(1, r.right() - side, r.top() + h4);
 
285
            pa.setPoint(2, r.right() - side, r.top() + h34);
 
286
            pa.setPoint(3, r.left() + w2, r.bottom());
 
287
            pa.setPoint(4, r.left() + side, r.top() + h34);
 
288
            pa.setPoint(5, r.left() + side, r.top() + h4);
 
289
            QwtPainter::drawPolygon(painter, pa);
 
290
            break;
 
291
        }
 
292
        default:;
 
293
    }
 
294
}
 
295
 
 
296
/*!
 
297
  \brief Draw the symbol at a specified point
 
298
 
 
299
  \param painter Painter
 
300
  \param pos Center of the symbol
 
301
*/
 
302
void QwtSymbol::draw(QPainter *painter, const QPoint &pos) const
 
303
{
 
304
    QRect rect;
 
305
    rect.setSize(QwtPainter::metricsMap().screenToLayout(d_size));
 
306
    rect.moveCenter(pos);
 
307
 
 
308
    painter->setBrush(d_brush);
 
309
    painter->setPen(d_pen);
 
310
    
 
311
    draw(painter, rect);
 
312
}
 
313
 
 
314
/*!
 
315
  \brief Specify the symbol style
 
316
 
 
317
  The following styles are defined:<dl>
 
318
  <dt>NoSymbol<dd>No Style. The symbol cannot be drawn.
 
319
  <dt>Ellipse<dd>Ellipse or circle
 
320
  <dt>Rect<dd>Rectangle
 
321
  <dt>Diamond<dd>Diamond
 
322
  <dt>Triangle<dd>Triangle pointing upwards
 
323
  <dt>DTriangle<dd>Triangle pointing downwards
 
324
  <dt>UTriangle<dd>Triangle pointing upwards
 
325
  <dt>LTriangle<dd>Triangle pointing left
 
326
  <dt>RTriangle<dd>Triangle pointing right
 
327
  <dt>Cross<dd>Cross (+)
 
328
  <dt>XCross<dd>Diagonal cross (X)
 
329
  <dt>HLine<dd>Horizontal line
 
330
  <dt>VLine<dd>Vertical line
 
331
  <dt>Star1<dd>X combined with +
 
332
  <dt>Star2<dd>Six-pointed star
 
333
  <dt>Hexagon<dd>Hexagon</dl>
 
334
 
 
335
  \param s style
 
336
*/
 
337
void QwtSymbol::setStyle(QwtSymbol::Style s)
 
338
{
 
339
    d_style = s;
 
340
}
 
341
 
 
342
//! == operator
 
343
bool QwtSymbol::operator==(const QwtSymbol &other) const
 
344
{
 
345
    return brush() == other.brush() && pen() == other.pen()
 
346
            && style() == other.style() && size() == other.size();
 
347
}
 
348
 
 
349
//! != operator
 
350
bool QwtSymbol::operator!=(const QwtSymbol &other) const
 
351
{
 
352
    return !(*this == other);
 
353
}