ctkSliderWidget.cpp 13 KB

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