ctkRangeSlider.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QMouseEvent>
  17. #include <QKeyEvent>
  18. #include <QStyleOptionSlider>
  19. #include <QApplication>
  20. #include <QStylePainter>
  21. #include <QStyle>
  22. #include <QToolTip>
  23. // CTK includes
  24. #include "ctkRangeSlider.h"
  25. class ctkRangeSliderPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkRangeSlider);
  28. protected:
  29. ctkRangeSlider* const q_ptr;
  30. public:
  31. /// Boolean indicates the selected handle
  32. /// True for the minimum range handle, false for the maximum range handle
  33. enum Handle {
  34. NoHandle = 0x0,
  35. MinimumHandle = 0x1,
  36. MaximumHandle = 0x2
  37. };
  38. Q_DECLARE_FLAGS(Handles, Handle);
  39. ctkRangeSliderPrivate(ctkRangeSlider& object);
  40. void init();
  41. /// Return the handle at the given pos, or none if no handle is at the pos.
  42. /// If a handle is selected, handleRect is set to the handle rect.
  43. /// otherwise return NoHandle and handleRect is set to the combined rect of
  44. /// the min and max handles
  45. Handle handleAtPos(const QPoint& pos, QRect& handleRect)const;
  46. /// Copied verbatim from QSliderPrivate class (see QSlider.cpp)
  47. int pixelPosToRangeValue(int pos) const;
  48. int pixelPosFromRangeValue(int val) const;
  49. /// Draw the bottom and top sliders.
  50. void drawMinimumSlider( QStylePainter* painter ) const;
  51. void drawMaximumSlider( QStylePainter* painter ) const;
  52. /// End points of the range on the Model
  53. int m_MaximumValue;
  54. int m_MinimumValue;
  55. /// End points of the range on the GUI. This is synced with the model.
  56. int m_MaximumPosition;
  57. int m_MinimumPosition;
  58. /// Controls selected ?
  59. QStyle::SubControl m_MinimumSliderSelected;
  60. QStyle::SubControl m_MaximumSliderSelected;
  61. /// See QSliderPrivate::clickOffset.
  62. /// Overrides this ivar
  63. int m_SubclassClickOffset;
  64. /// See QSliderPrivate::position
  65. /// Overrides this ivar.
  66. int m_SubclassPosition;
  67. /// Original width between the 2 bounds before any moves
  68. int m_SubclassWidth;
  69. ctkRangeSliderPrivate::Handles m_SelectedHandles;
  70. /// When symmetricMoves is true, moving a handle will move the other handle
  71. /// symmetrically, otherwise the handles are independent.
  72. bool m_SymmetricMoves;
  73. QString m_HandleToolTip;
  74. };
  75. // --------------------------------------------------------------------------
  76. ctkRangeSliderPrivate::ctkRangeSliderPrivate(ctkRangeSlider& object)
  77. :q_ptr(&object)
  78. {
  79. this->m_MinimumValue = 0;
  80. this->m_MaximumValue = 100;
  81. this->m_MinimumPosition = 0;
  82. this->m_MaximumPosition = 100;
  83. this->m_MinimumSliderSelected = QStyle::SC_None;
  84. this->m_MaximumSliderSelected = QStyle::SC_None;
  85. this->m_SubclassClickOffset = 0;
  86. this->m_SubclassPosition = 0;
  87. this->m_SubclassWidth = 0;
  88. this->m_SelectedHandles = 0;
  89. this->m_SymmetricMoves = false;
  90. }
  91. // --------------------------------------------------------------------------
  92. void ctkRangeSliderPrivate::init()
  93. {
  94. Q_Q(ctkRangeSlider);
  95. this->m_MinimumValue = q->minimum();
  96. this->m_MaximumValue = q->maximum();
  97. this->m_MinimumPosition = q->minimum();
  98. this->m_MaximumPosition = q->maximum();
  99. q->connect(q, SIGNAL(rangeChanged(int,int)), q, SLOT(onRangeChanged(int,int)));
  100. }
  101. // --------------------------------------------------------------------------
  102. ctkRangeSliderPrivate::Handle ctkRangeSliderPrivate::handleAtPos(const QPoint& pos, QRect& handleRect)const
  103. {
  104. Q_Q(const ctkRangeSlider);
  105. QStyleOptionSlider option;
  106. q->initStyleOption( &option );
  107. // The functinos hitTestComplexControl only know about 1 handle. As we have
  108. // 2, we change the position of the handle and test if the pos correspond to
  109. // any of the 2 positions.
  110. // Test the MinimumHandle
  111. option.sliderPosition = this->m_MinimumPosition;
  112. option.sliderValue = this->m_MinimumValue;
  113. QStyle::SubControl minimumControl = q->style()->hitTestComplexControl(
  114. QStyle::CC_Slider, &option, pos, q);
  115. QRect minimumHandleRect = q->style()->subControlRect(
  116. QStyle::CC_Slider, &option, QStyle::SC_SliderHandle, q);
  117. // Test if the pos is under the Maximum handle
  118. option.sliderPosition = this->m_MaximumPosition;
  119. option.sliderValue = this->m_MaximumValue;
  120. QStyle::SubControl maximumControl = q->style()->hitTestComplexControl(
  121. QStyle::CC_Slider, &option, pos, q);
  122. QRect maximumHandleRect = q->style()->subControlRect(
  123. QStyle::CC_Slider, &option, QStyle::SC_SliderHandle, q);
  124. // The pos is above both handles, select the closest handle
  125. if (minimumControl == QStyle::SC_SliderHandle &&
  126. maximumControl == QStyle::SC_SliderHandle)
  127. {
  128. int minDist = 0;
  129. int maxDist = 0;
  130. if (q->orientation() == Qt::Horizontal)
  131. {
  132. minDist = pos.x() - minimumHandleRect.left();
  133. maxDist = maximumHandleRect.right() - pos.x();
  134. }
  135. else //if (q->orientation() == Qt::Vertical)
  136. {
  137. minDist = minimumHandleRect.bottom() - pos.y();
  138. maxDist = pos.y() - maximumHandleRect.top();
  139. }
  140. Q_ASSERT( minDist >= 0 && maxDist >= 0);
  141. minimumControl = minDist < maxDist ? minimumControl : QStyle::SC_None;
  142. }
  143. if (minimumControl == QStyle::SC_SliderHandle)
  144. {
  145. handleRect = minimumHandleRect;
  146. return MinimumHandle;
  147. }
  148. else if (maximumControl == QStyle::SC_SliderHandle)
  149. {
  150. handleRect = maximumHandleRect;
  151. return MaximumHandle;
  152. }
  153. handleRect = minimumHandleRect.united(maximumHandleRect);
  154. return NoHandle;
  155. }
  156. // --------------------------------------------------------------------------
  157. // Copied verbatim from QSliderPrivate::pixelPosToRangeValue. See QSlider.cpp
  158. //
  159. int ctkRangeSliderPrivate::pixelPosToRangeValue( int pos ) const
  160. {
  161. Q_Q(const ctkRangeSlider);
  162. QStyleOptionSlider option;
  163. q->initStyleOption( &option );
  164. QRect gr = q->style()->subControlRect( QStyle::CC_Slider,
  165. &option,
  166. QStyle::SC_SliderGroove,
  167. q );
  168. QRect sr = q->style()->subControlRect( QStyle::CC_Slider,
  169. &option,
  170. QStyle::SC_SliderHandle,
  171. q );
  172. int sliderMin, sliderMax, sliderLength;
  173. if (option.orientation == Qt::Horizontal)
  174. {
  175. sliderLength = sr.width();
  176. sliderMin = gr.x();
  177. sliderMax = gr.right() - sliderLength + 1;
  178. }
  179. else
  180. {
  181. sliderLength = sr.height();
  182. sliderMin = gr.y();
  183. sliderMax = gr.bottom() - sliderLength + 1;
  184. }
  185. return QStyle::sliderValueFromPosition( q->minimum(),
  186. q->maximum(),
  187. pos - sliderMin,
  188. sliderMax - sliderMin,
  189. option.upsideDown );
  190. }
  191. //---------------------------------------------------------------------------
  192. int ctkRangeSliderPrivate::pixelPosFromRangeValue( int val ) const
  193. {
  194. Q_Q(const ctkRangeSlider);
  195. QStyleOptionSlider option;
  196. q->initStyleOption( &option );
  197. QRect gr = q->style()->subControlRect( QStyle::CC_Slider,
  198. &option,
  199. QStyle::SC_SliderGroove,
  200. q );
  201. QRect sr = q->style()->subControlRect( QStyle::CC_Slider,
  202. &option,
  203. QStyle::SC_SliderHandle,
  204. q );
  205. int sliderMin, sliderMax, sliderLength;
  206. if (option.orientation == Qt::Horizontal)
  207. {
  208. sliderLength = sr.width();
  209. sliderMin = gr.x();
  210. sliderMax = gr.right() - sliderLength + 1;
  211. }
  212. else
  213. {
  214. sliderLength = sr.height();
  215. sliderMin = gr.y();
  216. sliderMax = gr.bottom() - sliderLength + 1;
  217. }
  218. return QStyle::sliderPositionFromValue( q->minimum(),
  219. q->maximum(),
  220. val,
  221. sliderMax - sliderMin,
  222. option.upsideDown ) + sliderMin;
  223. }
  224. //---------------------------------------------------------------------------
  225. // Draw slider at the bottom end of the range
  226. void ctkRangeSliderPrivate::drawMinimumSlider( QStylePainter* painter ) const
  227. {
  228. Q_Q(const ctkRangeSlider);
  229. QStyleOptionSlider option;
  230. q->initMinimumSliderStyleOption( &option );
  231. option.subControls = QStyle::SC_SliderHandle;
  232. option.sliderValue = m_MinimumValue;
  233. option.sliderPosition = m_MinimumPosition;
  234. if (q->isMinimumSliderDown())
  235. {
  236. option.activeSubControls = QStyle::SC_SliderHandle;
  237. option.state |= QStyle::State_Sunken;
  238. }
  239. #ifdef Q_OS_MAC
  240. // On mac style, drawing just the handle actually draws also the groove.
  241. QRect clip = q->style()->subControlRect(QStyle::CC_Slider, &option,
  242. QStyle::SC_SliderHandle, q);
  243. painter->setClipRect(clip);
  244. #endif
  245. painter->drawComplexControl(QStyle::CC_Slider, option);
  246. }
  247. //---------------------------------------------------------------------------
  248. // Draw slider at the top end of the range
  249. void ctkRangeSliderPrivate::drawMaximumSlider( QStylePainter* painter ) const
  250. {
  251. Q_Q(const ctkRangeSlider);
  252. QStyleOptionSlider option;
  253. q->initMaximumSliderStyleOption( &option );
  254. option.subControls = QStyle::SC_SliderHandle;
  255. option.sliderValue = m_MaximumValue;
  256. option.sliderPosition = m_MaximumPosition;
  257. if (q->isMaximumSliderDown())
  258. {
  259. option.activeSubControls = QStyle::SC_SliderHandle;
  260. option.state |= QStyle::State_Sunken;
  261. }
  262. #ifdef Q_OS_MAC
  263. // On mac style, drawing just the handle actually draws also the groove.
  264. QRect clip = q->style()->subControlRect(QStyle::CC_Slider, &option,
  265. QStyle::SC_SliderHandle, q);
  266. painter->setClipRect(clip);
  267. #endif
  268. painter->drawComplexControl(QStyle::CC_Slider, option);
  269. }
  270. // --------------------------------------------------------------------------
  271. ctkRangeSlider::ctkRangeSlider(QWidget* parent)
  272. : QSlider(parent)
  273. , d_ptr(new ctkRangeSliderPrivate(*this))
  274. {
  275. Q_D(ctkRangeSlider);
  276. d->init();
  277. }
  278. // --------------------------------------------------------------------------
  279. ctkRangeSlider::ctkRangeSlider( Qt::Orientation o,
  280. QWidget* parentObject )
  281. :QSlider(o, parentObject)
  282. , d_ptr(new ctkRangeSliderPrivate(*this))
  283. {
  284. Q_D(ctkRangeSlider);
  285. d->init();
  286. }
  287. // --------------------------------------------------------------------------
  288. ctkRangeSlider::ctkRangeSlider(ctkRangeSliderPrivate* impl, QWidget* parent)
  289. : QSlider(parent)
  290. , d_ptr(impl)
  291. {
  292. Q_D(ctkRangeSlider);
  293. d->init();
  294. }
  295. // --------------------------------------------------------------------------
  296. ctkRangeSlider::ctkRangeSlider( ctkRangeSliderPrivate* impl, Qt::Orientation o,
  297. QWidget* parentObject )
  298. :QSlider(o, parentObject)
  299. , d_ptr(impl)
  300. {
  301. Q_D(ctkRangeSlider);
  302. d->init();
  303. }
  304. // --------------------------------------------------------------------------
  305. ctkRangeSlider::~ctkRangeSlider()
  306. {
  307. }
  308. // --------------------------------------------------------------------------
  309. int ctkRangeSlider::minimumValue() const
  310. {
  311. Q_D(const ctkRangeSlider);
  312. return d->m_MinimumValue;
  313. }
  314. // --------------------------------------------------------------------------
  315. void ctkRangeSlider::setMinimumValue( int min )
  316. {
  317. Q_D(ctkRangeSlider);
  318. this->setValues( min, qMax(d->m_MaximumValue,min) );
  319. }
  320. // --------------------------------------------------------------------------
  321. int ctkRangeSlider::maximumValue() const
  322. {
  323. Q_D(const ctkRangeSlider);
  324. return d->m_MaximumValue;
  325. }
  326. // --------------------------------------------------------------------------
  327. void ctkRangeSlider::setMaximumValue( int max )
  328. {
  329. Q_D(ctkRangeSlider);
  330. this->setValues( qMin(d->m_MinimumValue, max), max );
  331. }
  332. // --------------------------------------------------------------------------
  333. void ctkRangeSlider::setValues(int l, int u)
  334. {
  335. Q_D(ctkRangeSlider);
  336. const int minimumValue =
  337. qBound(this->minimum(), qMin(l,u), this->maximum());
  338. const int maximumValue =
  339. qBound(this->minimum(), qMax(l,u), this->maximum());
  340. bool emitMinValChanged = (minimumValue != d->m_MinimumValue);
  341. bool emitMaxValChanged = (maximumValue != d->m_MaximumValue);
  342. d->m_MinimumValue = minimumValue;
  343. d->m_MaximumValue = maximumValue;
  344. bool emitMinPosChanged =
  345. (minimumValue != d->m_MinimumPosition);
  346. bool emitMaxPosChanged =
  347. (maximumValue != d->m_MaximumPosition);
  348. d->m_MinimumPosition = minimumValue;
  349. d->m_MaximumPosition = maximumValue;
  350. if (isSliderDown())
  351. {
  352. if (emitMinPosChanged || emitMaxPosChanged)
  353. {
  354. emit positionsChanged(d->m_MinimumPosition, d->m_MaximumPosition);
  355. }
  356. if (emitMinPosChanged)
  357. {
  358. emit minimumPositionChanged(d->m_MinimumPosition);
  359. }
  360. if (emitMaxPosChanged)
  361. {
  362. emit maximumPositionChanged(d->m_MaximumPosition);
  363. }
  364. }
  365. if (emitMinValChanged || emitMaxValChanged)
  366. {
  367. emit valuesChanged(d->m_MinimumValue,
  368. d->m_MaximumValue);
  369. }
  370. if (emitMinValChanged)
  371. {
  372. emit minimumValueChanged(d->m_MinimumValue);
  373. }
  374. if (emitMaxValChanged)
  375. {
  376. emit maximumValueChanged(d->m_MaximumValue);
  377. }
  378. if (emitMinPosChanged || emitMaxPosChanged ||
  379. emitMinValChanged || emitMaxValChanged)
  380. {
  381. this->update();
  382. }
  383. }
  384. // --------------------------------------------------------------------------
  385. int ctkRangeSlider::minimumPosition() const
  386. {
  387. Q_D(const ctkRangeSlider);
  388. return d->m_MinimumPosition;
  389. }
  390. // --------------------------------------------------------------------------
  391. int ctkRangeSlider::maximumPosition() const
  392. {
  393. Q_D(const ctkRangeSlider);
  394. return d->m_MaximumPosition;
  395. }
  396. // --------------------------------------------------------------------------
  397. void ctkRangeSlider::setMinimumPosition(int l)
  398. {
  399. Q_D(const ctkRangeSlider);
  400. this->setPositions(l, qMax(l, d->m_MaximumPosition));
  401. }
  402. // --------------------------------------------------------------------------
  403. void ctkRangeSlider::setMaximumPosition(int u)
  404. {
  405. Q_D(const ctkRangeSlider);
  406. this->setPositions(qMin(d->m_MinimumPosition, u), u);
  407. }
  408. // --------------------------------------------------------------------------
  409. void ctkRangeSlider::setPositions(int min, int max)
  410. {
  411. Q_D(ctkRangeSlider);
  412. const int minPosition =
  413. qBound(this->minimum(), qMin(min, max), this->maximum());
  414. const int maxPosition =
  415. qBound(this->minimum(), qMax(min, max), this->maximum());
  416. bool emitMinPosChanged = (minPosition != d->m_MinimumPosition);
  417. bool emitMaxPosChanged = (maxPosition != d->m_MaximumPosition);
  418. if (!emitMinPosChanged && !emitMaxPosChanged)
  419. {
  420. return;
  421. }
  422. d->m_MinimumPosition = minPosition;
  423. d->m_MaximumPosition = maxPosition;
  424. if (!this->hasTracking())
  425. {
  426. this->update();
  427. }
  428. if (isSliderDown())
  429. {
  430. if (emitMinPosChanged)
  431. {
  432. emit minimumPositionChanged(d->m_MinimumPosition);
  433. }
  434. if (emitMaxPosChanged)
  435. {
  436. emit maximumPositionChanged(d->m_MaximumPosition);
  437. }
  438. if (emitMinPosChanged || emitMaxPosChanged)
  439. {
  440. emit positionsChanged(d->m_MinimumPosition, d->m_MaximumPosition);
  441. }
  442. }
  443. if (this->hasTracking())
  444. {
  445. this->triggerAction(SliderMove);
  446. this->setValues(d->m_MinimumPosition, d->m_MaximumPosition);
  447. }
  448. }
  449. // --------------------------------------------------------------------------
  450. void ctkRangeSlider::setSymmetricMoves(bool symmetry)
  451. {
  452. Q_D(ctkRangeSlider);
  453. d->m_SymmetricMoves = symmetry;
  454. }
  455. // --------------------------------------------------------------------------
  456. bool ctkRangeSlider::symmetricMoves()const
  457. {
  458. Q_D(const ctkRangeSlider);
  459. return d->m_SymmetricMoves;
  460. }
  461. // --------------------------------------------------------------------------
  462. void ctkRangeSlider::onRangeChanged(int minimum, int maximum)
  463. {
  464. Q_UNUSED(minimum);
  465. Q_UNUSED(maximum);
  466. Q_D(ctkRangeSlider);
  467. this->setValues(d->m_MinimumValue, d->m_MaximumValue);
  468. }
  469. // --------------------------------------------------------------------------
  470. // Render
  471. void ctkRangeSlider::paintEvent( QPaintEvent* )
  472. {
  473. Q_D(ctkRangeSlider);
  474. QStyleOptionSlider option;
  475. this->initStyleOption(&option);
  476. QStylePainter painter(this);
  477. option.subControls = QStyle::SC_SliderGroove;
  478. // Move to minimum to not highlight the SliderGroove.
  479. // On mac style, drawing just the slider groove also draws the handles,
  480. // therefore we give a negative (outside of view) position.
  481. option.sliderValue = this->minimum() - this->maximum();
  482. option.sliderPosition = this->minimum() - this->maximum();
  483. painter.drawComplexControl(QStyle::CC_Slider, option);
  484. option.sliderPosition = d->m_MinimumPosition;
  485. const QRect lr = style()->subControlRect( QStyle::CC_Slider,
  486. &option,
  487. QStyle::SC_SliderHandle,
  488. this);
  489. option.sliderPosition = d->m_MaximumPosition;
  490. const QRect ur = style()->subControlRect( QStyle::CC_Slider,
  491. &option,
  492. QStyle::SC_SliderHandle,
  493. this);
  494. QRect sr = style()->subControlRect( QStyle::CC_Slider,
  495. &option,
  496. QStyle::SC_SliderGroove,
  497. this);
  498. QRect rangeBox;
  499. if (option.orientation == Qt::Horizontal)
  500. {
  501. rangeBox = QRect(
  502. QPoint(qMin( lr.center().x(), ur.center().x() ), sr.center().y() - 2),
  503. QPoint(qMax( lr.center().x(), ur.center().x() ), sr.center().y() + 1));
  504. }
  505. else
  506. {
  507. rangeBox = QRect(
  508. QPoint(sr.center().x() - 2, qMin( lr.center().y(), ur.center().y() )),
  509. QPoint(sr.center().x() + 1, qMax( lr.center().y(), ur.center().y() )));
  510. }
  511. // -----------------------------
  512. // Render the range
  513. //
  514. QRect groove = this->style()->subControlRect( QStyle::CC_Slider,
  515. &option,
  516. QStyle::SC_SliderGroove,
  517. this );
  518. groove.adjust(0, 0, -1, 0);
  519. // Create default colors based on the transfer function.
  520. //
  521. QColor highlight = this->palette().color(QPalette::Normal, QPalette::Highlight);
  522. QLinearGradient gradient;
  523. if (option.orientation == Qt::Horizontal)
  524. {
  525. gradient = QLinearGradient( groove.center().x(), groove.top(),
  526. groove.center().x(), groove.bottom());
  527. }
  528. else
  529. {
  530. gradient = QLinearGradient( groove.left(), groove.center().y(),
  531. groove.right(), groove.center().y());
  532. }
  533. // TODO: Set this based on the supplied transfer function
  534. //QColor l = Qt::darkGray;
  535. //QColor u = Qt::black;
  536. gradient.setColorAt(0, highlight.darker(120));
  537. gradient.setColorAt(1, highlight.lighter(160));
  538. painter.setPen(QPen(highlight.darker(150), 0));
  539. painter.setBrush(gradient);
  540. painter.drawRect( rangeBox.intersected(groove) );
  541. // -----------------------------------
  542. // Render the sliders
  543. //
  544. if (this->isMinimumSliderDown())
  545. {
  546. d->drawMaximumSlider( &painter );
  547. d->drawMinimumSlider( &painter );
  548. }
  549. else
  550. {
  551. d->drawMinimumSlider( &painter );
  552. d->drawMaximumSlider( &painter );
  553. }
  554. }
  555. // --------------------------------------------------------------------------
  556. // Standard Qt UI events
  557. void ctkRangeSlider::mousePressEvent(QMouseEvent* mouseEvent)
  558. {
  559. Q_D(ctkRangeSlider);
  560. if (minimum() == maximum() || (mouseEvent->buttons() ^ mouseEvent->button()))
  561. {
  562. mouseEvent->ignore();
  563. return;
  564. }
  565. int pos = this->orientation() == Qt::Horizontal ?
  566. mouseEvent->pos().x() : mouseEvent->pos().y();
  567. QStyleOptionSlider option;
  568. this->initStyleOption( &option );
  569. QRect handleRect;
  570. ctkRangeSliderPrivate::Handle handle = d->handleAtPos(mouseEvent->pos(), handleRect);
  571. if (handle != ctkRangeSliderPrivate::NoHandle)
  572. {
  573. d->m_SubclassPosition = (handle == ctkRangeSliderPrivate::MinimumHandle)?
  574. d->m_MinimumPosition : d->m_MaximumPosition;
  575. // save the position of the mouse inside the handle for later
  576. d->m_SubclassClickOffset = pos - (this->orientation() == Qt::Horizontal ?
  577. handleRect.left() : handleRect.top());
  578. this->setSliderDown(true);
  579. if (d->m_SelectedHandles != handle)
  580. {
  581. d->m_SelectedHandles = handle;
  582. this->update(handleRect);
  583. }
  584. // Accept the mouseEvent
  585. mouseEvent->accept();
  586. return;
  587. }
  588. // if we are here, no handles have been pressed
  589. // Check if we pressed on the groove between the 2 handles
  590. QStyle::SubControl control = this->style()->hitTestComplexControl(
  591. QStyle::CC_Slider, &option, mouseEvent->pos(), this);
  592. QRect sr = style()->subControlRect(
  593. QStyle::CC_Slider, &option, QStyle::SC_SliderGroove, this);
  594. int minCenter = (this->orientation() == Qt::Horizontal ?
  595. handleRect.left() : handleRect.top());
  596. int maxCenter = (this->orientation() == Qt::Horizontal ?
  597. handleRect.right() : handleRect.bottom());
  598. if (control == QStyle::SC_SliderGroove &&
  599. pos > minCenter && pos < maxCenter)
  600. {
  601. // warning lost of precision it might be fatal
  602. d->m_SubclassPosition = (d->m_MinimumPosition + d->m_MaximumPosition) / 2.;
  603. d->m_SubclassClickOffset = pos - d->pixelPosFromRangeValue(d->m_SubclassPosition);
  604. d->m_SubclassWidth = (d->m_MaximumPosition - d->m_MinimumPosition) / 2;
  605. qMax(d->m_SubclassPosition - d->m_MinimumPosition, d->m_MaximumPosition - d->m_SubclassPosition);
  606. this->setSliderDown(true);
  607. if (!this->isMinimumSliderDown() || !this->isMaximumSliderDown())
  608. {
  609. d->m_SelectedHandles =
  610. QFlags<ctkRangeSliderPrivate::Handle>(ctkRangeSliderPrivate::MinimumHandle) |
  611. QFlags<ctkRangeSliderPrivate::Handle>(ctkRangeSliderPrivate::MaximumHandle);
  612. this->update(handleRect.united(sr));
  613. }
  614. mouseEvent->accept();
  615. return;
  616. }
  617. mouseEvent->ignore();
  618. }
  619. // --------------------------------------------------------------------------
  620. // Standard Qt UI events
  621. void ctkRangeSlider::mouseMoveEvent(QMouseEvent* mouseEvent)
  622. {
  623. Q_D(ctkRangeSlider);
  624. if (!d->m_SelectedHandles)
  625. {
  626. mouseEvent->ignore();
  627. return;
  628. }
  629. int pos = this->orientation() == Qt::Horizontal ?
  630. mouseEvent->pos().x() : mouseEvent->pos().y();
  631. QStyleOptionSlider option;
  632. this->initStyleOption(&option);
  633. const int m = style()->pixelMetric( QStyle::PM_MaximumDragDistance, &option, this );
  634. int newPosition = d->pixelPosToRangeValue(pos - d->m_SubclassClickOffset);
  635. if (m >= 0)
  636. {
  637. const QRect r = rect().adjusted(-m, -m, m, m);
  638. if (!r.contains(mouseEvent->pos()))
  639. {
  640. newPosition = d->m_SubclassPosition;
  641. }
  642. }
  643. // Only the lower/left slider is down
  644. if (this->isMinimumSliderDown() && !this->isMaximumSliderDown())
  645. {
  646. double newMinPos = qMin(newPosition,d->m_MaximumPosition);
  647. this->setPositions(newMinPos, d->m_MaximumPosition +
  648. (d->m_SymmetricMoves ? d->m_MinimumPosition - newMinPos : 0));
  649. }
  650. // Only the upper/right slider is down
  651. else if (this->isMaximumSliderDown() && !this->isMinimumSliderDown())
  652. {
  653. double newMaxPos = qMax(d->m_MinimumPosition, newPosition);
  654. this->setPositions(d->m_MinimumPosition -
  655. (d->m_SymmetricMoves ? newMaxPos - d->m_MaximumPosition: 0),
  656. newMaxPos);
  657. }
  658. // Both handles are down (the user clicked in between the handles)
  659. else if (this->isMinimumSliderDown() && this->isMaximumSliderDown())
  660. {
  661. this->setPositions(newPosition - d->m_SubclassWidth,
  662. newPosition + d->m_SubclassWidth );
  663. }
  664. mouseEvent->accept();
  665. }
  666. // --------------------------------------------------------------------------
  667. // Standard Qt UI mouseEvents
  668. void ctkRangeSlider::mouseReleaseEvent(QMouseEvent* mouseEvent)
  669. {
  670. Q_D(ctkRangeSlider);
  671. this->QSlider::mouseReleaseEvent(mouseEvent);
  672. setSliderDown(false);
  673. d->m_SelectedHandles = 0;
  674. this->update();
  675. }
  676. // --------------------------------------------------------------------------
  677. bool ctkRangeSlider::isMinimumSliderDown()const
  678. {
  679. Q_D(const ctkRangeSlider);
  680. return d->m_SelectedHandles & ctkRangeSliderPrivate::MinimumHandle;
  681. }
  682. // --------------------------------------------------------------------------
  683. bool ctkRangeSlider::isMaximumSliderDown()const
  684. {
  685. Q_D(const ctkRangeSlider);
  686. return d->m_SelectedHandles & ctkRangeSliderPrivate::MaximumHandle;
  687. }
  688. // --------------------------------------------------------------------------
  689. void ctkRangeSlider::initMinimumSliderStyleOption(QStyleOptionSlider* option) const
  690. {
  691. this->initStyleOption(option);
  692. }
  693. // --------------------------------------------------------------------------
  694. void ctkRangeSlider::initMaximumSliderStyleOption(QStyleOptionSlider* option) const
  695. {
  696. this->initStyleOption(option);
  697. }
  698. // --------------------------------------------------------------------------
  699. QString ctkRangeSlider::handleToolTip()const
  700. {
  701. Q_D(const ctkRangeSlider);
  702. return d->m_HandleToolTip;
  703. }
  704. // --------------------------------------------------------------------------
  705. void ctkRangeSlider::setHandleToolTip(const QString& toolTip)
  706. {
  707. Q_D(ctkRangeSlider);
  708. d->m_HandleToolTip = toolTip;
  709. }
  710. // --------------------------------------------------------------------------
  711. bool ctkRangeSlider::event(QEvent* event)
  712. {
  713. Q_D(ctkRangeSlider);
  714. switch(event->type())
  715. {
  716. case QEvent::ToolTip:
  717. {
  718. QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
  719. QStyleOptionSlider opt;
  720. // Test the MinimumHandle
  721. opt.sliderPosition = d->m_MinimumPosition;
  722. opt.sliderValue = d->m_MinimumValue;
  723. this->initStyleOption(&opt);
  724. QStyle::SubControl hoveredControl =
  725. this->style()->hitTestComplexControl(
  726. QStyle::CC_Slider, &opt, helpEvent->pos(), this);
  727. if (!d->m_HandleToolTip.isEmpty() &&
  728. hoveredControl == QStyle::SC_SliderHandle)
  729. {
  730. QToolTip::showText(helpEvent->globalPos(), d->m_HandleToolTip.arg(this->minimumValue()));
  731. event->accept();
  732. return true;
  733. }
  734. // Test the MaximumHandle
  735. opt.sliderPosition = d->m_MaximumPosition;
  736. opt.sliderValue = d->m_MaximumValue;
  737. this->initStyleOption(&opt);
  738. hoveredControl = this->style()->hitTestComplexControl(
  739. QStyle::CC_Slider, &opt, helpEvent->pos(), this);
  740. if (!d->m_HandleToolTip.isEmpty() &&
  741. hoveredControl == QStyle::SC_SliderHandle)
  742. {
  743. QToolTip::showText(helpEvent->globalPos(), d->m_HandleToolTip.arg(this->maximumValue()));
  744. event->accept();
  745. return true;
  746. }
  747. }
  748. default:
  749. break;
  750. }
  751. return this->Superclass::event(event);
  752. }