ctkSliderSpinBoxWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QDebug>
  12. #include <QMouseEvent>
  13. // CTK includes
  14. #include "ctkSliderSpinBoxWidget.h"
  15. #include "ui_ctkSliderSpinBoxWidget.h"
  16. //-----------------------------------------------------------------------------
  17. namespace
  18. {
  19. bool equal(double v1, double v2)
  20. {
  21. return qAbs(v1 - v2) < 0.0001;
  22. }
  23. }
  24. //-----------------------------------------------------------------------------
  25. class ctkSliderSpinBoxWidgetPrivate: public ctkPrivate<ctkSliderSpinBoxWidget>,
  26. public Ui_ctkSliderSpinBoxWidget
  27. {
  28. public:
  29. ctkSliderSpinBoxWidgetPrivate();
  30. void updateSpinBoxWidth();
  31. int synchronizedSpinBoxWidth()const;
  32. void synchronizeSiblingSpinBox(int newWidth);
  33. bool Tracking;
  34. bool Changing;
  35. double ValueBeforeChange;
  36. bool AutoSpinBoxWidth;
  37. };
  38. // --------------------------------------------------------------------------
  39. ctkSliderSpinBoxWidgetPrivate::ctkSliderSpinBoxWidgetPrivate()
  40. {
  41. this->Tracking = true;
  42. this->Changing = false;
  43. this->ValueBeforeChange = 0.;
  44. this->AutoSpinBoxWidth = true;
  45. }
  46. // --------------------------------------------------------------------------
  47. void ctkSliderSpinBoxWidgetPrivate::updateSpinBoxWidth()
  48. {
  49. int spinBoxWidth = this->synchronizedSpinBoxWidth();
  50. if (this->AutoSpinBoxWidth)
  51. {
  52. this->SpinBox->setMinimumWidth(spinBoxWidth);
  53. }
  54. else
  55. {
  56. this->SpinBox->setMinimumWidth(0);
  57. }
  58. this->synchronizeSiblingSpinBox(spinBoxWidth);
  59. }
  60. // --------------------------------------------------------------------------
  61. int ctkSliderSpinBoxWidgetPrivate::synchronizedSpinBoxWidth()const
  62. {
  63. CTK_P(const ctkSliderSpinBoxWidget);
  64. int maxWidth = this->SpinBox->sizeHint().width();
  65. if (!p->parent())
  66. {
  67. return maxWidth;
  68. }
  69. QList<ctkSliderSpinBoxWidget*> siblings =
  70. p->parent()->findChildren<ctkSliderSpinBoxWidget*>();
  71. foreach(ctkSliderSpinBoxWidget* sibling, siblings)
  72. {
  73. maxWidth = qMax(maxWidth, sibling->ctk_d()->SpinBox->sizeHint().width());
  74. }
  75. return maxWidth;
  76. }
  77. // --------------------------------------------------------------------------
  78. void ctkSliderSpinBoxWidgetPrivate::synchronizeSiblingSpinBox(int width)
  79. {
  80. CTK_P(const ctkSliderSpinBoxWidget);
  81. QList<ctkSliderSpinBoxWidget*> siblings =
  82. p->parent()->findChildren<ctkSliderSpinBoxWidget*>();
  83. foreach(ctkSliderSpinBoxWidget* sibling, siblings)
  84. {
  85. if (sibling != p && sibling->isAutoSpinBoxWidth())
  86. {
  87. sibling->ctk_d()->SpinBox->setMinimumWidth(width);
  88. }
  89. }
  90. }
  91. // --------------------------------------------------------------------------
  92. ctkSliderSpinBoxWidget::ctkSliderSpinBoxWidget(QWidget* _parent) : Superclass(_parent)
  93. {
  94. CTK_INIT_PRIVATE(ctkSliderSpinBoxWidget);
  95. CTK_D(ctkSliderSpinBoxWidget);
  96. d->setupUi(this);
  97. d->Slider->setMaximum(d->SpinBox->maximum());
  98. d->Slider->setMinimum(d->SpinBox->minimum());
  99. this->connect(d->Slider, SIGNAL(valueChanged(double)), d->SpinBox, SLOT(setValue(double)));
  100. this->connect(d->SpinBox, SIGNAL(valueChanged(double)), d->Slider, SLOT(setValue(double)));
  101. //this->connect(d->Slider, SIGNAL(valueChanged(double)), SIGNAL(valueChanged(double)));
  102. this->connect(d->Slider, SIGNAL(sliderPressed()), this, SLOT(startChanging()));
  103. this->connect(d->Slider, SIGNAL(sliderReleased()), this, SLOT(stopChanging()));
  104. this->connect(d->Slider, SIGNAL(valueChanged(double)), this, SLOT(changeValue(double)));
  105. d->SpinBox->installEventFilter(this);
  106. }
  107. // --------------------------------------------------------------------------
  108. double ctkSliderSpinBoxWidget::minimum()const
  109. {
  110. CTK_D(const ctkSliderSpinBoxWidget);
  111. Q_ASSERT(equal(d->SpinBox->minimum(),d->Slider->minimum()));
  112. return d->Slider->minimum();
  113. }
  114. // --------------------------------------------------------------------------
  115. double ctkSliderSpinBoxWidget::maximum()const
  116. {
  117. CTK_D(const ctkSliderSpinBoxWidget);
  118. Q_ASSERT(equal(d->SpinBox->maximum(),d->Slider->maximum()));
  119. return d->Slider->maximum();
  120. }
  121. // --------------------------------------------------------------------------
  122. void ctkSliderSpinBoxWidget::setMinimum(double min)
  123. {
  124. CTK_D(ctkSliderSpinBoxWidget);
  125. d->SpinBox->setMinimum(min);
  126. // SpinBox can truncate min (depending on decimals).
  127. // use Spinbox's min to set Slider's min
  128. d->Slider->setMinimum(d->SpinBox->minimum());
  129. Q_ASSERT(equal(d->SpinBox->minimum(),d->Slider->minimum()));
  130. d->updateSpinBoxWidth();
  131. }
  132. // --------------------------------------------------------------------------
  133. void ctkSliderSpinBoxWidget::setMaximum(double max)
  134. {
  135. CTK_D(ctkSliderSpinBoxWidget);
  136. d->SpinBox->setMaximum(max);
  137. // SpinBox can truncate max (depending on decimals).
  138. // use Spinbox's max to set Slider's max
  139. d->Slider->setMaximum(d->SpinBox->maximum());
  140. Q_ASSERT(equal(d->SpinBox->maximum(), d->Slider->maximum()));
  141. d->updateSpinBoxWidth();
  142. }
  143. // --------------------------------------------------------------------------
  144. void ctkSliderSpinBoxWidget::setRange(double min, double max)
  145. {
  146. CTK_D(ctkSliderSpinBoxWidget);
  147. d->SpinBox->setRange(min, max);
  148. // SpinBox can truncate the range (depending on decimals).
  149. // use Spinbox's range to set Slider's range
  150. d->Slider->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  151. Q_ASSERT(equal(d->SpinBox->minimum(), d->Slider->minimum()));
  152. Q_ASSERT(equal(d->SpinBox->maximum(), d->Slider->maximum()));
  153. d->updateSpinBoxWidth();
  154. }
  155. /*
  156. // --------------------------------------------------------------------------
  157. double ctkSliderSpinBoxWidget::sliderPosition()const
  158. {
  159. return ctk_d()->Slider->sliderPosition();
  160. }
  161. // --------------------------------------------------------------------------
  162. void ctkSliderSpinBoxWidget::setSliderPosition(double position)
  163. {
  164. ctk_d()->Slider->setSliderPosition(position);
  165. }
  166. */
  167. /*
  168. // --------------------------------------------------------------------------
  169. double ctkSliderSpinBoxWidget::previousSliderPosition()
  170. {
  171. return ctk_d()->Slider->previousSliderPosition();
  172. }
  173. */
  174. // --------------------------------------------------------------------------
  175. double ctkSliderSpinBoxWidget::value()const
  176. {
  177. CTK_D(const ctkSliderSpinBoxWidget);
  178. Q_ASSERT(equal(d->Slider->value(), d->SpinBox->value()));
  179. return d->Changing ? d->ValueBeforeChange : d->Slider->value();
  180. }
  181. // --------------------------------------------------------------------------
  182. void ctkSliderSpinBoxWidget::setValue(double _value)
  183. {
  184. CTK_D(ctkSliderSpinBoxWidget);
  185. // disable the tracking temporally to emit the
  186. // signal valueChanged if changeValue() is called
  187. bool isChanging = d->Changing;
  188. d->Changing = false;
  189. d->SpinBox->setValue(_value);
  190. // Why do we need to set the value to the slider ?
  191. //d->Slider->setValue(d->SpinBox->value());
  192. Q_ASSERT(equal(d->Slider->value(), d->SpinBox->value()));
  193. // restore the prop
  194. d->Changing = isChanging;
  195. }
  196. // --------------------------------------------------------------------------
  197. void ctkSliderSpinBoxWidget::startChanging()
  198. {
  199. CTK_D(ctkSliderSpinBoxWidget);
  200. if (d->Tracking)
  201. {
  202. return;
  203. }
  204. d->Changing = true;
  205. d->ValueBeforeChange = this->value();
  206. }
  207. // --------------------------------------------------------------------------
  208. void ctkSliderSpinBoxWidget::stopChanging()
  209. {
  210. CTK_D(ctkSliderSpinBoxWidget);
  211. if (d->Tracking)
  212. {
  213. return;
  214. }
  215. d->Changing = false;
  216. if (qAbs(this->value() - d->ValueBeforeChange) > (this->singleStep() * 0.000000001))
  217. {
  218. emit this->valueChanged(this->value());
  219. }
  220. }
  221. // --------------------------------------------------------------------------
  222. void ctkSliderSpinBoxWidget::changeValue(double newValue)
  223. {
  224. CTK_D(ctkSliderSpinBoxWidget);
  225. //if (d->Tracking)
  226. {
  227. emit this->valueIsChanging(newValue);
  228. }
  229. if (!d->Changing)
  230. {
  231. emit this->valueChanged(newValue);
  232. }
  233. }
  234. // --------------------------------------------------------------------------
  235. bool ctkSliderSpinBoxWidget::eventFilter(QObject *obj, QEvent *event)
  236. {
  237. if (event->type() == QEvent::MouseButtonPress)
  238. {
  239. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  240. if (mouseEvent->button() & Qt::LeftButton)
  241. {
  242. this->startChanging();
  243. }
  244. }
  245. else if (event->type() == QEvent::MouseButtonRelease)
  246. {
  247. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  248. if (mouseEvent->button() & Qt::LeftButton)
  249. {
  250. // here we might prevent ctkSliderSpinBoxWidget::stopChanging
  251. // from sending a valueChanged() event as the spinbox might
  252. // send a valueChanged() after eventFilter() is done.
  253. this->stopChanging();
  254. }
  255. }
  256. // standard event processing
  257. return this->Superclass::eventFilter(obj, event);
  258. }
  259. // --------------------------------------------------------------------------
  260. double ctkSliderSpinBoxWidget::singleStep()const
  261. {
  262. CTK_D(const ctkSliderSpinBoxWidget);
  263. Q_ASSERT(equal(d->Slider->singleStep(), d->SpinBox->singleStep()));
  264. return d->Slider->singleStep();
  265. }
  266. // --------------------------------------------------------------------------
  267. void ctkSliderSpinBoxWidget::setSingleStep(double step)
  268. {
  269. CTK_D(ctkSliderSpinBoxWidget);
  270. d->SpinBox->setSingleStep(step);
  271. d->Slider->setSingleStep(d->SpinBox->singleStep());
  272. Q_ASSERT(equal(d->Slider->singleStep(), d->SpinBox->singleStep()));
  273. }
  274. // --------------------------------------------------------------------------
  275. int ctkSliderSpinBoxWidget::decimals()const
  276. {
  277. CTK_D(const ctkSliderSpinBoxWidget);
  278. return d->SpinBox->decimals();
  279. }
  280. // --------------------------------------------------------------------------
  281. void ctkSliderSpinBoxWidget::setDecimals(int newDecimals)
  282. {
  283. CTK_D(ctkSliderSpinBoxWidget);
  284. d->SpinBox->setDecimals(newDecimals);
  285. }
  286. // --------------------------------------------------------------------------
  287. QString ctkSliderSpinBoxWidget::prefix()const
  288. {
  289. CTK_D(const ctkSliderSpinBoxWidget);
  290. return d->SpinBox->prefix();
  291. }
  292. // --------------------------------------------------------------------------
  293. void ctkSliderSpinBoxWidget::setPrefix(const QString& newPrefix)
  294. {
  295. CTK_D(ctkSliderSpinBoxWidget);
  296. d->SpinBox->setPrefix(newPrefix);
  297. }
  298. // --------------------------------------------------------------------------
  299. QString ctkSliderSpinBoxWidget::suffix()const
  300. {
  301. CTK_D(const ctkSliderSpinBoxWidget);
  302. return d->SpinBox->suffix();
  303. }
  304. // --------------------------------------------------------------------------
  305. void ctkSliderSpinBoxWidget::setSuffix(const QString& newSuffix)
  306. {
  307. CTK_D(ctkSliderSpinBoxWidget);
  308. d->SpinBox->setSuffix(newSuffix);
  309. }
  310. // --------------------------------------------------------------------------
  311. double ctkSliderSpinBoxWidget::tickInterval()const
  312. {
  313. CTK_D(const ctkSliderSpinBoxWidget);
  314. return d->Slider->tickInterval();
  315. }
  316. // --------------------------------------------------------------------------
  317. void ctkSliderSpinBoxWidget::setTickInterval(double ti)
  318. {
  319. CTK_D(ctkSliderSpinBoxWidget);
  320. d->Slider->setTickInterval(ti);
  321. }
  322. // -------------------------------------------------------------------------
  323. void ctkSliderSpinBoxWidget::reset()
  324. {
  325. this->setValue(0.);
  326. }
  327. // -------------------------------------------------------------------------
  328. void ctkSliderSpinBoxWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  329. {
  330. return ctk_d()->SpinBox->setAlignment(alignment);
  331. }
  332. // -------------------------------------------------------------------------
  333. Qt::Alignment ctkSliderSpinBoxWidget::spinBoxAlignment()const
  334. {
  335. return ctk_d()->SpinBox->alignment();
  336. }
  337. // -------------------------------------------------------------------------
  338. void ctkSliderSpinBoxWidget::setTracking(bool enable)
  339. {
  340. CTK_D(ctkSliderSpinBoxWidget);
  341. d->Tracking = enable;
  342. }
  343. // -------------------------------------------------------------------------
  344. bool ctkSliderSpinBoxWidget::hasTracking()const
  345. {
  346. CTK_D(const ctkSliderSpinBoxWidget);
  347. return d->Tracking;
  348. }
  349. // -------------------------------------------------------------------------
  350. bool ctkSliderSpinBoxWidget::isAutoSpinBoxWidth()const
  351. {
  352. CTK_D(const ctkSliderSpinBoxWidget);
  353. return d->AutoSpinBoxWidth;
  354. }
  355. // -------------------------------------------------------------------------
  356. void ctkSliderSpinBoxWidget::setAutoSpinBoxWidth(bool autoWidth)
  357. {
  358. CTK_D(ctkSliderSpinBoxWidget);
  359. d->AutoSpinBoxWidth = autoWidth;
  360. d->updateSpinBoxWidth();
  361. }