ctkRangeSlider.cpp 17 KB

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