ctkRangeWidget.cpp 26 KB

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