qwt_slider.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  290. QwtAbstractSlider::fontChange( f );
  291. #endif
  292. layoutSlider();
  293. }
  294. /*!
  295. Draw the slider into the specified rectangle.
  296. \param painter Painter
  297. \param r Rectangle
  298. */
  299. void QwtSlider::drawSlider( QPainter *painter, const QRect &r )
  300. {
  301. QRect cr( r );
  302. if ( d_data->bgStyle & BgTrough )
  303. {
  304. qDrawShadePanel( painter, r.x(), r.y(),
  305. r.width(), r.height(), palette(),
  306. true, d_data->borderWidth, 0 );
  307. cr.setRect( r.x() + d_data->borderWidth,
  308. r.y() + d_data->borderWidth,
  309. r.width() - 2 * d_data->borderWidth,
  310. r.height() - 2 * d_data->borderWidth );
  311. painter->fillRect( cr.x(), cr.y(), cr.width(), cr.height(),
  312. palette().brush( QPalette::Mid ) );
  313. }
  314. if ( d_data->bgStyle & BgSlot )
  315. {
  316. int ws = 4;
  317. int ds = d_data->thumbLength / 2 - 4;
  318. if ( ds < 1 )
  319. ds = 1;
  320. QRect rSlot;
  321. if ( orientation() == Qt::Horizontal )
  322. {
  323. if ( cr.height() & 1 )
  324. ws++;
  325. rSlot = QRect( cr.x() + ds,
  326. cr.y() + ( cr.height() - ws ) / 2,
  327. cr.width() - 2 * ds, ws );
  328. }
  329. else
  330. {
  331. if ( cr.width() & 1 )
  332. ws++;
  333. rSlot = QRect( cr.x() + ( cr.width() - ws ) / 2,
  334. cr.y() + ds,
  335. ws, cr.height() - 2 * ds );
  336. }
  337. painter->fillRect( rSlot.x(), rSlot.y(), rSlot.width(), rSlot.height(),
  338. palette().brush( QPalette::Dark ) );
  339. qDrawShadePanel( painter, rSlot.x(), rSlot.y(),
  340. rSlot.width(), rSlot.height(), palette(),
  341. true, 1 , 0 );
  342. }
  343. if ( isValid() )
  344. drawThumb( painter, cr, xyPosition( value() ) );
  345. }
  346. /*!
  347. Draw the thumb at a position
  348. \param painter Painter
  349. \param sliderRect Bounding rectangle of the slider
  350. \param pos Position of the slider thumb
  351. */
  352. void QwtSlider::drawThumb( QPainter *painter, const QRect &sliderRect, int pos )
  353. {
  354. pos++; // shade line points one pixel below
  355. if ( orientation() == Qt::Horizontal )
  356. {
  357. qDrawShadePanel( painter, pos - d_data->thumbLength / 2,
  358. sliderRect.y(), d_data->thumbLength, sliderRect.height(),
  359. palette(), false, d_data->borderWidth,
  360. &palette().brush( QPalette::Button ) );
  361. qDrawShadeLine( painter, pos, sliderRect.y(),
  362. pos, sliderRect.y() + sliderRect.height() - 2,
  363. palette(), true, 1 );
  364. }
  365. else // Vertical
  366. {
  367. qDrawShadePanel( painter, sliderRect.x(), pos - d_data->thumbLength / 2,
  368. sliderRect.width(), d_data->thumbLength,
  369. palette(), false, d_data->borderWidth,
  370. &palette().brush( QPalette::Button ) );
  371. qDrawShadeLine( painter, sliderRect.x(), pos,
  372. sliderRect.x() + sliderRect.width() - 2, pos,
  373. palette(), true, 1 );
  374. }
  375. }
  376. /*!
  377. Find the x/y position for a given value v
  378. \param value Value
  379. */
  380. int QwtSlider::xyPosition( double value ) const
  381. {
  382. return qRound( d_data->map.transform( value ) );
  383. }
  384. /*!
  385. Determine the value corresponding to a specified mouse location.
  386. \param pos Mouse position
  387. */
  388. double QwtSlider::getValue( const QPoint &pos )
  389. {
  390. return d_data->map.invTransform(
  391. orientation() == Qt::Horizontal ? pos.x() : pos.y() );
  392. }
  393. /*!
  394. \brief Determine scrolling mode and direction
  395. \param p point
  396. \param scrollMode Scrolling mode
  397. \param direction Direction
  398. */
  399. void QwtSlider::getScrollMode( const QPoint &p,
  400. int &scrollMode, int &direction )
  401. {
  402. if ( !d_data->sliderRect.contains( p ) )
  403. {
  404. scrollMode = ScrNone;
  405. direction = 0;
  406. return;
  407. }
  408. const int pos = ( orientation() == Qt::Horizontal ) ? p.x() : p.y();
  409. const int markerPos = xyPosition( value() );
  410. if ( ( pos > markerPos - d_data->thumbLength / 2 )
  411. && ( pos < markerPos + d_data->thumbLength / 2 ) )
  412. {
  413. scrollMode = ScrMouse;
  414. direction = 0;
  415. return;
  416. }
  417. scrollMode = ScrPage;
  418. direction = ( pos > markerPos ) ? 1 : -1;
  419. if ( scaleDraw()->map().p1() > scaleDraw()->map().p2() )
  420. direction = -direction;
  421. }
  422. /*!
  423. Qt paint event
  424. \param event Paint event
  425. */
  426. void QwtSlider::paintEvent( QPaintEvent *event )
  427. {
  428. const QRect &ur = event->rect();
  429. if ( ur.isValid() )
  430. {
  431. QPainter painter( this );
  432. draw( &painter, ur );
  433. }
  434. }
  435. //! Draw the QwtSlider
  436. void QwtSlider::draw( QPainter *painter, const QRect& )
  437. {
  438. if ( d_data->scalePos != NoScale )
  439. scaleDraw()->draw( painter, palette() );
  440. drawSlider( painter, d_data->sliderRect );
  441. if ( hasFocus() )
  442. QwtPainter::drawFocusRect( painter, this, d_data->sliderRect );
  443. }
  444. //! Qt resize event
  445. void QwtSlider::resizeEvent( QResizeEvent * )
  446. {
  447. layoutSlider( false );
  448. }
  449. /*!
  450. Recalculate the slider's geometry and layout based on
  451. the current rect and fonts.
  452. \param update_geometry notify the layout system and call update
  453. to redraw the scale
  454. */
  455. void QwtSlider::layoutSlider( bool update_geometry )
  456. {
  457. int sliderWidth = d_data->thumbWidth;
  458. int sld1 = d_data->thumbLength / 2 - 1;
  459. int sld2 = d_data->thumbLength / 2 + d_data->thumbLength % 2;
  460. if ( d_data->bgStyle & BgTrough )
  461. {
  462. sliderWidth += 2 * d_data->borderWidth;
  463. sld1 += d_data->borderWidth;
  464. sld2 += d_data->borderWidth;
  465. }
  466. int scd = 0;
  467. if ( d_data->scalePos != NoScale )
  468. {
  469. int d1, d2;
  470. scaleDraw()->getBorderDistHint( font(), d1, d2 );
  471. scd = qMax( d1, d2 );
  472. }
  473. int slo = scd - sld1;
  474. if ( slo < 0 )
  475. slo = 0;
  476. int x, y, length;
  477. const QRect r = rect();
  478. if ( orientation() == Qt::Horizontal )
  479. {
  480. switch ( d_data->scalePos )
  481. {
  482. case TopScale:
  483. {
  484. d_data->sliderRect.setRect(
  485. r.x() + d_data->xMargin + slo,
  486. r.y() + r.height() -
  487. d_data->yMargin - sliderWidth,
  488. r.width() - 2 * d_data->xMargin - 2 * slo,
  489. sliderWidth );
  490. x = d_data->sliderRect.x() + sld1;
  491. y = d_data->sliderRect.y() - d_data->scaleDist;
  492. break;
  493. }
  494. case BottomScale:
  495. {
  496. d_data->sliderRect.setRect(
  497. r.x() + d_data->xMargin + slo,
  498. r.y() + d_data->yMargin,
  499. r.width() - 2 * d_data->xMargin - 2 * slo,
  500. sliderWidth );
  501. x = d_data->sliderRect.x() + sld1;
  502. y = d_data->sliderRect.y() + d_data->sliderRect.height()
  503. + d_data->scaleDist;
  504. break;
  505. }
  506. case NoScale: // like Bottom, but no scale. See QwtSlider().
  507. default: // inconsistent orientation and scale position
  508. {
  509. d_data->sliderRect.setRect(
  510. r.x() + d_data->xMargin + slo,
  511. r.y() + d_data->yMargin,
  512. r.width() - 2 * d_data->xMargin - 2 * slo,
  513. sliderWidth );
  514. x = d_data->sliderRect.x() + sld1;
  515. y = 0;
  516. break;
  517. }
  518. }
  519. length = d_data->sliderRect.width() - ( sld1 + sld2 );
  520. }
  521. else // if (orientation() == Qt::Vertical
  522. {
  523. switch ( d_data->scalePos )
  524. {
  525. case RightScale:
  526. d_data->sliderRect.setRect(
  527. r.x() + d_data->xMargin,
  528. r.y() + d_data->yMargin + slo,
  529. sliderWidth,
  530. r.height() - 2 * d_data->yMargin - 2 * slo );
  531. x = d_data->sliderRect.x() + d_data->sliderRect.width()
  532. + d_data->scaleDist;
  533. y = d_data->sliderRect.y() + sld1;
  534. break;
  535. case LeftScale:
  536. d_data->sliderRect.setRect(
  537. r.x() + r.width() - sliderWidth - d_data->xMargin,
  538. r.y() + d_data->yMargin + slo,
  539. sliderWidth,
  540. r.height() - 2 * d_data->yMargin - 2 * slo );
  541. x = d_data->sliderRect.x() - d_data->scaleDist;
  542. y = d_data->sliderRect.y() + sld1;
  543. break;
  544. case NoScale: // like Left, but no scale. See QwtSlider().
  545. default: // inconsistent orientation and scale position
  546. d_data->sliderRect.setRect(
  547. r.x() + r.width() - sliderWidth - d_data->xMargin,
  548. r.y() + d_data->yMargin + slo,
  549. sliderWidth,
  550. r.height() - 2 * d_data->yMargin - 2 * slo );
  551. x = 0;
  552. y = d_data->sliderRect.y() + sld1;
  553. break;
  554. }
  555. length = d_data->sliderRect.height() - ( sld1 + sld2 );
  556. }
  557. scaleDraw()->move( x, y );
  558. scaleDraw()->setLength( length );
  559. d_data->map.setPaintInterval( scaleDraw()->map().p1(),
  560. scaleDraw()->map().p2() );
  561. if ( update_geometry )
  562. {
  563. d_data->sizeHintCache = QSize(); // invalidate
  564. updateGeometry();
  565. update();
  566. }
  567. }
  568. //! Notify change of value
  569. void QwtSlider::valueChange()
  570. {
  571. QwtAbstractSlider::valueChange();
  572. update();
  573. }
  574. //! Notify change of range
  575. void QwtSlider::rangeChange()
  576. {
  577. d_data->map.setScaleInterval( minValue(), maxValue() );
  578. if ( autoScale() )
  579. rescale( minValue(), maxValue() );
  580. QwtAbstractSlider::rangeChange();
  581. layoutSlider();
  582. }
  583. /*!
  584. \brief Set distances between the widget's border and internals.
  585. \param xMargin Horizontal margin
  586. \param yMargin Vertical margin
  587. */
  588. void QwtSlider::setMargins( int xMargin, int yMargin )
  589. {
  590. if ( xMargin < 0 )
  591. xMargin = 0;
  592. if ( yMargin < 0 )
  593. yMargin = 0;
  594. if ( xMargin != d_data->xMargin || yMargin != d_data->yMargin )
  595. {
  596. d_data->xMargin = xMargin;
  597. d_data->yMargin = yMargin;
  598. layoutSlider();
  599. }
  600. }
  601. /*!
  602. Set the background style.
  603. */
  604. void QwtSlider::setBgStyle( BGSTYLE st )
  605. {
  606. d_data->bgStyle = st;
  607. layoutSlider();
  608. }
  609. /*!
  610. \return the background style.
  611. */
  612. QwtSlider::BGSTYLE QwtSlider::bgStyle() const
  613. {
  614. return d_data->bgStyle;
  615. }
  616. /*!
  617. \return the thumb length.
  618. */
  619. int QwtSlider::thumbLength() const
  620. {
  621. return d_data->thumbLength;
  622. }
  623. /*!
  624. \return the thumb width.
  625. */
  626. int QwtSlider::thumbWidth() const
  627. {
  628. return d_data->thumbWidth;
  629. }
  630. /*!
  631. \return the border width.
  632. */
  633. int QwtSlider::borderWidth() const
  634. {
  635. return d_data->borderWidth;
  636. }
  637. /*!
  638. \return QwtSlider::minimumSizeHint()
  639. */
  640. QSize QwtSlider::sizeHint() const
  641. {
  642. return minimumSizeHint();
  643. }
  644. /*!
  645. \brief Return a minimum size hint
  646. \warning The return value of QwtSlider::minimumSizeHint() depends on
  647. the font and the scale.
  648. */
  649. QSize QwtSlider::minimumSizeHint() const
  650. {
  651. if ( !d_data->sizeHintCache.isEmpty() )
  652. return d_data->sizeHintCache;
  653. int sliderWidth = d_data->thumbWidth;
  654. if ( d_data->bgStyle & BgTrough )
  655. sliderWidth += 2 * d_data->borderWidth;
  656. int w = 0, h = 0;
  657. if ( d_data->scalePos != NoScale )
  658. {
  659. int d1, d2;
  660. scaleDraw()->getBorderDistHint( font(), d1, d2 );
  661. int msMbd = qMax( d1, d2 );
  662. int mbd = d_data->thumbLength / 2;
  663. if ( d_data->bgStyle & BgTrough )
  664. mbd += d_data->borderWidth;
  665. if ( mbd < msMbd )
  666. mbd = msMbd;
  667. const int sdExtent = qCeil( scaleDraw()->extent( font() ) );
  668. const int sdLength = scaleDraw()->minLength( font() );
  669. h = sliderWidth + sdExtent + d_data->scaleDist;
  670. w = sdLength - 2 * msMbd + 2 * mbd;
  671. }
  672. else // no scale
  673. {
  674. w = 200;
  675. h = sliderWidth;
  676. }
  677. if ( orientation() == Qt::Vertical )
  678. qSwap( w, h );
  679. w += 2 * d_data->xMargin;
  680. h += 2 * d_data->yMargin;
  681. d_data->sizeHintCache = QSize( w, h );
  682. return d_data->sizeHintCache;
  683. }