ctkDoubleRangeSlider.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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 <QHBoxLayout>
  17. // CTK includes
  18. #include "ctkRangeSlider.h"
  19. #include "ctkDoubleRangeSlider.h"
  20. // STD includes
  21. #include <limits>
  22. //-----------------------------------------------------------------------------
  23. class ctkDoubleRangeSliderPrivate
  24. {
  25. Q_DECLARE_PUBLIC(ctkDoubleRangeSlider);
  26. protected:
  27. ctkDoubleRangeSlider* const q_ptr;
  28. public:
  29. ctkDoubleRangeSliderPrivate(ctkDoubleRangeSlider& object);
  30. int toInt(double _value)const;
  31. double minFromInt(int _value)const;
  32. double maxFromInt(int _value)const;
  33. double safeMinFromInt(int _value)const;
  34. double safeMaxFromInt(int _value)const;
  35. void init();
  36. void connectSlider();
  37. void updateMinOffset(double value);
  38. void updateMaxOffset(double value);
  39. ctkRangeSlider* Slider;
  40. double Minimum;
  41. double Maximum;
  42. bool SettingRange;
  43. // we should have a MinValueOffset and MinPositionOffset (and MinimumOffset?)
  44. double MinOffset;
  45. // we should have a MaxValueOffset and MaxPositionOffset (and MaximumOffset?)
  46. double MaxOffset;
  47. double SingleStep;
  48. double MinValue;
  49. double MaxValue;
  50. private:
  51. Q_DISABLE_COPY(ctkDoubleRangeSliderPrivate);
  52. };
  53. // --------------------------------------------------------------------------
  54. ctkDoubleRangeSliderPrivate::ctkDoubleRangeSliderPrivate(ctkDoubleRangeSlider& object)
  55. :q_ptr(&object)
  56. {
  57. // the initial values will be overwritten in
  58. // ctkDoubleRangeSliderPrivate::init()
  59. this->Slider = 0;
  60. this->Minimum = 0.;
  61. this->Maximum = 99.;
  62. this->SettingRange = false;
  63. this->MinOffset = 0.;
  64. this->MaxOffset = 0.;
  65. this->SingleStep = 1.;
  66. this->MinValue = 0.;
  67. this->MaxValue = 99.;
  68. }
  69. // --------------------------------------------------------------------------
  70. void ctkDoubleRangeSliderPrivate::init()
  71. {
  72. Q_Q(ctkDoubleRangeSlider);
  73. this->Slider = new ctkRangeSlider(q);
  74. QHBoxLayout* l = new QHBoxLayout(q);
  75. l->addWidget(this->Slider);
  76. l->setContentsMargins(0,0,0,0);
  77. this->Minimum = this->Slider->minimum();
  78. this->Maximum = this->Slider->maximum();
  79. this->MinValue = this->Slider->minimumValue();
  80. this->MaxValue = this->Slider->maximumValue();
  81. this->SingleStep = this->Slider->singleStep();
  82. q->setSizePolicy(this->Slider->sizePolicy());
  83. q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  84. this->connectSlider();
  85. }
  86. // --------------------------------------------------------------------------
  87. void ctkDoubleRangeSliderPrivate::connectSlider()
  88. {
  89. Q_Q(ctkDoubleRangeSlider);
  90. q->connect(this->Slider, SIGNAL(valuesChanged(int,int)),
  91. q, SLOT(onValuesChanged(int,int)));
  92. q->connect(this->Slider, SIGNAL(minimumPositionChanged(int)),
  93. q, SLOT(onMinPosChanged(int)));
  94. q->connect(this->Slider, SIGNAL(maximumPositionChanged(int)),
  95. q, SLOT(onMaxPosChanged(int)));
  96. q->connect(this->Slider, SIGNAL(positionsChanged(int,int)),
  97. q, SLOT(onPositionsChanged(int,int)));
  98. q->connect(this->Slider, SIGNAL(sliderPressed()),
  99. q, SIGNAL(sliderPressed()));
  100. q->connect(this->Slider, SIGNAL(sliderReleased()),
  101. q, SIGNAL(sliderReleased()));
  102. q->connect(this->Slider, SIGNAL(rangeChanged(int,int)),
  103. q, SLOT(onRangeChanged(int,int)));
  104. }
  105. // --------------------------------------------------------------------------
  106. int ctkDoubleRangeSliderPrivate::toInt(double doubleValue)const
  107. {
  108. double tmp = doubleValue / this->SingleStep;
  109. static const double minInt = std::numeric_limits<int>::min();
  110. static const double maxInt = std::numeric_limits<int>::max();
  111. #ifndef QT_NO_DEBUG
  112. if (tmp < minInt || tmp > maxInt)
  113. {
  114. qWarning() << __FUNCTION__ << ": value " << doubleValue
  115. << " for singleStep " << this->SingleStep
  116. << " is out of integer bounds !";
  117. }
  118. #endif
  119. tmp = qBound(minInt, tmp, maxInt);
  120. int intValue = qRound(tmp);
  121. //qDebug() << __FUNCTION__ << doubleValue << tmp << intValue;
  122. return intValue;
  123. }
  124. // --------------------------------------------------------------------------
  125. double ctkDoubleRangeSliderPrivate::minFromInt(int intValue)const
  126. {
  127. double doubleValue = this->SingleStep * (this->MinOffset + intValue) ;
  128. return doubleValue;
  129. }
  130. // --------------------------------------------------------------------------
  131. double ctkDoubleRangeSliderPrivate::maxFromInt(int intValue)const
  132. {
  133. double doubleValue = this->SingleStep * (this->MaxOffset + intValue) ;
  134. return doubleValue;
  135. }
  136. // --------------------------------------------------------------------------
  137. double ctkDoubleRangeSliderPrivate::safeMinFromInt(int intValue)const
  138. {
  139. return qBound(this->Minimum, this->minFromInt(intValue), this->Maximum);
  140. }
  141. // --------------------------------------------------------------------------
  142. double ctkDoubleRangeSliderPrivate::safeMaxFromInt(int intValue)const
  143. {
  144. return qBound(this->Minimum, this->maxFromInt(intValue), this->Maximum);
  145. }
  146. // --------------------------------------------------------------------------
  147. void ctkDoubleRangeSliderPrivate::updateMinOffset(double value)
  148. {
  149. this->MinOffset = (value / this->SingleStep) - this->toInt(value);
  150. }
  151. // --------------------------------------------------------------------------
  152. void ctkDoubleRangeSliderPrivate::updateMaxOffset(double value)
  153. {
  154. this->MaxOffset = (value / this->SingleStep) - this->toInt(value);
  155. }
  156. // --------------------------------------------------------------------------
  157. ctkDoubleRangeSlider::ctkDoubleRangeSlider(QWidget* _parent) : Superclass(_parent)
  158. , d_ptr(new ctkDoubleRangeSliderPrivate(*this))
  159. {
  160. Q_D(ctkDoubleRangeSlider);
  161. d->init();
  162. }
  163. // --------------------------------------------------------------------------
  164. ctkDoubleRangeSlider::ctkDoubleRangeSlider(Qt::Orientation _orientation, QWidget* _parent)
  165. : Superclass(_parent)
  166. , d_ptr(new ctkDoubleRangeSliderPrivate(*this))
  167. {
  168. Q_D(ctkDoubleRangeSlider);
  169. d->init();
  170. this->setOrientation(_orientation);
  171. }
  172. // --------------------------------------------------------------------------
  173. ctkDoubleRangeSlider::~ctkDoubleRangeSlider()
  174. {
  175. }
  176. // --------------------------------------------------------------------------
  177. void ctkDoubleRangeSlider::setMinimum(double min)
  178. {
  179. Q_D(ctkDoubleRangeSlider);
  180. double oldMin = d->Minimum;
  181. d->Minimum = min;
  182. if (d->Minimum >= d->MinValue)
  183. {// TBD: use same offset
  184. d->updateMinOffset(d->Minimum);
  185. }
  186. if (d->Minimum >= d->MaxValue)
  187. {// TBD: use same offset
  188. d->updateMaxOffset(d->Minimum);
  189. }
  190. d->SettingRange = true;
  191. d->Slider->setMinimum(d->toInt(min));
  192. d->SettingRange = false;
  193. if (d->Minimum != oldMin)
  194. {
  195. emit this->rangeChanged(d->Minimum, d->Maximum);
  196. }
  197. }
  198. // --------------------------------------------------------------------------
  199. double ctkDoubleRangeSlider::minimum()const
  200. {
  201. Q_D(const ctkDoubleRangeSlider);
  202. return d->Minimum;
  203. }
  204. // --------------------------------------------------------------------------
  205. void ctkDoubleRangeSlider::setMaximum(double max)
  206. {
  207. Q_D(ctkDoubleRangeSlider);
  208. double oldMax = d->Maximum;
  209. d->Maximum = max;
  210. if (d->Maximum <= d->MinValue)
  211. {// TBD: use same offset
  212. d->updateMinOffset(d->Maximum);
  213. }
  214. if (d->Maximum <= d->MaxValue)
  215. {// TBD: use same offset ?
  216. d->updateMaxOffset(d->Maximum);
  217. }
  218. d->SettingRange = true;
  219. d->Slider->setMaximum(d->toInt(max));
  220. d->SettingRange = false;
  221. if (d->Maximum != oldMax)
  222. {
  223. emit this->rangeChanged(d->Minimum, d->Maximum);
  224. }
  225. }
  226. // --------------------------------------------------------------------------
  227. double ctkDoubleRangeSlider::maximum()const
  228. {
  229. Q_D(const ctkDoubleRangeSlider);
  230. return d->Maximum;
  231. }
  232. // --------------------------------------------------------------------------
  233. void ctkDoubleRangeSlider::setRange(double min, double max)
  234. {
  235. Q_D(ctkDoubleRangeSlider);
  236. double oldMin = d->Minimum;
  237. double oldMax = d->Maximum;
  238. d->Minimum = min;
  239. d->Maximum = max;
  240. if (d->Minimum >= d->MinValue)
  241. {// TBD: use same offset
  242. d->updateMinOffset(d->Minimum);
  243. }
  244. if (d->Minimum >= d->MaxValue)
  245. {// TBD: use same offset
  246. d->updateMaxOffset(d->Minimum);
  247. }
  248. if (d->Maximum <= d->MinValue)
  249. {// TBD: use same offset
  250. d->updateMinOffset(d->Maximum);
  251. }
  252. if (d->Maximum <= d->MaxValue)
  253. {// TBD: use same offset ?
  254. d->updateMaxOffset(d->Maximum);
  255. }
  256. d->SettingRange = true;
  257. d->Slider->setRange(d->toInt(min), d->toInt(max));
  258. d->SettingRange = false;
  259. if (d->Minimum != oldMin || d->Maximum != oldMax)
  260. {
  261. emit this->rangeChanged(d->Minimum, d->Maximum);
  262. }
  263. }
  264. // --------------------------------------------------------------------------
  265. double ctkDoubleRangeSlider::minimumPosition()const
  266. {
  267. Q_D(const ctkDoubleRangeSlider);
  268. return d->safeMinFromInt(d->Slider->minimumPosition());
  269. }
  270. // --------------------------------------------------------------------------
  271. void ctkDoubleRangeSlider::setMinimumPosition(double minPos)
  272. {
  273. Q_D(ctkDoubleRangeSlider);
  274. d->Slider->setMinimumPosition(d->toInt(minPos));
  275. }
  276. // --------------------------------------------------------------------------
  277. double ctkDoubleRangeSlider::maximumPosition()const
  278. {
  279. Q_D(const ctkDoubleRangeSlider);
  280. return d->safeMaxFromInt(d->Slider->maximumPosition());
  281. }
  282. // --------------------------------------------------------------------------
  283. void ctkDoubleRangeSlider::setMaximumPosition(double maxPos)
  284. {
  285. Q_D(ctkDoubleRangeSlider);
  286. d->Slider->setMaximumPosition(d->toInt(maxPos));
  287. }
  288. // --------------------------------------------------------------------------
  289. void ctkDoubleRangeSlider::setPositions(double minPos, double maxPos)
  290. {
  291. Q_D(ctkDoubleRangeSlider);
  292. d->Slider->setPositions(d->toInt(minPos), d->toInt(maxPos));
  293. }
  294. // --------------------------------------------------------------------------
  295. double ctkDoubleRangeSlider::minimumValue()const
  296. {
  297. Q_D(const ctkDoubleRangeSlider);
  298. return d->MinValue;
  299. }
  300. // --------------------------------------------------------------------------
  301. void ctkDoubleRangeSlider::setMinimumValue(double newMinValue)
  302. {
  303. Q_D(ctkDoubleRangeSlider);
  304. newMinValue = qBound(d->Minimum, newMinValue, d->Maximum);
  305. d->updateMinOffset(newMinValue);
  306. if (newMinValue >= d->MaxValue)
  307. {
  308. d->updateMaxOffset(newMinValue);
  309. }
  310. int newIntValue = d->toInt(newMinValue);
  311. if (newIntValue != d->Slider->minimumValue())
  312. {
  313. // d->Slider will emit a minimumValueChanged signal that is connected to
  314. // ctkDoubleSlider::onValueChanged
  315. d->Slider->setMinimumValue(newIntValue);
  316. }
  317. else
  318. {
  319. double oldValue = d->MinValue;
  320. d->MinValue = newMinValue;
  321. // don't emit a valuechanged signal if the new value is quite
  322. // similar to the old value.
  323. if (qAbs(newMinValue - oldValue) > (d->SingleStep * 0.000000001))
  324. {
  325. emit this->valuesChanged(newMinValue, this->maximumValue());
  326. emit this->minimumValueChanged(newMinValue);
  327. }
  328. }
  329. }
  330. // --------------------------------------------------------------------------
  331. double ctkDoubleRangeSlider::maximumValue()const
  332. {
  333. Q_D(const ctkDoubleRangeSlider);
  334. return d->MaxValue;
  335. }
  336. // --------------------------------------------------------------------------
  337. void ctkDoubleRangeSlider::setMaximumValue(double newMaxValue)
  338. {
  339. Q_D(ctkDoubleRangeSlider);
  340. newMaxValue = qBound(d->Minimum, newMaxValue, d->Maximum);
  341. d->updateMaxOffset(newMaxValue);
  342. if (newMaxValue <= d->MinValue)
  343. {
  344. d->updateMinOffset(newMaxValue);
  345. }
  346. int newIntValue = d->toInt(newMaxValue);
  347. if (newIntValue != d->Slider->maximumValue())
  348. {
  349. // d->Slider will emit a maximumValueChanged signal that is connected to
  350. // ctkDoubleSlider::onValueChanged
  351. d->Slider->setMaximumValue(newIntValue);
  352. }
  353. else
  354. {
  355. double oldValue = d->MaxValue;
  356. d->MaxValue = newMaxValue;
  357. // don't emit a valuechanged signal if the new value is quite
  358. // similar to the old value.
  359. if (qAbs(newMaxValue - oldValue) > (d->SingleStep * 0.000000001))
  360. {
  361. emit this->valuesChanged(this->minimumValue(), newMaxValue);
  362. emit this->maximumValueChanged(newMaxValue);
  363. }
  364. }
  365. }
  366. // --------------------------------------------------------------------------
  367. void ctkDoubleRangeSlider::setValues(double newMinVal, double newMaxVal)
  368. {
  369. Q_D(ctkDoubleRangeSlider);
  370. // We can't call setMinimumValue() and setMaximumValue() as they would
  371. // generate an inconsistent state. when minimumValueChanged() is fired the
  372. // new max value wouldn't be updated yet.
  373. double newMinValue = qBound(d->Minimum, qMin(newMinVal, newMaxVal), d->Maximum);
  374. double newMaxValue = qBound(d->Minimum, qMax(newMinVal, newMaxVal), d->Maximum);
  375. d->updateMinOffset(newMinValue);
  376. d->updateMaxOffset(newMaxValue);
  377. int newMinIntValue = d->toInt(newMinValue);
  378. int newMaxIntValue = d->toInt(newMaxValue);
  379. if (newMinIntValue != d->Slider->minimumValue() ||
  380. newMaxIntValue != d->Slider->maximumValue())
  381. {
  382. // d->Slider will emit a maximumValueChanged signal that is connected to
  383. // ctkDoubleSlider::onValueChanged
  384. d->Slider->setValues(newMinIntValue, newMaxIntValue);
  385. }
  386. else
  387. {
  388. double oldMinValue = d->MinValue;
  389. double oldMaxValue = d->MaxValue;
  390. d->MinValue = newMinValue;
  391. d->MaxValue = newMaxValue;
  392. // don't emit a valuechanged signal if the new value is quite
  393. // similar to the old value.
  394. bool minChanged = qAbs(newMinValue - oldMinValue) > (d->SingleStep * 0.000000001);
  395. bool maxChanged = qAbs(newMaxValue - oldMaxValue) > (d->SingleStep * 0.000000001);
  396. if (minChanged || maxChanged)
  397. {
  398. emit this->valuesChanged(newMinValue, newMaxValue);
  399. if (minChanged)
  400. {
  401. emit this->minimumValueChanged(newMinValue);
  402. }
  403. if (maxChanged)
  404. {
  405. emit this->maximumValueChanged(newMaxValue);
  406. }
  407. }
  408. }
  409. }
  410. // --------------------------------------------------------------------------
  411. double ctkDoubleRangeSlider::singleStep()const
  412. {
  413. Q_D(const ctkDoubleRangeSlider);
  414. return d->SingleStep;
  415. }
  416. // --------------------------------------------------------------------------
  417. void ctkDoubleRangeSlider::setSingleStep(double newStep)
  418. {
  419. Q_D(ctkDoubleRangeSlider);
  420. if (!this->isValidStep(newStep))
  421. {
  422. qWarning() << "Single step " << newStep << "is out of bounds.";
  423. return;
  424. }
  425. d->SingleStep = newStep;
  426. // The following can fire A LOT of signals that shouldn't be
  427. // fired.
  428. bool oldBlockSignals = this->blockSignals(true);
  429. d->updateMinOffset(d->MinValue);
  430. d->updateMaxOffset(d->MaxValue);
  431. // update the new values of the ctkRangeSlider
  432. double _minvalue = d->MinValue;
  433. double _maxvalue = d->MaxValue;
  434. // calling setMinimum or setMaximum can change the values MinimumValue
  435. // and MaximumValue, this is why we re-set them later.
  436. this->setMinimum(d->Minimum);
  437. this->setMaximum(d->Maximum);
  438. this->setMinimumValue(_minvalue);
  439. this->setMinimumPosition(_minvalue);
  440. this->setMaximumValue(_maxvalue);
  441. this->setMaximumPosition(_maxvalue);
  442. this->blockSignals(oldBlockSignals);
  443. }
  444. // --------------------------------------------------------------------------
  445. bool ctkDoubleRangeSlider::isValidStep(double step)const
  446. {
  447. const double minStep( qMax(this->maximum() / std::numeric_limits<int>::max(),
  448. std::numeric_limits<double>::epsilon()) );
  449. const double maxStep( qMin(this->maximum() - this->minimum(),
  450. static_cast<double>(std::numeric_limits<int>::max())) );
  451. return step > minStep && step < maxStep;
  452. }
  453. // --------------------------------------------------------------------------
  454. double ctkDoubleRangeSlider::tickInterval()const
  455. {
  456. Q_D(const ctkDoubleRangeSlider);
  457. return d->SingleStep * d->Slider->tickInterval();
  458. }
  459. // --------------------------------------------------------------------------
  460. void ctkDoubleRangeSlider::setTickInterval(double newTickInterval)
  461. {
  462. Q_D(ctkDoubleRangeSlider);
  463. d->Slider->setTickInterval(d->toInt(newTickInterval));
  464. }
  465. // --------------------------------------------------------------------------
  466. QSlider::TickPosition ctkDoubleRangeSlider::tickPosition()const
  467. {
  468. Q_D(const ctkDoubleRangeSlider);
  469. return d->Slider->tickPosition();
  470. }
  471. // --------------------------------------------------------------------------
  472. void ctkDoubleRangeSlider::setTickPosition(QSlider::TickPosition newTickPosition)
  473. {
  474. Q_D(ctkDoubleRangeSlider);
  475. d->Slider->setTickPosition(newTickPosition);
  476. }
  477. // --------------------------------------------------------------------------
  478. bool ctkDoubleRangeSlider::hasTracking()const
  479. {
  480. Q_D(const ctkDoubleRangeSlider);
  481. return d->Slider->hasTracking();
  482. }
  483. // --------------------------------------------------------------------------
  484. void ctkDoubleRangeSlider::setTracking(bool enable)
  485. {
  486. Q_D(ctkDoubleRangeSlider);
  487. d->Slider->setTracking(enable);
  488. }
  489. // --------------------------------------------------------------------------
  490. void ctkDoubleRangeSlider::triggerAction( QAbstractSlider::SliderAction action)
  491. {
  492. Q_D(ctkDoubleRangeSlider);
  493. d->Slider->triggerAction(action);
  494. }
  495. // --------------------------------------------------------------------------
  496. void ctkDoubleRangeSlider::setOrientation(Qt::Orientation newOrientation)
  497. {
  498. Q_D(ctkDoubleRangeSlider);
  499. if (this->orientation() == newOrientation)
  500. {
  501. return;
  502. }
  503. if (!testAttribute(Qt::WA_WState_OwnSizePolicy))
  504. {
  505. QSizePolicy sp = this->sizePolicy();
  506. sp.transpose();
  507. this->setSizePolicy(sp);
  508. this->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  509. }
  510. // d->Slider will take care of calling updateGeometry
  511. d->Slider->setOrientation(newOrientation);
  512. }
  513. // --------------------------------------------------------------------------
  514. Qt::Orientation ctkDoubleRangeSlider::orientation()const
  515. {
  516. Q_D(const ctkDoubleRangeSlider);
  517. return d->Slider->orientation();
  518. }
  519. // --------------------------------------------------------------------------
  520. bool ctkDoubleRangeSlider::symmetricMoves()const
  521. {
  522. Q_D(const ctkDoubleRangeSlider);
  523. return d->Slider->symmetricMoves();
  524. }
  525. // --------------------------------------------------------------------------
  526. void ctkDoubleRangeSlider::setSymmetricMoves(bool symmetry)
  527. {
  528. Q_D(ctkDoubleRangeSlider);
  529. d->Slider->setSymmetricMoves(symmetry);
  530. }
  531. // --------------------------------------------------------------------------
  532. void ctkDoubleRangeSlider::onValuesChanged(int newMinValue, int newMaxValue)
  533. {
  534. Q_D(ctkDoubleRangeSlider);
  535. double doubleNewMinValue = d->safeMinFromInt(newMinValue);
  536. double doubleNewMaxValue = d->safeMaxFromInt(newMaxValue);
  537. bool emitMinValueChanged = (d->MinValue != doubleNewMinValue);
  538. bool emitMaxValueChanged = (d->MaxValue != doubleNewMaxValue);
  539. if (!emitMinValueChanged && !emitMaxValueChanged)
  540. {
  541. return;
  542. }
  543. d->MinValue = doubleNewMinValue;
  544. d->MaxValue = doubleNewMaxValue;
  545. emit this->valuesChanged(d->MinValue, d->MaxValue);
  546. if (emitMinValueChanged)
  547. {
  548. emit this->minimumValueChanged(d->MinValue);
  549. }
  550. if (emitMaxValueChanged)
  551. {
  552. emit this->maximumValueChanged(d->MaxValue);
  553. }
  554. }
  555. // --------------------------------------------------------------------------
  556. void ctkDoubleRangeSlider::onMinPosChanged(int newPosition)
  557. {
  558. Q_D(const ctkDoubleRangeSlider);
  559. emit this->minimumPositionChanged(d->safeMinFromInt(newPosition));
  560. }
  561. // --------------------------------------------------------------------------
  562. void ctkDoubleRangeSlider::onMaxPosChanged(int newPosition)
  563. {
  564. Q_D(const ctkDoubleRangeSlider);
  565. emit this->maximumPositionChanged(d->safeMaxFromInt(newPosition));
  566. }
  567. // --------------------------------------------------------------------------
  568. void ctkDoubleRangeSlider::onPositionsChanged(int min, int max)
  569. {
  570. Q_D(const ctkDoubleRangeSlider);
  571. emit this->positionsChanged(d->safeMinFromInt(min), d->safeMaxFromInt(max));
  572. }
  573. // --------------------------------------------------------------------------
  574. void ctkDoubleRangeSlider::onRangeChanged(int min, int max)
  575. {
  576. Q_D(const ctkDoubleRangeSlider);
  577. if (!d->SettingRange)
  578. {
  579. this->setRange(d->minFromInt(min), d->maxFromInt(max));
  580. }
  581. }
  582. // --------------------------------------------------------------------------
  583. ctkRangeSlider* ctkDoubleRangeSlider::slider()const
  584. {
  585. Q_D(const ctkDoubleRangeSlider);
  586. return d->Slider;
  587. }
  588. // --------------------------------------------------------------------------
  589. void ctkDoubleRangeSlider::setSlider(ctkRangeSlider* newslider)
  590. {
  591. Q_D(ctkDoubleRangeSlider);
  592. newslider->setOrientation(d->Slider->orientation());
  593. newslider->setMinimum(d->Slider->minimum());
  594. newslider->setMaximum(d->Slider->maximum());
  595. newslider->setValues(d->Slider->minimumValue(), d->Slider->maximumValue());
  596. newslider->setSingleStep(d->Slider->singleStep());
  597. newslider->setTracking(d->Slider->hasTracking());
  598. newslider->setTickInterval(d->Slider->tickInterval());
  599. newslider->setTickPosition(d->Slider->tickPosition());
  600. delete d->Slider;
  601. qobject_cast<QHBoxLayout*>(this->layout())->addWidget(newslider);
  602. d->Slider = newslider;
  603. d->connectSlider();
  604. }