ctkSliderWidget.cpp 17 KB

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