ctkDoubleRangeSlider.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 <QHBoxLayout>
  17. // CTK includes
  18. #include "ctkRangeSlider.h"
  19. #include "ctkDoubleRangeSlider.h"
  20. //-----------------------------------------------------------------------------
  21. class ctkDoubleRangeSliderPrivate: public ctkPrivate<ctkDoubleRangeSlider>
  22. {
  23. public:
  24. ctkDoubleRangeSliderPrivate();
  25. int toInt(double _value)const;
  26. double minFromInt(int _value)const;
  27. double maxFromInt(int _value)const;
  28. void init();
  29. void connectSlider();
  30. void updateMinOffset(double value);
  31. void updateMaxOffset(double value);
  32. ctkRangeSlider* Slider;
  33. double Minimum;
  34. double Maximum;
  35. // we should have a MinValueOffset and MinPositionOffset (and MinimumOffset?)
  36. double MinOffset;
  37. // we should have a MaxValueOffset and MaxPositionOffset (and MaximumOffset?)
  38. double MaxOffset;
  39. double SingleStep;
  40. double MinValue;
  41. double MaxValue;
  42. };
  43. // --------------------------------------------------------------------------
  44. ctkDoubleRangeSliderPrivate::ctkDoubleRangeSliderPrivate()
  45. {
  46. // the initial values will be overwritten in
  47. // ctkDoubleRangeSliderPrivate::init()
  48. this->Slider = 0;
  49. this->Minimum = 0.;
  50. this->Maximum = 99.;
  51. this->MinOffset = 0.;
  52. this->MaxOffset = 0.;
  53. this->SingleStep = 1.;
  54. this->MinValue = 0.;
  55. this->MaxValue = 99.;
  56. }
  57. // --------------------------------------------------------------------------
  58. void ctkDoubleRangeSliderPrivate::init()
  59. {
  60. CTK_P(ctkDoubleRangeSlider);
  61. this->Slider = new ctkRangeSlider(p);
  62. QHBoxLayout* l = new QHBoxLayout(p);
  63. l->addWidget(this->Slider);
  64. l->setContentsMargins(0,0,0,0);
  65. this->Minimum = this->Slider->minimum();
  66. this->Maximum = this->Slider->maximum();
  67. this->MinValue = this->Slider->minimumValue();
  68. this->MaxValue = this->Slider->maximumValue();
  69. this->SingleStep = this->Slider->singleStep();
  70. this->connectSlider();
  71. }
  72. // --------------------------------------------------------------------------
  73. void ctkDoubleRangeSliderPrivate::connectSlider()
  74. {
  75. CTK_P(ctkDoubleRangeSlider);
  76. p->connect(this->Slider, SIGNAL(minimumValueChanged(int)),
  77. p, SLOT(onMinValueChanged(int)));
  78. p->connect(this->Slider, SIGNAL(maximumValueChanged(int)),
  79. p, SLOT(onMaxValueChanged(int)));
  80. p->connect(this->Slider, SIGNAL(valuesChanged(int,int)),
  81. p, SLOT(onValuesChanged(int,int)));
  82. p->connect(this->Slider, SIGNAL(minimumPositionChanged(int)),
  83. p, SLOT(onMinPosChanged(int)));
  84. p->connect(this->Slider, SIGNAL(maximumPositionChanged(int)),
  85. p, SLOT(onMaxPosChanged(int)));
  86. p->connect(this->Slider, SIGNAL(positionsChanged(int,int)),
  87. p, SLOT(onPositionsChanged(int,int)));
  88. p->connect(this->Slider, SIGNAL(sliderPressed()),
  89. p, SIGNAL(sliderPressed()));
  90. p->connect(this->Slider, SIGNAL(sliderReleased()),
  91. p, SIGNAL(sliderReleased()));
  92. }
  93. // --------------------------------------------------------------------------
  94. int ctkDoubleRangeSliderPrivate::toInt(double doubleValue)const
  95. {
  96. double tmp = doubleValue / this->SingleStep;
  97. int intValue = qRound(tmp);
  98. return intValue;
  99. }
  100. // --------------------------------------------------------------------------
  101. double ctkDoubleRangeSliderPrivate::minFromInt(int intValue)const
  102. {
  103. double doubleValue = this->SingleStep * (this->MinOffset + intValue) ;
  104. return doubleValue;
  105. }
  106. // --------------------------------------------------------------------------
  107. double ctkDoubleRangeSliderPrivate::maxFromInt(int intValue)const
  108. {
  109. double doubleValue = this->SingleStep * (this->MaxOffset + intValue) ;
  110. return doubleValue;
  111. }
  112. // --------------------------------------------------------------------------
  113. void ctkDoubleRangeSliderPrivate::updateMinOffset(double value)
  114. {
  115. this->MinOffset = (value / this->SingleStep) - this->toInt(value);
  116. }
  117. // --------------------------------------------------------------------------
  118. void ctkDoubleRangeSliderPrivate::updateMaxOffset(double value)
  119. {
  120. this->MaxOffset = (value / this->SingleStep) - this->toInt(value);
  121. }
  122. // --------------------------------------------------------------------------
  123. ctkDoubleRangeSlider::ctkDoubleRangeSlider(QWidget* _parent) : Superclass(_parent)
  124. {
  125. CTK_INIT_PRIVATE(ctkDoubleRangeSlider);
  126. ctk_d()->init();
  127. }
  128. // --------------------------------------------------------------------------
  129. ctkDoubleRangeSlider::ctkDoubleRangeSlider(Qt::Orientation _orientation, QWidget* _parent)
  130. : Superclass(_parent)
  131. {
  132. CTK_INIT_PRIVATE(ctkDoubleRangeSlider);
  133. ctk_d()->init();
  134. this->setOrientation(_orientation);
  135. }
  136. // --------------------------------------------------------------------------
  137. void ctkDoubleRangeSlider::setMinimum(double min)
  138. {
  139. CTK_D(ctkDoubleRangeSlider);
  140. d->Minimum = min;
  141. if (d->Minimum >= d->MinValue)
  142. {// TBD: use same offset
  143. d->updateMinOffset(d->Minimum);
  144. }
  145. if (d->Minimum >= d->MaxValue)
  146. {// TBD: use same offset
  147. d->updateMaxOffset(d->Minimum);
  148. }
  149. d->Slider->setMinimum(d->toInt(min));
  150. }
  151. // --------------------------------------------------------------------------
  152. double ctkDoubleRangeSlider::minimum()const
  153. {
  154. CTK_D(const ctkDoubleRangeSlider);
  155. return d->Minimum;
  156. }
  157. // --------------------------------------------------------------------------
  158. void ctkDoubleRangeSlider::setMaximum(double max)
  159. {
  160. CTK_D(ctkDoubleRangeSlider);
  161. d->Maximum = max;
  162. if (d->Maximum <= d->MinValue)
  163. {// TBD: use same offset
  164. d->updateMinOffset(d->Maximum);
  165. }
  166. if (d->Maximum <= d->MaxValue)
  167. {// TBD: use same offset ?
  168. d->updateMaxOffset(d->Maximum);
  169. }
  170. d->Slider->setMaximum(d->toInt(max));
  171. }
  172. // --------------------------------------------------------------------------
  173. double ctkDoubleRangeSlider::maximum()const
  174. {
  175. CTK_D(const ctkDoubleRangeSlider);
  176. return d->Maximum;
  177. }
  178. // --------------------------------------------------------------------------
  179. void ctkDoubleRangeSlider::setRange(double min, double max)
  180. {
  181. CTK_D(ctkDoubleRangeSlider);
  182. d->Minimum = min;
  183. d->Maximum = max;
  184. if (d->Minimum >= d->MinValue)
  185. {// TBD: use same offset
  186. d->updateMinOffset(d->Minimum);
  187. }
  188. if (d->Minimum >= d->MaxValue)
  189. {// TBD: use same offset
  190. d->updateMaxOffset(d->Minimum);
  191. }
  192. if (d->Maximum <= d->MinValue)
  193. {// TBD: use same offset
  194. d->updateMinOffset(d->Maximum);
  195. }
  196. if (d->Maximum <= d->MaxValue)
  197. {// TBD: use same offset ?
  198. d->updateMaxOffset(d->Maximum);
  199. }
  200. d->Slider->setRange(d->toInt(min), d->toInt(max));
  201. }
  202. // --------------------------------------------------------------------------
  203. double ctkDoubleRangeSlider::minimumPosition()const
  204. {
  205. CTK_D(const ctkDoubleRangeSlider);
  206. return d->minFromInt(d->Slider->minimumPosition());
  207. }
  208. // --------------------------------------------------------------------------
  209. void ctkDoubleRangeSlider::setMinimumPosition(double minPos)
  210. {
  211. CTK_D(ctkDoubleRangeSlider);
  212. d->Slider->setMinimumPosition(d->toInt(minPos));
  213. }
  214. // --------------------------------------------------------------------------
  215. double ctkDoubleRangeSlider::maximumPosition()const
  216. {
  217. CTK_D(const ctkDoubleRangeSlider);
  218. return d->maxFromInt(d->Slider->maximumPosition());
  219. }
  220. // --------------------------------------------------------------------------
  221. void ctkDoubleRangeSlider::setMaximumPosition(double maxPos)
  222. {
  223. CTK_D(ctkDoubleRangeSlider);
  224. d->Slider->setMaximumPosition(d->toInt(maxPos));
  225. }
  226. // --------------------------------------------------------------------------
  227. void ctkDoubleRangeSlider::setPositions(double minPos, double maxPos)
  228. {
  229. CTK_D(ctkDoubleRangeSlider);
  230. d->Slider->setPositions(d->toInt(minPos), d->toInt(maxPos));
  231. }
  232. // --------------------------------------------------------------------------
  233. double ctkDoubleRangeSlider::minimumValue()const
  234. {
  235. CTK_D(const ctkDoubleRangeSlider);
  236. return d->MinValue;
  237. }
  238. // --------------------------------------------------------------------------
  239. void ctkDoubleRangeSlider::setMinimumValue(double newMinValue)
  240. {
  241. CTK_D(ctkDoubleRangeSlider);
  242. newMinValue = qBound(d->Minimum, newMinValue, d->Maximum);
  243. d->updateMinOffset(newMinValue);
  244. if (newMinValue >= d->MaxValue)
  245. {
  246. d->updateMaxOffset(newMinValue);
  247. }
  248. int newIntValue = d->toInt(newMinValue);
  249. if (newIntValue != d->Slider->minimumValue())
  250. {
  251. // d->Slider will emit a minimumValueChanged signal that is connected to
  252. // ctkDoubleSlider::onValueChanged
  253. d->Slider->setMinimumValue(newIntValue);
  254. }
  255. else
  256. {
  257. double oldValue = d->MinValue;
  258. d->MinValue = newMinValue;
  259. // don't emit a valuechanged signal if the new value is quite
  260. // similar to the old value.
  261. if (qAbs(newMinValue - oldValue) > (d->SingleStep * 0.000000001))
  262. {
  263. emit this->minimumValueChanged(newMinValue);
  264. }
  265. }
  266. }
  267. // --------------------------------------------------------------------------
  268. double ctkDoubleRangeSlider::maximumValue()const
  269. {
  270. CTK_D(const ctkDoubleRangeSlider);
  271. return d->MaxValue;
  272. }
  273. // --------------------------------------------------------------------------
  274. void ctkDoubleRangeSlider::setMaximumValue(double newMaxValue)
  275. {
  276. CTK_D(ctkDoubleRangeSlider);
  277. newMaxValue = qBound(d->Minimum, newMaxValue, d->Maximum);
  278. d->updateMaxOffset(newMaxValue);
  279. if (newMaxValue <= d->MinValue)
  280. {
  281. d->updateMinOffset(newMaxValue);
  282. }
  283. int newIntValue = d->toInt(newMaxValue);
  284. if (newIntValue != d->Slider->maximumValue())
  285. {
  286. // d->Slider will emit a maximumValueChanged signal that is connected to
  287. // ctkDoubleSlider::onValueChanged
  288. d->Slider->setMaximumValue(newIntValue);
  289. }
  290. else
  291. {
  292. double oldValue = d->MaxValue;
  293. d->MaxValue = newMaxValue;
  294. // don't emit a valuechanged signal if the new value is quite
  295. // similar to the old value.
  296. if (qAbs(newMaxValue - oldValue) > (d->SingleStep * 0.000000001))
  297. {
  298. emit this->maximumValueChanged(newMaxValue);
  299. }
  300. }
  301. }
  302. // --------------------------------------------------------------------------
  303. void ctkDoubleRangeSlider::setValues(double newMinValue, double newMaxValue)
  304. {
  305. this->setMinimumValue(qMin(newMinValue, newMaxValue));
  306. this->setMaximumValue(qMax(newMinValue, newMaxValue));
  307. }
  308. // --------------------------------------------------------------------------
  309. double ctkDoubleRangeSlider::singleStep()const
  310. {
  311. CTK_D(const ctkDoubleRangeSlider);
  312. return d->SingleStep;
  313. }
  314. // --------------------------------------------------------------------------
  315. void ctkDoubleRangeSlider::setSingleStep(double newStep)
  316. {
  317. CTK_D(ctkDoubleRangeSlider);
  318. d->SingleStep = newStep;
  319. // The following can fire A LOT of signals that shouldn't be
  320. // fired.
  321. bool oldBlockSignals = this->blockSignals(true);
  322. d->updateMinOffset(d->MinValue);
  323. d->updateMaxOffset(d->MaxValue);
  324. // update the new values of the ctkRangeSlider
  325. double _minvalue = d->MinValue;
  326. double _maxvalue = d->MaxValue;
  327. // calling setMinimum or setMaximum can change the values MinimumValue
  328. // and MaximumValue, this is why we re-set them later.
  329. this->setMinimum(d->Minimum);
  330. this->setMaximum(d->Maximum);
  331. this->setMinimumValue(_minvalue);
  332. this->setMinimumPosition(_minvalue);
  333. this->setMaximumValue(_maxvalue);
  334. this->setMaximumPosition(_maxvalue);
  335. this->blockSignals(oldBlockSignals);
  336. }
  337. // --------------------------------------------------------------------------
  338. double ctkDoubleRangeSlider::tickInterval()const
  339. {
  340. CTK_D(const ctkDoubleRangeSlider);
  341. return d->minFromInt(d->Slider->tickInterval());
  342. }
  343. // --------------------------------------------------------------------------
  344. void ctkDoubleRangeSlider::setTickInterval(double newTickInterval)
  345. {
  346. CTK_D(ctkDoubleRangeSlider);
  347. d->Slider->setTickInterval(d->toInt(newTickInterval));
  348. }
  349. // --------------------------------------------------------------------------
  350. bool ctkDoubleRangeSlider::hasTracking()const
  351. {
  352. CTK_D(const ctkDoubleRangeSlider);
  353. return d->Slider->hasTracking();
  354. }
  355. // --------------------------------------------------------------------------
  356. void ctkDoubleRangeSlider::setTracking(bool enable)
  357. {
  358. CTK_D(ctkDoubleRangeSlider);
  359. d->Slider->setTracking(enable);
  360. }
  361. // --------------------------------------------------------------------------
  362. void ctkDoubleRangeSlider::triggerAction( QAbstractSlider::SliderAction action)
  363. {
  364. CTK_D(ctkDoubleRangeSlider);
  365. d->Slider->triggerAction(action);
  366. }
  367. // --------------------------------------------------------------------------
  368. void ctkDoubleRangeSlider::setOrientation(Qt::Orientation newOrientation)
  369. {
  370. CTK_D(ctkDoubleRangeSlider);
  371. d->Slider->setOrientation(newOrientation);
  372. }
  373. // --------------------------------------------------------------------------
  374. Qt::Orientation ctkDoubleRangeSlider::orientation()const
  375. {
  376. CTK_D(const ctkDoubleRangeSlider);
  377. return d->Slider->orientation();
  378. }
  379. // --------------------------------------------------------------------------
  380. void ctkDoubleRangeSlider::onMinValueChanged(int newValue)
  381. {
  382. CTK_D(ctkDoubleRangeSlider);
  383. double doubleNewValue = d->minFromInt(newValue);
  384. if (d->MinValue == doubleNewValue)
  385. {
  386. return;
  387. }
  388. d->MinValue = doubleNewValue;
  389. emit this->valuesChanged(d->MinValue, d->MaxValue);
  390. emit this->minimumValueChanged(d->MinValue);
  391. }
  392. // --------------------------------------------------------------------------
  393. void ctkDoubleRangeSlider::onMaxValueChanged(int newValue)
  394. {
  395. CTK_D(ctkDoubleRangeSlider);
  396. double doubleNewValue = d->maxFromInt(newValue);
  397. if (d->MaxValue == doubleNewValue)
  398. {
  399. return;
  400. }
  401. d->MaxValue = doubleNewValue;
  402. emit this->valuesChanged(d->MinValue, d->MaxValue);
  403. emit this->maximumValueChanged(d->MaxValue);
  404. }
  405. // --------------------------------------------------------------------------
  406. void ctkDoubleRangeSlider::onValuesChanged(int newMinValue, int newMaxValue)
  407. {
  408. CTK_D(ctkDoubleRangeSlider);
  409. double doubleNewMinValue = d->minFromInt(newMinValue);
  410. double doubleNewMaxValue = d->maxFromInt(newMaxValue);
  411. bool emitMinValueChanged = (d->MinValue != doubleNewMinValue);
  412. bool emitMaxValueChanged = (d->MaxValue != doubleNewMaxValue);
  413. if (!emitMinValueChanged && !emitMaxValueChanged)
  414. {
  415. return;
  416. }
  417. d->MinValue = doubleNewMinValue;
  418. d->MaxValue = doubleNewMaxValue;
  419. emit this->valuesChanged(d->MinValue, d->MaxValue);
  420. if (emitMinValueChanged)
  421. {
  422. emit this->minimumValueChanged(d->MinValue);
  423. }
  424. if (emitMaxValueChanged)
  425. {
  426. emit this->maximumValueChanged(d->MaxValue);
  427. }
  428. }
  429. // --------------------------------------------------------------------------
  430. void ctkDoubleRangeSlider::onMinPosChanged(int newPosition)
  431. {
  432. CTK_D(const ctkDoubleRangeSlider);
  433. emit this->minimumPositionChanged(d->minFromInt(newPosition));
  434. }
  435. // --------------------------------------------------------------------------
  436. void ctkDoubleRangeSlider::onMaxPosChanged(int newPosition)
  437. {
  438. CTK_D(const ctkDoubleRangeSlider);
  439. emit this->maximumPositionChanged(d->maxFromInt(newPosition));
  440. }
  441. // --------------------------------------------------------------------------
  442. void ctkDoubleRangeSlider::onPositionsChanged(int min, int max)
  443. {
  444. CTK_D(const ctkDoubleRangeSlider);
  445. emit this->positionsChanged(d->minFromInt(min), d->maxFromInt(max));
  446. }
  447. // --------------------------------------------------------------------------
  448. ctkRangeSlider* ctkDoubleRangeSlider::slider()const
  449. {
  450. CTK_D(const ctkDoubleRangeSlider);
  451. return d->Slider;
  452. }
  453. // --------------------------------------------------------------------------
  454. void ctkDoubleRangeSlider::setSlider(ctkRangeSlider* slider)
  455. {
  456. CTK_D(ctkDoubleRangeSlider);
  457. slider->setOrientation(d->Slider->orientation());
  458. slider->setMinimum(d->Slider->minimum());
  459. slider->setMaximum(d->Slider->maximum());
  460. slider->setValues(d->Slider->minimumValue(), d->Slider->maximumValue());
  461. slider->setSingleStep(d->Slider->singleStep());
  462. slider->setTracking(d->Slider->hasTracking());
  463. slider->setTickInterval(d->Slider->tickInterval());
  464. delete d->Slider;
  465. qobject_cast<QHBoxLayout*>(this->layout())->addWidget(slider);
  466. d->Slider = slider;
  467. d->connectSlider();
  468. }