ctkSliderWidget.cpp 15 KB

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