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 "qwt_plot_zoneitem.h"
|
---|
11 | #include "qwt_painter.h"
|
---|
12 | #include "qwt_scale_map.h"
|
---|
13 | #include <qpainter.h>
|
---|
14 |
|
---|
15 | class QwtPlotZoneItem::PrivateData
|
---|
16 | {
|
---|
17 | public:
|
---|
18 | PrivateData():
|
---|
19 | orientation( Qt::Vertical ),
|
---|
20 | pen( Qt::NoPen )
|
---|
21 | {
|
---|
22 | QColor c( Qt::darkGray );
|
---|
23 | c.setAlpha( 100 );
|
---|
24 | brush = QBrush( c );
|
---|
25 | }
|
---|
26 |
|
---|
27 | Qt::Orientation orientation;
|
---|
28 | QPen pen;
|
---|
29 | QBrush brush;
|
---|
30 | QwtInterval interval;
|
---|
31 | };
|
---|
32 |
|
---|
33 | /*!
|
---|
34 | \brief Constructor
|
---|
35 |
|
---|
36 | Initializes the zone with no pen and a semi transparent gray brush
|
---|
37 |
|
---|
38 | Sets the following item attributes:
|
---|
39 |
|
---|
40 | - QwtPlotItem::AutoScale: false
|
---|
41 | - QwtPlotItem::Legend: false
|
---|
42 |
|
---|
43 | The z value is initialized by 5
|
---|
44 |
|
---|
45 | \sa QwtPlotItem::setItemAttribute(), QwtPlotItem::setZ()
|
---|
46 | */
|
---|
47 | QwtPlotZoneItem::QwtPlotZoneItem():
|
---|
48 | QwtPlotItem( QwtText( "Zone" ) )
|
---|
49 | {
|
---|
50 | d_data = new PrivateData;
|
---|
51 |
|
---|
52 | setItemAttribute( QwtPlotItem::AutoScale, false );
|
---|
53 | setItemAttribute( QwtPlotItem::Legend, false );
|
---|
54 |
|
---|
55 | setZ( 5 );
|
---|
56 | }
|
---|
57 |
|
---|
58 | //! Destructor
|
---|
59 | QwtPlotZoneItem::~QwtPlotZoneItem()
|
---|
60 | {
|
---|
61 | delete d_data;
|
---|
62 | }
|
---|
63 |
|
---|
64 | //! \return QwtPlotItem::Rtti_PlotZone
|
---|
65 | int QwtPlotZoneItem::rtti() const
|
---|
66 | {
|
---|
67 | return QwtPlotItem::Rtti_PlotZone;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*!
|
---|
71 | Build and assign a pen
|
---|
72 |
|
---|
73 | In Qt5 the default pen width is 1.0 ( 0.0 in Qt4 ) what makes it
|
---|
74 | non cosmetic ( see QPen::isCosmetic() ). This method has been introduced
|
---|
75 | to hide this incompatibility.
|
---|
76 |
|
---|
77 | \param color Pen color
|
---|
78 | \param width Pen width
|
---|
79 | \param style Pen style
|
---|
80 |
|
---|
81 | \sa pen(), brush()
|
---|
82 | */
|
---|
83 | void QwtPlotZoneItem::setPen( const QColor &color, qreal width, Qt::PenStyle style )
|
---|
84 | {
|
---|
85 | setPen( QPen( color, width, style ) );
|
---|
86 | }
|
---|
87 |
|
---|
88 | /*!
|
---|
89 | \brief Assign a pen
|
---|
90 |
|
---|
91 | The pen is used to draw the border lines of the zone
|
---|
92 |
|
---|
93 | \param pen Pen
|
---|
94 | \sa pen(), setBrush()
|
---|
95 | */
|
---|
96 | void QwtPlotZoneItem::setPen( const QPen &pen )
|
---|
97 | {
|
---|
98 | if ( d_data->pen != pen )
|
---|
99 | {
|
---|
100 | d_data->pen = pen;
|
---|
101 | itemChanged();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*!
|
---|
106 | \return Pen used to draw the border lines
|
---|
107 | \sa setPen(), brush()
|
---|
108 | */
|
---|
109 | const QPen &QwtPlotZoneItem::pen() const
|
---|
110 | {
|
---|
111 | return d_data->pen;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*!
|
---|
115 | \brief Assign a brush
|
---|
116 |
|
---|
117 | The brush is used to fill the zone
|
---|
118 |
|
---|
119 | \param brush Brush
|
---|
120 | \sa pen(), setBrush()
|
---|
121 | */
|
---|
122 | void QwtPlotZoneItem::setBrush( const QBrush &brush )
|
---|
123 | {
|
---|
124 | if ( d_data->brush != brush )
|
---|
125 | {
|
---|
126 | d_data->brush = brush;
|
---|
127 | itemChanged();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /*!
|
---|
132 | \return Brush used to fill the zone
|
---|
133 | \sa setPen(), brush()
|
---|
134 | */
|
---|
135 | const QBrush &QwtPlotZoneItem::brush() const
|
---|
136 | {
|
---|
137 | return d_data->brush;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*!
|
---|
141 | \brief Set the orientation of the zone
|
---|
142 |
|
---|
143 | A horizontal zone highlights an interval of the y axis,
|
---|
144 | a vertical zone of the x axis. It is unbounded in the
|
---|
145 | opposite direction.
|
---|
146 |
|
---|
147 | \sa orientation(), QwtPlotItem::setAxes()
|
---|
148 | */
|
---|
149 | void QwtPlotZoneItem::setOrientation( Qt::Orientation orientation )
|
---|
150 | {
|
---|
151 | if ( d_data->orientation != orientation )
|
---|
152 | {
|
---|
153 | d_data->orientation = orientation;
|
---|
154 | itemChanged();
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*!
|
---|
159 | \return Orientation of the zone
|
---|
160 | \sa setOrientation()
|
---|
161 | */
|
---|
162 | Qt::Orientation QwtPlotZoneItem::orientation()
|
---|
163 | {
|
---|
164 | return d_data->orientation;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*!
|
---|
168 | Set the interval of the zone
|
---|
169 |
|
---|
170 | For a horizontal zone the interval is related to the y axis,
|
---|
171 | for a vertical zone it is related to the x axis.
|
---|
172 |
|
---|
173 | \param min Minimum of the interval
|
---|
174 | \param max Maximum of the interval
|
---|
175 |
|
---|
176 | \sa interval(), setOrientation()
|
---|
177 | */
|
---|
178 | void QwtPlotZoneItem::setInterval( double min, double max )
|
---|
179 | {
|
---|
180 | setInterval( QwtInterval( min, max ) );
|
---|
181 | }
|
---|
182 |
|
---|
183 | /*!
|
---|
184 | Set the interval of the zone
|
---|
185 |
|
---|
186 | For a horizontal zone the interval is related to the y axis,
|
---|
187 | for a vertical zone it is related to the x axis.
|
---|
188 |
|
---|
189 | \param interval Zone interval
|
---|
190 |
|
---|
191 | \sa interval(), setOrientation()
|
---|
192 | */
|
---|
193 | void QwtPlotZoneItem::setInterval( const QwtInterval &interval )
|
---|
194 | {
|
---|
195 | if ( d_data->interval != interval )
|
---|
196 | {
|
---|
197 | d_data->interval = interval;
|
---|
198 | itemChanged();
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /*!
|
---|
203 | \return Zone interval
|
---|
204 | \sa setInterval(), orientation()
|
---|
205 | */
|
---|
206 | QwtInterval QwtPlotZoneItem::interval() const
|
---|
207 | {
|
---|
208 | return d_data->interval;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /*!
|
---|
212 | Draw the zone
|
---|
213 |
|
---|
214 | \param painter Painter
|
---|
215 | \param xMap x Scale Map
|
---|
216 | \param yMap y Scale Map
|
---|
217 | \param canvasRect Contents rectangle of the canvas in painter coordinates
|
---|
218 | */
|
---|
219 |
|
---|
220 | void QwtPlotZoneItem::draw( QPainter *painter,
|
---|
221 | const QwtScaleMap &xMap, const QwtScaleMap &yMap,
|
---|
222 | const QRectF &canvasRect ) const
|
---|
223 | {
|
---|
224 | if ( !d_data->interval.isValid() )
|
---|
225 | return;
|
---|
226 |
|
---|
227 | QPen pen = d_data->pen;
|
---|
228 | pen.setCapStyle( Qt::FlatCap );
|
---|
229 |
|
---|
230 | const bool doAlign = QwtPainter::roundingAlignment( painter );
|
---|
231 |
|
---|
232 | if ( d_data->orientation == Qt::Horizontal )
|
---|
233 | {
|
---|
234 | double y1 = yMap.transform( d_data->interval.minValue() );
|
---|
235 | double y2 = yMap.transform( d_data->interval.maxValue() );
|
---|
236 |
|
---|
237 | if ( doAlign )
|
---|
238 | {
|
---|
239 | y1 = qRound( y1 );
|
---|
240 | y2 = qRound( y2 );
|
---|
241 | }
|
---|
242 |
|
---|
243 | QRectF r( canvasRect.left(), y1, canvasRect.width(), y2 - y1 );
|
---|
244 | r = r.normalized();
|
---|
245 |
|
---|
246 | if ( ( d_data->brush.style() != Qt::NoBrush ) && ( y1 != y2 ) )
|
---|
247 | {
|
---|
248 | QwtPainter::fillRect( painter, r, d_data->brush );
|
---|
249 | }
|
---|
250 |
|
---|
251 | if ( d_data->pen.style() != Qt::NoPen )
|
---|
252 | {
|
---|
253 | painter->setPen( d_data->pen );
|
---|
254 |
|
---|
255 | QwtPainter::drawLine( painter, r.left(), r.top(), r.right(), r.top() );
|
---|
256 | QwtPainter::drawLine( painter, r.left(), r.bottom(), r.right(), r.bottom() );
|
---|
257 | }
|
---|
258 | }
|
---|
259 | else
|
---|
260 | {
|
---|
261 | double x1 = xMap.transform( d_data->interval.minValue() );
|
---|
262 | double x2 = xMap.transform( d_data->interval.maxValue() );
|
---|
263 |
|
---|
264 | if ( doAlign )
|
---|
265 | {
|
---|
266 | x1 = qRound( x1 );
|
---|
267 | x2 = qRound( x2 );
|
---|
268 | }
|
---|
269 |
|
---|
270 | QRectF r( x1, canvasRect.top(), x2 - x1, canvasRect.height() );
|
---|
271 | r = r.normalized();
|
---|
272 |
|
---|
273 | if ( ( d_data->brush.style() != Qt::NoBrush ) && ( x1 != x2 ) )
|
---|
274 | {
|
---|
275 | QwtPainter::fillRect( painter, r, d_data->brush );
|
---|
276 | }
|
---|
277 |
|
---|
278 | if ( d_data->pen.style() != Qt::NoPen )
|
---|
279 | {
|
---|
280 | painter->setPen( d_data->pen );
|
---|
281 |
|
---|
282 | QwtPainter::drawLine( painter, r.left(), r.top(), r.left(), r.bottom() );
|
---|
283 | QwtPainter::drawLine( painter, r.right(), r.top(), r.right(), r.bottom() );
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | /*!
|
---|
289 | The bounding rectangle is build from the interval in one direction
|
---|
290 | and something invalid for the opposite direction.
|
---|
291 |
|
---|
292 | \return An invalid rectangle with valid boundaries in one direction
|
---|
293 | */
|
---|
294 | QRectF QwtPlotZoneItem::boundingRect() const
|
---|
295 | {
|
---|
296 | QRectF br = QwtPlotItem::boundingRect();
|
---|
297 |
|
---|
298 | const QwtInterval &intv = d_data->interval;
|
---|
299 |
|
---|
300 | if ( intv.isValid() )
|
---|
301 | {
|
---|
302 | if ( d_data->orientation == Qt::Horizontal )
|
---|
303 | {
|
---|
304 | br.setTop( intv.minValue() );
|
---|
305 | br.setBottom( intv.maxValue() );
|
---|
306 | }
|
---|
307 | else
|
---|
308 | {
|
---|
309 | br.setLeft( intv.minValue() );
|
---|
310 | br.setRight( intv.maxValue() );
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | return br;
|
---|
315 | }
|
---|