ctkRangeSlider.cpp 24 KB

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