| 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_scaleitem.h"
|
|---|
| 11 | #include "qwt_plot.h"
|
|---|
| 12 | #include "qwt_scale_map.h"
|
|---|
| 13 | #include "qwt_interval.h"
|
|---|
| 14 | #include "qwt_text.h"
|
|---|
| 15 |
|
|---|
| 16 | #include <qpalette.h>
|
|---|
| 17 | #include <qpainter.h>
|
|---|
| 18 |
|
|---|
| 19 | class QwtPlotScaleItem::PrivateData
|
|---|
| 20 | {
|
|---|
| 21 | public:
|
|---|
| 22 | PrivateData()
|
|---|
| 23 | : position( 0.0 )
|
|---|
| 24 | , borderDistance( -1 )
|
|---|
| 25 | , scaleDivFromAxis( true )
|
|---|
| 26 | , scaleDraw( new QwtScaleDraw() )
|
|---|
| 27 | {
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | ~PrivateData()
|
|---|
| 31 | {
|
|---|
| 32 | delete scaleDraw;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | QwtInterval scaleInterval( const QRectF&,
|
|---|
| 36 | const QwtScaleMap&, const QwtScaleMap& ) const;
|
|---|
| 37 |
|
|---|
| 38 | QPalette palette;
|
|---|
| 39 | QFont font;
|
|---|
| 40 | double position;
|
|---|
| 41 | int borderDistance;
|
|---|
| 42 | bool scaleDivFromAxis;
|
|---|
| 43 | QwtScaleDraw* scaleDraw;
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | QwtInterval QwtPlotScaleItem::PrivateData::scaleInterval( const QRectF& canvasRect,
|
|---|
| 47 | const QwtScaleMap& xMap, const QwtScaleMap& yMap ) const
|
|---|
| 48 | {
|
|---|
| 49 | QwtInterval interval;
|
|---|
| 50 | if ( scaleDraw->orientation() == Qt::Horizontal )
|
|---|
| 51 | {
|
|---|
| 52 | interval.setMinValue( xMap.invTransform( canvasRect.left() ) );
|
|---|
| 53 | interval.setMaxValue( xMap.invTransform( canvasRect.right() - 1 ) );
|
|---|
| 54 | }
|
|---|
| 55 | else
|
|---|
| 56 | {
|
|---|
| 57 | interval.setMinValue( yMap.invTransform( canvasRect.bottom() - 1 ) );
|
|---|
| 58 | interval.setMaxValue( yMap.invTransform( canvasRect.top() ) );
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | return interval;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /*!
|
|---|
| 65 | \brief Constructor for scale item at the position pos.
|
|---|
| 66 |
|
|---|
| 67 | \param alignment In case of QwtScaleDraw::BottomScale or QwtScaleDraw::TopScale
|
|---|
| 68 | the scale item is corresponding to the xAxis(),
|
|---|
| 69 | otherwise it corresponds to the yAxis().
|
|---|
| 70 |
|
|---|
| 71 | \param pos x or y position, depending on the corresponding axis.
|
|---|
| 72 |
|
|---|
| 73 | \sa setPosition(), setAlignment()
|
|---|
| 74 | */
|
|---|
| 75 | QwtPlotScaleItem::QwtPlotScaleItem(
|
|---|
| 76 | QwtScaleDraw::Alignment alignment, const double pos )
|
|---|
| 77 | : QwtPlotItem( QwtText( "Scale" ) )
|
|---|
| 78 | {
|
|---|
| 79 | m_data = new PrivateData;
|
|---|
| 80 | m_data->position = pos;
|
|---|
| 81 | m_data->scaleDraw->setAlignment( alignment );
|
|---|
| 82 |
|
|---|
| 83 | setItemInterest( QwtPlotItem::ScaleInterest, true );
|
|---|
| 84 | setZ( 11.0 );
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | //! Destructor
|
|---|
| 88 | QwtPlotScaleItem::~QwtPlotScaleItem()
|
|---|
| 89 | {
|
|---|
| 90 | delete m_data;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | //! \return QwtPlotItem::Rtti_PlotScale
|
|---|
| 94 | int QwtPlotScaleItem::rtti() const
|
|---|
| 95 | {
|
|---|
| 96 | return QwtPlotItem::Rtti_PlotScale;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /*!
|
|---|
| 100 | \brief Assign a scale division
|
|---|
| 101 |
|
|---|
| 102 | When assigning a scaleDiv the scale division won't be synchronized
|
|---|
| 103 | with the corresponding axis anymore.
|
|---|
| 104 |
|
|---|
| 105 | \param scaleDiv Scale division
|
|---|
| 106 | \sa scaleDiv(), setScaleDivFromAxis(), isScaleDivFromAxis()
|
|---|
| 107 | */
|
|---|
| 108 | void QwtPlotScaleItem::setScaleDiv( const QwtScaleDiv& scaleDiv )
|
|---|
| 109 | {
|
|---|
| 110 | m_data->scaleDivFromAxis = false;
|
|---|
| 111 | m_data->scaleDraw->setScaleDiv( scaleDiv );
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | //! \return Scale division
|
|---|
| 115 | const QwtScaleDiv& QwtPlotScaleItem::scaleDiv() const
|
|---|
| 116 | {
|
|---|
| 117 | return m_data->scaleDraw->scaleDiv();
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /*!
|
|---|
| 121 | Enable/Disable the synchronization of the scale division with
|
|---|
| 122 | the corresponding axis.
|
|---|
| 123 |
|
|---|
| 124 | \param on true/false
|
|---|
| 125 | \sa isScaleDivFromAxis()
|
|---|
| 126 | */
|
|---|
| 127 | void QwtPlotScaleItem::setScaleDivFromAxis( bool on )
|
|---|
| 128 | {
|
|---|
| 129 | if ( on != m_data->scaleDivFromAxis )
|
|---|
| 130 | {
|
|---|
| 131 | m_data->scaleDivFromAxis = on;
|
|---|
| 132 | if ( on )
|
|---|
| 133 | {
|
|---|
| 134 | const QwtPlot* plt = plot();
|
|---|
| 135 | if ( plt )
|
|---|
| 136 | {
|
|---|
| 137 | updateScaleDiv( plt->axisScaleDiv( xAxis() ),
|
|---|
| 138 | plt->axisScaleDiv( yAxis() ) );
|
|---|
| 139 | itemChanged();
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /*!
|
|---|
| 146 | \return True, if the synchronization of the scale division with
|
|---|
| 147 | the corresponding axis is enabled.
|
|---|
| 148 | \sa setScaleDiv(), setScaleDivFromAxis()
|
|---|
| 149 | */
|
|---|
| 150 | bool QwtPlotScaleItem::isScaleDivFromAxis() const
|
|---|
| 151 | {
|
|---|
| 152 | return m_data->scaleDivFromAxis;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /*!
|
|---|
| 156 | Set the palette
|
|---|
| 157 | \sa QwtAbstractScaleDraw::draw(), palette()
|
|---|
| 158 | */
|
|---|
| 159 | void QwtPlotScaleItem::setPalette( const QPalette& palette )
|
|---|
| 160 | {
|
|---|
| 161 | if ( palette != m_data->palette )
|
|---|
| 162 | {
|
|---|
| 163 | m_data->palette = palette;
|
|---|
| 164 |
|
|---|
| 165 | legendChanged();
|
|---|
| 166 | itemChanged();
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /*!
|
|---|
| 171 | \return palette
|
|---|
| 172 | \sa setPalette()
|
|---|
| 173 | */
|
|---|
| 174 | QPalette QwtPlotScaleItem::palette() const
|
|---|
| 175 | {
|
|---|
| 176 | return m_data->palette;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /*!
|
|---|
| 180 | Change the tick label font
|
|---|
| 181 | \sa font()
|
|---|
| 182 | */
|
|---|
| 183 | void QwtPlotScaleItem::setFont( const QFont& font )
|
|---|
| 184 | {
|
|---|
| 185 | if ( font != m_data->font )
|
|---|
| 186 | {
|
|---|
| 187 | m_data->font = font;
|
|---|
| 188 | itemChanged();
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /*!
|
|---|
| 193 | \return tick label font
|
|---|
| 194 | \sa setFont()
|
|---|
| 195 | */
|
|---|
| 196 | QFont QwtPlotScaleItem::font() const
|
|---|
| 197 | {
|
|---|
| 198 | return m_data->font;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /*!
|
|---|
| 202 | \brief Set a scale draw
|
|---|
| 203 |
|
|---|
| 204 | \param scaleDraw object responsible for drawing scales.
|
|---|
| 205 |
|
|---|
| 206 | The main use case for replacing the default QwtScaleDraw is
|
|---|
| 207 | to overload QwtAbstractScaleDraw::label, to replace or swallow
|
|---|
| 208 | tick labels.
|
|---|
| 209 |
|
|---|
| 210 | \sa scaleDraw()
|
|---|
| 211 | */
|
|---|
| 212 | void QwtPlotScaleItem::setScaleDraw( QwtScaleDraw* scaleDraw )
|
|---|
| 213 | {
|
|---|
| 214 | if ( scaleDraw == NULL )
|
|---|
| 215 | return;
|
|---|
| 216 |
|
|---|
| 217 | if ( scaleDraw != m_data->scaleDraw )
|
|---|
| 218 | delete m_data->scaleDraw;
|
|---|
| 219 |
|
|---|
| 220 | m_data->scaleDraw = scaleDraw;
|
|---|
| 221 |
|
|---|
| 222 | const QwtPlot* plt = plot();
|
|---|
| 223 | if ( plt )
|
|---|
| 224 | {
|
|---|
| 225 | updateScaleDiv( plt->axisScaleDiv( xAxis() ),
|
|---|
| 226 | plt->axisScaleDiv( yAxis() ) );
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | itemChanged();
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /*!
|
|---|
| 233 | \return Scale draw
|
|---|
| 234 | \sa setScaleDraw()
|
|---|
| 235 | */
|
|---|
| 236 | const QwtScaleDraw* QwtPlotScaleItem::scaleDraw() const
|
|---|
| 237 | {
|
|---|
| 238 | return m_data->scaleDraw;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /*!
|
|---|
| 242 | \return Scale draw
|
|---|
| 243 | \sa setScaleDraw()
|
|---|
| 244 | */
|
|---|
| 245 | QwtScaleDraw* QwtPlotScaleItem::scaleDraw()
|
|---|
| 246 | {
|
|---|
| 247 | return m_data->scaleDraw;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /*!
|
|---|
| 251 | Change the position of the scale
|
|---|
| 252 |
|
|---|
| 253 | The position is interpreted as y value for horizontal axes
|
|---|
| 254 | and as x value for vertical axes.
|
|---|
| 255 |
|
|---|
| 256 | The border distance is set to -1.
|
|---|
| 257 |
|
|---|
| 258 | \param pos New position
|
|---|
| 259 | \sa position(), setAlignment()
|
|---|
| 260 | */
|
|---|
| 261 | void QwtPlotScaleItem::setPosition( double pos )
|
|---|
| 262 | {
|
|---|
| 263 | if ( m_data->position != pos )
|
|---|
| 264 | {
|
|---|
| 265 | m_data->position = pos;
|
|---|
| 266 | m_data->borderDistance = -1;
|
|---|
| 267 | itemChanged();
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /*!
|
|---|
| 272 | \return Position of the scale
|
|---|
| 273 | \sa setPosition(), setAlignment()
|
|---|
| 274 | */
|
|---|
| 275 | double QwtPlotScaleItem::position() const
|
|---|
| 276 | {
|
|---|
| 277 | return m_data->position;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /*!
|
|---|
| 281 | \brief Align the scale to the canvas
|
|---|
| 282 |
|
|---|
| 283 | If distance is >= 0 the scale will be aligned to a
|
|---|
| 284 | border of the contents rectangle of the canvas. If
|
|---|
| 285 | alignment() is QwtScaleDraw::LeftScale, the scale will
|
|---|
| 286 | be aligned to the right border, if it is QwtScaleDraw::TopScale
|
|---|
| 287 | it will be aligned to the bottom (and vice versa),
|
|---|
| 288 |
|
|---|
| 289 | If distance is < 0 the scale will be at the position().
|
|---|
| 290 |
|
|---|
| 291 | \param distance Number of pixels between the canvas border and the
|
|---|
| 292 | backbone of the scale.
|
|---|
| 293 |
|
|---|
| 294 | \sa setPosition(), borderDistance()
|
|---|
| 295 | */
|
|---|
| 296 | void QwtPlotScaleItem::setBorderDistance( int distance )
|
|---|
| 297 | {
|
|---|
| 298 | if ( distance < 0 )
|
|---|
| 299 | distance = -1;
|
|---|
| 300 |
|
|---|
| 301 | if ( distance != m_data->borderDistance )
|
|---|
| 302 | {
|
|---|
| 303 | m_data->borderDistance = distance;
|
|---|
| 304 | itemChanged();
|
|---|
| 305 | }
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /*!
|
|---|
| 309 | \return Distance from a canvas border
|
|---|
| 310 | \sa setBorderDistance(), setPosition()
|
|---|
| 311 | */
|
|---|
| 312 | int QwtPlotScaleItem::borderDistance() const
|
|---|
| 313 | {
|
|---|
| 314 | return m_data->borderDistance;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /*!
|
|---|
| 318 | Change the alignment of the scale
|
|---|
| 319 |
|
|---|
| 320 | The alignment sets the orientation of the scale and the position of
|
|---|
| 321 | the ticks:
|
|---|
| 322 |
|
|---|
| 323 | - QwtScaleDraw::BottomScale: horizontal, ticks below
|
|---|
| 324 | - QwtScaleDraw::TopScale: horizontal, ticks above
|
|---|
| 325 | - QwtScaleDraw::LeftScale: vertical, ticks left
|
|---|
| 326 | - QwtScaleDraw::RightScale: vertical, ticks right
|
|---|
| 327 |
|
|---|
| 328 | For horizontal scales the position corresponds to QwtPlotItem::yAxis(),
|
|---|
| 329 | otherwise to QwtPlotItem::xAxis().
|
|---|
| 330 |
|
|---|
| 331 | \sa scaleDraw(), QwtScaleDraw::alignment(), setPosition()
|
|---|
| 332 | */
|
|---|
| 333 | void QwtPlotScaleItem::setAlignment( QwtScaleDraw::Alignment alignment )
|
|---|
| 334 | {
|
|---|
| 335 | QwtScaleDraw* sd = m_data->scaleDraw;
|
|---|
| 336 | if ( sd->alignment() != alignment )
|
|---|
| 337 | {
|
|---|
| 338 | sd->setAlignment( alignment );
|
|---|
| 339 | itemChanged();
|
|---|
| 340 | }
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /*!
|
|---|
| 344 | \brief Draw the scale
|
|---|
| 345 | */
|
|---|
| 346 | void QwtPlotScaleItem::draw( QPainter* painter,
|
|---|
| 347 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 348 | const QRectF& canvasRect ) const
|
|---|
| 349 | {
|
|---|
| 350 | QwtScaleDraw* sd = m_data->scaleDraw;
|
|---|
| 351 |
|
|---|
| 352 | if ( m_data->scaleDivFromAxis )
|
|---|
| 353 | {
|
|---|
| 354 | const QwtInterval interval =
|
|---|
| 355 | m_data->scaleInterval( canvasRect, xMap, yMap );
|
|---|
| 356 |
|
|---|
| 357 | if ( interval != sd->scaleDiv().interval() )
|
|---|
| 358 | {
|
|---|
| 359 | QwtScaleDiv scaleDiv = sd->scaleDiv();
|
|---|
| 360 | scaleDiv.setInterval( interval );
|
|---|
| 361 | sd->setScaleDiv( scaleDiv );
|
|---|
| 362 | }
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | QPen pen = painter->pen();
|
|---|
| 366 | pen.setStyle( Qt::SolidLine );
|
|---|
| 367 | painter->setPen( pen );
|
|---|
| 368 |
|
|---|
| 369 | if ( sd->orientation() == Qt::Horizontal )
|
|---|
| 370 | {
|
|---|
| 371 | double y;
|
|---|
| 372 | if ( m_data->borderDistance >= 0 )
|
|---|
| 373 | {
|
|---|
| 374 | if ( sd->alignment() == QwtScaleDraw::BottomScale )
|
|---|
| 375 | y = canvasRect.top() + m_data->borderDistance;
|
|---|
| 376 | else
|
|---|
| 377 | {
|
|---|
| 378 | y = canvasRect.bottom() - m_data->borderDistance;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | }
|
|---|
| 382 | else
|
|---|
| 383 | {
|
|---|
| 384 | y = yMap.transform( m_data->position );
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | if ( y < canvasRect.top() || y > canvasRect.bottom() )
|
|---|
| 388 | return;
|
|---|
| 389 |
|
|---|
| 390 | sd->move( canvasRect.left(), y );
|
|---|
| 391 | sd->setLength( canvasRect.width() - 1 );
|
|---|
| 392 |
|
|---|
| 393 | QwtTransform* transform = NULL;
|
|---|
| 394 | if ( xMap.transformation() )
|
|---|
| 395 | transform = xMap.transformation()->copy();
|
|---|
| 396 |
|
|---|
| 397 | sd->setTransformation( transform );
|
|---|
| 398 | }
|
|---|
| 399 | else // == Qt::Vertical
|
|---|
| 400 | {
|
|---|
| 401 | double x;
|
|---|
| 402 | if ( m_data->borderDistance >= 0 )
|
|---|
| 403 | {
|
|---|
| 404 | if ( sd->alignment() == QwtScaleDraw::RightScale )
|
|---|
| 405 | x = canvasRect.left() + m_data->borderDistance;
|
|---|
| 406 | else
|
|---|
| 407 | {
|
|---|
| 408 | x = canvasRect.right() - m_data->borderDistance;
|
|---|
| 409 | }
|
|---|
| 410 | }
|
|---|
| 411 | else
|
|---|
| 412 | {
|
|---|
| 413 | x = xMap.transform( m_data->position );
|
|---|
| 414 | }
|
|---|
| 415 | if ( x < canvasRect.left() || x > canvasRect.right() )
|
|---|
| 416 | return;
|
|---|
| 417 |
|
|---|
| 418 | sd->move( x, canvasRect.top() );
|
|---|
| 419 | sd->setLength( canvasRect.height() - 1 );
|
|---|
| 420 |
|
|---|
| 421 | QwtTransform* transform = NULL;
|
|---|
| 422 | if ( yMap.transformation() )
|
|---|
| 423 | transform = yMap.transformation()->copy();
|
|---|
| 424 |
|
|---|
| 425 | sd->setTransformation( transform );
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | painter->setFont( m_data->font );
|
|---|
| 429 |
|
|---|
| 430 | sd->draw( painter, m_data->palette );
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | /*!
|
|---|
| 434 | \brief Update the item to changes of the axes scale division
|
|---|
| 435 |
|
|---|
| 436 | In case of isScaleDivFromAxis(), the scale draw is synchronized
|
|---|
| 437 | to the correspond axis.
|
|---|
| 438 |
|
|---|
| 439 | \param xScaleDiv Scale division of the x-axis
|
|---|
| 440 | \param yScaleDiv Scale division of the y-axis
|
|---|
| 441 |
|
|---|
| 442 | \sa QwtPlot::updateAxes()
|
|---|
| 443 | */
|
|---|
| 444 |
|
|---|
| 445 | void QwtPlotScaleItem::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
|
|---|
| 446 | const QwtScaleDiv& yScaleDiv )
|
|---|
| 447 | {
|
|---|
| 448 | QwtScaleDraw* scaleDraw = m_data->scaleDraw;
|
|---|
| 449 |
|
|---|
| 450 | if ( m_data->scaleDivFromAxis && scaleDraw )
|
|---|
| 451 | {
|
|---|
| 452 | const QwtScaleDiv& scaleDiv =
|
|---|
| 453 | scaleDraw->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv;
|
|---|
| 454 |
|
|---|
| 455 | const QwtPlot* plt = plot();
|
|---|
| 456 | if ( plt != NULL )
|
|---|
| 457 | {
|
|---|
| 458 | const QRectF canvasRect = plt->canvas()->contentsRect();
|
|---|
| 459 |
|
|---|
| 460 | const QwtInterval interval = m_data->scaleInterval(
|
|---|
| 461 | canvasRect, plt->canvasMap( xAxis() ), plt->canvasMap( yAxis() ) );
|
|---|
| 462 |
|
|---|
| 463 | QwtScaleDiv sd = scaleDiv;
|
|---|
| 464 | sd.setInterval( interval );
|
|---|
| 465 |
|
|---|
| 466 | if ( sd != scaleDraw->scaleDiv() )
|
|---|
| 467 | {
|
|---|
| 468 | // the internal label cache of QwtScaleDraw
|
|---|
| 469 | // is cleared here, so better avoid pointless
|
|---|
| 470 | // assignments.
|
|---|
| 471 |
|
|---|
| 472 | scaleDraw->setScaleDiv( sd );
|
|---|
| 473 | }
|
|---|
| 474 | }
|
|---|
| 475 | else
|
|---|
| 476 | {
|
|---|
| 477 | scaleDraw->setScaleDiv( scaleDiv );
|
|---|
| 478 | }
|
|---|
| 479 | }
|
|---|
| 480 | }
|
|---|