ctkSliderWidget.cpp 20 KB

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