ctkSliderWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. if (!d->Tracking)
  244. {
  245. emit this->valueIsChanging(newValue);
  246. }
  247. if (!d->Changing)
  248. {
  249. emit this->valueChanged(newValue);
  250. }
  251. }
  252. // --------------------------------------------------------------------------
  253. bool ctkSliderWidget::eventFilter(QObject *obj, QEvent *event)
  254. {
  255. if (event->type() == QEvent::MouseButtonPress)
  256. {
  257. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  258. if (mouseEvent->button() & Qt::LeftButton)
  259. {
  260. this->startChanging();
  261. }
  262. }
  263. else if (event->type() == QEvent::MouseButtonRelease)
  264. {
  265. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  266. if (mouseEvent->button() & Qt::LeftButton)
  267. {
  268. // here we might prevent ctkSliderWidget::stopChanging
  269. // from sending a valueChanged() event as the spinbox might
  270. // send a valueChanged() after eventFilter() is done.
  271. this->stopChanging();
  272. }
  273. }
  274. // standard event processing
  275. return this->Superclass::eventFilter(obj, event);
  276. }
  277. // --------------------------------------------------------------------------
  278. double ctkSliderWidget::singleStep()const
  279. {
  280. Q_D(const ctkSliderWidget);
  281. Q_ASSERT(d->equal(d->SpinBox->singleStep(), d->Slider->singleStep()));
  282. return d->Slider->singleStep();
  283. }
  284. // --------------------------------------------------------------------------
  285. void ctkSliderWidget::setSingleStep(double step)
  286. {
  287. Q_D(ctkSliderWidget);
  288. d->SpinBox->setSingleStep(step);
  289. d->Slider->setSingleStep(d->SpinBox->singleStep());
  290. Q_ASSERT(d->equal(d->SpinBox->singleStep(), d->Slider->singleStep()));
  291. }
  292. // --------------------------------------------------------------------------
  293. double ctkSliderWidget::pageStep()const
  294. {
  295. Q_D(const ctkSliderWidget);
  296. return d->Slider->pageStep();
  297. }
  298. // --------------------------------------------------------------------------
  299. void ctkSliderWidget::setPageStep(double step)
  300. {
  301. Q_D(ctkSliderWidget);
  302. d->Slider->setPageStep(step);
  303. }
  304. // --------------------------------------------------------------------------
  305. int ctkSliderWidget::decimals()const
  306. {
  307. Q_D(const ctkSliderWidget);
  308. return d->SpinBox->decimals();
  309. }
  310. // --------------------------------------------------------------------------
  311. void ctkSliderWidget::setDecimals(int newDecimals)
  312. {
  313. Q_D(ctkSliderWidget);
  314. d->SpinBox->setDecimals(newDecimals);
  315. // The number of decimals can change the range values
  316. // i.e. 50.55 with 2 decimals -> 51 with 0 decimals
  317. // As the SpinBox range change doesn't fire signals,
  318. // we have to do the synchronization manually here
  319. d->Slider->setMinimum(d->SpinBox->minimum());
  320. d->Slider->setMaximum(d->SpinBox->maximum());
  321. }
  322. // --------------------------------------------------------------------------
  323. QString ctkSliderWidget::prefix()const
  324. {
  325. Q_D(const ctkSliderWidget);
  326. return d->SpinBox->prefix();
  327. }
  328. // --------------------------------------------------------------------------
  329. void ctkSliderWidget::setPrefix(const QString& newPrefix)
  330. {
  331. Q_D(ctkSliderWidget);
  332. d->SpinBox->setPrefix(newPrefix);
  333. #if QT_VERSION < 0x040800
  334. /// Setting the prefix doesn't recompute the sizehint, do it manually here:
  335. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  336. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  337. #endif
  338. d->updateSpinBoxWidth();
  339. }
  340. // --------------------------------------------------------------------------
  341. QString ctkSliderWidget::suffix()const
  342. {
  343. Q_D(const ctkSliderWidget);
  344. return d->SpinBox->suffix();
  345. }
  346. // --------------------------------------------------------------------------
  347. void ctkSliderWidget::setSuffix(const QString& newSuffix)
  348. {
  349. Q_D(ctkSliderWidget);
  350. d->SpinBox->setSuffix(newSuffix);
  351. #if QT_VERSION < 0x040800
  352. /// Setting the suffix doesn't recompute the sizehint, do it manually here:
  353. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  354. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  355. #endif
  356. d->updateSpinBoxWidth();
  357. }
  358. // --------------------------------------------------------------------------
  359. double ctkSliderWidget::tickInterval()const
  360. {
  361. Q_D(const ctkSliderWidget);
  362. return d->Slider->tickInterval();
  363. }
  364. // --------------------------------------------------------------------------
  365. void ctkSliderWidget::setTickInterval(double ti)
  366. {
  367. Q_D(ctkSliderWidget);
  368. d->Slider->setTickInterval(ti);
  369. }
  370. // -------------------------------------------------------------------------
  371. void ctkSliderWidget::reset()
  372. {
  373. this->setValue(0.);
  374. }
  375. // -------------------------------------------------------------------------
  376. void ctkSliderWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  377. {
  378. Q_D(ctkSliderWidget);
  379. return d->SpinBox->setAlignment(alignment);
  380. }
  381. // -------------------------------------------------------------------------
  382. Qt::Alignment ctkSliderWidget::spinBoxAlignment()const
  383. {
  384. Q_D(const ctkSliderWidget);
  385. return d->SpinBox->alignment();
  386. }
  387. // -------------------------------------------------------------------------
  388. void ctkSliderWidget::setTracking(bool enable)
  389. {
  390. Q_D(ctkSliderWidget);
  391. d->Tracking = enable;
  392. }
  393. // -------------------------------------------------------------------------
  394. bool ctkSliderWidget::hasTracking()const
  395. {
  396. Q_D(const ctkSliderWidget);
  397. return d->Tracking;
  398. }
  399. // -------------------------------------------------------------------------
  400. bool ctkSliderWidget::isAutoSpinBoxWidth()const
  401. {
  402. Q_D(const ctkSliderWidget);
  403. return d->AutoSpinBoxWidth;
  404. }
  405. // -------------------------------------------------------------------------
  406. void ctkSliderWidget::setAutoSpinBoxWidth(bool autoWidth)
  407. {
  408. Q_D(ctkSliderWidget);
  409. d->AutoSpinBoxWidth = autoWidth;
  410. d->updateSpinBoxWidth();
  411. }