ctkRangeWidget.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 <QPointer>
  18. // CTK includes
  19. #include "ctkRangeWidget.h"
  20. #include "ctkValueProxy.h"
  21. #include "ui_ctkRangeWidget.h"
  22. // STD includes
  23. #include <cmath>
  24. #include <limits>
  25. //-----------------------------------------------------------------------------
  26. class ctkRangeWidgetPrivate: public Ui_ctkRangeWidget
  27. {
  28. Q_DECLARE_PUBLIC(ctkRangeWidget);
  29. protected:
  30. ctkRangeWidget* const q_ptr;
  31. public:
  32. ctkRangeWidgetPrivate(ctkRangeWidget& object);
  33. void connectSlider();
  34. void updateSpinBoxWidth();
  35. int synchronizedSpinBoxWidth()const;
  36. void synchronizeSiblingSpinBox(int newWidth);
  37. bool equal(double v1, double v2)const;
  38. void relayout();
  39. bool useCustomSpinBoxesLimits()const;
  40. double clamp(double v, double min, double max);
  41. bool Tracking;
  42. bool Changing;
  43. bool SettingSliderRange;
  44. bool BlockSliderUpdate;
  45. double MinimumValueBeforeChange;
  46. double MaximumValueBeforeChange;
  47. bool AutoSpinBoxWidth;
  48. double CustomSpinBoxesLimitsMin;
  49. double CustomSpinBoxesLimitsMax;
  50. Qt::Alignment SpinBoxAlignment;
  51. QPointer<ctkValueProxy> Proxy;
  52. };
  53. // --------------------------------------------------------------------------
  54. bool ctkRangeWidgetPrivate::equal(double v1, double v2)const
  55. {
  56. if (v1 == v2)
  57. {// don't bother computing difference as it could fail for infinity numbers
  58. return true;
  59. }
  60. if (v1 != v1 && v2 != v2)
  61. {// NaN check
  62. return true;
  63. }
  64. return qAbs(v1 - v2) < pow(10., -this->MinimumSpinBox->decimals());
  65. }
  66. // --------------------------------------------------------------------------
  67. ctkRangeWidgetPrivate::ctkRangeWidgetPrivate(ctkRangeWidget& object)
  68. :q_ptr(&object)
  69. {
  70. this->Tracking = true;
  71. this->Changing = false;
  72. this->SettingSliderRange = false;
  73. this->BlockSliderUpdate = false;
  74. this->MinimumValueBeforeChange = 0.;
  75. this->MaximumValueBeforeChange = 0.;
  76. this->AutoSpinBoxWidth = true;
  77. this->CustomSpinBoxesLimitsMin = std::numeric_limits<double>::max();
  78. this->CustomSpinBoxesLimitsMax = std::numeric_limits<double>::min();
  79. this->SpinBoxAlignment = Qt::AlignVCenter;
  80. }
  81. // --------------------------------------------------------------------------
  82. void ctkRangeWidgetPrivate::connectSlider()
  83. {
  84. Q_Q(ctkRangeWidget);
  85. QObject::connect(this->Slider, SIGNAL(valuesChanged(double,double)),
  86. q, SLOT(changeValues(double,double)));
  87. QObject::connect(this->Slider, SIGNAL(minimumValueChanged(double)),
  88. q, SLOT(changeMinimumValue(double)));
  89. QObject::connect(this->Slider, SIGNAL(maximumValueChanged(double)),
  90. q, SLOT(changeMaximumValue(double)));
  91. QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
  92. q, SLOT(setSliderValues()));
  93. QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
  94. q, SLOT(setSliderValues()));
  95. QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
  96. q, SLOT(setMinimumToMaximumSpinBox(double)));
  97. QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
  98. q, SLOT(setMaximumToMinimumSpinBox(double)));
  99. QObject::connect(this->MinimumSpinBox, SIGNAL(decimalsChanged(int)),
  100. q, SLOT(setDecimals(int)));
  101. QObject::connect(this->MaximumSpinBox, SIGNAL(decimalsChanged(int)),
  102. q, SLOT(setDecimals(int)));
  103. QObject::connect(this->Slider, SIGNAL(sliderPressed()),
  104. q, SLOT(startChanging()));
  105. QObject::connect(this->Slider, SIGNAL(sliderReleased()),
  106. q, SLOT(stopChanging()));
  107. QObject::connect(this->Slider, SIGNAL(rangeChanged(double,double)),
  108. q, SLOT(onSliderRangeChanged(double,double)));
  109. }
  110. // --------------------------------------------------------------------------
  111. void ctkRangeWidgetPrivate::updateSpinBoxWidth()
  112. {
  113. int spinBoxWidth = this->synchronizedSpinBoxWidth();
  114. if (this->AutoSpinBoxWidth)
  115. {
  116. this->MinimumSpinBox->setMinimumWidth(spinBoxWidth);
  117. this->MaximumSpinBox->setMinimumWidth(spinBoxWidth);
  118. }
  119. else
  120. {
  121. this->MinimumSpinBox->setMinimumWidth(0);
  122. this->MaximumSpinBox->setMinimumWidth(0);
  123. }
  124. this->synchronizeSiblingSpinBox(spinBoxWidth);
  125. }
  126. // --------------------------------------------------------------------------
  127. int ctkRangeWidgetPrivate::synchronizedSpinBoxWidth()const
  128. {
  129. Q_Q(const ctkRangeWidget);
  130. //Q_ASSERT(this->MinimumSpinBox->sizeHint() == this->MaximumSpinBox->sizeHint());
  131. int maxWidth = qMax(this->MinimumSpinBox->sizeHint().width(),
  132. this->MaximumSpinBox->sizeHint().width());
  133. if (!q->parent())
  134. {
  135. return maxWidth;
  136. }
  137. QList<ctkRangeWidget*> siblings =
  138. q->parent()->findChildren<ctkRangeWidget*>();
  139. foreach(ctkRangeWidget* sibling, siblings)
  140. {
  141. maxWidth = qMax(maxWidth, qMax(sibling->d_func()->MaximumSpinBox->sizeHint().width(),
  142. sibling->d_func()->MaximumSpinBox->sizeHint().width()));
  143. }
  144. return maxWidth;
  145. }
  146. // --------------------------------------------------------------------------
  147. void ctkRangeWidgetPrivate::synchronizeSiblingSpinBox(int width)
  148. {
  149. Q_Q(const ctkRangeWidget);
  150. QList<ctkRangeWidget*> siblings =
  151. q->parent()->findChildren<ctkRangeWidget*>();
  152. foreach(ctkRangeWidget* sibling, siblings)
  153. {
  154. if (sibling != q && sibling->isAutoSpinBoxWidth())
  155. {
  156. sibling->d_func()->MinimumSpinBox->setMinimumWidth(width);
  157. sibling->d_func()->MaximumSpinBox->setMinimumWidth(width);
  158. }
  159. }
  160. }
  161. // --------------------------------------------------------------------------
  162. void ctkRangeWidgetPrivate::relayout()
  163. {
  164. this->GridLayout->removeWidget(this->MinimumSpinBox);
  165. this->GridLayout->removeWidget(this->MaximumSpinBox);
  166. this->GridLayout->removeWidget(this->Slider);
  167. if (this->SpinBoxAlignment & Qt::AlignTop)
  168. {
  169. this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
  170. this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
  171. this->GridLayout->addWidget(this->Slider,1,0,1,3);
  172. }
  173. else if (this->SpinBoxAlignment & Qt::AlignBottom)
  174. {
  175. this->GridLayout->addWidget(this->MinimumSpinBox,1,0);
  176. this->GridLayout->addWidget(this->MaximumSpinBox,1,2);
  177. this->GridLayout->addWidget(this->Slider,0, 0, 1, 3);
  178. }
  179. else if (this->SpinBoxAlignment & Qt::AlignRight)
  180. {
  181. this->GridLayout->addWidget(this->Slider, 0, 0);
  182. this->GridLayout->addWidget(this->MinimumSpinBox,0,1);
  183. this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
  184. }
  185. else if (this->SpinBoxAlignment & Qt::AlignLeft)
  186. {
  187. this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
  188. this->GridLayout->addWidget(this->MaximumSpinBox,0,1);
  189. this->GridLayout->addWidget(this->Slider, 0, 2);
  190. }
  191. else // Qt::AlignVCenter (or any other bad alignment)
  192. {
  193. this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
  194. this->GridLayout->addWidget(this->Slider,0,1);
  195. this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
  196. }
  197. }
  198. // --------------------------------------------------------------------------
  199. bool ctkRangeWidgetPrivate::useCustomSpinBoxesLimits()const
  200. {
  201. return this->CustomSpinBoxesLimitsMin <= this->CustomSpinBoxesLimitsMax;
  202. }
  203. // --------------------------------------------------------------------------
  204. double ctkRangeWidgetPrivate::clamp(double v, double min, double max)
  205. {
  206. if (v < min)
  207. {
  208. v = min;
  209. }
  210. if (v > max)
  211. {
  212. v = max;
  213. }
  214. return v;
  215. }
  216. // --------------------------------------------------------------------------
  217. ctkRangeWidget::ctkRangeWidget(QWidget* _parent) : Superclass(_parent)
  218. , d_ptr(new ctkRangeWidgetPrivate(*this))
  219. {
  220. Q_D(ctkRangeWidget);
  221. d->setupUi(this);
  222. if (d->useCustomSpinBoxesLimits())
  223. {
  224. d->MinimumSpinBox->setRange(d->CustomSpinBoxesLimitsMin, d->Slider->maximum());
  225. d->MaximumSpinBox->setRange(d->Slider->minimum(), d->CustomSpinBoxesLimitsMax);
  226. }
  227. else
  228. {
  229. d->MinimumSpinBox->setRange(d->Slider->minimum(), d->Slider->maximum());
  230. d->MaximumSpinBox->setRange(d->Slider->minimum(), d->Slider->maximum());
  231. }
  232. d->MinimumSpinBox->setValue(d->Slider->minimumValue());
  233. d->MaximumSpinBox->setValue(d->Slider->maximumValue());
  234. d->connectSlider();
  235. d->MinimumSpinBox->installEventFilter(this);
  236. d->MaximumSpinBox->installEventFilter(this);
  237. }
  238. // --------------------------------------------------------------------------
  239. ctkRangeWidget::~ctkRangeWidget()
  240. {
  241. }
  242. // --------------------------------------------------------------------------
  243. double ctkRangeWidget::minimum()const
  244. {
  245. Q_D(const ctkRangeWidget);
  246. if (!d->useCustomSpinBoxesLimits())
  247. {
  248. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  249. }
  250. return d->Slider->minimum();
  251. }
  252. // --------------------------------------------------------------------------
  253. double ctkRangeWidget::maximum()const
  254. {
  255. Q_D(const ctkRangeWidget);
  256. if (!d->useCustomSpinBoxesLimits())
  257. {
  258. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  259. }
  260. return d->Slider->maximum();
  261. }
  262. // --------------------------------------------------------------------------
  263. void ctkRangeWidget::setMinimum(double min)
  264. {
  265. Q_D(ctkRangeWidget);
  266. bool blocked = d->MinimumSpinBox->blockSignals(true);
  267. blocked = d->MaximumSpinBox->blockSignals(true);
  268. if (d->useCustomSpinBoxesLimits())
  269. {
  270. d->MinimumSpinBox->setMinimum(d->CustomSpinBoxesLimitsMin);
  271. }
  272. else
  273. {
  274. d->MinimumSpinBox->setMinimum(min);
  275. }
  276. d->MaximumSpinBox->setMinimum(min);
  277. d->MinimumSpinBox->blockSignals(blocked);
  278. d->MaximumSpinBox->blockSignals(blocked);
  279. // SpinBox can truncate min (depending on decimals).
  280. // use Spinbox's min to set Slider's min
  281. d->SettingSliderRange = true;
  282. d->Slider->setMinimum(d->MaximumSpinBox->minimum());
  283. d->SettingSliderRange = false;
  284. if (!d->useCustomSpinBoxesLimits())
  285. {
  286. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  287. }
  288. d->updateSpinBoxWidth();
  289. }
  290. // --------------------------------------------------------------------------
  291. void ctkRangeWidget::setMaximum(double max)
  292. {
  293. Q_D(ctkRangeWidget);
  294. bool blocked = d->MinimumSpinBox->blockSignals(true);
  295. blocked = d->MaximumSpinBox->blockSignals(true);
  296. if (d->useCustomSpinBoxesLimits())
  297. {
  298. d->MaximumSpinBox->setMaximum(d->CustomSpinBoxesLimitsMax);
  299. }
  300. else
  301. {
  302. d->MaximumSpinBox->setMaximum(max);
  303. }
  304. d->MinimumSpinBox->setMaximum(max);
  305. d->MinimumSpinBox->blockSignals(blocked);
  306. d->MaximumSpinBox->blockSignals(blocked);
  307. // SpinBox can truncate max (depending on decimals).
  308. // use Spinbox's max to set Slider's max
  309. d->SettingSliderRange = true;
  310. d->Slider->setMaximum(d->MinimumSpinBox->maximum());
  311. d->SettingSliderRange = false;
  312. if (!d->useCustomSpinBoxesLimits())
  313. {
  314. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  315. }
  316. d->updateSpinBoxWidth();
  317. }
  318. // --------------------------------------------------------------------------
  319. void ctkRangeWidget::setRange(double min, double max)
  320. {
  321. Q_D(ctkRangeWidget);
  322. if (d->useCustomSpinBoxesLimits())
  323. {
  324. min = d->clamp(min, d->CustomSpinBoxesLimitsMin,
  325. d->CustomSpinBoxesLimitsMax);
  326. max = d->clamp(max, d->CustomSpinBoxesLimitsMin,
  327. d->CustomSpinBoxesLimitsMax);
  328. }
  329. double oldMin = d->MinimumSpinBox->minimum();
  330. double oldMax = d->MaximumSpinBox->maximum();
  331. bool blocked = d->MinimumSpinBox->blockSignals(true);
  332. if (d->useCustomSpinBoxesLimits())
  333. {
  334. d->MinimumSpinBox->setRange(d->CustomSpinBoxesLimitsMin, qMax(min,max));
  335. }
  336. else
  337. {
  338. d->MinimumSpinBox->setRange(qMin(min,max), qMax(min,max));
  339. }
  340. d->MinimumSpinBox->blockSignals(blocked);
  341. blocked = d->MaximumSpinBox->blockSignals(true);
  342. if (d->useCustomSpinBoxesLimits())
  343. {
  344. d->MaximumSpinBox->setRange(qMin(min,max), d->CustomSpinBoxesLimitsMax);
  345. }
  346. else
  347. {
  348. d->MaximumSpinBox->setRange(qMin(min,max), qMax(min,max));
  349. }
  350. d->MaximumSpinBox->blockSignals(blocked);
  351. // SpinBox can truncate the range (depending on decimals).
  352. // use Spinbox's range to set Slider's range
  353. d->SettingSliderRange = true;
  354. d->Slider->setRange(d->MaximumSpinBox->minimum(), d->MinimumSpinBox->maximum());
  355. d->SettingSliderRange = false;
  356. if (!d->useCustomSpinBoxesLimits())
  357. {
  358. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(), d->Slider->minimum()));
  359. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  360. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  361. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  362. }
  363. d->updateSpinBoxWidth();
  364. if (!d->useCustomSpinBoxesLimits())
  365. {
  366. if (oldMin != d->MinimumSpinBox->minimum() ||
  367. oldMax != d->MaximumSpinBox->maximum())
  368. {
  369. emit rangeChanged(d->MinimumSpinBox->minimum(), d->MaximumSpinBox->maximum());
  370. }
  371. }
  372. }
  373. // --------------------------------------------------------------------------
  374. void ctkRangeWidget::range(double range[2])const
  375. {
  376. Q_D(const ctkRangeWidget);
  377. if (!d->useCustomSpinBoxesLimits())
  378. {
  379. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(), d->Slider->minimum()));
  380. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  381. }
  382. range[0] = d->Slider->minimum();
  383. range[1] = d->Slider->maximum();
  384. }
  385. // --------------------------------------------------------------------------
  386. void ctkRangeWidget::onSliderRangeChanged(double min, double max)
  387. {
  388. Q_D(ctkRangeWidget);
  389. if (!d->SettingSliderRange)
  390. {
  391. this->setRange(min, max);
  392. }
  393. }
  394. /*
  395. // --------------------------------------------------------------------------
  396. double ctkRangeWidget::sliderPosition()const
  397. {
  398. return d->Slider->sliderPosition();
  399. }
  400. // --------------------------------------------------------------------------
  401. void ctkRangeWidget::setSliderPosition(double position)
  402. {
  403. d->Slider->setSliderPosition(position);
  404. }
  405. */
  406. /*
  407. // --------------------------------------------------------------------------
  408. double ctkRangeWidget::previousSliderPosition()
  409. {
  410. return d->Slider->previousSliderPosition();
  411. }
  412. */
  413. // --------------------------------------------------------------------------
  414. void ctkRangeWidget::values(double &minValue, double &maxValue)const
  415. {
  416. Q_D(const ctkRangeWidget);
  417. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  418. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  419. minValue = d->Changing ? d->MinimumValueBeforeChange : d->Slider->minimumValue();
  420. maxValue = d->Changing ? d->MaximumValueBeforeChange : d->Slider->maximumValue();
  421. }
  422. // --------------------------------------------------------------------------
  423. double ctkRangeWidget::minimumValue()const
  424. {
  425. Q_D(const ctkRangeWidget);
  426. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  427. const double minValue =
  428. d->Changing ? d->MinimumValueBeforeChange : d->Slider->minimumValue();
  429. return minValue;
  430. }
  431. // --------------------------------------------------------------------------
  432. double ctkRangeWidget::maximumValue()const
  433. {
  434. Q_D(const ctkRangeWidget);
  435. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  436. const double maxValue =
  437. d->Changing ? d->MaximumValueBeforeChange : d->Slider->maximumValue();
  438. return maxValue;
  439. }
  440. // --------------------------------------------------------------------------
  441. void ctkRangeWidget::setMinimumValue(double _value)
  442. {
  443. Q_D(ctkRangeWidget);
  444. // disable the tracking temporally to emit the
  445. // signal valueChanged if changeValue() is called
  446. bool isChanging = d->Changing;
  447. d->Changing = false;
  448. d->MinimumSpinBox->setValue(_value);
  449. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  450. // restore the prop
  451. d->Changing = isChanging;
  452. }
  453. // --------------------------------------------------------------------------
  454. void ctkRangeWidget::setMaximumValue(double _value)
  455. {
  456. Q_D(ctkRangeWidget);
  457. // disable the tracking temporally to emit the
  458. // signal valueChanged if changeValue() is called
  459. bool isChanging = d->Changing;
  460. d->Changing = false;
  461. d->MaximumSpinBox->setValue(_value);
  462. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  463. // restore the prop
  464. d->Changing = isChanging;
  465. }
  466. // --------------------------------------------------------------------------
  467. void ctkRangeWidget::setValues(double newMinimumValue, double newMaximumValue)
  468. {
  469. Q_D(ctkRangeWidget);
  470. if (newMinimumValue > newMaximumValue)
  471. {
  472. qSwap(newMinimumValue, newMaximumValue);
  473. }
  474. // This test must take into account NaN values
  475. const bool minimumFirst = !(newMinimumValue > this->maximumValue());
  476. // disable the tracking temporally to emit the
  477. // signal valueChanged if changeValue() is called
  478. bool isChanging = d->Changing;
  479. d->Changing = false;
  480. // \todo: setting the spinbox separately is currently firing 2 signals and
  481. // between the signals, the state of the widget is inconsistent.
  482. bool wasBlocking = d->BlockSliderUpdate;
  483. d->BlockSliderUpdate = true;
  484. if (minimumFirst)
  485. {
  486. d->MinimumSpinBox->setValue(newMinimumValue);
  487. d->MaximumSpinBox->setValue(newMaximumValue);
  488. }
  489. else
  490. {
  491. d->MaximumSpinBox->setValue(newMaximumValue);
  492. d->MinimumSpinBox->setValue(newMinimumValue);
  493. }
  494. d->BlockSliderUpdate = wasBlocking;
  495. this->setSliderValues();
  496. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  497. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  498. // restore the prop
  499. d->Changing = isChanging;
  500. }
  501. // --------------------------------------------------------------------------
  502. void ctkRangeWidget::setSliderValues()
  503. {
  504. Q_D(ctkRangeWidget);
  505. if (d->BlockSliderUpdate)
  506. {
  507. return;
  508. }
  509. if (d->useCustomSpinBoxesLimits())
  510. {
  511. if (d->MinimumSpinBox->value() < d->Slider->minimum())
  512. {
  513. d->Slider->setMinimum(d->MinimumSpinBox->value());
  514. }
  515. if (d->MaximumSpinBox->value() > d->Slider->maximum())
  516. {
  517. d->Slider->setMaximum(d->MaximumSpinBox->value());
  518. }
  519. }
  520. d->Slider->setValues(d->MinimumSpinBox->value(), d->MaximumSpinBox->value());
  521. }
  522. // --------------------------------------------------------------------------
  523. void ctkRangeWidget::setMinimumToMaximumSpinBox(double minimum)
  524. {
  525. Q_D(ctkRangeWidget);
  526. if (minimum != minimum) // NaN check
  527. {
  528. return;
  529. }
  530. if (d->useCustomSpinBoxesLimits())
  531. {
  532. d->MaximumSpinBox->setRange(minimum, d->CustomSpinBoxesLimitsMax);
  533. }
  534. else
  535. {
  536. d->MaximumSpinBox->setRange(minimum, d->Slider->maximum());
  537. }
  538. }
  539. // --------------------------------------------------------------------------
  540. void ctkRangeWidget::setMaximumToMinimumSpinBox(double maximum)
  541. {
  542. Q_D(ctkRangeWidget);
  543. if (maximum != maximum) // NaN check
  544. {
  545. return;
  546. }
  547. if (d->useCustomSpinBoxesLimits())
  548. {
  549. d->MinimumSpinBox->setRange(d->CustomSpinBoxesLimitsMin, maximum);
  550. }
  551. else
  552. {
  553. d->MinimumSpinBox->setRange(d->Slider->minimum(), maximum);
  554. }
  555. }
  556. // --------------------------------------------------------------------------
  557. void ctkRangeWidget::startChanging()
  558. {
  559. Q_D(ctkRangeWidget);
  560. if (d->Tracking)
  561. {
  562. return;
  563. }
  564. d->MinimumValueBeforeChange = this->minimumValue();
  565. d->MaximumValueBeforeChange = this->maximumValue();
  566. d->Changing = true;
  567. }
  568. // --------------------------------------------------------------------------
  569. void ctkRangeWidget::stopChanging()
  570. {
  571. Q_D(ctkRangeWidget);
  572. if (d->Tracking)
  573. {
  574. return;
  575. }
  576. d->Changing = false;
  577. bool emitMinValChanged = qAbs(this->minimumValue() - d->MinimumValueBeforeChange) > (this->singleStep() * 0.000000001);
  578. bool emitMaxValChanged = qAbs(this->maximumValue() - d->MaximumValueBeforeChange) > (this->singleStep() * 0.000000001);
  579. if (emitMinValChanged || emitMaxValChanged)
  580. {
  581. // emit the valuesChanged signal first
  582. emit this->valuesChanged(this->minimumValue(), this->maximumValue());
  583. }
  584. if (emitMinValChanged)
  585. {
  586. emit this->minimumValueChanged(this->minimumValue());
  587. }
  588. if (emitMaxValChanged)
  589. {
  590. emit this->maximumValueChanged(this->maximumValue());
  591. }
  592. }
  593. // --------------------------------------------------------------------------
  594. void ctkRangeWidget::changeMinimumValue(double newValue)
  595. {
  596. Q_D(ctkRangeWidget);
  597. //if (d->Tracking)
  598. {
  599. emit this->minimumValueIsChanging(newValue);
  600. }
  601. if (!d->Changing)
  602. {
  603. emit this->minimumValueChanged(newValue);
  604. }
  605. }
  606. // --------------------------------------------------------------------------
  607. void ctkRangeWidget::changeMaximumValue(double newValue)
  608. {
  609. Q_D(ctkRangeWidget);
  610. //if (d->Tracking)
  611. {
  612. emit this->maximumValueIsChanging(newValue);
  613. }
  614. if (!d->Changing)
  615. {
  616. emit this->maximumValueChanged(newValue);
  617. }
  618. }
  619. // --------------------------------------------------------------------------
  620. void ctkRangeWidget::changeValues(double newMinValue, double newMaxValue)
  621. {
  622. Q_D(ctkRangeWidget);
  623. bool wasBlocking = d->BlockSliderUpdate;
  624. d->BlockSliderUpdate = true;
  625. d->MinimumSpinBox->setValue(newMinValue);
  626. d->MaximumSpinBox->setValue(newMaxValue);
  627. d->BlockSliderUpdate = wasBlocking;
  628. if (!d->Changing)
  629. {
  630. emit this->valuesChanged(newMinValue, newMaxValue);
  631. }
  632. }
  633. // --------------------------------------------------------------------------
  634. bool ctkRangeWidget::eventFilter(QObject *obj, QEvent *event)
  635. {
  636. if (event->type() == QEvent::MouseButtonPress)
  637. {
  638. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  639. if (mouseEvent->button() & Qt::LeftButton)
  640. {
  641. this->startChanging();
  642. }
  643. }
  644. else if (event->type() == QEvent::MouseButtonRelease)
  645. {
  646. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  647. if (mouseEvent->button() & Qt::LeftButton)
  648. {
  649. // here we might prevent ctkRangeWidget::stopChanging
  650. // from sending a valueChanged() event as the spinbox might
  651. // send a valueChanged() after eventFilter() is done.
  652. this->stopChanging();
  653. }
  654. }
  655. // standard event processing
  656. return this->Superclass::eventFilter(obj, event);
  657. }
  658. // --------------------------------------------------------------------------
  659. double ctkRangeWidget::singleStep()const
  660. {
  661. Q_D(const ctkRangeWidget);
  662. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()));
  663. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  664. return d->Slider->singleStep();
  665. }
  666. // --------------------------------------------------------------------------
  667. void ctkRangeWidget::setSingleStep(double step)
  668. {
  669. Q_D(ctkRangeWidget);
  670. if (!d->Slider->isValidStep(step))
  671. {
  672. qWarning() << "ctkRangeWidget::setSingleStep(" << step << ")"
  673. << "is outside valid bounds";
  674. return;
  675. }
  676. d->MinimumSpinBox->setSingleStep(step);
  677. d->MaximumSpinBox->setSingleStep(step);
  678. d->Slider->setSingleStep(d->MinimumSpinBox->singleStep());
  679. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()));
  680. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  681. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  682. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  683. }
  684. // --------------------------------------------------------------------------
  685. int ctkRangeWidget::decimals()const
  686. {
  687. Q_D(const ctkRangeWidget);
  688. Q_ASSERT(d->MinimumSpinBox->decimals() == d->MaximumSpinBox->decimals());
  689. return d->MinimumSpinBox->decimals();
  690. }
  691. // --------------------------------------------------------------------------
  692. void ctkRangeWidget::setDecimals(int newDecimals)
  693. {
  694. Q_D(ctkRangeWidget);
  695. d->MinimumSpinBox->setDecimals(newDecimals);
  696. d->MaximumSpinBox->setDecimals(newDecimals);
  697. // The number of decimals can change the range values
  698. // i.e. 50.55 with 2 decimals -> 51 with 0 decimals
  699. // As the SpinBox range change doesn't fire signals,
  700. // we have to do the synchronization manually here
  701. if (!d->useCustomSpinBoxesLimits())
  702. {
  703. d->Slider->setRange(d->MinimumSpinBox->minimum(), d->MaximumSpinBox->maximum());
  704. }
  705. }
  706. // --------------------------------------------------------------------------
  707. QString ctkRangeWidget::prefix()const
  708. {
  709. Q_D(const ctkRangeWidget);
  710. Q_ASSERT(d->MinimumSpinBox->prefix() == d->MaximumSpinBox->prefix());
  711. return d->MinimumSpinBox->prefix();
  712. }
  713. // --------------------------------------------------------------------------
  714. void ctkRangeWidget::setPrefix(const QString& newPrefix)
  715. {
  716. Q_D(ctkRangeWidget);
  717. d->MinimumSpinBox->setPrefix(newPrefix);
  718. d->MaximumSpinBox->setPrefix(newPrefix);
  719. }
  720. // --------------------------------------------------------------------------
  721. QString ctkRangeWidget::suffix()const
  722. {
  723. Q_D(const ctkRangeWidget);
  724. Q_ASSERT(d->MinimumSpinBox->suffix() == d->MaximumSpinBox->suffix());
  725. return d->MinimumSpinBox->suffix();
  726. }
  727. // --------------------------------------------------------------------------
  728. void ctkRangeWidget::setSuffix(const QString& newSuffix)
  729. {
  730. Q_D(ctkRangeWidget);
  731. d->MinimumSpinBox->setSuffix(newSuffix);
  732. d->MaximumSpinBox->setSuffix(newSuffix);
  733. }
  734. // --------------------------------------------------------------------------
  735. double ctkRangeWidget::tickInterval()const
  736. {
  737. Q_D(const ctkRangeWidget);
  738. return d->Slider->tickInterval();
  739. }
  740. // --------------------------------------------------------------------------
  741. void ctkRangeWidget::setTickInterval(double ti)
  742. {
  743. Q_D(ctkRangeWidget);
  744. d->Slider->setTickInterval(ti);
  745. }
  746. // -------------------------------------------------------------------------
  747. void ctkRangeWidget::reset()
  748. {
  749. this->setValues(this->minimum(), this->maximum());
  750. }
  751. // -------------------------------------------------------------------------
  752. void ctkRangeWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  753. {
  754. Q_D(ctkRangeWidget);
  755. if (d->SpinBoxAlignment == alignment)
  756. {
  757. return;
  758. }
  759. d->SpinBoxAlignment = alignment;
  760. d->relayout();
  761. }
  762. // -------------------------------------------------------------------------
  763. Qt::Alignment ctkRangeWidget::spinBoxAlignment()const
  764. {
  765. Q_D(const ctkRangeWidget);
  766. return d->SpinBoxAlignment;
  767. }
  768. // -------------------------------------------------------------------------
  769. void ctkRangeWidget::setSpinBoxTextAlignment(Qt::Alignment alignment)
  770. {
  771. Q_D(ctkRangeWidget);
  772. d->MinimumSpinBox->setAlignment(alignment);
  773. d->MaximumSpinBox->setAlignment(alignment);
  774. }
  775. // -------------------------------------------------------------------------
  776. Qt::Alignment ctkRangeWidget::spinBoxTextAlignment()const
  777. {
  778. Q_D(const ctkRangeWidget);
  779. Q_ASSERT(d->MinimumSpinBox->alignment() == d->MaximumSpinBox->alignment());
  780. return d->MinimumSpinBox->alignment();
  781. }
  782. // -------------------------------------------------------------------------
  783. void ctkRangeWidget::setTracking(bool enable)
  784. {
  785. Q_D(ctkRangeWidget);
  786. d->Tracking = enable;
  787. }
  788. // -------------------------------------------------------------------------
  789. bool ctkRangeWidget::hasTracking()const
  790. {
  791. Q_D(const ctkRangeWidget);
  792. return d->Tracking;
  793. }
  794. // -------------------------------------------------------------------------
  795. bool ctkRangeWidget::isAutoSpinBoxWidth()const
  796. {
  797. Q_D(const ctkRangeWidget);
  798. return d->AutoSpinBoxWidth;
  799. }
  800. // -------------------------------------------------------------------------
  801. void ctkRangeWidget::setAutoSpinBoxWidth(bool autoWidth)
  802. {
  803. Q_D(ctkRangeWidget);
  804. d->AutoSpinBoxWidth = autoWidth;
  805. d->updateSpinBoxWidth();
  806. }
  807. // --------------------------------------------------------------------------
  808. bool ctkRangeWidget::symmetricMoves()const
  809. {
  810. Q_D(const ctkRangeWidget);
  811. return d->Slider->symmetricMoves();
  812. }
  813. // --------------------------------------------------------------------------
  814. void ctkRangeWidget::setSymmetricMoves(bool symmetry)
  815. {
  816. Q_D(ctkRangeWidget);
  817. d->Slider->setSymmetricMoves(symmetry);
  818. }
  819. // --------------------------------------------------------------------------
  820. void ctkRangeWidget::setCustomSpinBoxesLimits(double min, double max)
  821. {
  822. Q_D(ctkRangeWidget);
  823. if (max < min)
  824. {
  825. return;
  826. }
  827. d->CustomSpinBoxesLimitsMin = min;
  828. d->CustomSpinBoxesLimitsMax = max;
  829. this->setRange(d->Slider->minimum(), d->Slider->maximum());
  830. }
  831. // --------------------------------------------------------------------------
  832. double ctkRangeWidget::customSpinBoxesLimitsMin()const
  833. {
  834. Q_D(const ctkRangeWidget);
  835. return d->CustomSpinBoxesLimitsMin;
  836. }
  837. // --------------------------------------------------------------------------
  838. double ctkRangeWidget::customSpinBoxesLimitsMax()const
  839. {
  840. Q_D(const ctkRangeWidget);
  841. return d->CustomSpinBoxesLimitsMax;
  842. }
  843. // -------------------------------------------------------------------------
  844. ctkDoubleRangeSlider* ctkRangeWidget::slider()const
  845. {
  846. Q_D(const ctkRangeWidget);
  847. return d->Slider;
  848. }
  849. // -------------------------------------------------------------------------
  850. void ctkRangeWidget::setSlider(ctkDoubleRangeSlider* slider)
  851. {
  852. Q_D(ctkRangeWidget);
  853. slider->setOrientation(d->Slider->orientation());
  854. slider->setRange(d->Slider->minimum(), d->Slider->maximum());
  855. slider->setValues(d->Slider->minimumValue(), d->Slider->maximumValue());
  856. slider->setSingleStep(d->Slider->singleStep());
  857. slider->setTracking(d->Slider->hasTracking());
  858. slider->setTickInterval(d->Slider->tickInterval());
  859. delete d->Slider;
  860. d->Slider = slider;
  861. d->connectSlider();
  862. d->relayout();
  863. }
  864. // -------------------------------------------------------------------------
  865. ctkDoubleSpinBox* ctkRangeWidget::minimumSpinBox()const
  866. {
  867. Q_D(const ctkRangeWidget);
  868. return d->MinimumSpinBox;
  869. }
  870. // -------------------------------------------------------------------------
  871. ctkDoubleSpinBox* ctkRangeWidget::maximumSpinBox()const
  872. {
  873. Q_D(const ctkRangeWidget);
  874. return d->MaximumSpinBox;
  875. }
  876. //----------------------------------------------------------------------------
  877. void ctkRangeWidget::setValueProxy(ctkValueProxy* proxy)
  878. {
  879. Q_D(ctkRangeWidget);
  880. if (proxy == d->Proxy.data())
  881. {
  882. return;
  883. }
  884. this->onValueProxyAboutToBeModified();
  885. if (d->Proxy)
  886. {
  887. disconnect(d->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  888. this, SLOT(onValueProxyAboutToBeModified()));
  889. disconnect(d->Proxy.data(), SIGNAL(proxyModified()),
  890. this, SLOT(onValueProxyModified()));
  891. }
  892. d->Proxy = proxy;
  893. if (d->Proxy)
  894. {
  895. connect(d->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  896. this, SLOT(onValueProxyAboutToBeModified()));
  897. }
  898. this->slider()->setValueProxy(proxy);
  899. this->minimumSpinBox()->setValueProxy(proxy);
  900. this->maximumSpinBox()->setValueProxy(proxy);
  901. if (d->Proxy)
  902. {
  903. connect(d->Proxy.data(), SIGNAL(proxyModified()),
  904. this, SLOT(onValueProxyModified()));
  905. }
  906. this->onValueProxyModified();
  907. }
  908. //----------------------------------------------------------------------------
  909. ctkValueProxy* ctkRangeWidget::valueProxy() const
  910. {
  911. Q_D(const ctkRangeWidget);
  912. return d->Proxy.data();
  913. }
  914. //-----------------------------------------------------------------------------
  915. void ctkRangeWidget::onValueProxyAboutToBeModified()
  916. {
  917. }
  918. //-----------------------------------------------------------------------------
  919. void ctkRangeWidget::onValueProxyModified()
  920. {
  921. }