ctkRangeSlider.cpp 27 KB

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