[4271] | 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_scale_widget.h"
|
---|
| 11 | #include "qwt_painter.h"
|
---|
| 12 | #include "qwt_color_map.h"
|
---|
| 13 | #include "qwt_scale_map.h"
|
---|
| 14 | #include "qwt_math.h"
|
---|
| 15 | #include "qwt_scale_div.h"
|
---|
| 16 | #include "qwt_text.h"
|
---|
| 17 | #include <qpainter.h>
|
---|
| 18 | #include <qevent.h>
|
---|
| 19 | #include <qmath.h>
|
---|
| 20 | #include <qstyle.h>
|
---|
| 21 | #include <qstyleoption.h>
|
---|
| 22 |
|
---|
| 23 | class QwtScaleWidget::PrivateData
|
---|
| 24 | {
|
---|
| 25 | public:
|
---|
| 26 | PrivateData():
|
---|
| 27 | scaleDraw( NULL )
|
---|
| 28 | {
|
---|
| 29 | colorBar.colorMap = NULL;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | ~PrivateData()
|
---|
| 33 | {
|
---|
| 34 | delete scaleDraw;
|
---|
| 35 | delete colorBar.colorMap;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | QwtScaleDraw *scaleDraw;
|
---|
| 39 |
|
---|
| 40 | int borderDist[2];
|
---|
| 41 | int minBorderDist[2];
|
---|
| 42 | int scaleLength;
|
---|
| 43 | int margin;
|
---|
| 44 |
|
---|
| 45 | int titleOffset;
|
---|
| 46 | int spacing;
|
---|
| 47 | QwtText title;
|
---|
| 48 |
|
---|
| 49 | QwtScaleWidget::LayoutFlags layoutFlags;
|
---|
| 50 |
|
---|
| 51 | struct t_colorBar
|
---|
| 52 | {
|
---|
| 53 | bool isEnabled;
|
---|
| 54 | int width;
|
---|
| 55 | QwtInterval interval;
|
---|
| 56 | QwtColorMap *colorMap;
|
---|
| 57 | } colorBar;
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | /*!
|
---|
| 61 | \brief Create a scale with the position QwtScaleWidget::Left
|
---|
| 62 | \param parent Parent widget
|
---|
| 63 | */
|
---|
| 64 | QwtScaleWidget::QwtScaleWidget( QWidget *parent ):
|
---|
| 65 | QWidget( parent )
|
---|
| 66 | {
|
---|
| 67 | initScale( QwtScaleDraw::LeftScale );
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /*!
|
---|
| 71 | \brief Constructor
|
---|
| 72 | \param align Alignment.
|
---|
| 73 | \param parent Parent widget
|
---|
| 74 | */
|
---|
| 75 | QwtScaleWidget::QwtScaleWidget(
|
---|
| 76 | QwtScaleDraw::Alignment align, QWidget *parent ):
|
---|
| 77 | QWidget( parent )
|
---|
| 78 | {
|
---|
| 79 | initScale( align );
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | //! Destructor
|
---|
| 83 | QwtScaleWidget::~QwtScaleWidget()
|
---|
| 84 | {
|
---|
| 85 | delete d_data;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | //! Initialize the scale
|
---|
| 89 | void QwtScaleWidget::initScale( QwtScaleDraw::Alignment align )
|
---|
| 90 | {
|
---|
| 91 | d_data = new PrivateData;
|
---|
| 92 |
|
---|
| 93 | d_data->layoutFlags = 0;
|
---|
| 94 | if ( align == QwtScaleDraw::RightScale )
|
---|
| 95 | d_data->layoutFlags |= TitleInverted;
|
---|
| 96 |
|
---|
| 97 | d_data->borderDist[0] = 0;
|
---|
| 98 | d_data->borderDist[1] = 0;
|
---|
| 99 | d_data->minBorderDist[0] = 0;
|
---|
| 100 | d_data->minBorderDist[1] = 0;
|
---|
| 101 | d_data->margin = 4;
|
---|
| 102 | d_data->titleOffset = 0;
|
---|
| 103 | d_data->spacing = 2;
|
---|
| 104 |
|
---|
| 105 | d_data->scaleDraw = new QwtScaleDraw;
|
---|
| 106 | d_data->scaleDraw->setAlignment( align );
|
---|
| 107 | d_data->scaleDraw->setLength( 10 );
|
---|
| 108 |
|
---|
| 109 | d_data->colorBar.colorMap = new QwtLinearColorMap();
|
---|
| 110 | d_data->colorBar.isEnabled = false;
|
---|
| 111 | d_data->colorBar.width = 10;
|
---|
| 112 |
|
---|
| 113 | const int flags = Qt::AlignHCenter
|
---|
| 114 | | Qt::TextExpandTabs | Qt::TextWordWrap;
|
---|
| 115 | d_data->title.setRenderFlags( flags );
|
---|
| 116 | d_data->title.setFont( font() );
|
---|
| 117 |
|
---|
| 118 | QSizePolicy policy( QSizePolicy::MinimumExpanding,
|
---|
| 119 | QSizePolicy::Fixed );
|
---|
| 120 | if ( d_data->scaleDraw->orientation() == Qt::Vertical )
|
---|
| 121 | policy.transpose();
|
---|
| 122 |
|
---|
| 123 | setSizePolicy( policy );
|
---|
| 124 |
|
---|
| 125 | setAttribute( Qt::WA_WState_OwnSizePolicy, false );
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /*!
|
---|
| 129 | Toggle an layout flag
|
---|
| 130 |
|
---|
| 131 | \param flag Layout flag
|
---|
| 132 | \param on true/false
|
---|
| 133 |
|
---|
| 134 | \sa testLayoutFlag(), LayoutFlag
|
---|
| 135 | */
|
---|
| 136 | void QwtScaleWidget::setLayoutFlag( LayoutFlag flag, bool on )
|
---|
| 137 | {
|
---|
| 138 | if ( ( ( d_data->layoutFlags & flag ) != 0 ) != on )
|
---|
| 139 | {
|
---|
| 140 | if ( on )
|
---|
| 141 | d_data->layoutFlags |= flag;
|
---|
| 142 | else
|
---|
| 143 | d_data->layoutFlags &= ~flag;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /*!
|
---|
| 148 | Test a layout flag
|
---|
| 149 |
|
---|
| 150 | \param flag Layout flag
|
---|
| 151 | \return true/false
|
---|
| 152 | \sa setLayoutFlag(), LayoutFlag
|
---|
| 153 | */
|
---|
| 154 | bool QwtScaleWidget::testLayoutFlag( LayoutFlag flag ) const
|
---|
| 155 | {
|
---|
| 156 | return ( d_data->layoutFlags & flag );
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /*!
|
---|
| 160 | Give title new text contents
|
---|
| 161 |
|
---|
| 162 | \param title New title
|
---|
| 163 | \sa title(), setTitle(const QwtText &);
|
---|
| 164 | */
|
---|
| 165 | void QwtScaleWidget::setTitle( const QString &title )
|
---|
| 166 | {
|
---|
| 167 | if ( d_data->title.text() != title )
|
---|
| 168 | {
|
---|
| 169 | d_data->title.setText( title );
|
---|
| 170 | layoutScale();
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /*!
|
---|
| 175 | Give title new text contents
|
---|
| 176 |
|
---|
| 177 | \param title New title
|
---|
| 178 | \sa title()
|
---|
| 179 | \warning The title flags are interpreted in
|
---|
| 180 | direction of the label, AlignTop, AlignBottom can't be set
|
---|
| 181 | as the title will always be aligned to the scale.
|
---|
| 182 | */
|
---|
| 183 | void QwtScaleWidget::setTitle( const QwtText &title )
|
---|
| 184 | {
|
---|
| 185 | QwtText t = title;
|
---|
| 186 | const int flags = title.renderFlags() & ~( Qt::AlignTop | Qt::AlignBottom );
|
---|
| 187 | t.setRenderFlags( flags );
|
---|
| 188 |
|
---|
| 189 | if ( t != d_data->title )
|
---|
| 190 | {
|
---|
| 191 | d_data->title = t;
|
---|
| 192 | layoutScale();
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /*!
|
---|
| 197 | Change the alignment
|
---|
| 198 |
|
---|
| 199 | \param alignment New alignment
|
---|
| 200 | \sa alignment()
|
---|
| 201 | */
|
---|
| 202 | void QwtScaleWidget::setAlignment( QwtScaleDraw::Alignment alignment )
|
---|
| 203 | {
|
---|
| 204 | if ( !testAttribute( Qt::WA_WState_OwnSizePolicy ) )
|
---|
| 205 | {
|
---|
| 206 | QSizePolicy policy( QSizePolicy::MinimumExpanding,
|
---|
| 207 | QSizePolicy::Fixed );
|
---|
| 208 | if ( d_data->scaleDraw->orientation() == Qt::Vertical )
|
---|
| 209 | policy.transpose();
|
---|
| 210 | setSizePolicy( policy );
|
---|
| 211 |
|
---|
| 212 | setAttribute( Qt::WA_WState_OwnSizePolicy, false );
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | if ( d_data->scaleDraw )
|
---|
| 216 | d_data->scaleDraw->setAlignment( alignment );
|
---|
| 217 | layoutScale();
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 |
|
---|
| 221 | /*!
|
---|
| 222 | \return position
|
---|
| 223 | \sa setPosition()
|
---|
| 224 | */
|
---|
| 225 | QwtScaleDraw::Alignment QwtScaleWidget::alignment() const
|
---|
| 226 | {
|
---|
| 227 | if ( !scaleDraw() )
|
---|
| 228 | return QwtScaleDraw::LeftScale;
|
---|
| 229 |
|
---|
| 230 | return scaleDraw()->alignment();
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /*!
|
---|
| 234 | Specify distances of the scale's endpoints from the
|
---|
| 235 | widget's borders. The actual borders will never be less
|
---|
| 236 | than minimum border distance.
|
---|
| 237 | \param dist1 Left or top Distance
|
---|
| 238 | \param dist2 Right or bottom distance
|
---|
| 239 | \sa borderDist()
|
---|
| 240 | */
|
---|
| 241 | void QwtScaleWidget::setBorderDist( int dist1, int dist2 )
|
---|
| 242 | {
|
---|
| 243 | if ( dist1 != d_data->borderDist[0] || dist2 != d_data->borderDist[1] )
|
---|
| 244 | {
|
---|
| 245 | d_data->borderDist[0] = dist1;
|
---|
| 246 | d_data->borderDist[1] = dist2;
|
---|
| 247 | layoutScale();
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /*!
|
---|
| 252 | \brief Specify the margin to the colorBar/base line.
|
---|
| 253 | \param margin Margin
|
---|
| 254 | \sa margin()
|
---|
| 255 | */
|
---|
| 256 | void QwtScaleWidget::setMargin( int margin )
|
---|
| 257 | {
|
---|
| 258 | margin = qMax( 0, margin );
|
---|
| 259 | if ( margin != d_data->margin )
|
---|
| 260 | {
|
---|
| 261 | d_data->margin = margin;
|
---|
| 262 | layoutScale();
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /*!
|
---|
| 267 | \brief Specify the distance between color bar, scale and title
|
---|
| 268 | \param spacing Spacing
|
---|
| 269 | \sa spacing()
|
---|
| 270 | */
|
---|
| 271 | void QwtScaleWidget::setSpacing( int spacing )
|
---|
| 272 | {
|
---|
| 273 | spacing = qMax( 0, spacing );
|
---|
| 274 | if ( spacing != d_data->spacing )
|
---|
| 275 | {
|
---|
| 276 | d_data->spacing = spacing;
|
---|
| 277 | layoutScale();
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /*!
|
---|
| 282 | \brief Change the alignment for the labels.
|
---|
| 283 |
|
---|
| 284 | \sa QwtScaleDraw::setLabelAlignment(), setLabelRotation()
|
---|
| 285 | */
|
---|
| 286 | void QwtScaleWidget::setLabelAlignment( Qt::Alignment alignment )
|
---|
| 287 | {
|
---|
| 288 | d_data->scaleDraw->setLabelAlignment( alignment );
|
---|
| 289 | layoutScale();
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /*!
|
---|
| 293 | \brief Change the rotation for the labels.
|
---|
| 294 | See QwtScaleDraw::setLabelRotation().
|
---|
| 295 |
|
---|
| 296 | \param rotation Rotation
|
---|
| 297 | \sa QwtScaleDraw::setLabelRotation(), setLabelFlags()
|
---|
| 298 | */
|
---|
| 299 | void QwtScaleWidget::setLabelRotation( double rotation )
|
---|
| 300 | {
|
---|
| 301 | d_data->scaleDraw->setLabelRotation( rotation );
|
---|
| 302 | layoutScale();
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | /*!
|
---|
| 306 | Set a scale draw
|
---|
| 307 | sd has to be created with new and will be deleted in
|
---|
| 308 | ~QwtScaleWidget() or the next call of setScaleDraw().
|
---|
| 309 |
|
---|
| 310 | \param sd ScaleDraw object
|
---|
| 311 | \sa scaleDraw()
|
---|
| 312 | */
|
---|
| 313 | void QwtScaleWidget::setScaleDraw( QwtScaleDraw *sd )
|
---|
| 314 | {
|
---|
| 315 | if ( sd == NULL || sd == d_data->scaleDraw )
|
---|
| 316 | return;
|
---|
| 317 |
|
---|
| 318 | if ( d_data->scaleDraw )
|
---|
| 319 | sd->setAlignment( d_data->scaleDraw->alignment() );
|
---|
| 320 |
|
---|
| 321 | delete d_data->scaleDraw;
|
---|
| 322 | d_data->scaleDraw = sd;
|
---|
| 323 |
|
---|
| 324 | layoutScale();
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /*!
|
---|
| 328 | scaleDraw of this scale
|
---|
| 329 | \sa setScaleDraw(), QwtScaleDraw::setScaleDraw()
|
---|
| 330 | */
|
---|
| 331 | const QwtScaleDraw *QwtScaleWidget::scaleDraw() const
|
---|
| 332 | {
|
---|
| 333 | return d_data->scaleDraw;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /*!
|
---|
| 337 | scaleDraw of this scale
|
---|
| 338 | \sa QwtScaleDraw::setScaleDraw()
|
---|
| 339 | */
|
---|
| 340 | QwtScaleDraw *QwtScaleWidget::scaleDraw()
|
---|
| 341 | {
|
---|
| 342 | return d_data->scaleDraw;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /*!
|
---|
| 346 | \return title
|
---|
| 347 | \sa setTitle()
|
---|
| 348 | */
|
---|
| 349 | QwtText QwtScaleWidget::title() const
|
---|
| 350 | {
|
---|
| 351 | return d_data->title;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /*!
|
---|
| 355 | \return start border distance
|
---|
| 356 | \sa setBorderDist()
|
---|
| 357 | */
|
---|
| 358 | int QwtScaleWidget::startBorderDist() const
|
---|
| 359 | {
|
---|
| 360 | return d_data->borderDist[0];
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | /*!
|
---|
| 364 | \return end border distance
|
---|
| 365 | \sa setBorderDist()
|
---|
| 366 | */
|
---|
| 367 | int QwtScaleWidget::endBorderDist() const
|
---|
| 368 | {
|
---|
| 369 | return d_data->borderDist[1];
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /*!
|
---|
| 373 | \return margin
|
---|
| 374 | \sa setMargin()
|
---|
| 375 | */
|
---|
| 376 | int QwtScaleWidget::margin() const
|
---|
| 377 | {
|
---|
| 378 | return d_data->margin;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | /*!
|
---|
| 382 | \return distance between scale and title
|
---|
| 383 | \sa setMargin()
|
---|
| 384 | */
|
---|
| 385 | int QwtScaleWidget::spacing() const
|
---|
| 386 | {
|
---|
| 387 | return d_data->spacing;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | /*!
|
---|
| 391 | \brief paintEvent
|
---|
| 392 | */
|
---|
| 393 | void QwtScaleWidget::paintEvent( QPaintEvent *event )
|
---|
| 394 | {
|
---|
| 395 | QPainter painter( this );
|
---|
| 396 | painter.setClipRegion( event->region() );
|
---|
| 397 |
|
---|
| 398 | QStyleOption opt;
|
---|
| 399 | opt.init(this);
|
---|
| 400 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
|
---|
| 401 |
|
---|
| 402 | draw( &painter );
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | /*!
|
---|
| 406 | \brief draw the scale
|
---|
| 407 | */
|
---|
| 408 | void QwtScaleWidget::draw( QPainter *painter ) const
|
---|
| 409 | {
|
---|
| 410 | d_data->scaleDraw->draw( painter, palette() );
|
---|
| 411 |
|
---|
| 412 | if ( d_data->colorBar.isEnabled && d_data->colorBar.width > 0 &&
|
---|
| 413 | d_data->colorBar.interval.isValid() )
|
---|
| 414 | {
|
---|
| 415 | drawColorBar( painter, colorBarRect( contentsRect() ) );
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | QRect r = contentsRect();
|
---|
| 419 | if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
|
---|
| 420 | {
|
---|
| 421 | r.setLeft( r.left() + d_data->borderDist[0] );
|
---|
| 422 | r.setWidth( r.width() - d_data->borderDist[1] );
|
---|
| 423 | }
|
---|
| 424 | else
|
---|
| 425 | {
|
---|
| 426 | r.setTop( r.top() + d_data->borderDist[0] );
|
---|
| 427 | r.setHeight( r.height() - d_data->borderDist[1] );
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | if ( !d_data->title.isEmpty() )
|
---|
| 431 | drawTitle( painter, d_data->scaleDraw->alignment(), r );
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | /*!
|
---|
| 435 | Calculate the the rectangle for the color bar
|
---|
| 436 |
|
---|
| 437 | \param rect Bounding rectangle for all components of the scale
|
---|
| 438 | \return Rectabgle for the color bar
|
---|
| 439 | */
|
---|
| 440 | QRectF QwtScaleWidget::colorBarRect( const QRectF& rect ) const
|
---|
| 441 | {
|
---|
| 442 | QRectF cr = rect;
|
---|
| 443 |
|
---|
| 444 | if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
|
---|
| 445 | {
|
---|
| 446 | cr.setLeft( cr.left() + d_data->borderDist[0] );
|
---|
| 447 | cr.setWidth( cr.width() - d_data->borderDist[1] + 1 );
|
---|
| 448 | }
|
---|
| 449 | else
|
---|
| 450 | {
|
---|
| 451 | cr.setTop( cr.top() + d_data->borderDist[0] );
|
---|
| 452 | cr.setHeight( cr.height() - d_data->borderDist[1] + 1 );
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | switch ( d_data->scaleDraw->alignment() )
|
---|
| 456 | {
|
---|
| 457 | case QwtScaleDraw::LeftScale:
|
---|
| 458 | {
|
---|
| 459 | cr.setLeft( cr.right() - d_data->margin
|
---|
| 460 | - d_data->colorBar.width );
|
---|
| 461 | cr.setWidth( d_data->colorBar.width );
|
---|
| 462 | break;
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | case QwtScaleDraw::RightScale:
|
---|
| 466 | {
|
---|
| 467 | cr.setLeft( cr.left() + d_data->margin );
|
---|
| 468 | cr.setWidth( d_data->colorBar.width );
|
---|
| 469 | break;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | case QwtScaleDraw::BottomScale:
|
---|
| 473 | {
|
---|
| 474 | cr.setTop( cr.top() + d_data->margin );
|
---|
| 475 | cr.setHeight( d_data->colorBar.width );
|
---|
| 476 | break;
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | case QwtScaleDraw::TopScale:
|
---|
| 480 | {
|
---|
| 481 | cr.setTop( cr.bottom() - d_data->margin
|
---|
| 482 | - d_data->colorBar.width );
|
---|
| 483 | cr.setHeight( d_data->colorBar.width );
|
---|
| 484 | break;
|
---|
| 485 | }
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | return cr;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | /*!
|
---|
| 492 | Event handler for resize event
|
---|
| 493 | \param event Resize event
|
---|
| 494 | */
|
---|
| 495 | void QwtScaleWidget::resizeEvent( QResizeEvent *event )
|
---|
| 496 | {
|
---|
| 497 | Q_UNUSED( event );
|
---|
| 498 | layoutScale( false );
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | /*!
|
---|
| 502 | Recalculate the scale's geometry and layout based on
|
---|
| 503 | the current rect and fonts.
|
---|
| 504 |
|
---|
| 505 | \param update_geometry Notify the layout system and call update
|
---|
| 506 | to redraw the scale
|
---|
| 507 | */
|
---|
| 508 |
|
---|
| 509 | void QwtScaleWidget::layoutScale( bool update_geometry )
|
---|
| 510 | {
|
---|
| 511 | int bd0, bd1;
|
---|
| 512 | getBorderDistHint( bd0, bd1 );
|
---|
| 513 | if ( d_data->borderDist[0] > bd0 )
|
---|
| 514 | bd0 = d_data->borderDist[0];
|
---|
| 515 | if ( d_data->borderDist[1] > bd1 )
|
---|
| 516 | bd1 = d_data->borderDist[1];
|
---|
| 517 |
|
---|
| 518 | int colorBarWidth = 0;
|
---|
| 519 | if ( d_data->colorBar.isEnabled && d_data->colorBar.interval.isValid() )
|
---|
| 520 | colorBarWidth = d_data->colorBar.width + d_data->spacing;
|
---|
| 521 |
|
---|
| 522 | const QRectF r = contentsRect();
|
---|
| 523 | double x, y, length;
|
---|
| 524 |
|
---|
| 525 | if ( d_data->scaleDraw->orientation() == Qt::Vertical )
|
---|
| 526 | {
|
---|
| 527 | y = r.top() + bd0;
|
---|
| 528 | length = r.height() - ( bd0 + bd1 );
|
---|
| 529 |
|
---|
| 530 | if ( d_data->scaleDraw->alignment() == QwtScaleDraw::LeftScale )
|
---|
| 531 | x = r.right() - 1.0 - d_data->margin - colorBarWidth;
|
---|
| 532 | else
|
---|
| 533 | x = r.left() + d_data->margin + colorBarWidth;
|
---|
| 534 | }
|
---|
| 535 | else
|
---|
| 536 | {
|
---|
| 537 | x = r.left() + bd0;
|
---|
| 538 | length = r.width() - ( bd0 + bd1 );
|
---|
| 539 |
|
---|
| 540 | if ( d_data->scaleDraw->alignment() == QwtScaleDraw::BottomScale )
|
---|
| 541 | y = r.top() + d_data->margin + colorBarWidth;
|
---|
| 542 | else
|
---|
| 543 | y = r.bottom() - 1.0 - d_data->margin - colorBarWidth;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | d_data->scaleDraw->move( x, y );
|
---|
| 547 | d_data->scaleDraw->setLength( length );
|
---|
| 548 |
|
---|
| 549 | const int extent = qCeil( d_data->scaleDraw->extent( font() ) );
|
---|
| 550 |
|
---|
| 551 | d_data->titleOffset =
|
---|
| 552 | d_data->margin + d_data->spacing + colorBarWidth + extent;
|
---|
| 553 |
|
---|
| 554 | if ( update_geometry )
|
---|
| 555 | {
|
---|
| 556 | updateGeometry();
|
---|
| 557 | update();
|
---|
| 558 | }
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | /*!
|
---|
| 562 | Draw the color bar of the scale widget
|
---|
| 563 |
|
---|
| 564 | \param painter Painter
|
---|
| 565 | \param rect Bounding rectangle for the color bar
|
---|
| 566 |
|
---|
| 567 | \sa setColorBarEnabled()
|
---|
| 568 | */
|
---|
| 569 | void QwtScaleWidget::drawColorBar( QPainter *painter, const QRectF& rect ) const
|
---|
| 570 | {
|
---|
| 571 | if ( !d_data->colorBar.interval.isValid() )
|
---|
| 572 | return;
|
---|
| 573 |
|
---|
| 574 | const QwtScaleDraw* sd = d_data->scaleDraw;
|
---|
| 575 |
|
---|
| 576 | QwtPainter::drawColorBar( painter, *d_data->colorBar.colorMap,
|
---|
| 577 | d_data->colorBar.interval.normalized(), sd->scaleMap(),
|
---|
| 578 | sd->orientation(), rect );
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | /*!
|
---|
| 582 | Rotate and paint a title according to its position into a given rectangle.
|
---|
| 583 |
|
---|
| 584 | \param painter Painter
|
---|
| 585 | \param align Alignment
|
---|
| 586 | \param rect Bounding rectangle
|
---|
| 587 | */
|
---|
| 588 |
|
---|
| 589 | void QwtScaleWidget::drawTitle( QPainter *painter,
|
---|
| 590 | QwtScaleDraw::Alignment align, const QRectF &rect ) const
|
---|
| 591 | {
|
---|
| 592 | QRectF r = rect;
|
---|
| 593 | double angle;
|
---|
| 594 | int flags = d_data->title.renderFlags() &
|
---|
| 595 | ~( Qt::AlignTop | Qt::AlignBottom | Qt::AlignVCenter );
|
---|
| 596 |
|
---|
| 597 | switch ( align )
|
---|
| 598 | {
|
---|
| 599 | case QwtScaleDraw::LeftScale:
|
---|
| 600 | angle = -90.0;
|
---|
| 601 | flags |= Qt::AlignTop;
|
---|
| 602 | r.setRect( r.left(), r.bottom(),
|
---|
| 603 | r.height(), r.width() - d_data->titleOffset );
|
---|
| 604 | break;
|
---|
| 605 |
|
---|
| 606 | case QwtScaleDraw::RightScale:
|
---|
| 607 | angle = -90.0;
|
---|
| 608 | flags |= Qt::AlignTop;
|
---|
| 609 | r.setRect( r.left() + d_data->titleOffset, r.bottom(),
|
---|
| 610 | r.height(), r.width() - d_data->titleOffset );
|
---|
| 611 | break;
|
---|
| 612 |
|
---|
| 613 | case QwtScaleDraw::BottomScale:
|
---|
| 614 | angle = 0.0;
|
---|
| 615 | flags |= Qt::AlignBottom;
|
---|
| 616 | r.setTop( r.top() + d_data->titleOffset );
|
---|
| 617 | break;
|
---|
| 618 |
|
---|
| 619 | case QwtScaleDraw::TopScale:
|
---|
| 620 | default:
|
---|
| 621 | angle = 0.0;
|
---|
| 622 | flags |= Qt::AlignTop;
|
---|
| 623 | r.setBottom( r.bottom() - d_data->titleOffset );
|
---|
| 624 | break;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | if ( d_data->layoutFlags & TitleInverted )
|
---|
| 628 | {
|
---|
| 629 | if ( align == QwtScaleDraw::LeftScale
|
---|
| 630 | || align == QwtScaleDraw::RightScale )
|
---|
| 631 | {
|
---|
| 632 | angle = -angle;
|
---|
| 633 | r.setRect( r.x() + r.height(), r.y() - r.width(),
|
---|
| 634 | r.width(), r.height() );
|
---|
| 635 | }
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | painter->save();
|
---|
| 639 | painter->setFont( font() );
|
---|
| 640 | painter->setPen( palette().color( QPalette::Text ) );
|
---|
| 641 |
|
---|
| 642 | painter->translate( r.x(), r.y() );
|
---|
| 643 | if ( angle != 0.0 )
|
---|
| 644 | painter->rotate( angle );
|
---|
| 645 |
|
---|
| 646 | QwtText title = d_data->title;
|
---|
| 647 | title.setRenderFlags( flags );
|
---|
| 648 | title.draw( painter, QRect( 0, 0, r.width(), r.height() ) );
|
---|
| 649 |
|
---|
| 650 | painter->restore();
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | /*!
|
---|
| 654 | \brief Notify a change of the scale
|
---|
| 655 |
|
---|
| 656 | This virtual function can be overloaded by derived
|
---|
| 657 | classes. The default implementation updates the geometry
|
---|
| 658 | and repaints the widget.
|
---|
| 659 | */
|
---|
| 660 |
|
---|
| 661 | void QwtScaleWidget::scaleChange()
|
---|
| 662 | {
|
---|
| 663 | layoutScale();
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | /*!
|
---|
| 667 | \return a size hint
|
---|
| 668 | */
|
---|
| 669 | QSize QwtScaleWidget::sizeHint() const
|
---|
| 670 | {
|
---|
| 671 | return minimumSizeHint();
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | /*!
|
---|
| 675 | \return a minimum size hint
|
---|
| 676 | */
|
---|
| 677 | QSize QwtScaleWidget::minimumSizeHint() const
|
---|
| 678 | {
|
---|
| 679 | const Qt::Orientation o = d_data->scaleDraw->orientation();
|
---|
| 680 |
|
---|
| 681 | // Border Distance cannot be less than the scale borderDistHint
|
---|
| 682 | // Note, the borderDistHint is already included in minHeight/minWidth
|
---|
| 683 | int length = 0;
|
---|
| 684 | int mbd1, mbd2;
|
---|
| 685 | getBorderDistHint( mbd1, mbd2 );
|
---|
| 686 | length += qMax( 0, d_data->borderDist[0] - mbd1 );
|
---|
| 687 | length += qMax( 0, d_data->borderDist[1] - mbd2 );
|
---|
| 688 | length += d_data->scaleDraw->minLength( font() );
|
---|
| 689 |
|
---|
| 690 | int dim = dimForLength( length, font() );
|
---|
| 691 | if ( length < dim )
|
---|
| 692 | {
|
---|
| 693 | // compensate for long titles
|
---|
| 694 | length = dim;
|
---|
| 695 | dim = dimForLength( length, font() );
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | QSize size( length + 2, dim );
|
---|
| 699 | if ( o == Qt::Vertical )
|
---|
| 700 | size.transpose();
|
---|
| 701 |
|
---|
| 702 | int left, right, top, bottom;
|
---|
| 703 | getContentsMargins( &left, &top, &right, &bottom );
|
---|
| 704 | return size + QSize( left + right, top + bottom );
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | /*!
|
---|
| 708 | \brief Find the height of the title for a given width.
|
---|
| 709 | \param width Width
|
---|
| 710 | \return height Height
|
---|
| 711 | */
|
---|
| 712 |
|
---|
| 713 | int QwtScaleWidget::titleHeightForWidth( int width ) const
|
---|
| 714 | {
|
---|
| 715 | return d_data->title.heightForWidth( width, font() );
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | /*!
|
---|
| 719 | \brief Find the minimum dimension for a given length.
|
---|
| 720 | dim is the height, length the width seen in
|
---|
| 721 | direction of the title.
|
---|
| 722 | \param length width for horizontal, height for vertical scales
|
---|
| 723 | \param scaleFont Font of the scale
|
---|
| 724 | \return height for horizontal, width for vertical scales
|
---|
| 725 | */
|
---|
| 726 |
|
---|
| 727 | int QwtScaleWidget::dimForLength( int length, const QFont &scaleFont ) const
|
---|
| 728 | {
|
---|
| 729 | const int extent = qCeil( d_data->scaleDraw->extent( scaleFont ) );
|
---|
| 730 |
|
---|
| 731 | int dim = d_data->margin + extent + 1;
|
---|
| 732 |
|
---|
| 733 | if ( !d_data->title.isEmpty() )
|
---|
| 734 | dim += titleHeightForWidth( length ) + d_data->spacing;
|
---|
| 735 |
|
---|
| 736 | if ( d_data->colorBar.isEnabled && d_data->colorBar.interval.isValid() )
|
---|
| 737 | dim += d_data->colorBar.width + d_data->spacing;
|
---|
| 738 |
|
---|
| 739 | return dim;
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | /*!
|
---|
| 743 | \brief Calculate a hint for the border distances.
|
---|
| 744 |
|
---|
| 745 | This member function calculates the distance
|
---|
| 746 | of the scale's endpoints from the widget borders which
|
---|
| 747 | is required for the mark labels to fit into the widget.
|
---|
| 748 | The maximum of this distance an the minimum border distance
|
---|
| 749 | is returned.
|
---|
| 750 |
|
---|
| 751 | \warning
|
---|
| 752 | <ul> <li>The minimum border distance depends on the font.</ul>
|
---|
| 753 | \sa setMinBorderDist(), getMinBorderDist(), setBorderDist()
|
---|
| 754 | */
|
---|
| 755 | void QwtScaleWidget::getBorderDistHint( int &start, int &end ) const
|
---|
| 756 | {
|
---|
| 757 | d_data->scaleDraw->getBorderDistHint( font(), start, end );
|
---|
| 758 |
|
---|
| 759 | if ( start < d_data->minBorderDist[0] )
|
---|
| 760 | start = d_data->minBorderDist[0];
|
---|
| 761 |
|
---|
| 762 | if ( end < d_data->minBorderDist[1] )
|
---|
| 763 | end = d_data->minBorderDist[1];
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | /*!
|
---|
| 767 | Set a minimum value for the distances of the scale's endpoints from
|
---|
| 768 | the widget borders. This is useful to avoid that the scales
|
---|
| 769 | are "jumping", when the tick labels or their positions change
|
---|
| 770 | often.
|
---|
| 771 |
|
---|
| 772 | \param start Minimum for the start border
|
---|
| 773 | \param end Minimum for the end border
|
---|
| 774 | \sa getMinBorderDist(), getBorderDistHint()
|
---|
| 775 | */
|
---|
| 776 | void QwtScaleWidget::setMinBorderDist( int start, int end )
|
---|
| 777 | {
|
---|
| 778 | d_data->minBorderDist[0] = start;
|
---|
| 779 | d_data->minBorderDist[1] = end;
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | /*!
|
---|
| 783 | Get the minimum value for the distances of the scale's endpoints from
|
---|
| 784 | the widget borders.
|
---|
| 785 |
|
---|
| 786 | \sa setMinBorderDist(), getBorderDistHint()
|
---|
| 787 | */
|
---|
| 788 | void QwtScaleWidget::getMinBorderDist( int &start, int &end ) const
|
---|
| 789 | {
|
---|
| 790 | start = d_data->minBorderDist[0];
|
---|
| 791 | end = d_data->minBorderDist[1];
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | /*!
|
---|
| 795 | \brief Assign a scale division
|
---|
| 796 |
|
---|
| 797 | The scale division determines where to set the tick marks.
|
---|
| 798 |
|
---|
| 799 | \param transformation Transformation, needed to translate between
|
---|
| 800 | scale and pixal values
|
---|
| 801 | \param scaleDiv Scale Division
|
---|
| 802 | \sa For more information about scale divisions, see QwtScaleDiv.
|
---|
| 803 | */
|
---|
| 804 | void QwtScaleWidget::setScaleDiv(
|
---|
| 805 | QwtScaleTransformation *transformation,
|
---|
| 806 | const QwtScaleDiv &scaleDiv )
|
---|
| 807 | {
|
---|
| 808 | QwtScaleDraw *sd = d_data->scaleDraw;
|
---|
| 809 | if ( sd->scaleDiv() != scaleDiv ||
|
---|
| 810 | sd->scaleMap().transformation()->type() != transformation->type() )
|
---|
| 811 | {
|
---|
| 812 | sd->setTransformation( transformation );
|
---|
| 813 | sd->setScaleDiv( scaleDiv );
|
---|
| 814 | layoutScale();
|
---|
| 815 |
|
---|
| 816 | Q_EMIT scaleDivChanged();
|
---|
| 817 | }
|
---|
| 818 | else
|
---|
| 819 | {
|
---|
| 820 | /*
|
---|
| 821 | The transformation doesn't anything different as the
|
---|
| 822 | previous one. So we better throw it silently away instead of
|
---|
| 823 | initiating heavy updates
|
---|
| 824 | */
|
---|
| 825 |
|
---|
| 826 | delete transformation;
|
---|
| 827 | }
|
---|
| 828 | }
|
---|
| 829 |
|
---|
| 830 | /*!
|
---|
| 831 | En/disable a color bar associated to the scale
|
---|
| 832 | \sa isColorBarEnabled(), setColorBarWidth()
|
---|
| 833 | */
|
---|
| 834 | void QwtScaleWidget::setColorBarEnabled( bool on )
|
---|
| 835 | {
|
---|
| 836 | if ( on != d_data->colorBar.isEnabled )
|
---|
| 837 | {
|
---|
| 838 | d_data->colorBar.isEnabled = on;
|
---|
| 839 | layoutScale();
|
---|
| 840 | }
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 | /*!
|
---|
| 844 | \return true, when the color bar is enabled
|
---|
| 845 | \sa setColorBarEnabled(), setColorBarWidth()
|
---|
| 846 | */
|
---|
| 847 | bool QwtScaleWidget::isColorBarEnabled() const
|
---|
| 848 | {
|
---|
| 849 | return d_data->colorBar.isEnabled;
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | /*!
|
---|
| 853 | Set the width of the color bar
|
---|
| 854 |
|
---|
| 855 | \param width Width
|
---|
| 856 | \sa colorBarWidth(), setColorBarEnabled()
|
---|
| 857 | */
|
---|
| 858 | void QwtScaleWidget::setColorBarWidth( int width )
|
---|
| 859 | {
|
---|
| 860 | if ( width != d_data->colorBar.width )
|
---|
| 861 | {
|
---|
| 862 | d_data->colorBar.width = width;
|
---|
| 863 | if ( isColorBarEnabled() )
|
---|
| 864 | layoutScale();
|
---|
| 865 | }
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | /*!
|
---|
| 869 | \return Width of the color bar
|
---|
| 870 | \sa setColorBarEnabled(), setColorBarEnabled()
|
---|
| 871 | */
|
---|
| 872 | int QwtScaleWidget::colorBarWidth() const
|
---|
| 873 | {
|
---|
| 874 | return d_data->colorBar.width;
|
---|
| 875 | }
|
---|
| 876 |
|
---|
| 877 | /*!
|
---|
| 878 | \return Value interval for the color bar
|
---|
| 879 | \sa setColorMap(), colorMap()
|
---|
| 880 | */
|
---|
| 881 | QwtInterval QwtScaleWidget::colorBarInterval() const
|
---|
| 882 | {
|
---|
| 883 | return d_data->colorBar.interval;
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 | /*!
|
---|
| 887 | Set the color map and value interval, that are used for displaying
|
---|
| 888 | the color bar.
|
---|
| 889 |
|
---|
| 890 | \param interval Value interval
|
---|
| 891 | \param colorMap Color map
|
---|
| 892 |
|
---|
| 893 | \sa colorMap(), colorBarInterval()
|
---|
| 894 | */
|
---|
| 895 | void QwtScaleWidget::setColorMap(
|
---|
| 896 | const QwtInterval &interval, QwtColorMap *colorMap )
|
---|
| 897 | {
|
---|
| 898 | d_data->colorBar.interval = interval;
|
---|
| 899 |
|
---|
| 900 | if ( colorMap != d_data->colorBar.colorMap )
|
---|
| 901 | {
|
---|
| 902 | delete d_data->colorBar.colorMap;
|
---|
| 903 | d_data->colorBar.colorMap = colorMap;
|
---|
| 904 | }
|
---|
| 905 |
|
---|
| 906 | if ( isColorBarEnabled() )
|
---|
| 907 | layoutScale();
|
---|
| 908 | }
|
---|
| 909 |
|
---|
| 910 | /*!
|
---|
| 911 | \return Color map
|
---|
| 912 | \sa setColorMap(), colorBarInterval()
|
---|
| 913 | */
|
---|
| 914 | const QwtColorMap *QwtScaleWidget::colorMap() const
|
---|
| 915 | {
|
---|
| 916 | return d_data->colorBar.colorMap;
|
---|
| 917 | }
|
---|