ctkRangeSlider.cpp 26 KB

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