ctkSliderWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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.commontk.org/LICENSE
  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. d->updateSpinBoxWidth();
  141. }
  142. // --------------------------------------------------------------------------
  143. void ctkSliderWidget::setMaximum(double max)
  144. {
  145. Q_D(ctkSliderWidget);
  146. bool wasBlocked = d->SpinBox->blockSignals(true);
  147. d->SpinBox->setMaximum(max);
  148. d->SpinBox->blockSignals(wasBlocked);
  149. // SpinBox can truncate max (depending on decimals).
  150. // use Spinbox's max to set Slider's max
  151. d->Slider->setMaximum(d->SpinBox->maximum());
  152. Q_ASSERT(d->equal(d->SpinBox->maximum(), d->Slider->maximum()));
  153. d->updateSpinBoxWidth();
  154. }
  155. // --------------------------------------------------------------------------
  156. void ctkSliderWidget::setRange(double min, double max)
  157. {
  158. Q_D(ctkSliderWidget);
  159. bool wasBlocked = d->SpinBox->blockSignals(true);
  160. d->SpinBox->setRange(min, max);
  161. d->SpinBox->blockSignals(wasBlocked);
  162. // SpinBox can truncate the range (depending on decimals).
  163. // use Spinbox's range to set Slider's range
  164. d->Slider->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  165. Q_ASSERT(d->equal(d->SpinBox->minimum(), d->Slider->minimum()));
  166. Q_ASSERT(d->equal(d->SpinBox->maximum(), d->Slider->maximum()));
  167. d->updateSpinBoxWidth();
  168. }
  169. /*
  170. // --------------------------------------------------------------------------
  171. double ctkSliderWidget::sliderPosition()const
  172. {
  173. return d->Slider->sliderPosition();
  174. }
  175. // --------------------------------------------------------------------------
  176. void ctkSliderWidget::setSliderPosition(double position)
  177. {
  178. d->Slider->setSliderPosition(position);
  179. }
  180. */
  181. /*
  182. // --------------------------------------------------------------------------
  183. double ctkSliderWidget::previousSliderPosition()
  184. {
  185. return d->Slider->previousSliderPosition();
  186. }
  187. */
  188. // --------------------------------------------------------------------------
  189. double ctkSliderWidget::value()const
  190. {
  191. Q_D(const ctkSliderWidget);
  192. Q_ASSERT(d->equal(d->SpinBox->value(), d->Slider->value()));
  193. return d->Changing ? d->ValueBeforeChange : d->Slider->value();
  194. }
  195. // --------------------------------------------------------------------------
  196. void ctkSliderWidget::setValue(double _value)
  197. {
  198. Q_D(ctkSliderWidget);
  199. // disable the tracking temporally to emit the
  200. // signal valueChanged if changeValue() is called
  201. bool isChanging = d->Changing;
  202. d->Changing = false;
  203. d->SpinBox->setValue(_value);
  204. // Why do we need to set the value to the slider ?
  205. //d->Slider->setValue(d->SpinBox->value());
  206. //double spinBoxValue = d->SpinBox->value();
  207. Q_ASSERT(d->equal(d->SpinBox->value(), d->Slider->value()));
  208. // restore the prop
  209. d->Changing = isChanging;
  210. }
  211. // --------------------------------------------------------------------------
  212. void ctkSliderWidget::startChanging()
  213. {
  214. Q_D(ctkSliderWidget);
  215. if (d->Tracking)
  216. {
  217. return;
  218. }
  219. d->Changing = true;
  220. d->ValueBeforeChange = this->value();
  221. }
  222. // --------------------------------------------------------------------------
  223. void ctkSliderWidget::stopChanging()
  224. {
  225. Q_D(ctkSliderWidget);
  226. if (d->Tracking)
  227. {
  228. return;
  229. }
  230. d->Changing = false;
  231. if (qAbs(this->value() - d->ValueBeforeChange) > (this->singleStep() * 0.000000001))
  232. {
  233. emit this->valueChanged(this->value());
  234. }
  235. }
  236. // --------------------------------------------------------------------------
  237. void ctkSliderWidget::changeValue(double newValue)
  238. {
  239. Q_D(ctkSliderWidget);
  240. bool wasBlocked = d->SpinBox->blockSignals(true);
  241. d->SpinBox->setValue(newValue);
  242. d->SpinBox->blockSignals(wasBlocked);
  243. Q_ASSERT(d->equal(d->SpinBox->value(), d->Slider->value()));
  244. if (!d->Tracking)
  245. {
  246. emit this->valueIsChanging(newValue);
  247. }
  248. if (!d->Changing)
  249. {
  250. emit this->valueChanged(newValue);
  251. }
  252. }
  253. // --------------------------------------------------------------------------
  254. bool ctkSliderWidget::eventFilter(QObject *obj, QEvent *event)
  255. {
  256. if (event->type() == QEvent::MouseButtonPress)
  257. {
  258. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  259. if (mouseEvent->button() & Qt::LeftButton)
  260. {
  261. this->startChanging();
  262. }
  263. }
  264. else if (event->type() == QEvent::MouseButtonRelease)
  265. {
  266. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  267. if (mouseEvent->button() & Qt::LeftButton)
  268. {
  269. // here we might prevent ctkSliderWidget::stopChanging
  270. // from sending a valueChanged() event as the spinbox might
  271. // send a valueChanged() after eventFilter() is done.
  272. this->stopChanging();
  273. }
  274. }
  275. // standard event processing
  276. return this->Superclass::eventFilter(obj, event);
  277. }
  278. // --------------------------------------------------------------------------
  279. double ctkSliderWidget::singleStep()const
  280. {
  281. Q_D(const ctkSliderWidget);
  282. Q_ASSERT(d->equal(d->SpinBox->singleStep(), d->Slider->singleStep()));
  283. return d->Slider->singleStep();
  284. }
  285. // --------------------------------------------------------------------------
  286. void ctkSliderWidget::setSingleStep(double step)
  287. {
  288. Q_D(ctkSliderWidget);
  289. d->SpinBox->setSingleStep(step);
  290. d->Slider->setSingleStep(d->SpinBox->singleStep());
  291. Q_ASSERT(d->equal(d->SpinBox->singleStep(), d->Slider->singleStep()));
  292. }
  293. // --------------------------------------------------------------------------
  294. double ctkSliderWidget::pageStep()const
  295. {
  296. Q_D(const ctkSliderWidget);
  297. return d->Slider->pageStep();
  298. }
  299. // --------------------------------------------------------------------------
  300. void ctkSliderWidget::setPageStep(double step)
  301. {
  302. Q_D(ctkSliderWidget);
  303. d->Slider->setPageStep(step);
  304. }
  305. // --------------------------------------------------------------------------
  306. int ctkSliderWidget::decimals()const
  307. {
  308. Q_D(const ctkSliderWidget);
  309. return d->SpinBox->decimals();
  310. }
  311. // --------------------------------------------------------------------------
  312. void ctkSliderWidget::setDecimals(int newDecimals)
  313. {
  314. Q_D(ctkSliderWidget);
  315. d->SpinBox->setDecimals(newDecimals);
  316. // The number of decimals can change the range values
  317. // i.e. 50.55 with 2 decimals -> 51 with 0 decimals
  318. // As the SpinBox range change doesn't fire signals,
  319. // we have to do the synchronization manually here
  320. d->Slider->setMinimum(d->SpinBox->minimum());
  321. d->Slider->setMaximum(d->SpinBox->maximum());
  322. }
  323. // --------------------------------------------------------------------------
  324. QString ctkSliderWidget::prefix()const
  325. {
  326. Q_D(const ctkSliderWidget);
  327. return d->SpinBox->prefix();
  328. }
  329. // --------------------------------------------------------------------------
  330. void ctkSliderWidget::setPrefix(const QString& newPrefix)
  331. {
  332. Q_D(ctkSliderWidget);
  333. d->SpinBox->setPrefix(newPrefix);
  334. #if QT_VERSION < 0x040800
  335. /// Setting the prefix doesn't recompute the sizehint, do it manually here:
  336. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  337. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  338. #endif
  339. d->updateSpinBoxWidth();
  340. }
  341. // --------------------------------------------------------------------------
  342. QString ctkSliderWidget::suffix()const
  343. {
  344. Q_D(const ctkSliderWidget);
  345. return d->SpinBox->suffix();
  346. }
  347. // --------------------------------------------------------------------------
  348. void ctkSliderWidget::setSuffix(const QString& newSuffix)
  349. {
  350. Q_D(ctkSliderWidget);
  351. d->SpinBox->setSuffix(newSuffix);
  352. #if QT_VERSION < 0x040800
  353. /// Setting the suffix doesn't recompute the sizehint, do it manually here:
  354. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  355. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  356. #endif
  357. d->updateSpinBoxWidth();
  358. }
  359. // --------------------------------------------------------------------------
  360. double ctkSliderWidget::tickInterval()const
  361. {
  362. Q_D(const ctkSliderWidget);
  363. return d->Slider->tickInterval();
  364. }
  365. // --------------------------------------------------------------------------
  366. void ctkSliderWidget::setTickInterval(double ti)
  367. {
  368. Q_D(ctkSliderWidget);
  369. d->Slider->setTickInterval(ti);
  370. }
  371. // -------------------------------------------------------------------------
  372. void ctkSliderWidget::reset()
  373. {
  374. this->setValue(0.);
  375. }
  376. // -------------------------------------------------------------------------
  377. void ctkSliderWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  378. {
  379. Q_D(ctkSliderWidget);
  380. return d->SpinBox->setAlignment(alignment);
  381. }
  382. // -------------------------------------------------------------------------
  383. Qt::Alignment ctkSliderWidget::spinBoxAlignment()const
  384. {
  385. Q_D(const ctkSliderWidget);
  386. return d->SpinBox->alignment();
  387. }
  388. // -------------------------------------------------------------------------
  389. void ctkSliderWidget::setTracking(bool enable)
  390. {
  391. Q_D(ctkSliderWidget);
  392. d->Tracking = enable;
  393. }
  394. // -------------------------------------------------------------------------
  395. bool ctkSliderWidget::hasTracking()const
  396. {
  397. Q_D(const ctkSliderWidget);
  398. return d->Tracking;
  399. }
  400. // -------------------------------------------------------------------------
  401. bool ctkSliderWidget::isAutoSpinBoxWidth()const
  402. {
  403. Q_D(const ctkSliderWidget);
  404. return d->AutoSpinBoxWidth;
  405. }
  406. // -------------------------------------------------------------------------
  407. void ctkSliderWidget::setAutoSpinBoxWidth(bool autoWidth)
  408. {
  409. Q_D(ctkSliderWidget);
  410. d->AutoSpinBoxWidth = autoWidth;
  411. d->updateSpinBoxWidth();
  412. }