ctkRangeWidget.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QMouseEvent>
  17. // CTK includes
  18. #include "ctkRangeWidget.h"
  19. #include "ui_ctkRangeWidget.h"
  20. //-----------------------------------------------------------------------------
  21. class ctkRangeWidgetPrivate: public ctkPrivate<ctkRangeWidget>,
  22. public Ui_ctkRangeWidget
  23. {
  24. public:
  25. ctkRangeWidgetPrivate();
  26. void updateSpinBoxWidth();
  27. int synchronizedSpinBoxWidth()const;
  28. void synchronizeSiblingSpinBox(int newWidth);
  29. static bool equal(double v1, double v2);
  30. bool Tracking;
  31. bool Changing;
  32. double MinimumValueBeforeChange;
  33. double MaximumValueBeforeChange;
  34. bool AutoSpinBoxWidth;
  35. };
  36. // --------------------------------------------------------------------------
  37. bool ctkRangeWidgetPrivate::equal(double v1, double v2)
  38. {
  39. return qAbs(v1 - v2) < 0.0001;
  40. }
  41. // --------------------------------------------------------------------------
  42. ctkRangeWidgetPrivate::ctkRangeWidgetPrivate()
  43. {
  44. this->Tracking = true;
  45. this->Changing = false;
  46. this->MinimumValueBeforeChange = 0.;
  47. this->MaximumValueBeforeChange = 0.;
  48. this->AutoSpinBoxWidth = true;
  49. }
  50. // --------------------------------------------------------------------------
  51. void ctkRangeWidgetPrivate::updateSpinBoxWidth()
  52. {
  53. int spinBoxWidth = this->synchronizedSpinBoxWidth();
  54. if (this->AutoSpinBoxWidth)
  55. {
  56. this->MinimumSpinBox->setMinimumWidth(spinBoxWidth);
  57. this->MaximumSpinBox->setMinimumWidth(spinBoxWidth);
  58. }
  59. else
  60. {
  61. this->MinimumSpinBox->setMinimumWidth(0);
  62. this->MaximumSpinBox->setMinimumWidth(0);
  63. }
  64. this->synchronizeSiblingSpinBox(spinBoxWidth);
  65. }
  66. // --------------------------------------------------------------------------
  67. int ctkRangeWidgetPrivate::synchronizedSpinBoxWidth()const
  68. {
  69. CTK_P(const ctkRangeWidget);
  70. //Q_ASSERT(this->MinimumSpinBox->sizeHint() == this->MaximumSpinBox->sizeHint());
  71. int maxWidth = qMax(this->MinimumSpinBox->sizeHint().width(),
  72. this->MaximumSpinBox->sizeHint().width());
  73. if (!p->parent())
  74. {
  75. return maxWidth;
  76. }
  77. QList<ctkRangeWidget*> siblings =
  78. p->parent()->findChildren<ctkRangeWidget*>();
  79. foreach(ctkRangeWidget* sibling, siblings)
  80. {
  81. maxWidth = qMax(maxWidth, qMax(sibling->ctk_d()->MaximumSpinBox->sizeHint().width(),
  82. sibling->ctk_d()->MaximumSpinBox->sizeHint().width()));
  83. }
  84. return maxWidth;
  85. }
  86. // --------------------------------------------------------------------------
  87. void ctkRangeWidgetPrivate::synchronizeSiblingSpinBox(int width)
  88. {
  89. CTK_P(const ctkRangeWidget);
  90. QList<ctkRangeWidget*> siblings =
  91. p->parent()->findChildren<ctkRangeWidget*>();
  92. foreach(ctkRangeWidget* sibling, siblings)
  93. {
  94. if (sibling != p && sibling->isAutoSpinBoxWidth())
  95. {
  96. sibling->ctk_d()->MinimumSpinBox->setMinimumWidth(width);
  97. sibling->ctk_d()->MaximumSpinBox->setMinimumWidth(width);
  98. }
  99. }
  100. }
  101. // --------------------------------------------------------------------------
  102. ctkRangeWidget::ctkRangeWidget(QWidget* _parent) : Superclass(_parent)
  103. {
  104. CTK_INIT_PRIVATE(ctkRangeWidget);
  105. CTK_D(ctkRangeWidget);
  106. d->setupUi(this);
  107. d->MinimumSpinBox->setMinimum(d->Slider->minimum());
  108. d->MinimumSpinBox->setMaximum(d->Slider->maximum());
  109. d->MaximumSpinBox->setMinimum(d->Slider->minimum());
  110. d->MaximumSpinBox->setMaximum(d->Slider->maximum());
  111. d->MinimumSpinBox->setValue(d->Slider->minimumValue());
  112. d->MaximumSpinBox->setValue(d->Slider->maximumValue());
  113. this->connect(d->Slider, SIGNAL(minimumValueChanged(double)), d->MinimumSpinBox, SLOT(setValue(double)));
  114. this->connect(d->MinimumSpinBox, SIGNAL(valueChanged(double)), d->Slider, SLOT(setMinimumValue(double)));
  115. this->connect(d->Slider, SIGNAL(maximumValueChanged(double)), d->MaximumSpinBox, SLOT(setValue(double)));
  116. this->connect(d->MaximumSpinBox, SIGNAL(valueChanged(double)), d->Slider, SLOT(setMaximumValue(double)));
  117. this->connect(d->MinimumSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setMinimumToMaximumSpinBox(double)));
  118. this->connect(d->MaximumSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setMaximumToMinimumSpinBox(double)));
  119. this->connect(d->Slider, SIGNAL(sliderPressed()), this, SLOT(startChanging()));
  120. this->connect(d->Slider, SIGNAL(sliderReleased()), this, SLOT(stopChanging()));
  121. this->connect(d->Slider, SIGNAL(minimumValueChanged(double)), this, SLOT(changeMinimumValue(double)));
  122. this->connect(d->Slider, SIGNAL(maximumValueChanged(double)), this, SLOT(changeMaximumValue(double)));
  123. d->MinimumSpinBox->installEventFilter(this);
  124. d->MaximumSpinBox->installEventFilter(this);
  125. }
  126. // --------------------------------------------------------------------------
  127. double ctkRangeWidget::minimum()const
  128. {
  129. CTK_D(const ctkRangeWidget);
  130. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  131. return d->Slider->minimum();
  132. }
  133. // --------------------------------------------------------------------------
  134. double ctkRangeWidget::maximum()const
  135. {
  136. CTK_D(const ctkRangeWidget);
  137. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  138. return d->Slider->maximum();
  139. }
  140. // --------------------------------------------------------------------------
  141. void ctkRangeWidget::setMinimum(double min)
  142. {
  143. CTK_D(ctkRangeWidget);
  144. d->MinimumSpinBox->setMinimum(min);
  145. // SpinBox can truncate min (depending on decimals).
  146. // use Spinbox's min to set Slider's min
  147. d->Slider->setMinimum(d->MinimumSpinBox->minimum());
  148. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
  149. d->updateSpinBoxWidth();
  150. }
  151. // --------------------------------------------------------------------------
  152. void ctkRangeWidget::setMaximum(double max)
  153. {
  154. CTK_D(ctkRangeWidget);
  155. d->MaximumSpinBox->setMaximum(max);
  156. // SpinBox can truncate max (depending on decimals).
  157. // use Spinbox's max to set Slider's max
  158. d->Slider->setMaximum(d->MaximumSpinBox->maximum());
  159. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  160. d->updateSpinBoxWidth();
  161. }
  162. // --------------------------------------------------------------------------
  163. void ctkRangeWidget::setRange(double min, double max)
  164. {
  165. CTK_D(ctkRangeWidget);
  166. d->MinimumSpinBox->setMinimum(qMin(min,max));
  167. d->MaximumSpinBox->setMaximum(qMax(min,max));
  168. // SpinBox can truncate the range (depending on decimals).
  169. // use Spinbox's range to set Slider's range
  170. d->Slider->setRange(d->MinimumSpinBox->minimum(), d->MaximumSpinBox->maximum());
  171. Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(), d->Slider->minimum()));
  172. Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
  173. d->updateSpinBoxWidth();
  174. }
  175. /*
  176. // --------------------------------------------------------------------------
  177. double ctkRangeWidget::sliderPosition()const
  178. {
  179. return ctk_d()->Slider->sliderPosition();
  180. }
  181. // --------------------------------------------------------------------------
  182. void ctkRangeWidget::setSliderPosition(double position)
  183. {
  184. ctk_d()->Slider->setSliderPosition(position);
  185. }
  186. */
  187. /*
  188. // --------------------------------------------------------------------------
  189. double ctkRangeWidget::previousSliderPosition()
  190. {
  191. return ctk_d()->Slider->previousSliderPosition();
  192. }
  193. */
  194. // --------------------------------------------------------------------------
  195. double ctkRangeWidget::minimumValue()const
  196. {
  197. CTK_D(const ctkRangeWidget);
  198. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  199. return d->Changing ? d->MinimumValueBeforeChange : d->Slider->minimumValue();
  200. }
  201. // --------------------------------------------------------------------------
  202. double ctkRangeWidget::maximumValue()const
  203. {
  204. CTK_D(const ctkRangeWidget);
  205. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  206. return d->Changing ? d->MaximumValueBeforeChange : d->Slider->maximumValue();
  207. }
  208. // --------------------------------------------------------------------------
  209. void ctkRangeWidget::setMinimumValue(double _value)
  210. {
  211. CTK_D(ctkRangeWidget);
  212. // disable the tracking temporally to emit the
  213. // signal valueChanged if changeValue() is called
  214. bool isChanging = d->Changing;
  215. d->Changing = false;
  216. d->MinimumSpinBox->setValue(_value);
  217. // Why do we need to set the value to the slider ?
  218. //d->Slider->setValue(d->SpinBox->value());
  219. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  220. // restore the prop
  221. d->Changing = isChanging;
  222. }
  223. // --------------------------------------------------------------------------
  224. void ctkRangeWidget::setMaximumValue(double _value)
  225. {
  226. CTK_D(ctkRangeWidget);
  227. // disable the tracking temporally to emit the
  228. // signal valueChanged if changeValue() is called
  229. bool isChanging = d->Changing;
  230. d->Changing = false;
  231. d->MaximumSpinBox->setValue(_value);
  232. // Why do we need to set the value to the slider ?
  233. //d->Slider->setValue(d->SpinBox->value());
  234. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  235. // restore the prop
  236. d->Changing = isChanging;
  237. }
  238. // --------------------------------------------------------------------------
  239. void ctkRangeWidget::setValues(double newMinimumValue, double newMaximumValue)
  240. {
  241. CTK_D(ctkRangeWidget);
  242. // disable the tracking temporally to emit the
  243. // signal valueChanged if changeValue() is called
  244. bool isChanging = d->Changing;
  245. d->Changing = false;
  246. d->MinimumSpinBox->setValue(newMinimumValue);
  247. d->MaximumSpinBox->setValue(newMaximumValue);
  248. // Why do we need to set the value to the slider ?
  249. //d->Slider->setValue(d->SpinBox->value());
  250. Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
  251. Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
  252. // restore the prop
  253. d->Changing = isChanging;
  254. }
  255. // --------------------------------------------------------------------------
  256. void ctkRangeWidget::setMinimumToMaximumSpinBox(double minimum)
  257. {
  258. ctk_d()->MaximumSpinBox->setMinimum(minimum);
  259. }
  260. // --------------------------------------------------------------------------
  261. void ctkRangeWidget::setMaximumToMinimumSpinBox(double maximum)
  262. {
  263. ctk_d()->MinimumSpinBox->setMaximum(maximum);
  264. }
  265. // --------------------------------------------------------------------------
  266. void ctkRangeWidget::startChanging()
  267. {
  268. CTK_D(ctkRangeWidget);
  269. if (d->Tracking)
  270. {
  271. return;
  272. }
  273. d->Changing = true;
  274. d->MinimumValueBeforeChange = this->minimumValue();
  275. d->MaximumValueBeforeChange = this->maximumValue();
  276. }
  277. // --------------------------------------------------------------------------
  278. void ctkRangeWidget::stopChanging()
  279. {
  280. CTK_D(ctkRangeWidget);
  281. if (d->Tracking)
  282. {
  283. return;
  284. }
  285. d->Changing = false;
  286. if (qAbs(this->minimumValue() - d->MinimumValueBeforeChange) > (this->singleStep() * 0.000000001))
  287. {
  288. emit this->minimumValueChanged(this->minimumValue());
  289. }
  290. if (qAbs(this->maximumValue() - d->MaximumValueBeforeChange) > (this->singleStep() * 0.000000001))
  291. {
  292. emit this->maximumValueChanged(this->maximumValue());
  293. }
  294. }
  295. // --------------------------------------------------------------------------
  296. void ctkRangeWidget::changeMinimumValue(double newValue)
  297. {
  298. CTK_D(ctkRangeWidget);
  299. //if (d->Tracking)
  300. {
  301. emit this->minimumValueIsChanging(newValue);
  302. }
  303. if (!d->Changing)
  304. {
  305. emit this->minimumValueChanged(newValue);
  306. }
  307. }
  308. // --------------------------------------------------------------------------
  309. void ctkRangeWidget::changeMaximumValue(double newValue)
  310. {
  311. CTK_D(ctkRangeWidget);
  312. //if (d->Tracking)
  313. {
  314. emit this->maximumValueIsChanging(newValue);
  315. }
  316. if (!d->Changing)
  317. {
  318. emit this->maximumValueChanged(newValue);
  319. }
  320. }
  321. // --------------------------------------------------------------------------
  322. bool ctkRangeWidget::eventFilter(QObject *obj, QEvent *event)
  323. {
  324. if (event->type() == QEvent::MouseButtonPress)
  325. {
  326. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  327. if (mouseEvent->button() & Qt::LeftButton)
  328. {
  329. this->startChanging();
  330. }
  331. }
  332. else if (event->type() == QEvent::MouseButtonRelease)
  333. {
  334. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  335. if (mouseEvent->button() & Qt::LeftButton)
  336. {
  337. // here we might prevent ctkRangeWidget::stopChanging
  338. // from sending a valueChanged() event as the spinbox might
  339. // send a valueChanged() after eventFilter() is done.
  340. this->stopChanging();
  341. }
  342. }
  343. // standard event processing
  344. return this->Superclass::eventFilter(obj, event);
  345. }
  346. // --------------------------------------------------------------------------
  347. double ctkRangeWidget::singleStep()const
  348. {
  349. CTK_D(const ctkRangeWidget);
  350. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()) &&
  351. d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  352. return d->Slider->singleStep();
  353. }
  354. // --------------------------------------------------------------------------
  355. void ctkRangeWidget::setSingleStep(double step)
  356. {
  357. CTK_D(ctkRangeWidget);
  358. d->MinimumSpinBox->setSingleStep(step);
  359. d->MaximumSpinBox->setSingleStep(step);
  360. d->Slider->setSingleStep(d->MinimumSpinBox->singleStep());
  361. Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()) &&
  362. d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));
  363. }
  364. // --------------------------------------------------------------------------
  365. int ctkRangeWidget::decimals()const
  366. {
  367. CTK_D(const ctkRangeWidget);
  368. Q_ASSERT(d->MinimumSpinBox->decimals() == d->MaximumSpinBox->decimals());
  369. return d->MinimumSpinBox->decimals();
  370. }
  371. // --------------------------------------------------------------------------
  372. void ctkRangeWidget::setDecimals(int newDecimals)
  373. {
  374. CTK_D(ctkRangeWidget);
  375. d->MinimumSpinBox->setDecimals(newDecimals);
  376. d->MaximumSpinBox->setDecimals(newDecimals);
  377. // The number of decimals can change the range values
  378. // i.e. 50.55 with 2 decimals -> 51 with 0 decimals
  379. // As the SpinBox range change doesn't fire signals,
  380. // we have to do the synchronization manually here
  381. d->Slider->setMinimum(d->MinimumSpinBox->minimum());
  382. d->Slider->setMaximum(d->MaximumSpinBox->maximum());
  383. }
  384. // --------------------------------------------------------------------------
  385. QString ctkRangeWidget::prefix()const
  386. {
  387. CTK_D(const ctkRangeWidget);
  388. Q_ASSERT(d->MinimumSpinBox->prefix() == d->MaximumSpinBox->prefix());
  389. return d->MinimumSpinBox->prefix();
  390. }
  391. // --------------------------------------------------------------------------
  392. void ctkRangeWidget::setPrefix(const QString& newPrefix)
  393. {
  394. CTK_D(ctkRangeWidget);
  395. d->MinimumSpinBox->setPrefix(newPrefix);
  396. d->MaximumSpinBox->setPrefix(newPrefix);
  397. }
  398. // --------------------------------------------------------------------------
  399. QString ctkRangeWidget::suffix()const
  400. {
  401. CTK_D(const ctkRangeWidget);
  402. Q_ASSERT(d->MinimumSpinBox->suffix() == d->MaximumSpinBox->suffix());
  403. return d->MinimumSpinBox->suffix();
  404. }
  405. // --------------------------------------------------------------------------
  406. void ctkRangeWidget::setSuffix(const QString& newSuffix)
  407. {
  408. CTK_D(ctkRangeWidget);
  409. d->MinimumSpinBox->setSuffix(newSuffix);
  410. d->MaximumSpinBox->setSuffix(newSuffix);
  411. }
  412. // --------------------------------------------------------------------------
  413. double ctkRangeWidget::tickInterval()const
  414. {
  415. CTK_D(const ctkRangeWidget);
  416. return d->Slider->tickInterval();
  417. }
  418. // --------------------------------------------------------------------------
  419. void ctkRangeWidget::setTickInterval(double ti)
  420. {
  421. CTK_D(ctkRangeWidget);
  422. d->Slider->setTickInterval(ti);
  423. }
  424. // -------------------------------------------------------------------------
  425. void ctkRangeWidget::reset()
  426. {
  427. this->setMinimumValue(this->minimum());
  428. this->setMaximumValue(this->maximum());
  429. }
  430. // -------------------------------------------------------------------------
  431. void ctkRangeWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  432. {
  433. CTK_D(ctkRangeWidget);
  434. d->MinimumSpinBox->setAlignment(alignment);
  435. d->MaximumSpinBox->setAlignment(alignment);
  436. }
  437. // -------------------------------------------------------------------------
  438. Qt::Alignment ctkRangeWidget::spinBoxAlignment()const
  439. {
  440. CTK_D(const ctkRangeWidget);
  441. Q_ASSERT(d->MinimumSpinBox->alignment() == d->MaximumSpinBox->alignment());
  442. return d->MinimumSpinBox->alignment();
  443. }
  444. // -------------------------------------------------------------------------
  445. void ctkRangeWidget::setTracking(bool enable)
  446. {
  447. CTK_D(ctkRangeWidget);
  448. d->Tracking = enable;
  449. }
  450. // -------------------------------------------------------------------------
  451. bool ctkRangeWidget::hasTracking()const
  452. {
  453. CTK_D(const ctkRangeWidget);
  454. return d->Tracking;
  455. }
  456. // -------------------------------------------------------------------------
  457. bool ctkRangeWidget::isAutoSpinBoxWidth()const
  458. {
  459. CTK_D(const ctkRangeWidget);
  460. return d->AutoSpinBoxWidth;
  461. }
  462. // -------------------------------------------------------------------------
  463. void ctkRangeWidget::setAutoSpinBoxWidth(bool autoWidth)
  464. {
  465. CTK_D(ctkRangeWidget);
  466. d->AutoSpinBoxWidth = autoWidth;
  467. d->updateSpinBoxWidth();
  468. }