ctkDoubleSlider.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. 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. d->Minimum = min;
  139. if (d->Minimum >= d->Value)
  140. {
  141. d->updateOffset(d->Minimum);
  142. }
  143. d->SettingRange = true;
  144. d->Slider->setMinimum(d->toInt(min));
  145. d->SettingRange = false;
  146. emit this->rangeChanged(d->Minimum, d->Maximum);
  147. }
  148. // --------------------------------------------------------------------------
  149. void ctkDoubleSlider::setMaximum(double max)
  150. {
  151. Q_D(ctkDoubleSlider);
  152. d->Maximum = max;
  153. if (d->Maximum <= d->Value)
  154. {
  155. d->updateOffset(d->Maximum);
  156. }
  157. d->SettingRange = true;
  158. d->Slider->setMaximum(d->toInt(max));
  159. d->SettingRange = false;
  160. emit this->rangeChanged(d->Minimum, d->Maximum);
  161. }
  162. // --------------------------------------------------------------------------
  163. void ctkDoubleSlider::setRange(double min, double max)
  164. {
  165. Q_D(ctkDoubleSlider);
  166. d->Minimum = min;
  167. d->Maximum = max;
  168. if (d->Minimum >= d->Value)
  169. {
  170. d->updateOffset(d->Minimum);
  171. }
  172. if (d->Maximum <= d->Value)
  173. {
  174. d->updateOffset(d->Maximum);
  175. }
  176. d->SettingRange = true;
  177. d->Slider->setRange(d->toInt(min), d->toInt(max));
  178. d->SettingRange = false;
  179. emit this->rangeChanged(d->Minimum, d->Maximum);
  180. }
  181. // --------------------------------------------------------------------------
  182. double ctkDoubleSlider::minimum()const
  183. {
  184. Q_D(const ctkDoubleSlider);
  185. return d->Minimum;
  186. }
  187. // --------------------------------------------------------------------------
  188. double ctkDoubleSlider::maximum()const
  189. {
  190. Q_D(const ctkDoubleSlider);
  191. return d->Maximum;
  192. }
  193. // --------------------------------------------------------------------------
  194. double ctkDoubleSlider::sliderPosition()const
  195. {
  196. Q_D(const ctkDoubleSlider);
  197. return d->safeFromInt(d->Slider->sliderPosition());
  198. }
  199. // --------------------------------------------------------------------------
  200. void ctkDoubleSlider::setSliderPosition(double newSliderPosition)
  201. {
  202. Q_D(ctkDoubleSlider);
  203. d->Slider->setSliderPosition(d->toInt(newSliderPosition));
  204. }
  205. // --------------------------------------------------------------------------
  206. double ctkDoubleSlider::value()const
  207. {
  208. Q_D(const ctkDoubleSlider);
  209. return d->Value;
  210. }
  211. // --------------------------------------------------------------------------
  212. void ctkDoubleSlider::setValue(double newValue)
  213. {
  214. Q_D(ctkDoubleSlider);
  215. newValue = qBound(d->Minimum, newValue, d->Maximum);
  216. d->updateOffset(newValue);
  217. int newIntValue = d->toInt(newValue);
  218. if (newIntValue != d->Slider->value())
  219. {
  220. // d->Slider will emit a valueChanged signal that is connected to
  221. // ctkDoubleSlider::onValueChanged
  222. d->Slider->setValue(newIntValue);
  223. }
  224. else
  225. {
  226. double oldValue = d->Value;
  227. d->Value = newValue;
  228. // don't emit a valuechanged signal if the new value is quite
  229. // similar to the old value.
  230. if (qAbs(newValue - oldValue) > (d->SingleStep * 0.000000001))
  231. {
  232. emit this->valueChanged(newValue);
  233. }
  234. }
  235. }
  236. // --------------------------------------------------------------------------
  237. double ctkDoubleSlider::singleStep()const
  238. {
  239. Q_D(const ctkDoubleSlider);
  240. return d->SingleStep;
  241. }
  242. // --------------------------------------------------------------------------
  243. void ctkDoubleSlider::setSingleStep(double newStep)
  244. {
  245. Q_D(ctkDoubleSlider);
  246. d->SingleStep = newStep;
  247. // update the new values of the QSlider
  248. double _value = d->Value;
  249. d->updateOffset(_value);
  250. bool oldBlockSignals = this->blockSignals(true);
  251. this->setRange(d->Minimum, d->Maximum);
  252. d->Slider->setValue(d->toInt(_value));
  253. d->Value = _value;
  254. d->Slider->setPageStep(d->toInt(d->PageStep));
  255. this->blockSignals(oldBlockSignals);
  256. }
  257. // --------------------------------------------------------------------------
  258. double ctkDoubleSlider::pageStep()const
  259. {
  260. Q_D(const ctkDoubleSlider);
  261. return d->PageStep;
  262. }
  263. // --------------------------------------------------------------------------
  264. void ctkDoubleSlider::setPageStep(double newStep)
  265. {
  266. Q_D(ctkDoubleSlider);
  267. d->PageStep = newStep;
  268. d->Slider->setPageStep(d->toInt(d->PageStep));
  269. }
  270. // --------------------------------------------------------------------------
  271. double ctkDoubleSlider::tickInterval()const
  272. {
  273. Q_D(const ctkDoubleSlider);
  274. // No need to apply Offset
  275. return d->SingleStep * d->Slider->tickInterval();
  276. }
  277. // --------------------------------------------------------------------------
  278. void ctkDoubleSlider::setTickInterval(double newTickInterval)
  279. {
  280. Q_D(ctkDoubleSlider);
  281. d->Slider->setTickInterval(d->toInt(newTickInterval));
  282. }
  283. // --------------------------------------------------------------------------
  284. bool ctkDoubleSlider::hasTracking()const
  285. {
  286. Q_D(const ctkDoubleSlider);
  287. return d->Slider->hasTracking();
  288. }
  289. // --------------------------------------------------------------------------
  290. void ctkDoubleSlider::setTracking(bool enable)
  291. {
  292. Q_D(ctkDoubleSlider);
  293. d->Slider->setTracking(enable);
  294. }
  295. // --------------------------------------------------------------------------
  296. void ctkDoubleSlider::triggerAction( QAbstractSlider::SliderAction action)
  297. {
  298. Q_D(ctkDoubleSlider);
  299. d->Slider->triggerAction(action);
  300. }
  301. // --------------------------------------------------------------------------
  302. Qt::Orientation ctkDoubleSlider::orientation()const
  303. {
  304. Q_D(const ctkDoubleSlider);
  305. return d->Slider->orientation();
  306. }
  307. // --------------------------------------------------------------------------
  308. void ctkDoubleSlider::setOrientation(Qt::Orientation newOrientation)
  309. {
  310. Q_D(ctkDoubleSlider);
  311. if (this->orientation() == newOrientation)
  312. {
  313. return;
  314. }
  315. if (!testAttribute(Qt::WA_WState_OwnSizePolicy))
  316. {
  317. QSizePolicy sp = this->sizePolicy();
  318. sp.transpose();
  319. this->setSizePolicy(sp);
  320. this->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  321. }
  322. // d->Slider will take care of calling updateGeometry
  323. d->Slider->setOrientation(newOrientation);
  324. }
  325. // --------------------------------------------------------------------------
  326. void ctkDoubleSlider::onValueChanged(int newValue)
  327. {
  328. Q_D(ctkDoubleSlider);
  329. double doubleNewValue = d->safeFromInt(newValue);
  330. /*
  331. qDebug() << "onValueChanged: " << newValue << "->"<< d->fromInt(newValue+d->Offset)
  332. << " old: " << d->Value << "->" << d->toInt(d->Value)
  333. << "offset:" << d->Offset << doubleNewValue;
  334. */
  335. if (d->Value == doubleNewValue)
  336. {
  337. return;
  338. }
  339. d->Value = doubleNewValue;
  340. emit this->valueChanged(d->Value);
  341. }
  342. // --------------------------------------------------------------------------
  343. void ctkDoubleSlider::onSliderMoved(int newPosition)
  344. {
  345. Q_D(const ctkDoubleSlider);
  346. emit this->sliderMoved(d->safeFromInt(newPosition));
  347. }
  348. // --------------------------------------------------------------------------
  349. void ctkDoubleSlider::onRangeChanged(int min, int max)
  350. {
  351. Q_D(const ctkDoubleSlider);
  352. if (!d->SettingRange)
  353. {
  354. this->setRange(d->fromInt(min), d->fromInt(max));
  355. }
  356. }