ctkRangeSlider.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QDebug>
  12. #include <QMouseEvent>
  13. #include <QKeyEvent>
  14. #include <QStyleOptionSlider>
  15. #include <QApplication>
  16. #include <QStylePainter>
  17. #include <QStyle>
  18. // CTK includes
  19. #include "ctkRangeSlider.h"
  20. class ctkRangeSliderPrivate:public ctkPrivate<ctkRangeSlider>
  21. {
  22. public:
  23. ctkRangeSliderPrivate();
  24. void init();
  25. // Description:
  26. // Copied verbatim from QSliderPrivate class (see QSlider.cpp)
  27. int pixelPosToRangeValue(int pos) const;
  28. // Description:
  29. // Draw the bottom and top sliders.
  30. void drawMinimumSlider( QStylePainter* painter ) const;
  31. void drawMaximumSlider( QStylePainter* painter ) const;
  32. // Description:
  33. // End points of the range on the Model
  34. int m_MaximumValue;
  35. int m_MinimumValue;
  36. // Description:
  37. // End points of the range on the GUI. This is synced with the model.
  38. int m_MaximumPosition;
  39. int m_MinimumPosition;
  40. // Description:
  41. // Controls selected ?
  42. QStyle::SubControl m_MinimumSliderSelected;
  43. QStyle::SubControl m_MaximumSliderSelected;
  44. // Description:
  45. // See QSliderPrivate::clickOffset.
  46. // Overrides this ivar
  47. int m_SubclassClickOffset;
  48. // Description:
  49. // See QSliderPrivate::position
  50. // Overrides this ivar.
  51. int m_SubclassPosition;
  52. // Description:
  53. // Boolean indicates the selected handle
  54. // True for the minimum range handle, false for the maximum range handle
  55. bool m_SelectedSlider;
  56. };
  57. // --------------------------------------------------------------------------
  58. ctkRangeSliderPrivate::ctkRangeSliderPrivate()
  59. {
  60. this->m_MinimumValue = 0;
  61. this->m_MaximumValue = 100;
  62. this->m_MinimumPosition = 0;
  63. this->m_MaximumPosition = 100;
  64. this->m_MinimumSliderSelected = QStyle::SC_None;
  65. this->m_MaximumSliderSelected = QStyle::SC_None;
  66. this->m_SubclassClickOffset = 0;
  67. this->m_SubclassPosition = 0;
  68. }
  69. // --------------------------------------------------------------------------
  70. void ctkRangeSliderPrivate::init()
  71. {
  72. CTK_P(ctkRangeSlider);
  73. this->m_MinimumValue = p->minimum();
  74. this->m_MaximumValue = p->maximum();
  75. this->m_MinimumPosition = p->minimum();
  76. this->m_MaximumPosition = p->maximum();
  77. p->connect(p, SIGNAL(rangeChanged(int, int)), p, SLOT(onRangeChanged(int, int)));
  78. }
  79. // --------------------------------------------------------------------------
  80. // Copied verbatim from QSliderPrivate::pixelPosToRangeValue. See QSlider.cpp
  81. //
  82. int ctkRangeSliderPrivate::pixelPosToRangeValue( int pos ) const
  83. {
  84. CTK_P(const ctkRangeSlider);
  85. QStyleOptionSlider option;
  86. p->initStyleOption( &option );
  87. QRect gr = p->style()->subControlRect( QStyle::CC_Slider,
  88. &option,
  89. QStyle::SC_SliderGroove,
  90. p );
  91. QRect sr = p->style()->subControlRect( QStyle::CC_Slider,
  92. &option,
  93. QStyle::SC_SliderHandle,
  94. p );
  95. int sliderLength = sr.width();
  96. int sliderMin = gr.x();
  97. int sliderMax = gr.right() - sliderLength + 1;
  98. return QStyle::sliderValueFromPosition( p->minimum(),
  99. p->maximum(),
  100. pos - sliderMin,
  101. sliderMax - sliderMin,
  102. option.upsideDown );
  103. }
  104. //---------------------------------------------------------------------------
  105. // Draw slider at the bottom end of the range
  106. void ctkRangeSliderPrivate::drawMinimumSlider( QStylePainter* painter ) const
  107. {
  108. CTK_P(const ctkRangeSlider);
  109. QStyleOptionSlider option;
  110. p->initStyleOption( &option );
  111. option.subControls = QStyle::SC_SliderHandle;
  112. option.sliderValue = m_MinimumValue;
  113. option.sliderPosition = m_MinimumPosition;
  114. if (m_MinimumSliderSelected == QStyle::SC_SliderHandle)
  115. {
  116. option.activeSubControls = m_MinimumSliderSelected;
  117. option.state |= QStyle::State_Sunken;
  118. }
  119. painter->drawComplexControl(QStyle::CC_Slider, option);
  120. }
  121. //---------------------------------------------------------------------------
  122. // Draw slider at the top end of the range
  123. void ctkRangeSliderPrivate::drawMaximumSlider( QStylePainter* painter ) const
  124. {
  125. CTK_P(const ctkRangeSlider);
  126. QStyleOptionSlider option;
  127. p->Superclass::initStyleOption( &option );
  128. option.subControls = QStyle::SC_SliderHandle;
  129. option.sliderValue = m_MaximumValue;
  130. option.sliderPosition = m_MaximumPosition;
  131. if (m_MaximumSliderSelected == QStyle::SC_SliderHandle)
  132. {
  133. option.activeSubControls = m_MaximumSliderSelected;
  134. option.state |= QStyle::State_Sunken;
  135. }
  136. painter->drawComplexControl(QStyle::CC_Slider, option);
  137. }
  138. // --------------------------------------------------------------------------
  139. ctkRangeSlider::ctkRangeSlider(QWidget* parent)
  140. : QSlider(parent)
  141. {
  142. CTK_INIT_PRIVATE(ctkRangeSlider);
  143. ctk_d()->init();
  144. }
  145. // --------------------------------------------------------------------------
  146. ctkRangeSlider::ctkRangeSlider( Qt::Orientation o,
  147. QWidget* parentObject )
  148. :QSlider(o, parentObject)
  149. {
  150. CTK_INIT_PRIVATE(ctkRangeSlider);
  151. ctk_d()->init();
  152. }
  153. // --------------------------------------------------------------------------
  154. ctkRangeSlider::~ctkRangeSlider()
  155. {
  156. }
  157. // --------------------------------------------------------------------------
  158. int ctkRangeSlider::minimumValue() const
  159. {
  160. CTK_D(const ctkRangeSlider);
  161. return d->m_MinimumValue;
  162. }
  163. // --------------------------------------------------------------------------
  164. void ctkRangeSlider::setMinimumValue( int min )
  165. {
  166. CTK_D(ctkRangeSlider);
  167. this->setValues( min, qMax(d->m_MaximumValue,min) );
  168. }
  169. // --------------------------------------------------------------------------
  170. int ctkRangeSlider::maximumValue() const
  171. {
  172. CTK_D(const ctkRangeSlider);
  173. return d->m_MaximumValue;
  174. }
  175. // --------------------------------------------------------------------------
  176. void ctkRangeSlider::setMaximumValue( int max )
  177. {
  178. CTK_D(ctkRangeSlider);
  179. this->setValues( qMin(d->m_MinimumValue, max), max );
  180. }
  181. // --------------------------------------------------------------------------
  182. void ctkRangeSlider::setValues(int l, int u)
  183. {
  184. CTK_D(ctkRangeSlider);
  185. const int minimumValue =
  186. qBound(this->minimum(), qMin(l,u), this->maximum());
  187. const int maximumValue =
  188. qBound(this->minimum(), qMax(l,u), this->maximum());
  189. bool emitMinValChanged = (minimumValue != d->m_MinimumValue);
  190. bool emitMaxValChanged = (maximumValue != d->m_MaximumValue);
  191. d->m_MinimumValue = minimumValue;
  192. d->m_MaximumValue = maximumValue;
  193. bool emitMinPosChanged =
  194. (minimumValue != d->m_MinimumPosition);
  195. bool emitMaxPosChanged =
  196. (maximumValue != d->m_MaximumPosition);
  197. d->m_MinimumPosition = minimumValue;
  198. d->m_MaximumPosition = maximumValue;
  199. if (isSliderDown())
  200. {
  201. if (emitMinPosChanged)
  202. {
  203. emit minimumPositionChanged(minimumValue);
  204. }
  205. if (emitMaxPosChanged)
  206. {
  207. emit maximumPositionChanged(maximumValue);
  208. }
  209. if (emitMinPosChanged || emitMaxPosChanged)
  210. {
  211. emit positionsChanged(minimumValue, maximumValue);
  212. }
  213. }
  214. if (emitMinValChanged)
  215. {
  216. emit minimumValueChanged(minimumValue);
  217. }
  218. if (emitMaxValChanged)
  219. {
  220. emit maximumValueChanged(maximumValue);
  221. }
  222. if (emitMinValChanged || emitMaxValChanged)
  223. {
  224. emit valuesChanged(d->m_MinimumValue,
  225. d->m_MaximumValue);
  226. }
  227. if (emitMinPosChanged || emitMaxPosChanged ||
  228. emitMinValChanged || emitMaxValChanged)
  229. {
  230. this->update();
  231. }
  232. }
  233. // --------------------------------------------------------------------------
  234. int ctkRangeSlider::minimumPosition() const
  235. {
  236. CTK_D(const ctkRangeSlider);
  237. return d->m_MinimumPosition;
  238. }
  239. // --------------------------------------------------------------------------
  240. int ctkRangeSlider::maximumPosition() const
  241. {
  242. CTK_D(const ctkRangeSlider);
  243. return d->m_MaximumPosition;
  244. }
  245. // --------------------------------------------------------------------------
  246. void ctkRangeSlider::setMinimumPosition(int l)
  247. {
  248. CTK_D(const ctkRangeSlider);
  249. this->setPositions(l, qMax(l, d->m_MaximumPosition));
  250. }
  251. // --------------------------------------------------------------------------
  252. void ctkRangeSlider::setMaximumPosition(int u)
  253. {
  254. CTK_D(const ctkRangeSlider);
  255. this->setPositions(qMin(d->m_MinimumPosition, u), u);
  256. }
  257. // --------------------------------------------------------------------------
  258. void ctkRangeSlider::setPositions(int min, int max)
  259. {
  260. CTK_D(ctkRangeSlider);
  261. const int minPosition =
  262. qBound(this->minimum(), qMin(min, max), this->maximum());
  263. const int maxPosition =
  264. qBound(this->minimum(), qMax(min, max), this->maximum());
  265. bool emitMinPosChanged = (minPosition != d->m_MinimumPosition);
  266. bool emitMaxPosChanged = (maxPosition != d->m_MaximumPosition);
  267. if (!emitMinPosChanged && !emitMaxPosChanged)
  268. {
  269. return;
  270. }
  271. d->m_MinimumPosition = minPosition;
  272. d->m_MaximumPosition = maxPosition;
  273. if (!this->hasTracking())
  274. {
  275. this->update();
  276. }
  277. if (isSliderDown())
  278. {
  279. if (emitMinPosChanged)
  280. {
  281. emit minimumPositionChanged(d->m_MinimumPosition);
  282. }
  283. if (emitMaxPosChanged)
  284. {
  285. emit maximumPositionChanged(d->m_MaximumPosition);
  286. }
  287. if (emitMinPosChanged || emitMaxPosChanged)
  288. {
  289. emit positionsChanged(d->m_MinimumPosition, d->m_MaximumPosition);
  290. }
  291. }
  292. if (this->hasTracking())
  293. {
  294. this->triggerAction(SliderMove);
  295. this->setValues(d->m_MinimumPosition, d->m_MaximumPosition);
  296. }
  297. }
  298. // --------------------------------------------------------------------------
  299. void ctkRangeSlider::onRangeChanged(int minimum, int maximum)
  300. {
  301. CTK_D(ctkRangeSlider);
  302. this->setValues(d->m_MinimumValue, d->m_MaximumValue);
  303. }
  304. // --------------------------------------------------------------------------
  305. // Render
  306. void ctkRangeSlider::paintEvent( QPaintEvent* )
  307. {
  308. CTK_D(ctkRangeSlider);
  309. QStyleOptionSlider option;
  310. this->initStyleOption(&option);
  311. QStylePainter painter(this);
  312. option.subControls = QStyle::SC_SliderGroove;
  313. painter.drawComplexControl(QStyle::CC_Slider, option);
  314. option.sliderPosition = d->m_MinimumPosition;
  315. const QRect lr = style()->subControlRect( QStyle::CC_Slider,
  316. &option,
  317. QStyle::SC_SliderHandle,
  318. this);
  319. option.sliderPosition = d->m_MaximumPosition;
  320. const QRect ur = style()->subControlRect( QStyle::CC_Slider,
  321. &option,
  322. QStyle::SC_SliderHandle,
  323. this);
  324. QRect sr = style()->subControlRect( QStyle::CC_Slider,
  325. &option,
  326. QStyle::SC_SliderGroove,
  327. this);
  328. QRect rangeBox = QRect(
  329. QPoint( qMin( lr.center().x(), ur.center().x() ), sr.center().y() - 2),
  330. QPoint(qMax( lr.center().x(), ur.center().x() ), sr.center().y() + 1));
  331. // -----------------------------
  332. // Render the range
  333. //
  334. QRect groove = this->style()->subControlRect( QStyle::CC_Slider,
  335. &option,
  336. QStyle::SC_SliderGroove,
  337. this );
  338. groove.adjust(0, 0, -1, 0);
  339. painter.setPen( QPen( this->palette().color(QPalette::Dark).light(90), 0));
  340. // Create default colors based on the transfer function.
  341. //
  342. QColor highlight = this->palette().color(QPalette::Highlight);
  343. QLinearGradient gradient( groove.center().x(), groove.top(),
  344. groove.center().x(), groove.bottom());
  345. // TODO: Set this based on the supplied transfer function
  346. QColor l = Qt::darkGray;
  347. QColor u = Qt::black;
  348. gradient.setColorAt(0, l);
  349. gradient.setColorAt(1, u);
  350. painter.setBrush(gradient);
  351. painter.setPen(QPen(highlight.dark(140), 0));
  352. painter.drawRect( rangeBox.intersected(groove) );
  353. // -----------------------------------
  354. // Render the sliders
  355. //
  356. if (d->m_SelectedSlider)
  357. {
  358. d->drawMaximumSlider( &painter );
  359. d->drawMinimumSlider( &painter );
  360. }
  361. else
  362. {
  363. d->drawMinimumSlider( &painter );
  364. d->drawMaximumSlider( &painter );
  365. }
  366. }
  367. // --------------------------------------------------------------------------
  368. // Standard Qt UI events
  369. void ctkRangeSlider::mousePressEvent(QMouseEvent* mouseEvent)
  370. {
  371. CTK_D(ctkRangeSlider);
  372. if (minimum() == maximum() || (mouseEvent->buttons() ^ mouseEvent->button()))
  373. {
  374. mouseEvent->ignore();
  375. return;
  376. }
  377. QStyleOptionSlider option;
  378. this->initStyleOption( &option );
  379. // Check if the first slider is pressed
  380. if (!this->isSliderDown())
  381. {
  382. option.sliderPosition = d->m_MinimumPosition;
  383. option.sliderValue = d->m_MinimumValue;
  384. QStyle::SubControl& control = d->m_MinimumSliderSelected;
  385. control = this->style()->hitTestComplexControl( QStyle::CC_Slider,
  386. &option,
  387. mouseEvent->pos(),
  388. this);
  389. if (control == QStyle::SC_SliderHandle)
  390. {
  391. d->m_SelectedSlider = true;
  392. d->m_SubclassPosition = d->m_MinimumValue;
  393. const QRect sr = this->style()->subControlRect( QStyle::CC_Slider,
  394. &option,
  395. QStyle::SC_SliderHandle,
  396. this);
  397. d->m_SubclassClickOffset = mouseEvent->pos().x() - sr.topLeft().x();
  398. this->setSliderDown(true);
  399. if (control != d->m_MinimumSliderSelected)
  400. {
  401. this->update(sr);
  402. }
  403. }
  404. }
  405. // Check if the other slider is pressed
  406. if (!this->isSliderDown())
  407. {
  408. option.sliderPosition = d->m_MaximumPosition;
  409. option.sliderValue = d->m_MaximumValue;
  410. QStyle::SubControl& control = d->m_MaximumSliderSelected;
  411. control = this->style()->hitTestComplexControl( QStyle::CC_Slider,
  412. &option,
  413. mouseEvent->pos(),
  414. this);
  415. if (control == QStyle::SC_SliderHandle)
  416. {
  417. d->m_SelectedSlider = false;
  418. d->m_SubclassPosition = d->m_MaximumValue;
  419. const QRect sr = this->style()->subControlRect( QStyle::CC_Slider,
  420. &option,
  421. QStyle::SC_SliderHandle,
  422. this);
  423. d->m_SubclassClickOffset = mouseEvent->pos().x() - sr.topLeft().x();
  424. this->setSliderDown(true);
  425. if (d->m_MaximumSliderSelected != control)
  426. {
  427. this->update(sr);
  428. }
  429. }
  430. }
  431. // Accept the mouseEvent
  432. mouseEvent->accept();
  433. }
  434. // --------------------------------------------------------------------------
  435. // Standard Qt UI events
  436. void ctkRangeSlider::mouseMoveEvent(QMouseEvent* mouseEvent)
  437. {
  438. CTK_D(ctkRangeSlider);
  439. if (d->m_MinimumSliderSelected == QStyle::SC_SliderHandle ||
  440. d->m_MaximumSliderSelected == QStyle::SC_SliderHandle)
  441. {
  442. QStyleOptionSlider option;
  443. this->initStyleOption(&option);
  444. const int m = style()->pixelMetric( QStyle::PM_MaximumDragDistance, &option, this );
  445. int newPosition = d->pixelPosToRangeValue(
  446. mouseEvent->pos().x() - d->m_SubclassClickOffset);
  447. if (m >= 0)
  448. {
  449. const QRect r = rect().adjusted(-m, -m, m, m);
  450. if (!r.contains(mouseEvent->pos()))
  451. {
  452. newPosition = d->m_SubclassPosition;
  453. }
  454. }
  455. if (d->m_MinimumSliderSelected == QStyle::SC_SliderHandle)
  456. {
  457. this->setMinimumPosition(qMin(newPosition,d->m_MaximumPosition));
  458. }
  459. else if (d->m_MaximumSliderSelected == QStyle::SC_SliderHandle)
  460. {
  461. this->setMaximumPosition(qMax(d->m_MinimumPosition, newPosition));
  462. }
  463. mouseEvent->accept();
  464. }
  465. mouseEvent->ignore();
  466. }
  467. // --------------------------------------------------------------------------
  468. // Standard Qt UI mouseEvents
  469. void ctkRangeSlider::mouseReleaseEvent(QMouseEvent* mouseEvent)
  470. {
  471. CTK_D(ctkRangeSlider);
  472. QSlider::mouseReleaseEvent(mouseEvent);
  473. setSliderDown(false);
  474. d->m_MinimumSliderSelected = QStyle::SC_None;
  475. d->m_MaximumSliderSelected = QStyle::SC_None;
  476. this->update();
  477. }