ctkRangeWidget.cpp 24 KB

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