ctkSliderWidget.cpp 17 KB

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