ctkSliderSpinBoxWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "ctkSliderSpinBoxWidget.h"
  19. #include "ui_ctkSliderSpinBoxWidget.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 ctkSliderSpinBoxWidgetPrivate: public ctkPrivate<ctkSliderSpinBoxWidget>,
  30. public Ui_ctkSliderSpinBoxWidget
  31. {
  32. public:
  33. ctkSliderSpinBoxWidgetPrivate();
  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. ctkSliderSpinBoxWidgetPrivate::ctkSliderSpinBoxWidgetPrivate()
  44. {
  45. this->Tracking = true;
  46. this->Changing = false;
  47. this->ValueBeforeChange = 0.;
  48. this->AutoSpinBoxWidth = true;
  49. }
  50. // --------------------------------------------------------------------------
  51. void ctkSliderSpinBoxWidgetPrivate::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 ctkSliderSpinBoxWidgetPrivate::synchronizedSpinBoxWidth()const
  66. {
  67. CTK_P(const ctkSliderSpinBoxWidget);
  68. int maxWidth = this->SpinBox->sizeHint().width();
  69. if (!p->parent())
  70. {
  71. return maxWidth;
  72. }
  73. QList<ctkSliderSpinBoxWidget*> siblings =
  74. p->parent()->findChildren<ctkSliderSpinBoxWidget*>();
  75. foreach(ctkSliderSpinBoxWidget* sibling, siblings)
  76. {
  77. maxWidth = qMax(maxWidth, sibling->ctk_d()->SpinBox->sizeHint().width());
  78. }
  79. return maxWidth;
  80. }
  81. // --------------------------------------------------------------------------
  82. void ctkSliderSpinBoxWidgetPrivate::synchronizeSiblingSpinBox(int width)
  83. {
  84. CTK_P(const ctkSliderSpinBoxWidget);
  85. QList<ctkSliderSpinBoxWidget*> siblings =
  86. p->parent()->findChildren<ctkSliderSpinBoxWidget*>();
  87. foreach(ctkSliderSpinBoxWidget* sibling, siblings)
  88. {
  89. if (sibling != p && sibling->isAutoSpinBoxWidth())
  90. {
  91. sibling->ctk_d()->SpinBox->setMinimumWidth(width);
  92. }
  93. }
  94. }
  95. // --------------------------------------------------------------------------
  96. ctkSliderSpinBoxWidget::ctkSliderSpinBoxWidget(QWidget* _parent) : Superclass(_parent)
  97. {
  98. CTK_INIT_PRIVATE(ctkSliderSpinBoxWidget);
  99. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::minimum()const
  113. {
  114. CTK_D(const ctkSliderSpinBoxWidget);
  115. Q_ASSERT(equal(d->SpinBox->minimum(),d->Slider->minimum()));
  116. return d->Slider->minimum();
  117. }
  118. // --------------------------------------------------------------------------
  119. double ctkSliderSpinBoxWidget::maximum()const
  120. {
  121. CTK_D(const ctkSliderSpinBoxWidget);
  122. Q_ASSERT(equal(d->SpinBox->maximum(),d->Slider->maximum()));
  123. return d->Slider->maximum();
  124. }
  125. // --------------------------------------------------------------------------
  126. void ctkSliderSpinBoxWidget::setMinimum(double min)
  127. {
  128. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::setMaximum(double max)
  138. {
  139. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::setRange(double min, double max)
  149. {
  150. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::sliderPosition()const
  162. {
  163. return ctk_d()->Slider->sliderPosition();
  164. }
  165. // --------------------------------------------------------------------------
  166. void ctkSliderSpinBoxWidget::setSliderPosition(double position)
  167. {
  168. ctk_d()->Slider->setSliderPosition(position);
  169. }
  170. */
  171. /*
  172. // --------------------------------------------------------------------------
  173. double ctkSliderSpinBoxWidget::previousSliderPosition()
  174. {
  175. return ctk_d()->Slider->previousSliderPosition();
  176. }
  177. */
  178. // --------------------------------------------------------------------------
  179. double ctkSliderSpinBoxWidget::value()const
  180. {
  181. CTK_D(const ctkSliderSpinBoxWidget);
  182. Q_ASSERT(equal(d->Slider->value(), d->SpinBox->value()));
  183. return d->Changing ? d->ValueBeforeChange : d->Slider->value();
  184. }
  185. // --------------------------------------------------------------------------
  186. void ctkSliderSpinBoxWidget::setValue(double _value)
  187. {
  188. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::startChanging()
  202. {
  203. CTK_D(ctkSliderSpinBoxWidget);
  204. if (d->Tracking)
  205. {
  206. return;
  207. }
  208. d->Changing = true;
  209. d->ValueBeforeChange = this->value();
  210. }
  211. // --------------------------------------------------------------------------
  212. void ctkSliderSpinBoxWidget::stopChanging()
  213. {
  214. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::changeValue(double newValue)
  227. {
  228. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::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 ctkSliderSpinBoxWidget::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 ctkSliderSpinBoxWidget::singleStep()const
  265. {
  266. CTK_D(const ctkSliderSpinBoxWidget);
  267. Q_ASSERT(equal(d->Slider->singleStep(), d->SpinBox->singleStep()));
  268. return d->Slider->singleStep();
  269. }
  270. // --------------------------------------------------------------------------
  271. void ctkSliderSpinBoxWidget::setSingleStep(double step)
  272. {
  273. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::decimals()const
  280. {
  281. CTK_D(const ctkSliderSpinBoxWidget);
  282. return d->SpinBox->decimals();
  283. }
  284. // --------------------------------------------------------------------------
  285. void ctkSliderSpinBoxWidget::setDecimals(int newDecimals)
  286. {
  287. CTK_D(ctkSliderSpinBoxWidget);
  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 ctkSliderSpinBoxWidget::prefix()const
  298. {
  299. CTK_D(const ctkSliderSpinBoxWidget);
  300. return d->SpinBox->prefix();
  301. }
  302. // --------------------------------------------------------------------------
  303. void ctkSliderSpinBoxWidget::setPrefix(const QString& newPrefix)
  304. {
  305. CTK_D(ctkSliderSpinBoxWidget);
  306. d->SpinBox->setPrefix(newPrefix);
  307. }
  308. // --------------------------------------------------------------------------
  309. QString ctkSliderSpinBoxWidget::suffix()const
  310. {
  311. CTK_D(const ctkSliderSpinBoxWidget);
  312. return d->SpinBox->suffix();
  313. }
  314. // --------------------------------------------------------------------------
  315. void ctkSliderSpinBoxWidget::setSuffix(const QString& newSuffix)
  316. {
  317. CTK_D(ctkSliderSpinBoxWidget);
  318. d->SpinBox->setSuffix(newSuffix);
  319. }
  320. // --------------------------------------------------------------------------
  321. double ctkSliderSpinBoxWidget::tickInterval()const
  322. {
  323. CTK_D(const ctkSliderSpinBoxWidget);
  324. return d->Slider->tickInterval();
  325. }
  326. // --------------------------------------------------------------------------
  327. void ctkSliderSpinBoxWidget::setTickInterval(double ti)
  328. {
  329. CTK_D(ctkSliderSpinBoxWidget);
  330. d->Slider->setTickInterval(ti);
  331. }
  332. // -------------------------------------------------------------------------
  333. void ctkSliderSpinBoxWidget::reset()
  334. {
  335. this->setValue(0.);
  336. }
  337. // -------------------------------------------------------------------------
  338. void ctkSliderSpinBoxWidget::setSpinBoxAlignment(Qt::Alignment alignment)
  339. {
  340. return ctk_d()->SpinBox->setAlignment(alignment);
  341. }
  342. // -------------------------------------------------------------------------
  343. Qt::Alignment ctkSliderSpinBoxWidget::spinBoxAlignment()const
  344. {
  345. return ctk_d()->SpinBox->alignment();
  346. }
  347. // -------------------------------------------------------------------------
  348. void ctkSliderSpinBoxWidget::setTracking(bool enable)
  349. {
  350. CTK_D(ctkSliderSpinBoxWidget);
  351. d->Tracking = enable;
  352. }
  353. // -------------------------------------------------------------------------
  354. bool ctkSliderSpinBoxWidget::hasTracking()const
  355. {
  356. CTK_D(const ctkSliderSpinBoxWidget);
  357. return d->Tracking;
  358. }
  359. // -------------------------------------------------------------------------
  360. bool ctkSliderSpinBoxWidget::isAutoSpinBoxWidth()const
  361. {
  362. CTK_D(const ctkSliderSpinBoxWidget);
  363. return d->AutoSpinBoxWidth;
  364. }
  365. // -------------------------------------------------------------------------
  366. void ctkSliderSpinBoxWidget::setAutoSpinBoxWidth(bool autoWidth)
  367. {
  368. CTK_D(ctkSliderSpinBoxWidget);
  369. d->AutoSpinBoxWidth = autoWidth;
  370. d->updateSpinBoxWidth();
  371. }