ctkSliderWidget.cpp 23 KB

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