ctkDoubleSlider.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <QHBoxLayout>
  17. // CTK includes
  18. #include "ctkDoubleSlider.h"
  19. // STD includes
  20. #include <limits>
  21. //-----------------------------------------------------------------------------
  22. class ctkDoubleSliderPrivate: public ctkPrivate<ctkDoubleSlider>
  23. {
  24. public:
  25. ctkDoubleSliderPrivate();
  26. int toInt(double _value)const;
  27. double fromInt(int _value)const;
  28. void init();
  29. void updateOffset(double value);
  30. QSlider* Slider;
  31. double Minimum;
  32. double Maximum;
  33. // we should have a Offset and SliderPositionOffset (and MinimumOffset?)
  34. double Offset;
  35. double SingleStep;
  36. double Value;
  37. };
  38. // --------------------------------------------------------------------------
  39. ctkDoubleSliderPrivate::ctkDoubleSliderPrivate()
  40. {
  41. this->Slider = 0;
  42. this->Minimum = 0.;
  43. this->Maximum = 100.;
  44. this->Offset = 0.;
  45. this->SingleStep = 1.;
  46. this->Value = 0.;
  47. }
  48. // --------------------------------------------------------------------------
  49. void ctkDoubleSliderPrivate::init()
  50. {
  51. CTK_P(ctkDoubleSlider);
  52. this->Slider = new QSlider(p);
  53. QHBoxLayout* l = new QHBoxLayout(p);
  54. l->addWidget(this->Slider);
  55. l->setContentsMargins(0,0,0,0);
  56. this->Minimum = this->Slider->minimum();
  57. this->Maximum = this->Slider->maximum();
  58. this->SingleStep = this->Slider->singleStep();
  59. this->Value = this->Slider->value();
  60. p->connect(this->Slider, SIGNAL(valueChanged(int)), p, SLOT(onValueChanged(int)));
  61. p->connect(this->Slider, SIGNAL(sliderMoved(int)), p, SLOT(onSliderMoved(int)));
  62. p->connect(this->Slider, SIGNAL(sliderPressed()), p, SIGNAL(sliderPressed()));
  63. p->connect(this->Slider, SIGNAL(sliderReleased()), p, SIGNAL(sliderReleased()));
  64. }
  65. // --------------------------------------------------------------------------
  66. int ctkDoubleSliderPrivate::toInt(double doubleValue)const
  67. {
  68. double tmp = doubleValue / this->SingleStep;
  69. static const double minInt = std::numeric_limits<int>::min();
  70. static const double maxInt = std::numeric_limits<int>::max();
  71. #ifndef QT_NO_DEBUG
  72. if (tmp < minInt || tmp > maxInt)
  73. {
  74. qWarning("ctkDoubleSliderPrivate::toInt value out of bounds !");
  75. }
  76. #endif
  77. tmp = qBound(minInt, tmp, maxInt);
  78. int intValue = qRound(tmp);
  79. //qDebug() << __FUNCTION__ << doubleValue << tmp << intValue;
  80. return intValue;
  81. }
  82. // --------------------------------------------------------------------------
  83. double ctkDoubleSliderPrivate::fromInt(int intValue)const
  84. {
  85. double doubleValue = this->SingleStep * (this->Offset + intValue) ;
  86. //qDebug() << __FUNCTION__ << intValue << doubleValue;
  87. return doubleValue;
  88. }
  89. // --------------------------------------------------------------------------
  90. void ctkDoubleSliderPrivate::updateOffset(double value)
  91. {
  92. this->Offset = (value / this->SingleStep) - this->toInt(value);
  93. }
  94. // --------------------------------------------------------------------------
  95. ctkDoubleSlider::ctkDoubleSlider(QWidget* _parent) : Superclass(_parent)
  96. {
  97. CTK_INIT_PRIVATE(ctkDoubleSlider);
  98. ctk_d()->init();
  99. }
  100. // --------------------------------------------------------------------------
  101. ctkDoubleSlider::ctkDoubleSlider(Qt::Orientation _orientation, QWidget* _parent)
  102. : Superclass(_parent)
  103. {
  104. CTK_INIT_PRIVATE(ctkDoubleSlider);
  105. ctk_d()->init();
  106. this->setOrientation(_orientation);
  107. }
  108. // --------------------------------------------------------------------------
  109. ctkDoubleSlider::~ctkDoubleSlider()
  110. {
  111. }
  112. // --------------------------------------------------------------------------
  113. void ctkDoubleSlider::setMinimum(double min)
  114. {
  115. CTK_D(ctkDoubleSlider);
  116. d->Minimum = min;
  117. if (d->Minimum >= d->Value)
  118. {
  119. d->updateOffset(d->Minimum);
  120. }
  121. d->Slider->setMinimum(d->toInt(min));
  122. }
  123. // --------------------------------------------------------------------------
  124. void ctkDoubleSlider::setMaximum(double max)
  125. {
  126. CTK_D(ctkDoubleSlider);
  127. d->Maximum = max;
  128. if (d->Maximum <= d->Value)
  129. {
  130. d->updateOffset(d->Maximum);
  131. }
  132. d->Slider->setMaximum(d->toInt(max));
  133. }
  134. // --------------------------------------------------------------------------
  135. void ctkDoubleSlider::setRange(double min, double max)
  136. {
  137. CTK_D(ctkDoubleSlider);
  138. d->Minimum = min;
  139. d->Maximum = max;
  140. if (d->Minimum >= d->Value)
  141. {
  142. d->updateOffset(d->Minimum);
  143. }
  144. if (d->Maximum <= d->Value)
  145. {
  146. d->updateOffset(d->Maximum);
  147. }
  148. d->Slider->setRange(d->toInt(min), d->toInt(max));
  149. }
  150. // --------------------------------------------------------------------------
  151. double ctkDoubleSlider::minimum()const
  152. {
  153. CTK_D(const ctkDoubleSlider);
  154. return d->Minimum;
  155. }
  156. // --------------------------------------------------------------------------
  157. double ctkDoubleSlider::maximum()const
  158. {
  159. CTK_D(const ctkDoubleSlider);
  160. return d->Maximum;
  161. }
  162. // --------------------------------------------------------------------------
  163. double ctkDoubleSlider::sliderPosition()const
  164. {
  165. CTK_D(const ctkDoubleSlider);
  166. return d->fromInt(d->Slider->sliderPosition());
  167. }
  168. // --------------------------------------------------------------------------
  169. void ctkDoubleSlider::setSliderPosition(double newSliderPosition)
  170. {
  171. CTK_D(ctkDoubleSlider);
  172. d->Slider->setSliderPosition(d->toInt(newSliderPosition));
  173. }
  174. // --------------------------------------------------------------------------
  175. double ctkDoubleSlider::value()const
  176. {
  177. CTK_D(const ctkDoubleSlider);
  178. return d->Value;
  179. }
  180. // --------------------------------------------------------------------------
  181. void ctkDoubleSlider::setValue(double newValue)
  182. {
  183. CTK_D(ctkDoubleSlider);
  184. newValue = qBound(d->Minimum, newValue, d->Maximum);
  185. d->updateOffset(newValue);
  186. int newIntValue = d->toInt(newValue);
  187. if (newIntValue != d->Slider->value())
  188. {
  189. // d->Slider will emit a valueChanged signal that is connected to
  190. // ctkDoubleSlider::onValueChanged
  191. d->Slider->setValue(newIntValue);
  192. }
  193. else
  194. {
  195. double oldValue = d->Value;
  196. d->Value = newValue;
  197. // don't emit a valuechanged signal if the new value is quite
  198. // similar to the old value.
  199. if (qAbs(newValue - oldValue) > (d->SingleStep * 0.000000001))
  200. {
  201. emit this->valueChanged(newValue);
  202. }
  203. }
  204. }
  205. // --------------------------------------------------------------------------
  206. double ctkDoubleSlider::singleStep()const
  207. {
  208. CTK_D(const ctkDoubleSlider);
  209. return d->SingleStep;
  210. }
  211. // --------------------------------------------------------------------------
  212. void ctkDoubleSlider::setSingleStep(double newStep)
  213. {
  214. CTK_D(ctkDoubleSlider);
  215. d->SingleStep = newStep;
  216. // update the new values of the QSlider
  217. double _value = d->Value;
  218. d->updateOffset(_value);
  219. bool oldBlockSignals = this->blockSignals(true);
  220. this->setRange(d->Minimum, d->Maximum);
  221. d->Slider->setValue(d->toInt(_value));
  222. d->Value = _value;
  223. this->blockSignals(oldBlockSignals);
  224. }
  225. // --------------------------------------------------------------------------
  226. double ctkDoubleSlider::tickInterval()const
  227. {
  228. CTK_D(const ctkDoubleSlider);
  229. return d->fromInt(d->Slider->tickInterval());
  230. }
  231. // --------------------------------------------------------------------------
  232. void ctkDoubleSlider::setTickInterval(double newTickInterval)
  233. {
  234. CTK_D(ctkDoubleSlider);
  235. d->Slider->setTickInterval(d->toInt(newTickInterval));
  236. }
  237. // --------------------------------------------------------------------------
  238. bool ctkDoubleSlider::hasTracking()const
  239. {
  240. CTK_D(const ctkDoubleSlider);
  241. return d->Slider->hasTracking();
  242. }
  243. // --------------------------------------------------------------------------
  244. void ctkDoubleSlider::setTracking(bool enable)
  245. {
  246. CTK_D(ctkDoubleSlider);
  247. d->Slider->setTracking(enable);
  248. }
  249. // --------------------------------------------------------------------------
  250. void ctkDoubleSlider::triggerAction( QAbstractSlider::SliderAction action)
  251. {
  252. CTK_D(ctkDoubleSlider);
  253. d->Slider->triggerAction(action);
  254. }
  255. // --------------------------------------------------------------------------
  256. Qt::Orientation ctkDoubleSlider::orientation()const
  257. {
  258. CTK_D(const ctkDoubleSlider);
  259. return d->Slider->orientation();
  260. }
  261. // --------------------------------------------------------------------------
  262. void ctkDoubleSlider::setOrientation(Qt::Orientation newOrientation)
  263. {
  264. CTK_D(ctkDoubleSlider);
  265. d->Slider->setOrientation(newOrientation);
  266. }
  267. // --------------------------------------------------------------------------
  268. void ctkDoubleSlider::onValueChanged(int newValue)
  269. {
  270. CTK_D(ctkDoubleSlider);
  271. double doubleNewValue = d->fromInt(newValue);
  272. /*
  273. qDebug() << "onValueChanged: " << newValue << "->"<< d->fromInt(newValue+d->Offset)
  274. << " old: " << d->Value << "->" << d->toInt(d->Value)
  275. << "offset:" << d->Offset << doubleNewValue;
  276. */
  277. if (d->Value == doubleNewValue)
  278. {
  279. return;
  280. }
  281. d->Value = doubleNewValue;
  282. emit this->valueChanged(d->Value);
  283. }
  284. // --------------------------------------------------------------------------
  285. void ctkDoubleSlider::onSliderMoved(int newPosition)
  286. {
  287. CTK_D(const ctkDoubleSlider);
  288. emit this->sliderMoved(d->fromInt(newPosition));
  289. }