Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
18class 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
51 : QwtPlotItem( QwtText( "Zone" ) )
52{
53 m_data = new PrivateData;
54
57
58 setZ( 5 );
59}
60
63{
64 delete m_data;
65}
66
72
86void QwtPlotZoneItem::setPen( const QColor& color, qreal width, Qt::PenStyle style )
87{
88 setPen( QPen( color, width, style ) );
89}
90
99void QwtPlotZoneItem::setPen( const QPen& pen )
100{
101 if ( m_data->pen != pen )
102 {
103 m_data->pen = pen;
104 itemChanged();
105 }
106}
107
112const QPen& QwtPlotZoneItem::pen() const
113{
114 return m_data->pen;
115}
116
125void QwtPlotZoneItem::setBrush( const QBrush& brush )
126{
127 if ( m_data->brush != brush )
128 {
129 m_data->brush = brush;
130 itemChanged();
131 }
132}
133
138const QBrush& QwtPlotZoneItem::brush() const
139{
140 return m_data->brush;
141}
142
152void QwtPlotZoneItem::setOrientation( Qt::Orientation orientation )
153{
154 if ( m_data->orientation != orientation )
155 {
156 m_data->orientation = orientation;
157 itemChanged();
158 }
159}
160
165Qt::Orientation QwtPlotZoneItem::orientation() const
166{
167 return m_data->orientation;
168}
169
181void QwtPlotZoneItem::setInterval( double min, double max )
182{
183 setInterval( QwtInterval( min, max ) );
184}
185
197{
198 if ( m_data->interval != interval )
199 {
200 m_data->interval = interval;
201 itemChanged();
202 }
203}
204
210{
211 return m_data->interval;
212}
213
223void 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
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}
A class representing an interval.
double minValue() const
double maxValue() const
bool isValid() const
static void fillRect(QPainter *, const QRectF &, const QBrush &)
Wrapper for QPainter::fillRect()
static bool roundingAlignment()
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Base class for items on the plot canvas.
void setZ(double z)
Set the z value.
void setItemAttribute(ItemAttribute, bool on=true)
@ Rtti_PlotZone
For QwtPlotZoneItem.
virtual void itemChanged()
@ Legend
The item is represented on the legend.
virtual QRectF boundingRect() const
const QBrush & brush() const
void setOrientation(Qt::Orientation)
Set the orientation of the zone.
const QPen & pen() const
void setBrush(const QBrush &)
Assign a brush.
virtual QRectF boundingRect() const override
QwtInterval interval() const
virtual ~QwtPlotZoneItem()
Destructor.
void setPen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
QwtPlotZoneItem()
Constructor.
void setInterval(double min, double max)
virtual int rtti() const override
Qt::Orientation orientation() const
virtual void draw(QPainter *, const QwtScaleMap &, const QwtScaleMap &, const QRectF &canvasRect) const override
A scale map.
double transform(double s) const
A class representing a text.
Definition qwt_text.h:52