ctkRangeWidget.cpp 25 KB

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