ctkRangeWidget.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. Q_D(const ctkRangeWidget);
  214. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  215. return d->Slider->minimum();
  216. }
  217. // --------------------------------------------------------------------------
  218. double ctkRangeWidget::maximum()const
  219. {
  220. Q_D(const ctkRangeWidget);
  221. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  222. return d->Slider->maximum();
  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. }
  233. // --------------------------------------------------------------------------
  234. void ctkRangeWidget::setMinimum(double min)
  235. {
  236. Q_D(ctkRangeWidget);
  237. bool blocked = d->MinimumSpinBox->blockSignals(true);
  238. blocked = d->MaximumSpinBox->blockSignals(true);
  239. d->MinimumSpinBox->setMinimum(min);
  240. d->MaximumSpinBox->setMinimum(min);
  241. d->MinimumSpinBox->blockSignals(blocked);
  242. d->MaximumSpinBox->blockSignals(blocked);
  243. // SpinBox can truncate min (depending on decimals).
  244. // use Spinbox's min to set Slider's min
  245. d->SettingSliderRange = true;
  246. d->Slider->setMinimum(d->MinimumSpinBox->minimum());
  247. d->SettingSliderRange = false;
  248. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  249. d->updateSpinBoxWidth();
  250. }
  251. // --------------------------------------------------------------------------
  252. void ctkRangeWidget::setMaximum(double max)
  253. {
  254. Q_D(ctkRangeWidget);
  255. bool blocked = d->MinimumSpinBox->blockSignals(true);
  256. blocked = d->MaximumSpinBox->blockSignals(true);
  257. d->MinimumSpinBox->setMaximum(max);
  258. d->MaximumSpinBox->setMaximum(max);
  259. d->MinimumSpinBox->blockSignals(blocked);
  260. d->MaximumSpinBox->blockSignals(blocked);
  261. // SpinBox can truncate max (depending on decimals).
  262. // use Spinbox's max to set Slider's max
  263. d->SettingSliderRange = true;
  264. d->Slider->setMaximum(d->MaximumSpinBox->maximum());
  265. d->SettingSliderRange = false;
  266. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  267. d->updateSpinBoxWidth();
  268. }
  269. // --------------------------------------------------------------------------
  270. void ctkRangeWidget::setRange(double min, double max)
  271. {
  272. Q_D(ctkRangeWidget);
  273. double oldMin = d->MinimumSpinBox->minimum();
  274. double oldMax = d->MaximumSpinBox->maximum();
  275. bool blocked = d->MinimumSpinBox->blockSignals(true);
  276. d->MinimumSpinBox->setRange(qMin(min,max), qMax(min,max));
  277. d->MinimumSpinBox->blockSignals(blocked);
  278. blocked = d->MaximumSpinBox->blockSignals(true);
  279. d->MaximumSpinBox->setRange(qMin(min,max), qMax(min,max));
  280. d->MaximumSpinBox->blockSignals(blocked);
  281. // SpinBox can truncate the range (depending on decimals).
  282. // use Spinbox's range to set Slider's range
  283. d->SettingSliderRange = true;
  284. d->Slider->setRange(d->MinimumSpinBox->minimum(), d->MaximumSpinBox->maximum());
  285. d->SettingSliderRange = false;
  286. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(), d->Slider->minimum()));
  287. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  288. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  289. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  290. d->updateSpinBoxWidth();
  291. if (oldMin != d->MinimumSpinBox->minimum() ||
  292. oldMax != d->MaximumSpinBox->maximum())
  293. {
  294. emit rangeChanged(d->MinimumSpinBox->minimum(), d->MaximumSpinBox->maximum());
  295. }
  296. }
  297. // --------------------------------------------------------------------------
  298. void ctkRangeWidget::onSliderRangeChanged(double min, double max)
  299. {
  300. Q_D(ctkRangeWidget);
  301. if (!d->SettingSliderRange)
  302. {
  303. this->setRange(min, max);
  304. }
  305. }
  306. /*
  307. // --------------------------------------------------------------------------
  308. double ctkRangeWidget::sliderPosition()const
  309. {
  310. return d->Slider->sliderPosition();
  311. }
  312. // --------------------------------------------------------------------------
  313. void ctkRangeWidget::setSliderPosition(double position)
  314. {
  315. d->Slider->setSliderPosition(position);
  316. }
  317. */
  318. /*
  319. // --------------------------------------------------------------------------
  320. double ctkRangeWidget::previousSliderPosition()
  321. {
  322. return d->Slider->previousSliderPosition();
  323. }
  324. */
  325. // --------------------------------------------------------------------------
  326. void ctkRangeWidget::values(double &minValue, double &maxValue)const
  327. {
  328. Q_D(const ctkRangeWidget);
  329. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  330. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  331. minValue = d->Changing ? d->MinimumValueBeforeChange : d->Slider->minimumValue();
  332. maxValue = d->Changing ? d->MaximumValueBeforeChange : d->Slider->maximumValue();
  333. if (d->Proxy)
  334. {
  335. minValue = d->Proxy.data()->valueFromProxyValue(minValue);
  336. maxValue = d->Proxy.data()->valueFromProxyValue(maxValue);
  337. }
  338. }
  339. // --------------------------------------------------------------------------
  340. double ctkRangeWidget::minimumValue()const
  341. {
  342. Q_D(const ctkRangeWidget);
  343. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  344. double val =
  345. d->Changing ? d->MinimumValueBeforeChange : d->Slider->minimumValue();
  346. if (d->Proxy)
  347. {
  348. val = d->Proxy.data()->valueFromProxyValue(val);
  349. }
  350. return val;
  351. }
  352. // --------------------------------------------------------------------------
  353. double ctkRangeWidget::maximumValue()const
  354. {
  355. Q_D(const ctkRangeWidget);
  356. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  357. double val =
  358. d->Changing ? d->MaximumValueBeforeChange : d->Slider->maximumValue();
  359. if (d->Proxy)
  360. {
  361. val = d->Proxy.data()->valueFromProxyValue(val);
  362. }
  363. return val;
  364. }
  365. // --------------------------------------------------------------------------
  366. void ctkRangeWidget::setMinimumValue(double _value)
  367. {
  368. Q_D(ctkRangeWidget);
  369. if (d->Proxy)
  370. {
  371. _value = d->Proxy.data()->proxyValueFromValue(_value);
  372. }
  373. // disable the tracking temporally to emit the
  374. // signal valueChanged if changeValue() is called
  375. bool isChanging = d->Changing;
  376. d->Changing = false;
  377. d->MinimumSpinBox->setValue(_value);
  378. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  379. // restore the prop
  380. d->Changing = isChanging;
  381. }
  382. // --------------------------------------------------------------------------
  383. void ctkRangeWidget::setMaximumValue(double _value)
  384. {
  385. Q_D(ctkRangeWidget);
  386. if (d->Proxy)
  387. {
  388. _value = d->Proxy.data()->proxyValueFromValue(_value);
  389. }
  390. // disable the tracking temporally to emit the
  391. // signal valueChanged if changeValue() is called
  392. bool isChanging = d->Changing;
  393. d->Changing = false;
  394. d->MaximumSpinBox->setValue(_value);
  395. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  396. // restore the prop
  397. d->Changing = isChanging;
  398. }
  399. // --------------------------------------------------------------------------
  400. void ctkRangeWidget::setValues(double newMinimumValue, double newMaximumValue)
  401. {
  402. Q_D(ctkRangeWidget);
  403. if (d->Proxy)
  404. {
  405. newMinimumValue = d->Proxy.data()->proxyValueFromValue(newMinimumValue);
  406. newMaximumValue = d->Proxy.data()->proxyValueFromValue(newMaximumValue);
  407. }
  408. if (newMinimumValue > newMaximumValue)
  409. {
  410. qSwap(newMinimumValue, newMaximumValue);
  411. }
  412. const bool minimumFirst = (newMinimumValue <= this->maximumValue());
  413. // disable the tracking temporally to emit the
  414. // signal valueChanged if changeValue() is called
  415. bool isChanging = d->Changing;
  416. d->Changing = false;
  417. // \todo: setting the spinbox separately is currently firing 2 signals and
  418. // between the signals, the state of the widget is inconsistent.
  419. if (minimumFirst)
  420. {
  421. d->MinimumSpinBox->setValue(newMinimumValue);
  422. d->MaximumSpinBox->setValue(newMaximumValue);
  423. }
  424. else
  425. {
  426. d->MaximumSpinBox->setValue(newMaximumValue);
  427. d->MinimumSpinBox->setValue(newMinimumValue);
  428. }
  429. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  430. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  431. // restore the prop
  432. d->Changing = isChanging;
  433. }
  434. // --------------------------------------------------------------------------
  435. void ctkRangeWidget::setMinimumToMaximumSpinBox(double minimum)
  436. {
  437. Q_D(ctkRangeWidget);
  438. if (minimum != minimum) // NaN check
  439. {
  440. return;
  441. }
  442. d->MaximumSpinBox->setMinimum(minimum);
  443. }
  444. // --------------------------------------------------------------------------
  445. void ctkRangeWidget::setMaximumToMinimumSpinBox(double maximum)
  446. {
  447. Q_D(ctkRangeWidget);
  448. if (maximum != maximum) // NaN check
  449. {
  450. return;
  451. }
  452. d->MinimumSpinBox->setMaximum(maximum);
  453. }
  454. // --------------------------------------------------------------------------
  455. void ctkRangeWidget::startChanging()
  456. {
  457. Q_D(ctkRangeWidget);
  458. if (d->Tracking)
  459. {
  460. return;
  461. }
  462. d->Changing = true;
  463. d->MinimumValueBeforeChange = this->minimumValue();
  464. d->MaximumValueBeforeChange = this->maximumValue();
  465. }
  466. // --------------------------------------------------------------------------
  467. void ctkRangeWidget::stopChanging()
  468. {
  469. Q_D(ctkRangeWidget);
  470. if (d->Tracking)
  471. {
  472. return;
  473. }
  474. d->Changing = false;
  475. bool emitMinValChanged = qAbs(this->minimumValue() - d->MinimumValueBeforeChange) > (this->singleStep() * 0.000000001);
  476. bool emitMaxValChanged = qAbs(this->maximumValue() - d->MaximumValueBeforeChange) > (this->singleStep() * 0.000000001);
  477. if (emitMinValChanged || emitMaxValChanged)
  478. {
  479. // emit the valuesChanged signal first
  480. emit this->valuesChanged(this->minimumValue(), this->maximumValue());
  481. }
  482. if (emitMinValChanged)
  483. {
  484. emit this->minimumValueChanged(this->minimumValue());
  485. }
  486. if (emitMaxValChanged)
  487. {
  488. emit this->maximumValueChanged(this->maximumValue());
  489. }
  490. }
  491. // --------------------------------------------------------------------------
  492. void ctkRangeWidget::changeMinimumValue(double newValue)
  493. {
  494. Q_D(ctkRangeWidget);
  495. //if (d->Tracking)
  496. {
  497. emit this->minimumValueIsChanging(newValue);
  498. }
  499. if (!d->Changing)
  500. {
  501. emit this->minimumValueChanged(newValue);
  502. }
  503. }
  504. // --------------------------------------------------------------------------
  505. void ctkRangeWidget::changeMaximumValue(double newValue)
  506. {
  507. Q_D(ctkRangeWidget);
  508. //if (d->Tracking)
  509. {
  510. emit this->maximumValueIsChanging(newValue);
  511. }
  512. if (!d->Changing)
  513. {
  514. emit this->maximumValueChanged(newValue);
  515. }
  516. }
  517. // --------------------------------------------------------------------------
  518. void ctkRangeWidget::changeValues(double newMinValue, double newMaxValue)
  519. {
  520. Q_D(ctkRangeWidget);
  521. d->MinimumSpinBox->setValue(newMinValue);
  522. d->MaximumSpinBox->setValue(newMaxValue);
  523. if (!d->Changing)
  524. {
  525. emit this->valuesChanged(newMinValue, newMaxValue);
  526. }
  527. }
  528. // --------------------------------------------------------------------------
  529. bool ctkRangeWidget::eventFilter(QObject *obj, QEvent *event)
  530. {
  531. if (event->type() == QEvent::MouseButtonPress)
  532. {
  533. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  534. if (mouseEvent->button() & Qt::LeftButton)
  535. {
  536. this->startChanging();
  537. }
  538. }
  539. else if (event->type() == QEvent::MouseButtonRelease)
  540. {
  541. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  542. if (mouseEvent->button() & Qt::LeftButton)
  543. {
  544. // here we might prevent ctkRangeWidget::stopChanging
  545. // from sending a valueChanged() event as the spinbox might
  546. // send a valueChanged() after eventFilter() is done.
  547. this->stopChanging();
  548. }
  549. }
  550. // standard event processing
  551. return this->Superclass::eventFilter(obj, event);
  552. }
  553. // --------------------------------------------------------------------------
  554. double ctkRangeWidget::singleStep()const
  555. {
  556. Q_D(const ctkRangeWidget);
  557. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()));
  558. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  559. return d->Slider->singleStep();
  560. }
  561. // --------------------------------------------------------------------------
  562. void ctkRangeWidget::setSingleStep(double step)
  563. {
  564. Q_D(ctkRangeWidget);
  565. if (!d->Slider->isValidStep(step))
  566. {
  567. qWarning() << "Single step" << step << "is out of bounds.";
  568. return;
  569. }
  570. d->MinimumSpinBox->setSingleStep(step);
  571. d->MaximumSpinBox->setSingleStep(step);
  572. d->Slider->setSingleStep(d->MinimumSpinBox->singleStep());
  573. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()));
  574. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  575. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  576. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  577. }
  578. // --------------------------------------------------------------------------
  579. int ctkRangeWidget::decimals()const
  580. {
  581. Q_D(const ctkRangeWidget);
  582. Q_ASSERT(d->MinimumSpinBox->decimals() == d->MaximumSpinBox->decimals());
  583. return d->MinimumSpinBox->decimals();
  584. }
  585. // --------------------------------------------------------------------------
  586. void ctkRangeWidget::setDecimals(int newDecimals)
  587. {
  588. Q_D(ctkRangeWidget);
  589. d->MinimumSpinBox->setDecimals(newDecimals);
  590. d->MaximumSpinBox->setDecimals(newDecimals);
  591. // The number of decimals can change the range values
  592. // i.e. 50.55 with 2 decimals -> 51 with 0 decimals
  593. // As the SpinBox range change doesn't fire signals,
  594. // we have to do the synchronization manually here
  595. d->Slider->setMinimum(d->MinimumSpinBox->minimum());
  596. d->Slider->setMaximum(d->MaximumSpinBox->maximum());
  597. }
  598. // --------------------------------------------------------------------------
  599. QString ctkRangeWidget::prefix()const
  600. {
  601. Q_D(const ctkRangeWidget);
  602. Q_ASSERT(d->MinimumSpinBox->prefix() == d->MaximumSpinBox->prefix());
  603. return d->MinimumSpinBox->prefix();
  604. }
  605. // --------------------------------------------------------------------------
  606. void ctkRangeWidget::setPrefix(const QString& newPrefix)
  607. {
  608. Q_D(ctkRangeWidget);
  609. d->MinimumSpinBox->setPrefix(newPrefix);
  610. d->MaximumSpinBox->setPrefix(newPrefix);
  611. }
  612. // --------------------------------------------------------------------------
  613. QString ctkRangeWidget::suffix()const
  614. {
  615. Q_D(const ctkRangeWidget);
  616. Q_ASSERT(d->MinimumSpinBox->suffix() == d->MaximumSpinBox->suffix());
  617. return d->MinimumSpinBox->suffix();
  618. }
  619. // --------------------------------------------------------------------------
  620. void ctkRangeWidget::setSuffix(const QString& newSuffix)
  621. {
  622. Q_D(ctkRangeWidget);
  623. d->MinimumSpinBox->setSuffix(newSuffix);
  624. d->MaximumSpinBox->setSuffix(newSuffix);
  625. }
  626. // --------------------------------------------------------------------------
  627. double ctkRangeWidget::tickInterval()const
  628. {
  629. Q_D(const ctkRangeWidget);
  630. return d->Slider->tickInterval();
  631. }
  632. // --------------------------------------------------------------------------
  633. void ctkRangeWidget::setTickInterval(double ti)
  634. {
  635. Q_D(ctkRangeWidget);
  636. d->Slider->setTickInterval(ti);
  637. }
  638. // -------------------------------------------------------------------------
  639. void ctkRangeWidget::reset()
  640. {
  641. this->setMinimumValue(this->minimum());
  642. this->setMaximumValue(this->maximum());
  643. }
  644. // -------------------------------------------------------------------------
  645. void ctkRangeWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  646. {
  647. Q_D(ctkRangeWidget);
  648. if (d->SpinBoxAlignment == alignment)
  649. {
  650. return;
  651. }
  652. d->SpinBoxAlignment = alignment;
  653. d->relayout();
  654. }
  655. // -------------------------------------------------------------------------
  656. Qt::Alignment ctkRangeWidget::spinBoxAlignment()const
  657. {
  658. Q_D(const ctkRangeWidget);
  659. return d->SpinBoxAlignment;
  660. }
  661. // -------------------------------------------------------------------------
  662. void ctkRangeWidget::setSpinBoxTextAlignment(Qt::Alignment alignment)
  663. {
  664. Q_D(ctkRangeWidget);
  665. d->MinimumSpinBox->setAlignment(alignment);
  666. d->MaximumSpinBox->setAlignment(alignment);
  667. }
  668. // -------------------------------------------------------------------------
  669. Qt::Alignment ctkRangeWidget::spinBoxTextAlignment()const
  670. {
  671. Q_D(const ctkRangeWidget);
  672. Q_ASSERT(d->MinimumSpinBox->alignment() == d->MaximumSpinBox->alignment());
  673. return d->MinimumSpinBox->alignment();
  674. }
  675. // -------------------------------------------------------------------------
  676. void ctkRangeWidget::setTracking(bool enable)
  677. {
  678. Q_D(ctkRangeWidget);
  679. d->Tracking = enable;
  680. }
  681. // -------------------------------------------------------------------------
  682. bool ctkRangeWidget::hasTracking()const
  683. {
  684. Q_D(const ctkRangeWidget);
  685. return d->Tracking;
  686. }
  687. // -------------------------------------------------------------------------
  688. bool ctkRangeWidget::isAutoSpinBoxWidth()const
  689. {
  690. Q_D(const ctkRangeWidget);
  691. return d->AutoSpinBoxWidth;
  692. }
  693. // -------------------------------------------------------------------------
  694. void ctkRangeWidget::setAutoSpinBoxWidth(bool autoWidth)
  695. {
  696. Q_D(ctkRangeWidget);
  697. d->AutoSpinBoxWidth = autoWidth;
  698. d->updateSpinBoxWidth();
  699. }
  700. // --------------------------------------------------------------------------
  701. bool ctkRangeWidget::symmetricMoves()const
  702. {
  703. Q_D(const ctkRangeWidget);
  704. return d->Slider->symmetricMoves();
  705. }
  706. // --------------------------------------------------------------------------
  707. void ctkRangeWidget::setSymmetricMoves(bool symmetry)
  708. {
  709. Q_D(ctkRangeWidget);
  710. d->Slider->setSymmetricMoves(symmetry);
  711. }
  712. // -------------------------------------------------------------------------
  713. ctkDoubleRangeSlider* ctkRangeWidget::slider()const
  714. {
  715. Q_D(const ctkRangeWidget);
  716. return d->Slider;
  717. }
  718. // -------------------------------------------------------------------------
  719. void ctkRangeWidget::setSlider(ctkDoubleRangeSlider* slider)
  720. {
  721. Q_D(ctkRangeWidget);
  722. slider->setOrientation(d->Slider->orientation());
  723. slider->setMinimum(d->Slider->minimum());
  724. slider->setMaximum(d->Slider->maximum());
  725. slider->setValues(d->Slider->minimumValue(), d->Slider->maximumValue());
  726. slider->setSingleStep(d->Slider->singleStep());
  727. slider->setTracking(d->Slider->hasTracking());
  728. slider->setTickInterval(d->Slider->tickInterval());
  729. delete d->Slider;
  730. d->Slider = slider;
  731. d->connectSlider();
  732. d->relayout();
  733. }
  734. // -------------------------------------------------------------------------
  735. ctkDoubleSpinBox* ctkRangeWidget::minimumSpinBox()const
  736. {
  737. Q_D(const ctkRangeWidget);
  738. return d->MinimumSpinBox;
  739. }
  740. // -------------------------------------------------------------------------
  741. ctkDoubleSpinBox* ctkRangeWidget::maximumSpinBox()const
  742. {
  743. Q_D(const ctkRangeWidget);
  744. return d->MaximumSpinBox;
  745. }
  746. //----------------------------------------------------------------------------
  747. void ctkRangeWidget::setValueProxy(ctkValueProxy* proxy)
  748. {
  749. Q_D(ctkRangeWidget);
  750. if (d->Proxy.data() == proxy)
  751. {
  752. return;
  753. }
  754. d->Proxy = proxy;
  755. }
  756. //----------------------------------------------------------------------------
  757. ctkValueProxy* ctkRangeWidget::valueProxy() const
  758. {
  759. Q_D(const ctkRangeWidget);
  760. return d->Proxy.data();
  761. }