ctkRangeSlider.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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()
  218. {
  219. }
  220. // --------------------------------------------------------------------------
  221. int ctkRangeSlider::minimumValue() const
  222. {
  223. Q_D(const ctkRangeSlider);
  224. return d->m_MinimumValue;
  225. }
  226. // --------------------------------------------------------------------------
  227. void ctkRangeSlider::setMinimumValue( int min )
  228. {
  229. Q_D(ctkRangeSlider);
  230. this->setValues( min, qMax(d->m_MaximumValue,min) );
  231. }
  232. // --------------------------------------------------------------------------
  233. int ctkRangeSlider::maximumValue() const
  234. {
  235. Q_D(const ctkRangeSlider);
  236. return d->m_MaximumValue;
  237. }
  238. // --------------------------------------------------------------------------
  239. void ctkRangeSlider::setMaximumValue( int max )
  240. {
  241. Q_D(ctkRangeSlider);
  242. this->setValues( qMin(d->m_MinimumValue, max), max );
  243. }
  244. // --------------------------------------------------------------------------
  245. void ctkRangeSlider::setValues(int l, int u)
  246. {
  247. Q_D(ctkRangeSlider);
  248. const int minimumValue =
  249. qBound(this->minimum(), qMin(l,u), this->maximum());
  250. const int maximumValue =
  251. qBound(this->minimum(), qMax(l,u), this->maximum());
  252. bool emitMinValChanged = (minimumValue != d->m_MinimumValue);
  253. bool emitMaxValChanged = (maximumValue != d->m_MaximumValue);
  254. d->m_MinimumValue = minimumValue;
  255. d->m_MaximumValue = maximumValue;
  256. bool emitMinPosChanged =
  257. (minimumValue != d->m_MinimumPosition);
  258. bool emitMaxPosChanged =
  259. (maximumValue != d->m_MaximumPosition);
  260. d->m_MinimumPosition = minimumValue;
  261. d->m_MaximumPosition = maximumValue;
  262. if (isSliderDown())
  263. {
  264. if (emitMinPosChanged || emitMaxPosChanged)
  265. {
  266. emit positionsChanged(minimumValue, maximumValue);
  267. }
  268. if (emitMinPosChanged)
  269. {
  270. emit minimumPositionChanged(minimumValue);
  271. }
  272. if (emitMaxPosChanged)
  273. {
  274. emit maximumPositionChanged(maximumValue);
  275. }
  276. }
  277. if (emitMinValChanged || emitMaxValChanged)
  278. {
  279. emit valuesChanged(d->m_MinimumValue,
  280. d->m_MaximumValue);
  281. }
  282. if (emitMinValChanged)
  283. {
  284. emit minimumValueChanged(minimumValue);
  285. }
  286. if (emitMaxValChanged)
  287. {
  288. emit maximumValueChanged(maximumValue);
  289. }
  290. if (emitMinPosChanged || emitMaxPosChanged ||
  291. emitMinValChanged || emitMaxValChanged)
  292. {
  293. this->update();
  294. }
  295. }
  296. // --------------------------------------------------------------------------
  297. int ctkRangeSlider::minimumPosition() const
  298. {
  299. Q_D(const ctkRangeSlider);
  300. return d->m_MinimumPosition;
  301. }
  302. // --------------------------------------------------------------------------
  303. int ctkRangeSlider::maximumPosition() const
  304. {
  305. Q_D(const ctkRangeSlider);
  306. return d->m_MaximumPosition;
  307. }
  308. // --------------------------------------------------------------------------
  309. void ctkRangeSlider::setMinimumPosition(int l)
  310. {
  311. Q_D(const ctkRangeSlider);
  312. this->setPositions(l, qMax(l, d->m_MaximumPosition));
  313. }
  314. // --------------------------------------------------------------------------
  315. void ctkRangeSlider::setMaximumPosition(int u)
  316. {
  317. Q_D(const ctkRangeSlider);
  318. this->setPositions(qMin(d->m_MinimumPosition, u), u);
  319. }
  320. // --------------------------------------------------------------------------
  321. void ctkRangeSlider::setPositions(int min, int max)
  322. {
  323. Q_D(ctkRangeSlider);
  324. const int minPosition =
  325. qBound(this->minimum(), qMin(min, max), this->maximum());
  326. const int maxPosition =
  327. qBound(this->minimum(), qMax(min, max), this->maximum());
  328. bool emitMinPosChanged = (minPosition != d->m_MinimumPosition);
  329. bool emitMaxPosChanged = (maxPosition != d->m_MaximumPosition);
  330. if (!emitMinPosChanged && !emitMaxPosChanged)
  331. {
  332. return;
  333. }
  334. d->m_MinimumPosition = minPosition;
  335. d->m_MaximumPosition = maxPosition;
  336. if (!this->hasTracking())
  337. {
  338. this->update();
  339. }
  340. if (isSliderDown())
  341. {
  342. if (emitMinPosChanged)
  343. {
  344. emit minimumPositionChanged(d->m_MinimumPosition);
  345. }
  346. if (emitMaxPosChanged)
  347. {
  348. emit maximumPositionChanged(d->m_MaximumPosition);
  349. }
  350. if (emitMinPosChanged || emitMaxPosChanged)
  351. {
  352. emit positionsChanged(d->m_MinimumPosition, d->m_MaximumPosition);
  353. }
  354. }
  355. if (this->hasTracking())
  356. {
  357. this->triggerAction(SliderMove);
  358. this->setValues(d->m_MinimumPosition, d->m_MaximumPosition);
  359. }
  360. }
  361. // --------------------------------------------------------------------------
  362. void ctkRangeSlider::onRangeChanged(int minimum, int maximum)
  363. {
  364. Q_UNUSED(minimum);
  365. Q_UNUSED(maximum);
  366. Q_D(ctkRangeSlider);
  367. this->setValues(d->m_MinimumValue, d->m_MaximumValue);
  368. }
  369. // --------------------------------------------------------------------------
  370. // Render
  371. void ctkRangeSlider::paintEvent( QPaintEvent* )
  372. {
  373. Q_D(ctkRangeSlider);
  374. QStyleOptionSlider option;
  375. this->initStyleOption(&option);
  376. QStylePainter painter(this);
  377. option.subControls = QStyle::SC_SliderGroove;
  378. option.sliderPosition = this->minimum(); // don't highlight the SliderGroove
  379. painter.drawComplexControl(QStyle::CC_Slider, option);
  380. option.sliderPosition = d->m_MinimumPosition;
  381. const QRect lr = style()->subControlRect( QStyle::CC_Slider,
  382. &option,
  383. QStyle::SC_SliderHandle,
  384. this);
  385. option.sliderPosition = d->m_MaximumPosition;
  386. const QRect ur = style()->subControlRect( QStyle::CC_Slider,
  387. &option,
  388. QStyle::SC_SliderHandle,
  389. this);
  390. QRect sr = style()->subControlRect( QStyle::CC_Slider,
  391. &option,
  392. QStyle::SC_SliderGroove,
  393. this);
  394. QRect rangeBox;
  395. if (option.orientation == Qt::Horizontal)
  396. {
  397. rangeBox = QRect(
  398. QPoint(qMin( lr.center().x(), ur.center().x() ), sr.center().y() - 2),
  399. QPoint(qMax( lr.center().x(), ur.center().x() ), sr.center().y() + 1));
  400. }
  401. else
  402. {
  403. rangeBox = QRect(
  404. QPoint(sr.center().x() - 2, qMin( lr.center().y(), ur.center().y() )),
  405. QPoint(sr.center().x() + 1, qMax( lr.center().y(), ur.center().y() )));
  406. }
  407. // -----------------------------
  408. // Render the range
  409. //
  410. QRect groove = this->style()->subControlRect( QStyle::CC_Slider,
  411. &option,
  412. QStyle::SC_SliderGroove,
  413. this );
  414. groove.adjust(0, 0, -1, 0);
  415. // Create default colors based on the transfer function.
  416. //
  417. QColor highlight = this->palette().color(QPalette::Normal, QPalette::Highlight);
  418. QLinearGradient gradient;
  419. if (option.orientation == Qt::Horizontal)
  420. {
  421. gradient = QLinearGradient( groove.center().x(), groove.top(),
  422. groove.center().x(), groove.bottom());
  423. }
  424. else
  425. {
  426. gradient = QLinearGradient( groove.left(), groove.center().y(),
  427. groove.right(), groove.center().y());
  428. }
  429. // TODO: Set this based on the supplied transfer function
  430. QColor l = Qt::darkGray;
  431. QColor u = Qt::black;
  432. gradient.setColorAt(0, highlight.darker(120));
  433. gradient.setColorAt(1, highlight.lighter(160));
  434. painter.setPen(QPen(highlight.darker(150), 0));
  435. painter.setBrush(gradient);
  436. painter.drawRect( rangeBox.intersected(groove) );
  437. // -----------------------------------
  438. // Render the sliders
  439. //
  440. if (d->m_SelectedHandles & ctkRangeSliderPrivate::MinimumHandle)
  441. {
  442. d->drawMaximumSlider( &painter );
  443. d->drawMinimumSlider( &painter );
  444. }
  445. else
  446. {
  447. d->drawMinimumSlider( &painter );
  448. d->drawMaximumSlider( &painter );
  449. }
  450. }
  451. // --------------------------------------------------------------------------
  452. // Standard Qt UI events
  453. void ctkRangeSlider::mousePressEvent(QMouseEvent* mouseEvent)
  454. {
  455. Q_D(ctkRangeSlider);
  456. if (minimum() == maximum() || (mouseEvent->buttons() ^ mouseEvent->button()))
  457. {
  458. mouseEvent->ignore();
  459. return;
  460. }
  461. int pos = this->orientation() == Qt::Horizontal ?
  462. mouseEvent->pos().x() : mouseEvent->pos().y();
  463. QStyleOptionSlider option;
  464. this->initStyleOption( &option );
  465. // Check if the first handle is pressed
  466. option.sliderPosition = d->m_MinimumPosition;
  467. option.sliderValue = d->m_MinimumValue;
  468. QStyle::SubControl control;
  469. control = this->style()->hitTestComplexControl( QStyle::CC_Slider,
  470. &option,
  471. mouseEvent->pos(),
  472. this);
  473. const QRect lr = this->style()->subControlRect( QStyle::CC_Slider,
  474. &option,
  475. QStyle::SC_SliderHandle,
  476. this);
  477. if (control == QStyle::SC_SliderHandle)
  478. {
  479. d->m_SubclassPosition = d->m_MinimumPosition;
  480. // save the position of the mouse inside the handle for later
  481. d->m_SubclassClickOffset = pos - (this->orientation() == Qt::Horizontal ?
  482. lr.left() : lr.top());
  483. this->setSliderDown(true);
  484. if (d->m_SelectedHandles != ctkRangeSliderPrivate::MinimumHandle)
  485. {
  486. d->m_SelectedHandles = ctkRangeSliderPrivate::MinimumHandle;
  487. this->update(lr);
  488. }
  489. // Accept the mouseEvent
  490. mouseEvent->accept();
  491. return;
  492. }
  493. // The user didn't press on the minimum handle,
  494. // Check if the other handle is pressed
  495. option.sliderPosition = d->m_MaximumPosition;
  496. option.sliderValue = d->m_MaximumValue;
  497. control = this->style()->hitTestComplexControl( QStyle::CC_Slider,
  498. &option,
  499. mouseEvent->pos(),
  500. this);
  501. const QRect ur = this->style()->subControlRect( QStyle::CC_Slider,
  502. &option,
  503. QStyle::SC_SliderHandle,
  504. this);
  505. if (control == QStyle::SC_SliderHandle)
  506. {
  507. d->m_SubclassPosition = d->m_MaximumPosition;
  508. // save the position of the mouse inside the handle for later
  509. d->m_SubclassClickOffset = pos - (this->orientation() == Qt::Horizontal ?
  510. ur.left() : ur.top());
  511. this->setSliderDown(true);
  512. if (d->m_SelectedHandles != ctkRangeSliderPrivate::MaximumHandle)
  513. {
  514. d->m_SelectedHandles = ctkRangeSliderPrivate::MaximumHandle;
  515. this->update(ur);
  516. }
  517. // Accept the mouseEvent
  518. mouseEvent->accept();
  519. return;
  520. }
  521. // if we are here, no handles have been pressed
  522. // Check if we pressed on the groove between the 2 handles
  523. control = this->style()->hitTestComplexControl( QStyle::CC_Slider,
  524. &option,
  525. mouseEvent->pos(),
  526. this);
  527. QRect sr = style()->subControlRect( QStyle::CC_Slider,
  528. &option,
  529. QStyle::SC_SliderGroove,
  530. this);
  531. int minCenter = (this->orientation() == Qt::Horizontal ?
  532. lr.center().x() : ur.center().y());
  533. int maxCenter = (this->orientation() == Qt::Horizontal ?
  534. ur.center().x() : lr.center().y());
  535. if (control == QStyle::SC_SliderGroove &&
  536. pos > minCenter && pos < maxCenter)
  537. {
  538. // warning lost of precision it might be fatal
  539. d->m_SubclassPosition = (d->m_MinimumPosition + d->m_MaximumPosition) / 2.;
  540. d->m_SubclassClickOffset = pos - d->pixelPosFromRangeValue(d->m_SubclassPosition);
  541. d->m_SubclassWidth = (d->m_MaximumPosition - d->m_MinimumPosition) / 2;
  542. qMax(d->m_SubclassPosition - d->m_MinimumPosition, d->m_MaximumPosition - d->m_SubclassPosition);
  543. this->setSliderDown(true);
  544. if (!(d->m_SelectedHandles & QFlags<ctkRangeSliderPrivate::Handle>(
  545. ctkRangeSliderPrivate::MinimumHandle)) ||
  546. !(d->m_SelectedHandles & QFlags<ctkRangeSliderPrivate::Handle>(ctkRangeSliderPrivate::MaximumHandle)))
  547. {
  548. d->m_SelectedHandles =
  549. QFlags<ctkRangeSliderPrivate::Handle>(ctkRangeSliderPrivate::MinimumHandle) |
  550. QFlags<ctkRangeSliderPrivate::Handle>(ctkRangeSliderPrivate::MaximumHandle);
  551. this->update(lr.united(ur).united(sr));
  552. }
  553. mouseEvent->accept();
  554. return;
  555. }
  556. mouseEvent->ignore();
  557. }
  558. // --------------------------------------------------------------------------
  559. // Standard Qt UI events
  560. void ctkRangeSlider::mouseMoveEvent(QMouseEvent* mouseEvent)
  561. {
  562. Q_D(ctkRangeSlider);
  563. if (!d->m_SelectedHandles)
  564. {
  565. mouseEvent->ignore();
  566. return;
  567. }
  568. int pos = this->orientation() == Qt::Horizontal ?
  569. mouseEvent->pos().x() : mouseEvent->pos().y();
  570. QStyleOptionSlider option;
  571. this->initStyleOption(&option);
  572. const int m = style()->pixelMetric( QStyle::PM_MaximumDragDistance, &option, this );
  573. int newPosition = d->pixelPosToRangeValue(pos - d->m_SubclassClickOffset);
  574. if (m >= 0)
  575. {
  576. const QRect r = rect().adjusted(-m, -m, m, m);
  577. if (!r.contains(mouseEvent->pos()))
  578. {
  579. newPosition = d->m_SubclassPosition;
  580. }
  581. }
  582. if (d->m_SelectedHandles == ctkRangeSliderPrivate::MinimumHandle)
  583. {
  584. this->setMinimumPosition(qMin(newPosition,d->m_MaximumPosition));
  585. }
  586. else if (d->m_SelectedHandles == ctkRangeSliderPrivate::MaximumHandle)
  587. {
  588. this->setMaximumPosition(qMax(d->m_MinimumPosition, newPosition));
  589. }
  590. else if (d->m_SelectedHandles & ctkRangeSliderPrivate::MinimumHandle &&
  591. d->m_SelectedHandles & ctkRangeSliderPrivate::MaximumHandle)
  592. {
  593. this->setPositions(newPosition - d->m_SubclassWidth, newPosition + d->m_SubclassWidth );
  594. }
  595. mouseEvent->accept();
  596. }
  597. // --------------------------------------------------------------------------
  598. // Standard Qt UI mouseEvents
  599. void ctkRangeSlider::mouseReleaseEvent(QMouseEvent* mouseEvent)
  600. {
  601. Q_D(ctkRangeSlider);
  602. this->QSlider::mouseReleaseEvent(mouseEvent);
  603. setSliderDown(false);
  604. d->m_SelectedHandles = 0;
  605. this->update();
  606. }
  607. // --------------------------------------------------------------------------
  608. bool ctkRangeSlider::isMinimumSliderDown()const
  609. {
  610. Q_D(const ctkRangeSlider);
  611. return d->m_SelectedHandles & ctkRangeSliderPrivate::MinimumHandle;
  612. }
  613. // --------------------------------------------------------------------------
  614. bool ctkRangeSlider::isMaximumSliderDown()const
  615. {
  616. Q_D(const ctkRangeSlider);
  617. return d->m_SelectedHandles & ctkRangeSliderPrivate::MaximumHandle;
  618. }
  619. // --------------------------------------------------------------------------
  620. void ctkRangeSlider::initMinimumSliderStyleOption(QStyleOptionSlider* option) const
  621. {
  622. this->initStyleOption(option);
  623. }
  624. // --------------------------------------------------------------------------
  625. void ctkRangeSlider::initMaximumSliderStyleOption(QStyleOptionSlider* option) const
  626. {
  627. this->initStyleOption(option);
  628. }