| 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 |
|
|---|
| 36 | /*!
|
|---|
| 37 | \brief Constructor
|
|---|
| 38 |
|
|---|
| 39 | Initializes the zone with no pen and a semi transparent gray brush
|
|---|
| 40 |
|
|---|
| 41 | Sets the following item attributes:
|
|---|
| 42 |
|
|---|
| 43 | - QwtPlotItem::AutoScale: false
|
|---|
| 44 | - QwtPlotItem::Legend: false
|
|---|
| 45 |
|
|---|
| 46 | The z value is initialized by 5
|
|---|
| 47 |
|
|---|
| 48 | \sa QwtPlotItem::setItemAttribute(), QwtPlotItem::setZ()
|
|---|
| 49 | */
|
|---|
| 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 |
|
|---|
| 61 | //! Destructor
|
|---|
| 62 | QwtPlotZoneItem::~QwtPlotZoneItem()
|
|---|
| 63 | {
|
|---|
| 64 | delete m_data;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | //! \return QwtPlotItem::Rtti_PlotZone
|
|---|
| 68 | int QwtPlotZoneItem::rtti() const
|
|---|
| 69 | {
|
|---|
| 70 | return QwtPlotItem::Rtti_PlotZone;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /*!
|
|---|
| 74 | Build and assign a pen
|
|---|
| 75 |
|
|---|
| 76 | In Qt5 the default pen width is 1.0 ( 0.0 in Qt4 ) what makes it
|
|---|
| 77 | non cosmetic ( see QPen::isCosmetic() ). This method has been introduced
|
|---|
| 78 | to hide this incompatibility.
|
|---|
| 79 |
|
|---|
| 80 | \param color Pen color
|
|---|
| 81 | \param width Pen width
|
|---|
| 82 | \param style Pen style
|
|---|
| 83 |
|
|---|
| 84 | \sa pen(), brush()
|
|---|
| 85 | */
|
|---|
| 86 | void QwtPlotZoneItem::setPen( const QColor& color, qreal width, Qt::PenStyle style )
|
|---|
| 87 | {
|
|---|
| 88 | setPen( QPen( color, width, style ) );
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /*!
|
|---|
| 92 | \brief Assign a pen
|
|---|
| 93 |
|
|---|
| 94 | The pen is used to draw the border lines of the zone
|
|---|
| 95 |
|
|---|
| 96 | \param pen Pen
|
|---|
| 97 | \sa pen(), setBrush()
|
|---|
| 98 | */
|
|---|
| 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 |
|
|---|
| 108 | /*!
|
|---|
| 109 | \return Pen used to draw the border lines
|
|---|
| 110 | \sa setPen(), brush()
|
|---|
| 111 | */
|
|---|
| 112 | const QPen& QwtPlotZoneItem::pen() const
|
|---|
| 113 | {
|
|---|
| 114 | return m_data->pen;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /*!
|
|---|
| 118 | \brief Assign a brush
|
|---|
| 119 |
|
|---|
| 120 | The brush is used to fill the zone
|
|---|
| 121 |
|
|---|
| 122 | \param brush Brush
|
|---|
| 123 | \sa pen(), setBrush()
|
|---|
| 124 | */
|
|---|
| 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 |
|
|---|
| 134 | /*!
|
|---|
| 135 | \return Brush used to fill the zone
|
|---|
| 136 | \sa setPen(), brush()
|
|---|
| 137 | */
|
|---|
| 138 | const QBrush& QwtPlotZoneItem::brush() const
|
|---|
| 139 | {
|
|---|
| 140 | return m_data->brush;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /*!
|
|---|
| 144 | \brief Set the orientation of the zone
|
|---|
| 145 |
|
|---|
| 146 | A horizontal zone highlights an interval of the y axis,
|
|---|
| 147 | a vertical zone of the x axis. It is unbounded in the
|
|---|
| 148 | opposite direction.
|
|---|
| 149 |
|
|---|
| 150 | \sa orientation(), QwtPlotItem::setAxes()
|
|---|
| 151 | */
|
|---|
| 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 |
|
|---|
| 161 | /*!
|
|---|
| 162 | \return Orientation of the zone
|
|---|
| 163 | \sa setOrientation()
|
|---|
| 164 | */
|
|---|
| 165 | Qt::Orientation QwtPlotZoneItem::orientation() const
|
|---|
| 166 | {
|
|---|
| 167 | return m_data->orientation;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /*!
|
|---|
| 171 | Set the interval of the zone
|
|---|
| 172 |
|
|---|
| 173 | For a horizontal zone the interval is related to the y axis,
|
|---|
| 174 | for a vertical zone it is related to the x axis.
|
|---|
| 175 |
|
|---|
| 176 | \param min Minimum of the interval
|
|---|
| 177 | \param max Maximum of the interval
|
|---|
| 178 |
|
|---|
| 179 | \sa interval(), setOrientation()
|
|---|
| 180 | */
|
|---|
| 181 | void QwtPlotZoneItem::setInterval( double min, double max )
|
|---|
| 182 | {
|
|---|
| 183 | setInterval( QwtInterval( min, max ) );
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /*!
|
|---|
| 187 | Set the interval of the zone
|
|---|
| 188 |
|
|---|
| 189 | For a horizontal zone the interval is related to the y axis,
|
|---|
| 190 | for a vertical zone it is related to the x axis.
|
|---|
| 191 |
|
|---|
| 192 | \param interval Zone interval
|
|---|
| 193 |
|
|---|
| 194 | \sa interval(), setOrientation()
|
|---|
| 195 | */
|
|---|
| 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 |
|
|---|
| 205 | /*!
|
|---|
| 206 | \return Zone interval
|
|---|
| 207 | \sa setInterval(), orientation()
|
|---|
| 208 | */
|
|---|
| 209 | QwtInterval QwtPlotZoneItem::interval() const
|
|---|
| 210 | {
|
|---|
| 211 | return m_data->interval;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /*!
|
|---|
| 215 | Draw the zone
|
|---|
| 216 |
|
|---|
| 217 | \param painter Painter
|
|---|
| 218 | \param xMap x Scale Map
|
|---|
| 219 | \param yMap y Scale Map
|
|---|
| 220 | \param canvasRect Contents rectangle of the canvas in painter coordinates
|
|---|
| 221 | */
|
|---|
| 222 |
|
|---|
| 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 |
|
|---|
| 291 | /*!
|
|---|
| 292 | The bounding rectangle is build from the interval in one direction
|
|---|
| 293 | and something invalid for the opposite direction.
|
|---|
| 294 |
|
|---|
| 295 | \return An invalid rectangle with valid boundaries in one direction
|
|---|
| 296 | */
|
|---|
| 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 | }
|
|---|