ctkRangeWidget.cpp 28 KB

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