ctkRangeSlider.cpp 23 KB

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