ctkDoubleSlider.cpp 11 KB

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