ctkDoubleSlider.cpp 11 KB

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