qwt_scale_widget.cpp 22 KB

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