ctkRangeSlider.cpp 25 KB

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