ctkRangeWidget.cpp 31 KB

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