Qwt User's Guide: qwt_plot_zoneitem.cpp Source File

Qwt User's Guide 6.2.0

qwt_plot_zoneitem.cpp
1 /******************************************************************************
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 "qwt_text.h"
14 #include "qwt_interval.h"
15
16 #include <qpainter.h>
17
18 class QwtPlotZoneItem::PrivateData
19 {
20 public:
21 PrivateData()
22 : orientation( Qt::Vertical )
23 , pen( Qt::NoPen )
24 {
25 QColor c( Qt::darkGray );
26 c.setAlpha( 100 );
27 brush = QBrush( c );
28 }
29
30 Qt::Orientation orientation;
31 QPen pen;
32 QBrush brush;
33 QwtInterval interval;
34 };
35
50 QwtPlotZoneItem::QwtPlotZoneItem()
51 : QwtPlotItem( QwtText( "Zone" ) )
52 {
53 m_data = new PrivateData;
54
55 setItemAttribute( QwtPlotItem::AutoScale, false );
56 setItemAttribute( QwtPlotItem::Legend, false );
57
58 setZ( 5 );
59 }
60
62 QwtPlotZoneItem::~QwtPlotZoneItem()
63 {
64 delete m_data;
65 }
66
68 int QwtPlotZoneItem::rtti() const
69 {
70 return QwtPlotItem::Rtti_PlotZone;
71 }
72
86 void QwtPlotZoneItem::setPen( const QColor& color, qreal width, Qt::PenStyle style )
87 {
88 setPen( QPen( color, width, style ) );
89 }
90
99 void QwtPlotZoneItem::setPen( const QPen& pen )
100 {
101 if ( m_data->pen != pen )
102 {
103 m_data->pen = pen;
104 itemChanged();
105 }
106 }
107
112 const QPen& QwtPlotZoneItem::pen() const
113 {
114 return m_data->pen;
115 }
116
125 void QwtPlotZoneItem::setBrush( const QBrush& brush )
126 {
127 if ( m_data->brush != brush )
128 {
129 m_data->brush = brush;
130 itemChanged();
131 }
132 }
133
138 const QBrush& QwtPlotZoneItem::brush() const
139 {
140 return m_data->brush;
141 }
142
152 void QwtPlotZoneItem::setOrientation( Qt::Orientation orientation )
153 {
154 if ( m_data->orientation != orientation )
155 {
156 m_data->orientation = orientation;
157 itemChanged();
158 }
159 }
160
165 Qt::Orientation QwtPlotZoneItem::orientation() const
166 {
167 return m_data->orientation;
168 }
169
181 void QwtPlotZoneItem::setInterval( double min, double max )
182 {
183 setInterval( QwtInterval( min, max ) );
184 }
185
196 void QwtPlotZoneItem::setInterval( const QwtInterval& interval )
197 {
198 if ( m_data->interval != interval )
199 {
200 m_data->interval = interval;
201 itemChanged();
202 }
203 }
204
209 QwtInterval QwtPlotZoneItem::interval() const
210 {
211 return m_data->interval;
212 }
213
223 void QwtPlotZoneItem::draw( QPainter* painter,
224 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
225 const QRectF& canvasRect ) const
226 {
227 if ( !m_data->interval.isValid() )
228 return;
229
230 QPen pen = m_data->pen;
231 pen.setCapStyle( Qt::FlatCap );
232
233 const bool doAlign = QwtPainter::roundingAlignment( painter );
234
235 if ( m_data->orientation == Qt::Horizontal )
236 {
237 double y1 = yMap.transform( m_data->interval.minValue() );
238 double y2 = yMap.transform( m_data->interval.maxValue() );
239
240 if ( doAlign )
241 {
242 y1 = qRound( y1 );
243 y2 = qRound( y2 );
244 }
245
246 QRectF r( canvasRect.left(), y1, canvasRect.width(), y2 - y1 );
247 r = r.normalized();
248
249 if ( ( m_data->brush.style() != Qt::NoBrush ) && ( y1 != y2 ) )
250 {
251 QwtPainter::fillRect( painter, r, m_data->brush );
252 }
253
254 if ( m_data->pen.style() != Qt::NoPen )
255 {
256 painter->setPen( m_data->pen );
257
258 QwtPainter::drawLine( painter, r.left(), r.top(), r.right(), r.top() );
259 QwtPainter::drawLine( painter, r.left(), r.bottom(), r.right(), r.bottom() );
260 }
261 }
262 else
263 {
264 double x1 = xMap.transform( m_data->interval.minValue() );
265 double x2 = xMap.transform( m_data->interval.maxValue() );
266
267 if ( doAlign )
268 {
269 x1 = qRound( x1 );
270 x2 = qRound( x2 );
271 }
272
273 QRectF r( x1, canvasRect.top(), x2 - x1, canvasRect.height() );
274 r = r.normalized();
275
276 if ( ( m_data->brush.style() != Qt::NoBrush ) && ( x1 != x2 ) )
277 {
278 QwtPainter::fillRect( painter, r, m_data->brush );
279 }
280
281 if ( m_data->pen.style() != Qt::NoPen )
282 {
283 painter->setPen( m_data->pen );
284
285 QwtPainter::drawLine( painter, r.left(), r.top(), r.left(), r.bottom() );
286 QwtPainter::drawLine( painter, r.right(), r.top(), r.right(), r.bottom() );
287 }
288 }
289 }
290
297 QRectF QwtPlotZoneItem::boundingRect() const
298 {
299 QRectF br = QwtPlotItem::boundingRect();
300
301 const QwtInterval& intv = m_data->interval;
302
303 if ( intv.isValid() )
304 {
305 if ( m_data->orientation == Qt::Horizontal )
306 {
307 br.setTop( intv.minValue() );
308 br.setBottom( intv.maxValue() );
309 }
310 else
311 {
312 br.setLeft( intv.minValue() );
313 br.setRight( intv.maxValue() );
314 }
315 }
316
317 return br;
318 }
QwtInterval
A class representing an interval.
Definition: qwt_interval.h:23
QwtInterval::minValue
double minValue() const
Definition: qwt_interval.h:192
QwtInterval::maxValue
double maxValue() const
Definition: qwt_interval.h:198
QwtInterval::isValid
bool isValid() const
Definition: qwt_interval.h:210
QwtPainter::fillRect
static void fillRect(QPainter *, const QRectF &, const QBrush &)
Wrapper for QPainter::fillRect()
Definition: qwt_painter.cpp:382
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:67
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition: qwt_plot_item.cpp:228
QwtPlotItem::Rtti_PlotZone
@ Rtti_PlotZone
For QwtPlotZoneItem.
Definition: qwt_plot_item.h:126
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition: qwt_plot_item.h:150
QwtPlotItem::AutoScale
@ AutoScale
Definition: qwt_plot_item.h:157
QwtPlotItem::boundingRect
virtual QRectF boundingRect() const
Definition: qwt_plot_item.cpp:568
QwtPlotZoneItem::brush
const QBrush & brush() const
Definition: qwt_plot_zoneitem.cpp:138
QwtPlotZoneItem::setOrientation
void setOrientation(Qt::Orientation)
Set the orientation of the zone.
Definition: qwt_plot_zoneitem.cpp:152
QwtPlotZoneItem::pen
const QPen & pen() const
Definition: qwt_plot_zoneitem.cpp:112
QwtPlotZoneItem::setBrush
void setBrush(const QBrush &)
Assign a brush.
Definition: qwt_plot_zoneitem.cpp:125
QwtPlotZoneItem::boundingRect
virtual QRectF boundingRect() const override
Definition: qwt_plot_zoneitem.cpp:297
QwtPlotZoneItem::interval
QwtInterval interval() const
Definition: qwt_plot_zoneitem.cpp:209
QwtPlotZoneItem::~QwtPlotZoneItem
virtual ~QwtPlotZoneItem()
Destructor.
Definition: qwt_plot_zoneitem.cpp:62
QwtPlotZoneItem::setPen
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_plot_zoneitem.cpp:86
QwtPlotZoneItem::QwtPlotZoneItem
QwtPlotZoneItem()
Constructor.
Definition: qwt_plot_zoneitem.cpp:50
QwtPlotZoneItem::setInterval
void setInterval(double min, double max)
Definition: qwt_plot_zoneitem.cpp:181
QwtPlotZoneItem::rtti
virtual int rtti() const override
Definition: qwt_plot_zoneitem.cpp:68
QwtPlotZoneItem::orientation
Qt::Orientation orientation() const
Definition: qwt_plot_zoneitem.cpp:165
QwtPlotZoneItem::draw
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &canvasRect) const override
Definition: qwt_plot_zoneitem.cpp:223
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtText
A class representing a text.
Definition: qwt_text.h:52