ctkSliderWidget.cpp 23 KB

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