ctkDoubleSlider.cpp 11 KB

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