ctkRangeWidget.cpp 29 KB

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