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