ctkSliderWidget.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 "ctkPopupWidget.h"
  19. #include "ctkSliderWidget.h"
  20. #include "ui_ctkSliderWidget.h"
  21. // STD includes
  22. #include <cmath>
  23. //-----------------------------------------------------------------------------
  24. class ctkSliderWidgetPrivate: public Ui_ctkSliderWidget
  25. {
  26. Q_DECLARE_PUBLIC(ctkSliderWidget);
  27. protected:
  28. ctkSliderWidget* const q_ptr;
  29. public:
  30. ctkSliderWidgetPrivate(ctkSliderWidget& object);
  31. virtual ~ctkSliderWidgetPrivate();
  32. void updateSpinBoxWidth();
  33. void updateSpinBoxDecimals();
  34. int synchronizedSpinBoxWidth() const;
  35. void synchronizeSiblingWidth(int width);
  36. void synchronizeSiblingDecimals(int decimals);
  37. bool equal(double spinBoxValue, double sliderValue)const
  38. {
  39. return qAbs(sliderValue - spinBoxValue) < std::pow(10., -this->SpinBox->decimals());
  40. }
  41. bool Tracking;
  42. bool Changing;
  43. double ValueBeforeChange;
  44. ctkSliderWidget::SynchronizeSiblings SynchronizeMode;
  45. ctkPopupWidget* SliderPopup;
  46. };
  47. // --------------------------------------------------------------------------
  48. ctkSliderWidgetPrivate::ctkSliderWidgetPrivate(ctkSliderWidget& object)
  49. :q_ptr(&object)
  50. {
  51. qRegisterMetaType<ctkSliderWidget::SynchronizeSiblings>(
  52. "ctkSliderWidget::SynchronizeSiblings");
  53. this->Tracking = true;
  54. this->Changing = false;
  55. this->ValueBeforeChange = 0.;
  56. this->SynchronizeMode =
  57. ctkSliderWidget::SynchronizeWidth | ctkSliderWidget::SynchronizeDecimals;
  58. this->SliderPopup = 0;
  59. }
  60. // --------------------------------------------------------------------------
  61. ctkSliderWidgetPrivate::~ctkSliderWidgetPrivate()
  62. {
  63. }
  64. // --------------------------------------------------------------------------
  65. void ctkSliderWidgetPrivate::updateSpinBoxWidth()
  66. {
  67. int spinBoxWidth = this->synchronizedSpinBoxWidth();
  68. if (this->SynchronizeMode.testFlag(ctkSliderWidget::SynchronizeWidth))
  69. {
  70. this->SpinBox->setMinimumWidth(spinBoxWidth);
  71. }
  72. else
  73. {
  74. this->SpinBox->setMinimumWidth(0);
  75. }
  76. this->synchronizeSiblingWidth(spinBoxWidth);
  77. }
  78. // --------------------------------------------------------------------------
  79. void ctkSliderWidgetPrivate::updateSpinBoxDecimals()
  80. {
  81. if (this->SynchronizeMode.testFlag(ctkSliderWidget::SynchronizeDecimals))
  82. {
  83. this->synchronizeSiblingDecimals(this->SpinBox->decimals());
  84. }
  85. }
  86. // --------------------------------------------------------------------------
  87. int ctkSliderWidgetPrivate::synchronizedSpinBoxWidth()const
  88. {
  89. Q_Q(const ctkSliderWidget);
  90. int maxWidth = this->SpinBox->sizeHint().width();
  91. if (!q->parent())
  92. {
  93. return maxWidth;
  94. }
  95. QList<ctkSliderWidget*> siblings =
  96. q->parent()->findChildren<ctkSliderWidget*>();
  97. foreach(ctkSliderWidget* sibling, siblings)
  98. {
  99. maxWidth = qMax(maxWidth, sibling->d_func()->SpinBox->sizeHint().width());
  100. }
  101. return maxWidth;
  102. }
  103. // --------------------------------------------------------------------------
  104. void ctkSliderWidgetPrivate::synchronizeSiblingWidth(int width)
  105. {
  106. Q_Q(const ctkSliderWidget);
  107. QList<ctkSliderWidget*> siblings =
  108. q->parent()->findChildren<ctkSliderWidget*>();
  109. foreach(ctkSliderWidget* sibling, siblings)
  110. {
  111. if (sibling != q
  112. && sibling->synchronizeSiblings().testFlag(ctkSliderWidget::SynchronizeWidth))
  113. {
  114. sibling->d_func()->SpinBox->setMinimumWidth(
  115. this->SpinBox->minimumWidth());
  116. }
  117. }
  118. }
  119. // --------------------------------------------------------------------------
  120. void ctkSliderWidgetPrivate::synchronizeSiblingDecimals(int decimals)
  121. {
  122. Q_Q(const ctkSliderWidget);
  123. QList<ctkSliderWidget*> siblings =
  124. q->parent()->findChildren<ctkSliderWidget*>();
  125. foreach(ctkSliderWidget* sibling, siblings)
  126. {
  127. if (sibling != q
  128. && sibling->synchronizeSiblings().testFlag(ctkSliderWidget::SynchronizeDecimals))
  129. {
  130. sibling->d_func()->SpinBox->setDecimals(this->SpinBox->decimals());
  131. }
  132. }
  133. }
  134. // --------------------------------------------------------------------------
  135. ctkSliderWidget::ctkSliderWidget(QWidget* _parent) : Superclass(_parent)
  136. , d_ptr(new ctkSliderWidgetPrivate(*this))
  137. {
  138. Q_D(ctkSliderWidget);
  139. d->setupUi(this);
  140. d->Slider->setMaximum(d->SpinBox->maximum());
  141. d->Slider->setMinimum(d->SpinBox->minimum());
  142. this->connect(d->SpinBox, SIGNAL(valueChanged(double)), d->Slider, SLOT(setValue(double)));
  143. this->connect(d->SpinBox, SIGNAL(decimalsChanged(int)), this, SLOT(setDecimals(int)));
  144. //this->connect(d->Slider, SIGNAL(valueChanged(double)), SIGNAL(valueChanged(double)));
  145. this->connect(d->Slider, SIGNAL(sliderPressed()), this, SLOT(startChanging()));
  146. this->connect(d->Slider, SIGNAL(sliderReleased()), this, SLOT(stopChanging()));
  147. this->connect(d->Slider, SIGNAL(valueChanged(double)), this, SLOT(changeValue(double)));
  148. d->SpinBox->installEventFilter(this);
  149. }
  150. // --------------------------------------------------------------------------
  151. ctkSliderWidget::~ctkSliderWidget()
  152. {
  153. }
  154. // --------------------------------------------------------------------------
  155. double ctkSliderWidget::minimum()const
  156. {
  157. Q_D(const ctkSliderWidget);
  158. Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
  159. return d->Slider->minimum();
  160. }
  161. // --------------------------------------------------------------------------
  162. double ctkSliderWidget::maximum()const
  163. {
  164. Q_D(const ctkSliderWidget);
  165. Q_ASSERT(d->equal(d->SpinBox->maximum(),d->Slider->maximum()));
  166. return d->Slider->maximum();
  167. }
  168. // --------------------------------------------------------------------------
  169. void ctkSliderWidget::setMinimum(double min)
  170. {
  171. Q_D(ctkSliderWidget);
  172. bool wasBlocked = d->SpinBox->blockSignals(true);
  173. d->SpinBox->setMinimum(min);
  174. d->SpinBox->blockSignals(wasBlocked);
  175. // SpinBox can truncate min (depending on decimals).
  176. // use Spinbox's min to set Slider's min
  177. d->Slider->setMinimum(d->SpinBox->minimum());
  178. Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
  179. Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value()));
  180. Q_ASSERT(d->equal(d->SpinBox->maximum(),d->Slider->maximum()));
  181. d->updateSpinBoxWidth();
  182. }
  183. // --------------------------------------------------------------------------
  184. void ctkSliderWidget::setMaximum(double max)
  185. {
  186. Q_D(ctkSliderWidget);
  187. bool wasBlocked = d->SpinBox->blockSignals(true);
  188. d->SpinBox->setMaximum(max);
  189. d->SpinBox->blockSignals(wasBlocked);
  190. // SpinBox can truncate max (depending on decimals).
  191. // use Spinbox's max to set Slider's max
  192. d->Slider->setMaximum(d->SpinBox->maximum());
  193. Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
  194. Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value()));
  195. Q_ASSERT(d->equal(d->SpinBox->maximum(),d->Slider->maximum()));
  196. d->updateSpinBoxWidth();
  197. }
  198. // --------------------------------------------------------------------------
  199. void ctkSliderWidget::setRange(double min, double max)
  200. {
  201. Q_D(ctkSliderWidget);
  202. bool wasBlocked = d->SpinBox->blockSignals(true);
  203. d->SpinBox->setRange(min, max);
  204. d->SpinBox->blockSignals(wasBlocked);
  205. // SpinBox can truncate the range (depending on decimals).
  206. // use Spinbox's range to set Slider's range
  207. d->Slider->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  208. Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
  209. Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value()));
  210. Q_ASSERT(d->equal(d->SpinBox->maximum(),d->Slider->maximum()));
  211. d->updateSpinBoxWidth();
  212. }
  213. /*
  214. // --------------------------------------------------------------------------
  215. double ctkSliderWidget::sliderPosition()const
  216. {
  217. return d->Slider->sliderPosition();
  218. }
  219. // --------------------------------------------------------------------------
  220. void ctkSliderWidget::setSliderPosition(double position)
  221. {
  222. d->Slider->setSliderPosition(position);
  223. }
  224. */
  225. /*
  226. // --------------------------------------------------------------------------
  227. double ctkSliderWidget::previousSliderPosition()
  228. {
  229. return d->Slider->previousSliderPosition();
  230. }
  231. */
  232. // --------------------------------------------------------------------------
  233. double ctkSliderWidget::value()const
  234. {
  235. Q_D(const ctkSliderWidget);
  236. Q_ASSERT(d->equal(d->SpinBox->value(), d->Slider->value()));
  237. return d->Changing ? d->ValueBeforeChange : d->Slider->value();
  238. }
  239. // --------------------------------------------------------------------------
  240. void ctkSliderWidget::setValue(double _value)
  241. {
  242. Q_D(ctkSliderWidget);
  243. // disable the tracking temporally to emit the
  244. // signal valueChanged if changeValue() is called
  245. bool isChanging = d->Changing;
  246. d->Changing = false;
  247. d->SpinBox->setValue(_value);
  248. // Why do we need to set the value to the slider ?
  249. //d->Slider->setValue(d->SpinBox->value());
  250. //double spinBoxValue = d->SpinBox->value();
  251. Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
  252. Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value()));
  253. Q_ASSERT(d->equal(d->SpinBox->maximum(),d->Slider->maximum()));
  254. // restore the prop
  255. d->Changing = isChanging;
  256. }
  257. // --------------------------------------------------------------------------
  258. void ctkSliderWidget::startChanging()
  259. {
  260. Q_D(ctkSliderWidget);
  261. if (d->Tracking)
  262. {
  263. return;
  264. }
  265. d->Changing = true;
  266. d->ValueBeforeChange = this->value();
  267. }
  268. // --------------------------------------------------------------------------
  269. void ctkSliderWidget::stopChanging()
  270. {
  271. Q_D(ctkSliderWidget);
  272. if (d->Tracking)
  273. {
  274. return;
  275. }
  276. d->Changing = false;
  277. if (qAbs(this->value() - d->ValueBeforeChange) > (this->singleStep() * 0.000000001))
  278. {
  279. emit this->valueChanged(this->value());
  280. }
  281. }
  282. // --------------------------------------------------------------------------
  283. void ctkSliderWidget::changeValue(double newValue)
  284. {
  285. Q_D(ctkSliderWidget);
  286. bool wasBlocked = d->SpinBox->blockSignals(true);
  287. d->SpinBox->setValue(newValue);
  288. d->SpinBox->blockSignals(wasBlocked);
  289. Q_ASSERT(d->equal(d->SpinBox->value(), d->Slider->value()));
  290. if (!d->Tracking)
  291. {
  292. emit this->valueIsChanging(newValue);
  293. }
  294. if (!d->Changing)
  295. {
  296. emit this->valueChanged(newValue);
  297. }
  298. }
  299. // --------------------------------------------------------------------------
  300. bool ctkSliderWidget::eventFilter(QObject *obj, QEvent *event)
  301. {
  302. if (event->type() == QEvent::MouseButtonPress)
  303. {
  304. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  305. if (mouseEvent->button() & Qt::LeftButton)
  306. {
  307. this->startChanging();
  308. }
  309. }
  310. else if (event->type() == QEvent::MouseButtonRelease)
  311. {
  312. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  313. if (mouseEvent->button() & Qt::LeftButton)
  314. {
  315. // here we might prevent ctkSliderWidget::stopChanging
  316. // from sending a valueChanged() event as the spinbox might
  317. // send a valueChanged() after eventFilter() is done.
  318. this->stopChanging();
  319. }
  320. }
  321. // standard event processing
  322. return this->Superclass::eventFilter(obj, event);
  323. }
  324. // --------------------------------------------------------------------------
  325. double ctkSliderWidget::singleStep()const
  326. {
  327. Q_D(const ctkSliderWidget);
  328. Q_ASSERT(d->equal(d->SpinBox->singleStep(), d->Slider->singleStep()));
  329. return d->Slider->singleStep();
  330. }
  331. // --------------------------------------------------------------------------
  332. void ctkSliderWidget::setSingleStep(double step)
  333. {
  334. Q_D(ctkSliderWidget);
  335. d->SpinBox->setSingleStep(step);
  336. d->Slider->setSingleStep(d->SpinBox->singleStep());
  337. Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
  338. Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value()));
  339. Q_ASSERT(d->equal(d->SpinBox->maximum(),d->Slider->maximum()));
  340. }
  341. // --------------------------------------------------------------------------
  342. double ctkSliderWidget::pageStep()const
  343. {
  344. Q_D(const ctkSliderWidget);
  345. return d->Slider->pageStep();
  346. }
  347. // --------------------------------------------------------------------------
  348. void ctkSliderWidget::setPageStep(double step)
  349. {
  350. Q_D(ctkSliderWidget);
  351. d->Slider->setPageStep(step);
  352. }
  353. // --------------------------------------------------------------------------
  354. int ctkSliderWidget::decimals()const
  355. {
  356. Q_D(const ctkSliderWidget);
  357. return d->SpinBox->decimals();
  358. }
  359. // --------------------------------------------------------------------------
  360. void ctkSliderWidget::setDecimals(int newDecimals)
  361. {
  362. Q_D(ctkSliderWidget);
  363. d->SpinBox->setDecimals(newDecimals);
  364. // The number of decimals can change the range values
  365. // i.e. 50.55 with 2 decimals -> 51 with 0 decimals
  366. // As the SpinBox range change doesn't fire signals,
  367. // we have to do the synchronization manually here
  368. d->Slider->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  369. Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
  370. Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value()));
  371. Q_ASSERT(d->equal(d->SpinBox->maximum(),d->Slider->maximum()));
  372. d->updateSpinBoxDecimals();
  373. }
  374. // --------------------------------------------------------------------------
  375. QString ctkSliderWidget::prefix()const
  376. {
  377. Q_D(const ctkSliderWidget);
  378. return d->SpinBox->prefix();
  379. }
  380. // --------------------------------------------------------------------------
  381. void ctkSliderWidget::setPrefix(const QString& newPrefix)
  382. {
  383. Q_D(ctkSliderWidget);
  384. d->SpinBox->setPrefix(newPrefix);
  385. d->updateSpinBoxWidth();
  386. }
  387. // --------------------------------------------------------------------------
  388. QString ctkSliderWidget::suffix()const
  389. {
  390. Q_D(const ctkSliderWidget);
  391. return d->SpinBox->suffix();
  392. }
  393. // --------------------------------------------------------------------------
  394. void ctkSliderWidget::setSuffix(const QString& newSuffix)
  395. {
  396. Q_D(ctkSliderWidget);
  397. d->SpinBox->setSuffix(newSuffix);
  398. d->updateSpinBoxWidth();
  399. }
  400. // --------------------------------------------------------------------------
  401. double ctkSliderWidget::tickInterval()const
  402. {
  403. Q_D(const ctkSliderWidget);
  404. return d->Slider->tickInterval();
  405. }
  406. // --------------------------------------------------------------------------
  407. void ctkSliderWidget::setTickInterval(double ti)
  408. {
  409. Q_D(ctkSliderWidget);
  410. d->Slider->setTickInterval(ti);
  411. }
  412. // --------------------------------------------------------------------------
  413. QSlider::TickPosition ctkSliderWidget::tickPosition()const
  414. {
  415. Q_D(const ctkSliderWidget);
  416. return d->Slider->tickPosition();
  417. }
  418. // --------------------------------------------------------------------------
  419. void ctkSliderWidget::setTickPosition(QSlider::TickPosition newTickPosition)
  420. {
  421. Q_D(ctkSliderWidget);
  422. d->Slider->setTickPosition(newTickPosition);
  423. }
  424. // -------------------------------------------------------------------------
  425. void ctkSliderWidget::reset()
  426. {
  427. this->setValue(0.);
  428. }
  429. // -------------------------------------------------------------------------
  430. void ctkSliderWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  431. {
  432. Q_D(ctkSliderWidget);
  433. return d->SpinBox->setAlignment(alignment);
  434. }
  435. // -------------------------------------------------------------------------
  436. Qt::Alignment ctkSliderWidget::spinBoxAlignment()const
  437. {
  438. Q_D(const ctkSliderWidget);
  439. return d->SpinBox->alignment();
  440. }
  441. // -------------------------------------------------------------------------
  442. void ctkSliderWidget::setTracking(bool enable)
  443. {
  444. Q_D(ctkSliderWidget);
  445. d->Tracking = enable;
  446. }
  447. // -------------------------------------------------------------------------
  448. bool ctkSliderWidget::hasTracking()const
  449. {
  450. Q_D(const ctkSliderWidget);
  451. return d->Tracking;
  452. }
  453. // -------------------------------------------------------------------------
  454. ctkSliderWidget::SynchronizeSiblings
  455. ctkSliderWidget::synchronizeSiblings() const
  456. {
  457. Q_D(const ctkSliderWidget);
  458. return d->SynchronizeMode;
  459. }
  460. // -------------------------------------------------------------------------
  461. void ctkSliderWidget
  462. ::setSynchronizeSiblings(ctkSliderWidget::SynchronizeSiblings flag)
  463. {
  464. Q_D(ctkSliderWidget);
  465. d->SynchronizeMode = flag;
  466. d->updateSpinBoxWidth();
  467. d->updateSpinBoxDecimals();
  468. }
  469. // -------------------------------------------------------------------------
  470. bool ctkSliderWidget::isSpinBoxVisible()const
  471. {
  472. Q_D(const ctkSliderWidget);
  473. return d->SpinBox->isVisibleTo(const_cast<ctkSliderWidget*>(this));
  474. }
  475. // -------------------------------------------------------------------------
  476. void ctkSliderWidget::setSpinBoxVisible(bool visible)
  477. {
  478. Q_D(ctkSliderWidget);
  479. d->SpinBox->setVisible(visible);
  480. }
  481. // --------------------------------------------------------------------------
  482. bool ctkSliderWidget::hasPopupSlider()const
  483. {
  484. Q_D(const ctkSliderWidget);
  485. return d->SliderPopup != 0;
  486. }
  487. // --------------------------------------------------------------------------
  488. void ctkSliderWidget::setPopupSlider(bool popup)
  489. {
  490. Q_D(ctkSliderWidget);
  491. if (this->hasPopupSlider() == popup)
  492. {
  493. return;
  494. }
  495. if (popup)
  496. {
  497. d->SliderPopup = new ctkPopupWidget(this);
  498. d->SliderPopup->setObjectName("DoubleSliderPopup");
  499. QHBoxLayout* layout = new QHBoxLayout(d->SliderPopup);
  500. layout->setContentsMargins(0,0,0,0);
  501. /// If the Slider has already been created, it will try to keep its
  502. /// size.
  503. layout->addWidget(d->Slider);
  504. d->SliderPopup->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  505. d->SliderPopup->setOrientation(Qt::Horizontal);
  506. d->SliderPopup->setHorizontalDirection(Qt::RightToLeft);
  507. }
  508. else
  509. {
  510. qobject_cast<QHBoxLayout*>(this->layout())->insertWidget(0,d->Slider);
  511. d->SliderPopup->deleteLater();
  512. d->SliderPopup = 0;
  513. }
  514. }
  515. // --------------------------------------------------------------------------
  516. ctkPopupWidget* ctkSliderWidget::popup()const
  517. {
  518. Q_D(const ctkSliderWidget);
  519. return d->SliderPopup;
  520. }
  521. // --------------------------------------------------------------------------
  522. ctkDoubleSpinBox* ctkSliderWidget::spinBox()
  523. {
  524. Q_D(ctkSliderWidget);
  525. return d->SpinBox;
  526. }
  527. // --------------------------------------------------------------------------
  528. ctkDoubleSlider* ctkSliderWidget::slider()
  529. {
  530. Q_D(ctkSliderWidget);
  531. return d->Slider;
  532. }