ctkSliderWidget.cpp 20 KB

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