ctkRangeWidget.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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.commontk.org/LICENSE
  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. // CTK includes
  18. #include "ctkRangeWidget.h"
  19. #include "ui_ctkRangeWidget.h"
  20. // STD includes
  21. #include <cmath>
  22. //-----------------------------------------------------------------------------
  23. class ctkRangeWidgetPrivate: public Ui_ctkRangeWidget
  24. {
  25. Q_DECLARE_PUBLIC(ctkRangeWidget);
  26. protected:
  27. ctkRangeWidget* const q_ptr;
  28. public:
  29. ctkRangeWidgetPrivate(ctkRangeWidget& object);
  30. void connectSlider();
  31. void updateSpinBoxWidth();
  32. int synchronizedSpinBoxWidth()const;
  33. void synchronizeSiblingSpinBox(int newWidth);
  34. bool equal(double v1, double v2)const;
  35. void relayout();
  36. bool Tracking;
  37. bool Changing;
  38. bool SettingSliderRange;
  39. double MinimumValueBeforeChange;
  40. double MaximumValueBeforeChange;
  41. bool AutoSpinBoxWidth;
  42. Qt::Alignment SpinBoxAlignment;
  43. };
  44. // --------------------------------------------------------------------------
  45. bool ctkRangeWidgetPrivate::equal(double v1, double v2)const
  46. {
  47. return qAbs(v1 - v2) < pow(10., -this->MinimumSpinBox->decimals());
  48. }
  49. // --------------------------------------------------------------------------
  50. ctkRangeWidgetPrivate::ctkRangeWidgetPrivate(ctkRangeWidget& object)
  51. :q_ptr(&object)
  52. {
  53. this->Tracking = true;
  54. this->Changing = false;
  55. this->SettingSliderRange = false;
  56. this->MinimumValueBeforeChange = 0.;
  57. this->MaximumValueBeforeChange = 0.;
  58. this->AutoSpinBoxWidth = true;
  59. this->SpinBoxAlignment = Qt::AlignVCenter;
  60. }
  61. // --------------------------------------------------------------------------
  62. void ctkRangeWidgetPrivate::connectSlider()
  63. {
  64. Q_Q(ctkRangeWidget);
  65. QObject::connect(this->Slider, SIGNAL(valuesChanged(double, double)),
  66. q, SLOT(changeValues(double,double)));
  67. QObject::connect(this->Slider, SIGNAL(minimumValueChanged(double)),
  68. q, SLOT(changeMinimumValue(double)));
  69. QObject::connect(this->Slider, SIGNAL(maximumValueChanged(double)),
  70. q, SLOT(changeMaximumValue(double)));
  71. QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
  72. this->Slider, SLOT(setMinimumValue(double)));
  73. QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
  74. this->Slider, SLOT(setMaximumValue(double)));
  75. QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
  76. q, SLOT(setMinimumToMaximumSpinBox(double)));
  77. QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
  78. q, SLOT(setMaximumToMinimumSpinBox(double)));
  79. QObject::connect(this->Slider, SIGNAL(sliderPressed()),
  80. q, SLOT(startChanging()));
  81. QObject::connect(this->Slider, SIGNAL(sliderReleased()),
  82. q, SLOT(stopChanging()));
  83. QObject::connect(this->Slider, SIGNAL(rangeChanged(double, double)),
  84. q, SLOT(onSliderRangeChanged(double, double)));
  85. }
  86. // --------------------------------------------------------------------------
  87. void ctkRangeWidgetPrivate::updateSpinBoxWidth()
  88. {
  89. int spinBoxWidth = this->synchronizedSpinBoxWidth();
  90. if (this->AutoSpinBoxWidth)
  91. {
  92. this->MinimumSpinBox->setMinimumWidth(spinBoxWidth);
  93. this->MaximumSpinBox->setMinimumWidth(spinBoxWidth);
  94. }
  95. else
  96. {
  97. this->MinimumSpinBox->setMinimumWidth(0);
  98. this->MaximumSpinBox->setMinimumWidth(0);
  99. }
  100. this->synchronizeSiblingSpinBox(spinBoxWidth);
  101. }
  102. // --------------------------------------------------------------------------
  103. int ctkRangeWidgetPrivate::synchronizedSpinBoxWidth()const
  104. {
  105. Q_Q(const ctkRangeWidget);
  106. //Q_ASSERT(this->MinimumSpinBox->sizeHint() == this->MaximumSpinBox->sizeHint());
  107. int maxWidth = qMax(this->MinimumSpinBox->sizeHint().width(),
  108. this->MaximumSpinBox->sizeHint().width());
  109. if (!q->parent())
  110. {
  111. return maxWidth;
  112. }
  113. QList<ctkRangeWidget*> siblings =
  114. q->parent()->findChildren<ctkRangeWidget*>();
  115. foreach(ctkRangeWidget* sibling, siblings)
  116. {
  117. maxWidth = qMax(maxWidth, qMax(sibling->d_func()->MaximumSpinBox->sizeHint().width(),
  118. sibling->d_func()->MaximumSpinBox->sizeHint().width()));
  119. }
  120. return maxWidth;
  121. }
  122. // --------------------------------------------------------------------------
  123. void ctkRangeWidgetPrivate::synchronizeSiblingSpinBox(int width)
  124. {
  125. Q_Q(const ctkRangeWidget);
  126. QList<ctkRangeWidget*> siblings =
  127. q->parent()->findChildren<ctkRangeWidget*>();
  128. foreach(ctkRangeWidget* sibling, siblings)
  129. {
  130. if (sibling != q && sibling->isAutoSpinBoxWidth())
  131. {
  132. sibling->d_func()->MinimumSpinBox->setMinimumWidth(width);
  133. sibling->d_func()->MaximumSpinBox->setMinimumWidth(width);
  134. }
  135. }
  136. }
  137. // --------------------------------------------------------------------------
  138. void ctkRangeWidgetPrivate::relayout()
  139. {
  140. this->GridLayout->removeWidget(this->MinimumSpinBox);
  141. this->GridLayout->removeWidget(this->MaximumSpinBox);
  142. this->GridLayout->removeWidget(this->Slider);
  143. if (this->SpinBoxAlignment & Qt::AlignTop)
  144. {
  145. this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
  146. this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
  147. this->GridLayout->addWidget(this->Slider,1,0,1,3);
  148. }
  149. else if (this->SpinBoxAlignment & Qt::AlignBottom)
  150. {
  151. this->GridLayout->addWidget(this->MinimumSpinBox,1,0);
  152. this->GridLayout->addWidget(this->MaximumSpinBox,1,2);
  153. this->GridLayout->addWidget(this->Slider,0, 0, 1, 3);
  154. }
  155. else if (this->SpinBoxAlignment & Qt::AlignRight)
  156. {
  157. this->GridLayout->addWidget(this->Slider, 0, 0);
  158. this->GridLayout->addWidget(this->MinimumSpinBox,0,1);
  159. this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
  160. }
  161. else if (this->SpinBoxAlignment & Qt::AlignLeft)
  162. {
  163. this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
  164. this->GridLayout->addWidget(this->MaximumSpinBox,0,1);
  165. this->GridLayout->addWidget(this->Slider, 0, 2);
  166. }
  167. else // Qt::AlignVCenter (or any other bad alignment)
  168. {
  169. this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
  170. this->GridLayout->addWidget(this->Slider,0,1);
  171. this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
  172. }
  173. }
  174. // --------------------------------------------------------------------------
  175. ctkRangeWidget::ctkRangeWidget(QWidget* _parent) : Superclass(_parent)
  176. , d_ptr(new ctkRangeWidgetPrivate(*this))
  177. {
  178. Q_D(ctkRangeWidget);
  179. d->setupUi(this);
  180. d->MinimumSpinBox->setMinimum(d->Slider->minimum());
  181. d->MinimumSpinBox->setMaximum(d->Slider->maximum());
  182. d->MaximumSpinBox->setMinimum(d->Slider->minimum());
  183. d->MaximumSpinBox->setMaximum(d->Slider->maximum());
  184. d->MinimumSpinBox->setValue(d->Slider->minimumValue());
  185. d->MaximumSpinBox->setValue(d->Slider->maximumValue());
  186. d->connectSlider();
  187. d->MinimumSpinBox->installEventFilter(this);
  188. d->MaximumSpinBox->installEventFilter(this);
  189. }
  190. // --------------------------------------------------------------------------
  191. ctkRangeWidget::~ctkRangeWidget()
  192. {
  193. }
  194. // --------------------------------------------------------------------------
  195. double ctkRangeWidget::minimum()const
  196. {
  197. Q_D(const ctkRangeWidget);
  198. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  199. return d->Slider->minimum();
  200. }
  201. // --------------------------------------------------------------------------
  202. double ctkRangeWidget::maximum()const
  203. {
  204. Q_D(const ctkRangeWidget);
  205. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  206. return d->Slider->maximum();
  207. }
  208. // --------------------------------------------------------------------------
  209. void ctkRangeWidget::setMinimum(double min)
  210. {
  211. Q_D(ctkRangeWidget);
  212. bool blocked = d->MinimumSpinBox->blockSignals(true);
  213. blocked = d->MaximumSpinBox->blockSignals(true);
  214. d->MinimumSpinBox->setMinimum(min);
  215. d->MaximumSpinBox->setMinimum(min);
  216. d->MinimumSpinBox->blockSignals(blocked);
  217. d->MaximumSpinBox->blockSignals(blocked);
  218. // SpinBox can truncate min (depending on decimals).
  219. // use Spinbox's min to set Slider's min
  220. d->SettingSliderRange = true;
  221. d->Slider->setMinimum(d->MinimumSpinBox->minimum());
  222. d->SettingSliderRange = false;
  223. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  224. d->updateSpinBoxWidth();
  225. }
  226. // --------------------------------------------------------------------------
  227. void ctkRangeWidget::setMaximum(double max)
  228. {
  229. Q_D(ctkRangeWidget);
  230. bool blocked = d->MinimumSpinBox->blockSignals(true);
  231. blocked = d->MaximumSpinBox->blockSignals(true);
  232. d->MinimumSpinBox->setMaximum(max);
  233. d->MaximumSpinBox->setMaximum(max);
  234. d->MinimumSpinBox->blockSignals(blocked);
  235. d->MaximumSpinBox->blockSignals(blocked);
  236. // SpinBox can truncate max (depending on decimals).
  237. // use Spinbox's max to set Slider's max
  238. d->SettingSliderRange = true;
  239. d->Slider->setMaximum(d->MaximumSpinBox->maximum());
  240. d->SettingSliderRange = false;
  241. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  242. d->updateSpinBoxWidth();
  243. }
  244. // --------------------------------------------------------------------------
  245. void ctkRangeWidget::setRange(double min, double max)
  246. {
  247. Q_D(ctkRangeWidget);
  248. double oldMin = d->MinimumSpinBox->minimum();
  249. double oldMax = d->MaximumSpinBox->maximum();
  250. bool blocked = d->MinimumSpinBox->blockSignals(true);
  251. d->MinimumSpinBox->setRange(qMin(min,max), qMax(min,max));
  252. d->MinimumSpinBox->blockSignals(blocked);
  253. blocked = d->MaximumSpinBox->blockSignals(true);
  254. d->MaximumSpinBox->setRange(qMin(min,max), qMax(min,max));
  255. d->MaximumSpinBox->blockSignals(blocked);
  256. // SpinBox can truncate the range (depending on decimals).
  257. // use Spinbox's range to set Slider's range
  258. d->SettingSliderRange = true;
  259. d->Slider->setRange(d->MinimumSpinBox->minimum(), d->MaximumSpinBox->maximum());
  260. d->SettingSliderRange = false;
  261. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(), d->Slider->minimum()));
  262. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  263. d->updateSpinBoxWidth();
  264. if (oldMin != d->MinimumSpinBox->minimum() ||
  265. oldMax != d->MaximumSpinBox->maximum())
  266. {
  267. emit rangeChanged(d->MinimumSpinBox->minimum(), d->MaximumSpinBox->maximum());
  268. }
  269. }
  270. // --------------------------------------------------------------------------
  271. void ctkRangeWidget::onSliderRangeChanged(double min, double max)
  272. {
  273. Q_D(ctkRangeWidget);
  274. if (!d->SettingSliderRange)
  275. {
  276. this->setRange(min, max);
  277. }
  278. }
  279. /*
  280. // --------------------------------------------------------------------------
  281. double ctkRangeWidget::sliderPosition()const
  282. {
  283. return d->Slider->sliderPosition();
  284. }
  285. // --------------------------------------------------------------------------
  286. void ctkRangeWidget::setSliderPosition(double position)
  287. {
  288. d->Slider->setSliderPosition(position);
  289. }
  290. */
  291. /*
  292. // --------------------------------------------------------------------------
  293. double ctkRangeWidget::previousSliderPosition()
  294. {
  295. return d->Slider->previousSliderPosition();
  296. }
  297. */
  298. // --------------------------------------------------------------------------
  299. void ctkRangeWidget::values(double &minValue, double &maxValue)const
  300. {
  301. Q_D(const ctkRangeWidget);
  302. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  303. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  304. minValue = d->Changing ? d->MinimumValueBeforeChange : d->Slider->minimumValue();
  305. maxValue = d->Changing ? d->MaximumValueBeforeChange : d->Slider->maximumValue();
  306. }
  307. // --------------------------------------------------------------------------
  308. double ctkRangeWidget::minimumValue()const
  309. {
  310. Q_D(const ctkRangeWidget);
  311. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  312. return d->Changing ? d->MinimumValueBeforeChange : d->Slider->minimumValue();
  313. }
  314. // --------------------------------------------------------------------------
  315. double ctkRangeWidget::maximumValue()const
  316. {
  317. Q_D(const ctkRangeWidget);
  318. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  319. return d->Changing ? d->MaximumValueBeforeChange : d->Slider->maximumValue();
  320. }
  321. // --------------------------------------------------------------------------
  322. void ctkRangeWidget::setMinimumValue(double _value)
  323. {
  324. Q_D(ctkRangeWidget);
  325. // disable the tracking temporally to emit the
  326. // signal valueChanged if changeValue() is called
  327. bool isChanging = d->Changing;
  328. d->Changing = false;
  329. d->MinimumSpinBox->setValue(_value);
  330. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  331. // restore the prop
  332. d->Changing = isChanging;
  333. }
  334. // --------------------------------------------------------------------------
  335. void ctkRangeWidget::setMaximumValue(double _value)
  336. {
  337. Q_D(ctkRangeWidget);
  338. // disable the tracking temporally to emit the
  339. // signal valueChanged if changeValue() is called
  340. bool isChanging = d->Changing;
  341. d->Changing = false;
  342. d->MaximumSpinBox->setValue(_value);
  343. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  344. // restore the prop
  345. d->Changing = isChanging;
  346. }
  347. // --------------------------------------------------------------------------
  348. void ctkRangeWidget::setValues(double newMinimumValue, double newMaximumValue)
  349. {
  350. Q_D(ctkRangeWidget);
  351. // disable the tracking temporally to emit the
  352. // signal valueChanged if changeValue() is called
  353. bool isChanging = d->Changing;
  354. d->Changing = false;
  355. // the pb here is that setting the spinbox separately will fired 2 signals and
  356. // between the state will be inconsistent
  357. d->MinimumSpinBox->setValue(newMinimumValue);
  358. d->MaximumSpinBox->setValue(newMaximumValue);
  359. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  360. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  361. // restore the prop
  362. d->Changing = isChanging;
  363. }
  364. // --------------------------------------------------------------------------
  365. void ctkRangeWidget::setMinimumToMaximumSpinBox(double minimum)
  366. {
  367. Q_D(ctkRangeWidget);
  368. d->MaximumSpinBox->setMinimum(minimum);
  369. }
  370. // --------------------------------------------------------------------------
  371. void ctkRangeWidget::setMaximumToMinimumSpinBox(double maximum)
  372. {
  373. Q_D(ctkRangeWidget);
  374. d->MinimumSpinBox->setMaximum(maximum);
  375. }
  376. // --------------------------------------------------------------------------
  377. void ctkRangeWidget::startChanging()
  378. {
  379. Q_D(ctkRangeWidget);
  380. if (d->Tracking)
  381. {
  382. return;
  383. }
  384. d->Changing = true;
  385. d->MinimumValueBeforeChange = this->minimumValue();
  386. d->MaximumValueBeforeChange = this->maximumValue();
  387. }
  388. // --------------------------------------------------------------------------
  389. void ctkRangeWidget::stopChanging()
  390. {
  391. Q_D(ctkRangeWidget);
  392. if (d->Tracking)
  393. {
  394. return;
  395. }
  396. d->Changing = false;
  397. bool emitMinValChanged = qAbs(this->minimumValue() - d->MinimumValueBeforeChange) > (this->singleStep() * 0.000000001);
  398. bool emitMaxValChanged = qAbs(this->maximumValue() - d->MaximumValueBeforeChange) > (this->singleStep() * 0.000000001);
  399. if (emitMinValChanged || emitMaxValChanged)
  400. {
  401. // emit the valuesChanged signal first
  402. emit this->valuesChanged(this->minimumValue(), this->maximumValue());
  403. }
  404. if (emitMinValChanged)
  405. {
  406. emit this->minimumValueChanged(this->minimumValue());
  407. }
  408. if (emitMaxValChanged)
  409. {
  410. emit this->maximumValueChanged(this->maximumValue());
  411. }
  412. }
  413. // --------------------------------------------------------------------------
  414. void ctkRangeWidget::changeMinimumValue(double newValue)
  415. {
  416. Q_D(ctkRangeWidget);
  417. //if (d->Tracking)
  418. {
  419. emit this->minimumValueIsChanging(newValue);
  420. }
  421. if (!d->Changing)
  422. {
  423. emit this->valuesChanged(newValue, this->maximumValue());
  424. emit this->minimumValueChanged(newValue);
  425. }
  426. }
  427. // --------------------------------------------------------------------------
  428. void ctkRangeWidget::changeMaximumValue(double newValue)
  429. {
  430. Q_D(ctkRangeWidget);
  431. //if (d->Tracking)
  432. {
  433. emit this->maximumValueIsChanging(newValue);
  434. }
  435. if (!d->Changing)
  436. {
  437. emit this->valuesChanged(this->minimumValue(), newValue);
  438. emit this->maximumValueChanged(newValue);
  439. }
  440. }
  441. // --------------------------------------------------------------------------
  442. void ctkRangeWidget::changeValues(double newMinValue, double newMaxValue)
  443. {
  444. Q_D(ctkRangeWidget);
  445. d->MinimumSpinBox->setValue(newMinValue);
  446. d->MaximumSpinBox->setValue(newMaxValue);
  447. }
  448. // --------------------------------------------------------------------------
  449. bool ctkRangeWidget::eventFilter(QObject *obj, QEvent *event)
  450. {
  451. if (event->type() == QEvent::MouseButtonPress)
  452. {
  453. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  454. if (mouseEvent->button() & Qt::LeftButton)
  455. {
  456. this->startChanging();
  457. }
  458. }
  459. else if (event->type() == QEvent::MouseButtonRelease)
  460. {
  461. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  462. if (mouseEvent->button() & Qt::LeftButton)
  463. {
  464. // here we might prevent ctkRangeWidget::stopChanging
  465. // from sending a valueChanged() event as the spinbox might
  466. // send a valueChanged() after eventFilter() is done.
  467. this->stopChanging();
  468. }
  469. }
  470. // standard event processing
  471. return this->Superclass::eventFilter(obj, event);
  472. }
  473. // --------------------------------------------------------------------------
  474. double ctkRangeWidget::singleStep()const
  475. {
  476. Q_D(const ctkRangeWidget);
  477. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()) &&
  478. d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  479. return d->Slider->singleStep();
  480. }
  481. // --------------------------------------------------------------------------
  482. void ctkRangeWidget::setSingleStep(double step)
  483. {
  484. Q_D(ctkRangeWidget);
  485. d->MinimumSpinBox->setSingleStep(step);
  486. d->MaximumSpinBox->setSingleStep(step);
  487. d->Slider->setSingleStep(d->MinimumSpinBox->singleStep());
  488. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()) &&
  489. d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  490. }
  491. // --------------------------------------------------------------------------
  492. int ctkRangeWidget::decimals()const
  493. {
  494. Q_D(const ctkRangeWidget);
  495. Q_ASSERT(d->MinimumSpinBox->decimals() == d->MaximumSpinBox->decimals());
  496. return d->MinimumSpinBox->decimals();
  497. }
  498. // --------------------------------------------------------------------------
  499. void ctkRangeWidget::setDecimals(int newDecimals)
  500. {
  501. Q_D(ctkRangeWidget);
  502. d->MinimumSpinBox->setDecimals(newDecimals);
  503. d->MaximumSpinBox->setDecimals(newDecimals);
  504. // The number of decimals can change the range values
  505. // i.e. 50.55 with 2 decimals -> 51 with 0 decimals
  506. // As the SpinBox range change doesn't fire signals,
  507. // we have to do the synchronization manually here
  508. d->Slider->setMinimum(d->MinimumSpinBox->minimum());
  509. d->Slider->setMaximum(d->MaximumSpinBox->maximum());
  510. }
  511. // --------------------------------------------------------------------------
  512. QString ctkRangeWidget::prefix()const
  513. {
  514. Q_D(const ctkRangeWidget);
  515. Q_ASSERT(d->MinimumSpinBox->prefix() == d->MaximumSpinBox->prefix());
  516. return d->MinimumSpinBox->prefix();
  517. }
  518. // --------------------------------------------------------------------------
  519. void ctkRangeWidget::setPrefix(const QString& newPrefix)
  520. {
  521. Q_D(ctkRangeWidget);
  522. d->MinimumSpinBox->setPrefix(newPrefix);
  523. d->MaximumSpinBox->setPrefix(newPrefix);
  524. }
  525. // --------------------------------------------------------------------------
  526. QString ctkRangeWidget::suffix()const
  527. {
  528. Q_D(const ctkRangeWidget);
  529. Q_ASSERT(d->MinimumSpinBox->suffix() == d->MaximumSpinBox->suffix());
  530. return d->MinimumSpinBox->suffix();
  531. }
  532. // --------------------------------------------------------------------------
  533. void ctkRangeWidget::setSuffix(const QString& newSuffix)
  534. {
  535. Q_D(ctkRangeWidget);
  536. d->MinimumSpinBox->setSuffix(newSuffix);
  537. d->MaximumSpinBox->setSuffix(newSuffix);
  538. }
  539. // --------------------------------------------------------------------------
  540. double ctkRangeWidget::tickInterval()const
  541. {
  542. Q_D(const ctkRangeWidget);
  543. return d->Slider->tickInterval();
  544. }
  545. // --------------------------------------------------------------------------
  546. void ctkRangeWidget::setTickInterval(double ti)
  547. {
  548. Q_D(ctkRangeWidget);
  549. d->Slider->setTickInterval(ti);
  550. }
  551. // -------------------------------------------------------------------------
  552. void ctkRangeWidget::reset()
  553. {
  554. this->setMinimumValue(this->minimum());
  555. this->setMaximumValue(this->maximum());
  556. }
  557. // -------------------------------------------------------------------------
  558. void ctkRangeWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  559. {
  560. Q_D(ctkRangeWidget);
  561. if (d->SpinBoxAlignment == alignment)
  562. {
  563. return;
  564. }
  565. d->SpinBoxAlignment = alignment;
  566. d->relayout();
  567. }
  568. // -------------------------------------------------------------------------
  569. Qt::Alignment ctkRangeWidget::spinBoxAlignment()const
  570. {
  571. Q_D(const ctkRangeWidget);
  572. return d->SpinBoxAlignment;
  573. }
  574. // -------------------------------------------------------------------------
  575. void ctkRangeWidget::setSpinBoxTextAlignment(Qt::Alignment alignment)
  576. {
  577. Q_D(ctkRangeWidget);
  578. d->MinimumSpinBox->setAlignment(alignment);
  579. d->MaximumSpinBox->setAlignment(alignment);
  580. }
  581. // -------------------------------------------------------------------------
  582. Qt::Alignment ctkRangeWidget::spinBoxTextAlignment()const
  583. {
  584. Q_D(const ctkRangeWidget);
  585. Q_ASSERT(d->MinimumSpinBox->alignment() == d->MaximumSpinBox->alignment());
  586. return d->MinimumSpinBox->alignment();
  587. }
  588. // -------------------------------------------------------------------------
  589. void ctkRangeWidget::setTracking(bool enable)
  590. {
  591. Q_D(ctkRangeWidget);
  592. d->Tracking = enable;
  593. }
  594. // -------------------------------------------------------------------------
  595. bool ctkRangeWidget::hasTracking()const
  596. {
  597. Q_D(const ctkRangeWidget);
  598. return d->Tracking;
  599. }
  600. // -------------------------------------------------------------------------
  601. bool ctkRangeWidget::isAutoSpinBoxWidth()const
  602. {
  603. Q_D(const ctkRangeWidget);
  604. return d->AutoSpinBoxWidth;
  605. }
  606. // -------------------------------------------------------------------------
  607. void ctkRangeWidget::setAutoSpinBoxWidth(bool autoWidth)
  608. {
  609. Q_D(ctkRangeWidget);
  610. d->AutoSpinBoxWidth = autoWidth;
  611. d->updateSpinBoxWidth();
  612. }
  613. // --------------------------------------------------------------------------
  614. bool ctkRangeWidget::symmetricMoves()const
  615. {
  616. Q_D(const ctkRangeWidget);
  617. return d->Slider->symmetricMoves();
  618. }
  619. // --------------------------------------------------------------------------
  620. void ctkRangeWidget::setSymmetricMoves(bool symmetry)
  621. {
  622. Q_D(ctkRangeWidget);
  623. d->Slider->setSymmetricMoves(symmetry);
  624. }
  625. // -------------------------------------------------------------------------
  626. ctkDoubleRangeSlider* ctkRangeWidget::slider()const
  627. {
  628. Q_D(const ctkRangeWidget);
  629. return d->Slider;
  630. }
  631. // -------------------------------------------------------------------------
  632. void ctkRangeWidget::setSlider(ctkDoubleRangeSlider* slider)
  633. {
  634. Q_D(ctkRangeWidget);
  635. slider->setOrientation(d->Slider->orientation());
  636. slider->setMinimum(d->Slider->minimum());
  637. slider->setMaximum(d->Slider->maximum());
  638. slider->setValues(d->Slider->minimumValue(), d->Slider->maximumValue());
  639. slider->setSingleStep(d->Slider->singleStep());
  640. slider->setTracking(d->Slider->hasTracking());
  641. slider->setTickInterval(d->Slider->tickInterval());
  642. delete d->Slider;
  643. d->Slider = slider;
  644. d->connectSlider();
  645. d->relayout();
  646. }