ctkRangeWidget.cpp 24 KB

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