ctkSliderWidget.cpp 14 KB

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