1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
11
#include <qapplication.h>
12
#include "qwt_painter.h"
13
#include "qwt_polygon.h"
14
#include "qwt_symbol.h"
19
The symbol is constructed with gray interior,
20
black outline with zero width, no size and style 'NoSymbol'.
22
QwtSymbol::QwtSymbol():
26
d_style(QwtSymbol::NoSymbol)
32
\param style Symbol Style
33
\param brush brush to fill the interior
34
\param pen outline pen
37
QwtSymbol::QwtSymbol(QwtSymbol::Style style, const QBrush &brush,
38
const QPen &pen, const QSize &size):
47
QwtSymbol::~QwtSymbol()
51
QwtSymbol *QwtSymbol::clone() const
53
QwtSymbol *other = new QwtSymbol;
60
\brief Specify the symbol's size
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).
66
\param h height (defaults to -1)
68
void QwtSymbol::setSize(int w, int h)
70
if ((w >= 0) && (h < 0))
75
//! Set the symbol's size
76
void QwtSymbol::setSize(const QSize &s)
85
The brush is used to draw the interior of the symbol.
88
void QwtSymbol::setBrush(const QBrush &br)
96
The pen is used to draw the symbol's outline.
100
void QwtSymbol::setPen(const QPen &pn)
106
\brief Draw the symbol at a point (x,y).
108
void QwtSymbol::draw(QPainter *painter, int x, int y) const
110
draw(painter, QPoint(x, y));
115
\brief Draw the symbol into a bounding rectangle.
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.
121
\param painter Painter
122
\param r Bounding rectangle
124
void QwtSymbol::draw(QPainter *painter, const QRect& r) const
128
case QwtSymbol::Ellipse:
129
QwtPainter::drawEllipse(painter, r);
131
case QwtSymbol::Rect:
132
QwtPainter::drawRect(painter, r);
134
case QwtSymbol::Diamond:
136
const int w2 = r.width() / 2;
137
const int h2 = r.height() / 2;
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);
147
case QwtSymbol::Cross:
149
const int w2 = r.width() / 2;
150
const int h2 = r.height() / 2;
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);
158
case QwtSymbol::XCross:
160
QwtPainter::drawLine(painter, r.left(), r.top(),
161
r.right(), r.bottom());
162
QwtPainter::drawLine(painter, r.left(), r.bottom(),
166
case QwtSymbol::Triangle:
167
case QwtSymbol::UTriangle:
169
const int w2 = r.width() / 2;
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);
178
case QwtSymbol::DTriangle:
180
const int w2 = r.width() / 2;
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);
189
case QwtSymbol::RTriangle:
191
const int h2 = r.height() / 2;
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);
200
case QwtSymbol::LTriangle:
202
const int h2 = r.height() / 2;
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);
211
case QwtSymbol::HLine:
213
const int h2 = r.height() / 2;
214
QwtPainter::drawLine(painter, r.left(), r.top() + h2,
215
r.right(), r.top() + h2);
218
case QwtSymbol::VLine:
220
const int w2 = r.width() / 2;
221
QwtPainter::drawLine(painter, r.left() + w2, r.top(),
222
r.left() + w2, r.bottom());
225
case QwtSymbol::Star1:
227
const double sqrt1_2 = 0.70710678118654752440; /* 1/sqrt(2) */
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) );
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);
243
case QwtSymbol::Star2:
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;
253
pa.setPoint(0, r.left() + (w / 2), r.top());
254
pa.setPoint(1, r.right() - (side + (w - 2 * side) / 3),
256
pa.setPoint(2, r.right() - side, r.top() + h4);
257
pa.setPoint(3, r.right() - (side + (w / 2 - side) / 3),
259
pa.setPoint(4, r.right() - side, r.top() + h34);
260
pa.setPoint(5, r.right() - (side + (w - 2 * side) / 3),
262
pa.setPoint(6, r.left() + (w / 2), r.bottom());
263
pa.setPoint(7, r.left() + (side + (w - 2 * side) / 3),
265
pa.setPoint(8, r.left() + side, r.top() + h34);
266
pa.setPoint(9, r.left() + (side + (w / 2 - side) / 3),
268
pa.setPoint(10, r.left() + side, r.top() + h4);
269
pa.setPoint(11, r.left() + (side + (w - 2 * side) / 3),
271
QwtPainter::drawPolygon(painter, pa);
274
case QwtSymbol::Hexagon:
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;
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);
297
\brief Draw the symbol at a specified point
299
\param painter Painter
300
\param pos Center of the symbol
302
void QwtSymbol::draw(QPainter *painter, const QPoint &pos) const
305
rect.setSize(QwtPainter::metricsMap().screenToLayout(d_size));
306
rect.moveCenter(pos);
308
painter->setBrush(d_brush);
309
painter->setPen(d_pen);
315
\brief Specify the symbol style
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>
337
void QwtSymbol::setStyle(QwtSymbol::Style s)
343
bool QwtSymbol::operator==(const QwtSymbol &other) const
345
return brush() == other.brush() && pen() == other.pen()
346
&& style() == other.style() && size() == other.size();
350
bool QwtSymbol::operator!=(const QwtSymbol &other) const
352
return !(*this == other);