| 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_grid.h"
 | 
|---|
| 11 | #include "qwt_painter.h"
 | 
|---|
| 12 | #include "qwt_text.h"
 | 
|---|
| 13 | #include "qwt_scale_map.h"
 | 
|---|
| 14 | #include "qwt_scale_div.h"
 | 
|---|
| 15 | #include "qwt_math.h"
 | 
|---|
| 16 | #include <qpainter.h>
 | 
|---|
| 17 | #include <qpen.h>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | class QwtPlotGrid::PrivateData
 | 
|---|
| 20 | {
 | 
|---|
| 21 | public:
 | 
|---|
| 22 |     PrivateData():
 | 
|---|
| 23 |         xEnabled( true ),
 | 
|---|
| 24 |         yEnabled( true ),
 | 
|---|
| 25 |         xMinEnabled( false ),
 | 
|---|
| 26 |         yMinEnabled( false )
 | 
|---|
| 27 |     {
 | 
|---|
| 28 |     }
 | 
|---|
| 29 | 
 | 
|---|
| 30 |     bool xEnabled;
 | 
|---|
| 31 |     bool yEnabled;
 | 
|---|
| 32 |     bool xMinEnabled;
 | 
|---|
| 33 |     bool yMinEnabled;
 | 
|---|
| 34 | 
 | 
|---|
| 35 |     QwtScaleDiv xScaleDiv;
 | 
|---|
| 36 |     QwtScaleDiv yScaleDiv;
 | 
|---|
| 37 | 
 | 
|---|
| 38 |     QPen majPen;
 | 
|---|
| 39 |     QPen minPen;
 | 
|---|
| 40 | };
 | 
|---|
| 41 | 
 | 
|---|
| 42 | //! Enables major grid, disables minor grid
 | 
|---|
| 43 | QwtPlotGrid::QwtPlotGrid():
 | 
|---|
| 44 |     QwtPlotItem( QwtText( "Grid" ) )
 | 
