ctkDoubleSlider.cpp 9.7 KB

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