ctkSliderWidget.cpp 14 KB

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