|---|
| 45 | {
 | 
|---|
| 46 |     d_data = new PrivateData;
 | 
|---|
| 47 |     setZ( 10.0 );
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | //! Destructor
 | 
|---|
| 51 | QwtPlotGrid::~QwtPlotGrid()
 | 
|---|
| 52 | {
 | 
|---|
| 53 |     delete d_data;
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | //! \return QwtPlotItem::Rtti_PlotGrid
 | 
|---|
| 57 | int QwtPlotGrid::rtti() const
 | 
|---|
| 58 | {
 | 
|---|
| 59 |     return QwtPlotItem::Rtti_PlotGrid;
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /*!
 | 
|---|
| 63 |   \brief Enable or disable vertical gridlines
 | 
|---|
| 64 |   \param tf Enable (true) or disable
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   \sa Minor gridlines can be enabled or disabled with
 | 
|---|
| 67 |       enableXMin()
 | 
|---|
| 68 | */
 | 
|---|
| 69 | void QwtPlotGrid::enableX( bool tf )
 | 
|---|
| 70 | {
 | 
|---|
| 71 |     if ( d_data->xEnabled != tf )
 | 
|---|
| 72 |     {
 | 
|---|
| 73 |         d_data->xEnabled = tf;
 | 
|---|
| 74 |         itemChanged();
 | 
|---|
| 75 |     }
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | /*!
 | 
|---|
| 79 |   \brief Enable or disable horizontal gridlines
 | 
|---|
| 80 |   \param tf Enable (true) or disable
 | 
|---|
| 81 |   \sa Minor gridlines can be enabled or disabled with enableYMin()
 | 
|---|
| 82 | */
 | 
|---|
| 83 | void QwtPlotGrid::enableY( bool tf )
 | 
|---|
| 84 | {
 | 
|---|
| 85 |     if ( d_data->yEnabled != tf )
 | 
|---|
| 86 |     {
 | 
|---|
| 87 |         d_data->yEnabled = tf;
 | 
|---|
| 88 |         itemChanged();
 | 
|---|
| 89 |     }
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | /*!
 | 
|---|
| 93 |   \brief Enable or disable  minor vertical gridlines.
 | 
|---|
| 94 |   \param tf Enable (true) or disable
 | 
|---|
| 95 |   \sa enableX()
 | 
|---|
| 96 | */
 | 
|---|
| 97 | void QwtPlotGrid::enableXMin( bool tf )
 | 
|---|
| 98 | {
 | 
|---|
| 99 |     if ( d_data->xMinEnabled != tf )
 | 
|---|
| 100 |     {
 | 
|---|
| 101 |         d_data->xMinEnabled = tf;
 | 
|---|
| 102 |         itemChanged();
 | 
|---|
| 103 |     }
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | /*!
 | 
|---|
| 107 |   \brief Enable or disable minor horizontal gridlines
 | 
|---|
| 108 |   \param tf Enable (true) or disable
 | 
|---|
| 109 |   \sa enableY()
 | 
|---|
| 110 | */
 | 
|---|
| 111 | void QwtPlotGrid::enableYMin( bool tf )
 | 
|---|
| 112 | {
 | 
|---|
| 113 |     if ( d_data->yMinEnabled != tf )
 | 
|---|
| 114 |     {
 | 
|---|
| 115 |         d_data->yMinEnabled = tf;
 | 
|---|
| 116 |         itemChanged();
 | 
|---|
| 117 |     }
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | /*!
 | 
|---|
| 121 |   Assign an x axis scale division
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   \param scaleDiv Scale division
 | 
|---|
| 124 | */
 | 
|---|
| 125 | void QwtPlotGrid::setXDiv( const QwtScaleDiv &scaleDiv )
 | 
|---|
| 126 | {
 | 
|---|
| 127 |     if ( d_data->xScaleDiv != scaleDiv )
 | 
|---|
| 128 |     {
 | 
|---|
| 129 |         d_data->xScaleDiv = scaleDiv;
 | 
|---|
| 130 |         itemChanged();
 | 
|---|
| 131 |     }
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | /*!
 | 
|---|
| 135 |   Assign a y axis division
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   \param scaleDiv Scale division
 | 
|---|
| 138 | */
 | 
|---|
| 139 | void QwtPlotGrid::setYDiv( const QwtScaleDiv &scaleDiv )
 | 
|---|
| 140 | {
 | 
|---|
| 141 |     if ( d_data->yScaleDiv != scaleDiv )
 | 
|---|
| 142 |     {
 | 
|---|
| 143 |         d_data->yScaleDiv = scaleDiv;
 | 
|---|
| 144 |         itemChanged();
 | 
|---|
| 145 |     }
 | 
|---|
| 146 | }
 | 
|---|
| 147 | 
 | 
|---|
| 148 | /*!
 | 
|---|
| 149 |   Assign a pen for both major and minor gridlines
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   \param pen Pen
 | 
|---|
| 152 |   \sa setMajPen(), setMinPen()
 | 
|---|
| 153 | */
 | 
|---|
| 154 | void QwtPlotGrid::setPen( const QPen &pen )
 | 
|---|
| 155 | {
 | 
|---|
| 156 |     if ( d_data->majPen != pen || d_data->minPen != pen )
 | 
|---|
| 157 |     {
 | 
|---|
| 158 |         d_data->majPen = pen;
 | 
|---|
| 159 |         d_data->minPen = pen;
 | 
|---|
| 160 |         itemChanged();
 | 
|---|
| 161 |     }
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | /*!
 | 
|---|
| 165 |   Assign a pen for the major gridlines
 | 
|---|
| 166 | 
 | 
|---|
| 167 |   \param pen Pen
 | 
|---|
| 168 |   \sa majPen(), setMinPen(), setPen()
 | 
|---|
| 169 | */
 | 
|---|
| 170 | void QwtPlotGrid::setMajPen( const QPen &pen )
 | 
|---|
| 171 | {
 | 
|---|
| 172 |     if ( d_data->majPen != pen )
 | 
|---|
| 173 |     {
 | 
|---|
| 174 |         d_data->majPen = pen;
 | 
|---|
| 175 |         itemChanged();
 | 
|---|
| 176 |     }
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | /*!
 | 
|---|
| 180 |   Assign a pen for the minor gridlines
 | 
|---|
| 181 | 
 | 
|---|
| 182 |   \param pen Pen
 | 
|---|
| 183 |   \sa minPen(), setMajPen(), setPen()
 | 
|---|
| 184 | */
 | 
|---|
| 185 | void QwtPlotGrid::setMinPen( const QPen &pen )
 | 
|---|
| 186 | {
 | 
|---|
| 187 |     if ( d_data->minPen != pen )
 | 
|---|
| 188 |     {
 | 
|---|
| 189 |         d_data->minPen = pen;
 | 
|---|
| 190 |         itemChanged();
 | 
|---|
| 191 |     }
 | 
|---|
| 192 | }
 | 
|---|
| 193 | 
 | 
|---|
| 194 | /*!
 | 
|---|
| 195 |   \brief Draw the grid
 | 
|---|
| 196 | 
 | 
|---|
| 197 |   The grid is drawn into the bounding rectangle such that
 | 
|---|
| 198 |   gridlines begin and end at the rectangle's borders. The X and Y
 | 
|---|
| 199 |   maps are used to map the scale divisions into the drawing region
 | 
|---|
| 200 |   screen.
 | 
|---|
| 201 |   \param painter  Painter
 | 
|---|
| 202 |   \param xMap X axis map
 | 
|---|
| 203 |   \param yMap Y axis
 | 
|---|
| 204 |   \param canvasRect Contents rect of the plot canvas
 | 
|---|
| 205 | */
 | 
|---|
| 206 | void QwtPlotGrid::draw( QPainter *painter,
 | 
|---|
| 207 |     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
 | 
|---|
| 208 |     const QRectF &canvasRect ) const
 | 
|---|
| 209 | {
 | 
|---|
| 210 |     //  draw minor gridlines
 | 
|---|
| 211 |     QPen minPen = d_data->minPen;
 | 
|---|
| 212 |     minPen.setCapStyle( Qt::FlatCap );
 | 
|---|
| 213 | 
 | 
|---|
| 214 |     painter->setPen( minPen );
 | 
|---|
| 215 | 
 | 
|---|
| 216 |     if ( d_data->xEnabled && d_data->xMinEnabled )
 | 
|---|
| 217 |     {
 | 
|---|
| 218 |         drawLines( painter, canvasRect, Qt::Vertical, xMap,
 | 
|---|
| 219 |             d_data->xScaleDiv.ticks( QwtScaleDiv::MinorTick ) );
 | 
|---|
| 220 |         drawLines( painter, canvasRect, Qt::Vertical, xMap,
 | 
|---|
| 221 |             d_data->xScaleDiv.ticks( QwtScaleDiv::MediumTick ) );
 | 
|---|
| 222 |     }
 | 
|---|
| 223 | 
 | 
|---|
| 224 |     if ( d_data->yEnabled && d_data->yMinEnabled )
 | 
|---|
| 225 |     {
 | 
|---|
| 226 |         drawLines( painter, canvasRect, Qt::Horizontal, yMap,
 | 
|---|
| 227 |             d_data->yScaleDiv.ticks( QwtScaleDiv::MinorTick ) );
 | 
|---|
| 228 |         drawLines( painter, canvasRect, Qt::Horizontal, yMap,
 | 
|---|
| 229 |             d_data->yScaleDiv.ticks( QwtScaleDiv::MediumTick ) );
 | 
|---|
| 230 |     }
 | 
|---|
| 231 | 
 | 
|---|
| 232 |     //  draw major gridlines
 | 
|---|
| 233 |     QPen majPen = d_data->majPen;
 | 
|---|
| 234 |     majPen.setCapStyle( Qt::FlatCap );
 | 
|---|
| 235 | 
 | 
|---|
| 236 |     painter->setPen( majPen );
 | 
|---|
| 237 | 
 | 
|---|
| 238 |     if ( d_data->xEnabled )
 | 
|---|
| 239 |     {
 | 
|---|
| 240 |         drawLines( painter, canvasRect, Qt::Vertical, xMap,
 | 
|---|
| 241 |             d_data->xScaleDiv.ticks( QwtScaleDiv::MajorTick ) );
 | 
|---|
| 242 |     }
 | 
|---|
| 243 | 
 | 
|---|
| 244 |     if ( d_data->yEnabled )
 | 
|---|
| 245 |     {
 | 
|---|
| 246 |         drawLines( painter, canvasRect, Qt::Horizontal, yMap,
 | 
|---|
| 247 |             d_data->yScaleDiv.ticks( QwtScaleDiv::MajorTick ) );
 | 
|---|
| 248 |     }
 | 
|---|
| 249 | }
 | 
|---|
| 250 | 
 | 
|---|
| 251 | void QwtPlotGrid::drawLines( QPainter *painter, const QRectF &canvasRect,
 | 
|---|
| 252 |     Qt::Orientation orientation, const QwtScaleMap &scaleMap,
 | 
|---|
| 253 |     const QList<double> &values ) const
 | 
|---|
| 254 | {
 | 
|---|
| 255 |     const double x1 = canvasRect.left();
 | 
|---|
| 256 |     const double x2 = canvasRect.right() - 1.0;
 | 
|---|
| 257 |     const double y1 = canvasRect.top();
 | 
|---|
| 258 |     const double y2 = canvasRect.bottom() - 1.0;
 | 
|---|
| 259 | 
 | 
|---|
| 260 |     const bool doAlign = QwtPainter::roundingAlignment( painter );
 | 
|---|
| 261 | 
 | 
|---|
| 262 |     for ( int i = 0; i < values.count(); i++ )
 | 
|---|
| 263 |     {
 | 
|---|
| 264 |         double value = scaleMap.transform( values[i] );
 | 
|---|
| 265 |         if ( doAlign )
 | 
|---|
| 266 |             value = qRound( value );
 | 
|---|
| 267 | 
 | 
|---|
| 268 |         if ( orientation == Qt::Horizontal )
 | 
|---|
| 269 |         {
 | 
|---|
| 270 |             if ( qwtFuzzyGreaterOrEqual( value, y1 ) &&
 | 
|---|
| 271 |                 qwtFuzzyLessOrEqual( value, y2 ) )
 | 
|---|
| 272 |             {
 | 
|---|
| 273 |                 QwtPainter::drawLine( painter, x1, value, x2, value );
 | 
|---|
| 274 |             }
 | 
|---|
| 275 |         }
 | 
|---|
| 276 |         else
 | 
|---|
| 277 |         {
 | 
|---|
| 278 |             if ( qwtFuzzyGreaterOrEqual( value, x1 ) &&
 | 
|---|
| 279 |                 qwtFuzzyLessOrEqual( value, x2 ) )
 | 
|---|
| 280 |             {
 | 
|---|
| 281 |                 QwtPainter::drawLine( painter, value, y1, value, y2 );
 | 
|---|
| 282 |             }
 | 
|---|
| 283 |         }
 | 
|---|
| 284 |     }
 | 
|---|
| 285 | }
 | 
|---|
| 286 | 
 | 
|---|
| 287 | /*!
 | 
|---|
| 288 |   \return the pen for the major gridlines
 | 
|---|
| 289 |   \sa setMajPen(), setMinPen(), setPen()
 | 
|---|
| 290 | */
 | 
|---|
| 291 | const QPen &QwtPlotGrid::majPen() const
 | 
|---|
| 292 | {
 | 
|---|
| 293 |     return d_data->majPen;
 | 
|---|
| 294 | }
 | 
|---|
| 295 | 
 | 
|---|
| 296 | /*!
 | 
|---|
| 297 |   \return the pen for the minor gridlines
 | 
|---|
| 298 |   \sa setMinPen(), setMajPen(), setPen()
 | 
|---|
| 299 | */
 | 
|---|
| 300 | const QPen &QwtPlotGrid::minPen() const
 | 
|---|
| 301 | {
 | 
|---|
| 302 |     return d_data->minPen;
 | 
|---|
| 303 | }
 | 
|---|
| 304 | 
 | 
|---|
| 305 | /*!
 | 
|---|
| 306 |   \return true if vertical gridlines are enabled
 | 
|---|
| 307 |   \sa enableX()
 | 
|---|
| 308 | */
 | 
|---|
| 309 | bool QwtPlotGrid::xEnabled() const
 | 
|---|
| 310 | {
 | 
|---|
| 311 |     return d_data->xEnabled;
 | 
|---|
| 312 | }
 | 
|---|
| 313 | 
 | 
|---|
| 314 | /*!
 | 
|---|
| 315 |   \return true if minor vertical gridlines are enabled
 | 
|---|
| 316 |   \sa enableXMin()
 | 
|---|
| 317 | */
 | 
|---|
| 318 | bool QwtPlotGrid::xMinEnabled() const
 | 
|---|
| 319 | {
 | 
|---|
| 320 |     return d_data->xMinEnabled;
 | 
|---|
| 321 | }
 | 
|---|
| 322 | 
 | 
|---|
| 323 | /*!
 | 
|---|
| 324 |   \return true if horizontal gridlines are enabled
 | 
|---|
| 325 |   \sa enableY()
 | 
|---|
| 326 | */
 | 
|---|
| 327 | bool QwtPlotGrid::yEnabled() const
 | 
|---|
| 328 | {
 | 
|---|
| 329 |     return d_data->yEnabled;
 | 
|---|
| 330 | }
 | 
|---|
| 331 | 
 | 
|---|
| 332 | /*!
 | 
|---|
| 333 |   \return true if minor horizontal gridlines are enabled
 | 
|---|
| 334 |   \sa enableYMin()
 | 
|---|
| 335 | */
 | 
|---|
| 336 | bool QwtPlotGrid::yMinEnabled() const
 | 
|---|
| 337 | {
 | 
|---|
| 338 |     return d_data->yMinEnabled;
 | 
|---|
| 339 | }
 | 
|---|
| 340 | 
 | 
|---|
| 341 | 
 | 
|---|
| 342 | /*! \return the scale division of the x axis */
 | 
|---|
| 343 | const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const
 | 
|---|
| 344 | {
 | 
|---|
| 345 |     return d_data->xScaleDiv;
 | 
|---|
| 346 | }
 | 
|---|
| 347 | 
 | 
|---|
| 348 | /*! \return the scale division of the y axis */
 | 
|---|
| 349 | const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const
 | 
|---|
| 350 | {
 | 
|---|
| 351 |     return d_data->yScaleDiv;
 | 
|---|
| 352 | }
 | 
|---|
| 353 | 
 | 
|---|
| 354 | /*!
 | 
|---|
| 355 |    Update the grid to changes of the axes scale division
 | 
|---|
| 356 | 
 | 
|---|
| 357 |    \param xScaleDiv Scale division of the x-axis
 | 
|---|
| 358 |    \param yScaleDiv Scale division of the y-axis
 | 
|---|
| 359 | 
 | 
|---|
| 360 |    \sa QwtPlot::updateAxes()
 | 
|---|
| 361 | */
 | 
|---|
| 362 | void QwtPlotGrid::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
 | 
|---|
| 363 |     const QwtScaleDiv& yScaleDiv )
 | 
|---|
| 364 | {
 | 
|---|
| 365 |     setXDiv( xScaleDiv );
 | 
|---|
| 366 |     setYDiv( yScaleDiv );
 | 
|---|
| 367 | }
 | 
|---|