ctkDoubleSlider.cpp 10 KB

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