qwt_slider.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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_slider.h"
  10. #include "qwt_painter.h"
  11. #include "qwt_scale_draw.h"
  12. #include "qwt_scale_map.h"
  13. #include <qevent.h>
  14. #include <qdrawutil.h>
  15. #include <qpainter.h>
  16. #include <qalgorithms.h>
  17. #include <qmath.h>
  18. class QwtSlider::PrivateData
  19. {
  20. public:
  21. QRect sliderRect;
  22. int thumbLength;
  23. int thumbWidth;
  24. int borderWidth;
  25. int scaleDist;
  26. int xMargin;
  27. int yMargin;
  28. QwtSlider::ScalePos scalePos;
  29. QwtSlider::BGSTYLE bgStyle;
  30. /*
  31. Scale and values might have different maps. This is
  32. confusing and I can't see strong arguments for such
  33. a feature. TODO ...
  34. */
  35. QwtScaleMap map; // linear map
  36. mutable QSize sizeHintCache;
  37. };
  38. /*!
  39. \brief Constructor
  40. \param parent parent widget
  41. \param orientation Orientation of the slider. Can be Qt::Horizontal
  42. or Qt::Vertical. Defaults to Qt::Horizontal.
  43. \param scalePos Position of the scale.
  44. Defaults to QwtSlider::NoScale.
  45. \param bgStyle Background style. QwtSlider::BgTrough draws the
  46. slider button in a trough, QwtSlider::BgSlot draws
  47. a slot underneath the button. An or-combination of both
  48. may also be used. The default is QwtSlider::BgTrough.
  49. QwtSlider enforces valid combinations of its orientation and scale position.
  50. If the combination is invalid, the scale position will be set to NoScale.
  51. Valid combinations are:
  52. - Qt::Horizonal with NoScale, TopScale, or BottomScale;
  53. - Qt::Vertical with NoScale, LeftScale, or RightScale.
  54. */
  55. QwtSlider::QwtSlider( QWidget *parent,
  56. Qt::Orientation orientation, ScalePos scalePos, BGSTYLE bgStyle ):
  57. QwtAbstractSlider( orientation, parent )
  58. {
  59. initSlider( orientation, scalePos, bgStyle );
  60. }
  61. void QwtSlider::initSlider( Qt::Orientation orientation,
  62. ScalePos scalePos, BGSTYLE bgStyle )
  63. {
  64. if ( orientation == Qt::Vertical )
  65. setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
  66. else
  67. setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
  68. setAttribute( Qt::WA_WState_OwnSizePolicy, false );
  69. d_data = new QwtSlider::PrivateData;
  70. d_data->borderWidth = 2;
  71. d_data->scaleDist = 4;
  72. d_data->scalePos = scalePos;
  73. d_data->xMargin = 0;
  74. d_data->yMargin = 0;
  75. d_data->bgStyle = bgStyle;
  76. if ( bgStyle == BgSlot )
  77. {
  78. d_data->thumbLength = 16;
  79. d_data->thumbWidth = 30;
  80. }
  81. else
  82. {
  83. d_data->thumbLength = 31;
  84. d_data->thumbWidth = 16;
  85. }
  86. d_data->sliderRect.setRect( 0, 0, 8, 8 );
  87. QwtScaleDraw::Alignment align;
  88. if ( orientation == Qt::Vertical )
  89. {
  90. // enforce a valid combination of scale position and orientation
  91. if ( ( d_data->scalePos == BottomScale ) || ( d_data->scalePos == TopScale ) )
  92. d_data->scalePos = NoScale;
  93. // adopt the policy of layoutSlider (NoScale lays out like Left)
  94. if ( d_data->scalePos == RightScale )
  95. align = QwtScaleDraw::RightScale;
  96. else
  97. align = QwtScaleDraw::LeftScale;
  98. }
  99. else
  100. {
  101. // enforce a valid combination of scale position and orientation
  102. if ( ( d_data->scalePos == LeftScale ) || ( d_data->scalePos == RightScale ) )
  103. d_data->scalePos = NoScale;
  104. // adopt the policy of layoutSlider (NoScale lays out like Bottom)
  105. if ( d_data->scalePos == TopScale )
  106. align = QwtScaleDraw::TopScale;
  107. else
  108. align = QwtScaleDraw::BottomScale;
  109. }
  110. scaleDraw()->setAlignment( align );
  111. scaleDraw()->setLength( 100 );
  112. setRange( 0.0, 100.0, 1.0 );
  113. setValue( 0.0 );
  114. }
  115. QwtSlider::~QwtSlider()
  116. {
  117. delete d_data;
  118. }
  119. /*!
  120. \brief Set the orientation.
  121. \param o Orientation. Allowed values are Qt::Horizontal and Qt::Vertical.
  122. If the new orientation and the old scale position are an invalid combination,
  123. the scale position will be set to QwtSlider::NoScale.
  124. \sa QwtAbstractSlider::orientation()
  125. */
  126. void QwtSlider::setOrientation( Qt::Orientation o )
  127. {
  128. if ( o == orientation() )
  129. return;
  130. if ( o == Qt::Horizontal )
  131. {
  132. if ( ( d_data->scalePos == LeftScale ) || ( d_data->scalePos == RightScale ) )
  133. d_data->scalePos = NoScale;
  134. }
  135. else // if (o == Qt::Vertical)
  136. {
  137. if ( ( d_data->scalePos == BottomScale ) || ( d_data->scalePos == TopScale ) )
  138. d_data->scalePos = NoScale;
  139. }
  140. if ( !testAttribute( Qt::WA_WState_OwnSizePolicy ) )
  141. {
  142. QSizePolicy sp = sizePolicy();
  143. sp.transpose();
  144. setSizePolicy( sp );
  145. setAttribute( Qt::WA_WState_OwnSizePolicy, false );
  146. }
  147. QwtAbstractSlider::setOrientation( o );
  148. layoutSlider();
  149. }
  150. /*!
  151. \brief Change the scale position (and slider orientation).
  152. \param s Position of the scale.
  153. A valid combination of scale position and orientation is enforced:
  154. - if the new scale position is Left or Right, the scale orientation will
  155. become Qt::Vertical;
  156. - if the new scale position is Bottom or Top the scale orientation will
  157. become Qt::Horizontal;
  158. - if the new scale position is QwtSlider::NoScale, the scale
  159. orientation will not change.
  160. */
  161. void QwtSlider::setScalePosition( ScalePos s )
  162. {
  163. if ( d_data->scalePos == s )
  164. return;
  165. d_data->scalePos = s;
  166. switch ( d_data->scalePos )
  167. {
  168. case BottomScale:
  169. {
  170. setOrientation( Qt::Horizontal );
  171. scaleDraw()->setAlignment( QwtScaleDraw::BottomScale );
  172. break;
  173. }
  174. case TopScale:
  175. {
  176. setOrientation( Qt::Horizontal );
  177. scaleDraw()->setAlignment( QwtScaleDraw::TopScale );
  178. break;
  179. }
  180. case LeftScale:
  181. {
  182. setOrientation( Qt::Vertical );
  183. scaleDraw()->setAlignment( QwtScaleDraw::LeftScale );
  184. break;
  185. }
  186. case RightScale:
  187. {
  188. setOrientation( Qt::Vertical );
  189. scaleDraw()->setAlignment( QwtScaleDraw::RightScale );
  190. break;
  191. }
  192. default:
  193. {
  194. // nothing
  195. }
  196. }
  197. layoutSlider();
  198. }
  199. //! Return the scale position.
  200. QwtSlider::ScalePos QwtSlider::scalePosition() const
  201. {
  202. return d_data->scalePos;
  203. }
  204. /*!
  205. \brief Change the slider's border width
  206. \param bd border width
  207. */
  208. void QwtSlider::setBorderWidth( int bd )
  209. {
  210. if ( bd < 0 )
  211. bd = 0;
  212. if ( bd != d_data->borderWidth )
  213. {
  214. d_data->borderWidth = bd;
  215. layoutSlider();
  216. }
  217. }
  218. /*!
  219. \brief Set the slider's thumb length
  220. \param thumbLength new length
  221. */
  222. void QwtSlider::setThumbLength( int thumbLength )
  223. {
  224. if ( thumbLength < 8 )
  225. thumbLength = 8;
  226. if ( thumbLength != d_data->thumbLength )
  227. {
  228. d_data->thumbLength = thumbLength;
  229. layoutSlider();
  230. }
  231. }
  232. /*!
  233. \brief Change the width of the thumb
  234. \param w new width
  235. */
  236. void QwtSlider::setThumbWidth( int w )
  237. {
  238. if ( w < 4 )
  239. w = 4;
  240. if ( d_data->thumbWidth != w )
  241. {
  242. d_data->thumbWidth = w;
  243. layoutSlider();
  244. }
  245. }
  246. /*!
  247. \brief Set a scale draw
  248. For changing the labels of the scales, it
  249. is necessary to derive from QwtScaleDraw and
  250. overload QwtScaleDraw::label().
  251. \param scaleDraw ScaleDraw object, that has to be created with
  252. new and will be deleted in ~QwtSlider or the next
  253. call of setScaleDraw().
  254. */
  255. void QwtSlider::setScaleDraw( QwtScaleDraw *scaleDraw )
  256. {
  257. const QwtScaleDraw *previousScaleDraw = this->scaleDraw();
  258. if ( scaleDraw == NULL || scaleDraw == previousScaleDraw )
  259. return;
  260. if ( previousScaleDraw )
  261. scaleDraw->setAlignment( previousScaleDraw->alignment() );
  262. setAbstractScaleDraw( scaleDraw );
  263. layoutSlider();
  264. }
  265. /*!
  266. \return the scale draw of the slider
  267. \sa setScaleDraw()
  268. */
  269. const QwtScaleDraw *QwtSlider::scaleDraw() const
  270. {
  271. return static_cast<const QwtScaleDraw *>( abstractScaleDraw() );
  272. }
  273. /*!
  274. \return the scale draw of the slider
  275. \sa setScaleDraw()
  276. */
  277. QwtScaleDraw *QwtSlider::scaleDraw()
  278. {
  279. return static_cast<QwtScaleDraw *>( abstractScaleDraw() );
  280. }
  281. //! Notify changed scale
  282. void QwtSlider::scaleChange()
  283. {
  284. layoutSlider();
  285. }
  286. //! Notify change in font
  287. void QwtSlider::fontChange( const QFont &f )
  288. {
  289. QwtAbstractSlider::fontChange( f );
  290. layoutSlider();
  291. }
  292. /*!
  293. Draw the slider into the specified rectangle.
  294. \param painter Painter
  295. \param r Rectangle
  296. */
  297. void QwtSlider::drawSlider( QPainter *painter, const QRect &r )
  298. {
  299. QRect cr( r );
  300. if ( d_data->bgStyle & BgTrough )
  301. {
  302. qDrawShadePanel( painter, r.x(), r.y(),
  303. r.width(), r.height(), palette(),
  304. true, d_data->borderWidth, 0 );
  305. cr.setRect( r.x() + d_data->borderWidth,
  306. r.y() + d_data->borderWidth,
  307. r.width() - 2 * d_data->borderWidth,
  308. r.height() - 2 * d_data->borderWidth );
  309. painter->fillRect( cr.x(), cr.y(), cr.width(), cr.height(),
  310. palette().brush( QPalette::Mid ) );
  311. }
  312. if ( d_data->bgStyle & BgSlot )
  313. {
  314. int ws = 4;
  315. int ds = d_data->thumbLength / 2 - 4;
  316. if ( ds < 1 )
  317. ds = 1;
  318. QRect rSlot;
  319. if ( orientation() == Qt::Horizontal )
  320. {
  321. if ( cr.height() & 1 )
  322. ws++;
  323. rSlot = QRect( cr.x() + ds,
  324. cr.y() + ( cr.height() - ws ) / 2,
  325. cr.width() - 2 * ds, ws );
  326. }
  327. else
  328. {
  329. if ( cr.width() & 1 )
  330. ws++;
  331. rSlot = QRect( cr.x() + ( cr.width() - ws ) / 2,
  332. cr.y() + ds,
  333. ws, cr.height() - 2 * ds );
  334. }
  335. painter->fillRect( rSlot.x(), rSlot.y(), rSlot.width(), rSlot.height(),
  336. palette().brush( QPalette::Dark ) );
  337. qDrawShadePanel( painter, rSlot.x(), rSlot.y(),
  338. rSlot.width(), rSlot.height(), palette(),
  339. true, 1 , 0 );
  340. }
  341. if ( isValid() )
  342. drawThumb( painter, cr, xyPosition( value() ) );
  343. }
  344. /*!
  345. Draw the thumb at a position
  346. \param painter Painter
  347. \param sliderRect Bounding rectangle of the slider
  348. \param pos Position of the slider thumb
  349. */
  350. void QwtSlider::drawThumb( QPainter *painter, const QRect &sliderRect, int pos )
  351. {
  352. pos++; // shade line points one pixel below
  353. if ( orientation() == Qt::Horizontal )
  354. {
  355. qDrawShadePanel( painter, pos - d_data->thumbLength / 2,
  356. sliderRect.y(), d_data->thumbLength, sliderRect.height(),
  357. palette(), false, d_data->borderWidth,
  358. &palette().brush( QPalette::Button ) );
  359. qDrawShadeLine( painter, pos, sliderRect.y(),
  360. pos, sliderRect.y() + sliderRect.height() - 2,
  361. palette(), true, 1 );
  362. }
  363. else // Vertical
  364. {
  365. qDrawShadePanel( painter, sliderRect.x(), pos - d_data->thumbLength / 2,
  366. sliderRect.width(), d_data->thumbLength,
  367. palette(), false, d_data->borderWidth,
  368. &palette().brush( QPalette::Button ) );
  369. qDrawShadeLine( painter, sliderRect.x(), pos,
  370. sliderRect.x() + sliderRect.width() - 2, pos,
  371. palette(), true, 1 );
  372. }
  373. }
  374. /*!
  375. Find the x/y position for a given value v
  376. \param value Value
  377. */
  378. int QwtSlider::xyPosition( double value ) const
  379. {
  380. return qRound( d_data->map.transform( value ) );
  381. }
  382. /*!
  383. Determine the value corresponding to a specified mouse location.
  384. \param pos Mouse position
  385. */
  386. double QwtSlider::getValue( const QPoint &pos )
  387. {
  388. return d_data->map.invTransform(
  389. orientation() == Qt::Horizontal ? pos.x() : pos.y() );
  390. }
  391. /*!
  392. \brief Determine scrolling mode and direction
  393. \param p point
  394. \param scrollMode Scrolling mode
  395. \param direction Direction
  396. */
  397. void QwtSlider::getScrollMode( const QPoint &p,
  398. int &scrollMode, int &direction )
  399. {
  400. if ( !d_data->sliderRect.contains( p ) )
  401. {
  402. scrollMode = ScrNone;
  403. direction = 0;
  404. return;
  405. }
  406. const int pos = ( orientation() == Qt::Horizontal ) ? p.x() : p.y();
  407. const int markerPos = xyPosition( value() );
  408. if ( ( pos > markerPos - d_data->thumbLength / 2 )
  409. && ( pos < markerPos + d_data->thumbLength / 2 ) )
  410. {
  411. scrollMode = ScrMouse;
  412. direction = 0;
  413. return;
  414. }
  415. scrollMode = ScrPage;
  416. direction = ( pos > markerPos ) ? 1 : -1;
  417. if ( scaleDraw()->map().p1() > scaleDraw()->map().p2() )
  418. direction = -direction;
  419. }
  420. /*!
  421. Qt paint event
  422. \param event Paint event
  423. */
  424. void QwtSlider::paintEvent( QPaintEvent *event )
  425. {
  426. const QRect &ur = event->rect();
  427. if ( ur.isValid() )
  428. {
  429. QPainter painter( this );
  430. draw( &painter, ur );
  431. }
  432. }
  433. //! Draw the QwtSlider
  434. void QwtSlider::draw( QPainter *painter, const QRect& )
  435. {
  436. if ( d_data->scalePos != NoScale )
  437. scaleDraw()->draw( painter, palette() );
  438. drawSlider( painter, d_data->sliderRect );
  439. if ( hasFocus() )
  440. QwtPainter::drawFocusRect( painter, this, d_data->sliderRect );
  441. }
  442. //! Qt resize event
  443. void QwtSlider::resizeEvent( QResizeEvent * )
  444. {
  445. layoutSlider( false );
  446. }
  447. /*!
  448. Recalculate the slider's geometry and layout based on
  449. the current rect and fonts.
  450. \param update_geometry notify the layout system and call update
  451. to redraw the scale
  452. */
  453. void QwtSlider::layoutSlider( bool update_geometry )
  454. {
  455. int sliderWidth = d_data->thumbWidth;
  456. int sld1 = d_data->thumbLength / 2 - 1;
  457. int sld2 = d_data->thumbLength / 2 + d_data->thumbLength % 2;
  458. if ( d_data->bgStyle & BgTrough )
  459. {
  460. sliderWidth += 2 * d_data->borderWidth;
  461. sld1 += d_data->borderWidth;
  462. sld2 += d_data->borderWidth;
  463. }
  464. int scd = 0;
  465. if ( d_data->scalePos != NoScale )
  466. {
  467. int d1, d2;
  468. scaleDraw()->getBorderDistHint( font(), d1, d2 );
  469. scd = qMax( d1, d2 );
  470. }
  471. int slo = scd - sld1;
  472. if ( slo < 0 )
  473. slo = 0;
  474. int x, y, length;
  475. const QRect r = rect();
  476. if ( orientation() == Qt::Horizontal )
  477. {
  478. switch ( d_data->scalePos )
  479. {
  480. case TopScale:
  481. {
  482. d_data->sliderRect.setRect(
  483. r.x() + d_data->xMargin + slo,
  484. r.y() + r.height() -
  485. d_data->yMargin - sliderWidth,
  486. r.width() - 2 * d_data->xMargin - 2 * slo,
  487. sliderWidth );
  488. x = d_data->sliderRect.x() + sld1;
  489. y = d_data->sliderRect.y() - d_data->scaleDist;
  490. break;
  491. }
  492. case BottomScale:
  493. {
  494. d_data->sliderRect.setRect(
  495. r.x() + d_data->xMargin + slo,
  496. r.y() + d_data->yMargin,
  497. r.width() - 2 * d_data->xMargin - 2 * slo,
  498. sliderWidth );
  499. x = d_data->sliderRect.x() + sld1;
  500. y = d_data->sliderRect.y() + d_data->sliderRect.height()
  501. + d_data->scaleDist;
  502. break;
  503. }
  504. case NoScale: // like Bottom, but no scale. See QwtSlider().
  505. default: // inconsistent orientation and scale position
  506. {
  507. d_data->sliderRect.setRect(
  508. r.x() + d_data->xMargin + slo,
  509. r.y() + d_data->yMargin,
  510. r.width() - 2 * d_data->xMargin - 2 * slo,
  511. sliderWidth );
  512. x = d_data->sliderRect.x() + sld1;
  513. y = 0;
  514. break;
  515. }
  516. }
  517. length = d_data->sliderRect.width() - ( sld1 + sld2 );
  518. }
  519. else // if (orientation() == Qt::Vertical
  520. {
  521. switch ( d_data->scalePos )
  522. {
  523. case RightScale:
  524. d_data->sliderRect.setRect(
  525. r.x() + d_data->xMargin,
  526. r.y() + d_data->yMargin + slo,
  527. sliderWidth,
  528. r.height() - 2 * d_data->yMargin - 2 * slo );
  529. x = d_data->sliderRect.x() + d_data->sliderRect.width()
  530. + d_data->scaleDist;
  531. y = d_data->sliderRect.y() + sld1;
  532. break;
  533. case LeftScale:
  534. d_data->sliderRect.setRect(
  535. r.x() + r.width() - sliderWidth - d_data->xMargin,
  536. r.y() + d_data->yMargin + slo,
  537. sliderWidth,
  538. r.height() - 2 * d_data->yMargin - 2 * slo );
  539. x = d_data->sliderRect.x() - d_data->scaleDist;
  540. y = d_data->sliderRect.y() + sld1;
  541. break;
  542. case NoScale: // like Left, but no scale. See QwtSlider().
  543. default: // inconsistent orientation and scale position
  544. d_data->sliderRect.setRect(
  545. r.x() + r.width() - sliderWidth - d_data->xMargin,
  546. r.y() + d_data->yMargin + slo,
  547. sliderWidth,
  548. r.height() - 2 * d_data->yMargin - 2 * slo );
  549. x = 0;
  550. y = d_data->sliderRect.y() + sld1;
  551. break;
  552. }
  553. length = d_data->sliderRect.height() - ( sld1 + sld2 );
  554. }
  555. scaleDraw()->move( x, y );
  556. scaleDraw()->setLength( length );
  557. d_data->map.setPaintInterval( scaleDraw()->map().p1(),
  558. scaleDraw()->map().p2() );
  559. if ( update_geometry )
  560. {
  561. d_data->sizeHintCache = QSize(); // invalidate
  562. updateGeometry();
  563. update();
  564. }
  565. }
  566. //! Notify change of value
  567. void QwtSlider::valueChange()
  568. {
  569. QwtAbstractSlider::valueChange();
  570. update();
  571. }
  572. //! Notify change of range
  573. void QwtSlider::rangeChange()
  574. {
  575. d_data->map.setScaleInterval( minValue(), maxValue() );
  576. if ( autoScale() )
  577. rescale( minValue(), maxValue() );
  578. QwtAbstractSlider::rangeChange();
  579. layoutSlider();
  580. }
  581. /*!
  582. \brief Set distances between the widget's border and internals.
  583. \param xMargin Horizontal margin
  584. \param yMargin Vertical margin
  585. */
  586. void QwtSlider::setMargins( int xMargin, int yMargin )
  587. {
  588. if ( xMargin < 0 )
  589. xMargin = 0;
  590. if ( yMargin < 0 )
  591. yMargin = 0;
  592. if ( xMargin != d_data->xMargin || yMargin != d_data->yMargin )
  593. {
  594. d_data->xMargin = xMargin;
  595. d_data->yMargin = yMargin;
  596. layoutSlider();
  597. }
  598. }
  599. /*!
  600. Set the background style.
  601. */
  602. void QwtSlider::setBgStyle( BGSTYLE st )
  603. {
  604. d_data->bgStyle = st;
  605. layoutSlider();
  606. }
  607. /*!
  608. \return the background style.
  609. */
  610. QwtSlider::BGSTYLE QwtSlider::bgStyle() const
  611. {
  612. return d_data->bgStyle;
  613. }
  614. /*!
  615. \return the thumb length.
  616. */
  617. int QwtSlider::thumbLength() const
  618. {
  619. return d_data->thumbLength;
  620. }
  621. /*!
  622. \return the thumb width.
  623. */
  624. int QwtSlider::thumbWidth() const
  625. {
  626. return d_data->thumbWidth;
  627. }
  628. /*!
  629. \return the border width.
  630. */
  631. int QwtSlider::borderWidth() const
  632. {
  633. return d_data->borderWidth;
  634. }
  635. /*!
  636. \return QwtSlider::minimumSizeHint()
  637. */
  638. QSize QwtSlider::sizeHint() const
  639. {
  640. return minimumSizeHint();
  641. }
  642. /*!
  643. \brief Return a minimum size hint
  644. \warning The return value of QwtSlider::minimumSizeHint() depends on
  645. the font and the scale.
  646. */
  647. QSize QwtSlider::minimumSizeHint() const
  648. {
  649. if ( !d_data->sizeHintCache.isEmpty() )
  650. return d_data->sizeHintCache;
  651. int sliderWidth = d_data->thumbWidth;
  652. if ( d_data->bgStyle & BgTrough )
  653. sliderWidth += 2 * d_data->borderWidth;
  654. int w = 0, h = 0;
  655. if ( d_data->scalePos != NoScale )
  656. {
  657. int d1, d2;
  658. scaleDraw()->getBorderDistHint( font(), d1, d2 );
  659. int msMbd = qMax( d1, d2 );
  660. int mbd = d_data->thumbLength / 2;
  661. if ( d_data->bgStyle & BgTrough )
  662. mbd += d_data->borderWidth;
  663. if ( mbd < msMbd )
  664. mbd = msMbd;
  665. const int sdExtent = qCeil( scaleDraw()->extent( font() ) );
  666. const int sdLength = scaleDraw()->minLength( font() );
  667. h = sliderWidth + sdExtent + d_data->scaleDist;
  668. w = sdLength - 2 * msMbd + 2 * mbd;
  669. }
  670. else // no scale
  671. {
  672. w = 200;
  673. h = sliderWidth;
  674. }
  675. if ( orientation() == Qt::Vertical )
  676. qSwap( w, h );
  677. w += 2 * d_data->xMargin;
  678. h += 2 * d_data->yMargin;
  679. d_data->sizeHintCache = QSize( w, h );
  680. return d_data->sizeHintCache;
  681. }