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