ctkDoubleSlider.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #include <QHelpEvent>
  18. #include <QStyle>
  19. #include <QStyleOptionSlider>
  20. #include <QToolTip>
  21. // CTK includes
  22. #include "ctkDoubleSlider.h"
  23. // STD includes
  24. #include <limits>
  25. //-----------------------------------------------------------------------------
  26. class ctkSlider: public QSlider
  27. {
  28. public:
  29. ctkSlider(QWidget* parent);
  30. using QSlider::initStyleOption;
  31. };
  32. //-----------------------------------------------------------------------------
  33. ctkSlider::ctkSlider(QWidget* parent): QSlider(parent)
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. class ctkDoubleSliderPrivate
  38. {
  39. Q_DECLARE_PUBLIC(ctkDoubleSlider);
  40. protected:
  41. ctkDoubleSlider* const q_ptr;
  42. public:
  43. ctkDoubleSliderPrivate(ctkDoubleSlider& object);
  44. int toInt(double value)const;
  45. double fromInt(int value)const;
  46. double safeFromInt(int value)const;
  47. void init();
  48. void updateOffset(double value);
  49. ctkSlider* Slider;
  50. QString HandleToolTip;
  51. double Minimum;
  52. double Maximum;
  53. bool SettingRange;
  54. // we should have a Offset and SliderPositionOffset (and MinimumOffset?)
  55. double Offset;
  56. double SingleStep;
  57. double PageStep;
  58. double Value;
  59. };
  60. // --------------------------------------------------------------------------
  61. ctkDoubleSliderPrivate::ctkDoubleSliderPrivate(ctkDoubleSlider& object)
  62. :q_ptr(&object)
  63. {
  64. this->Slider = 0;
  65. this->Minimum = 0.;
  66. this->Maximum = 100.;
  67. this->SettingRange = false;
  68. this->Offset = 0.;
  69. this->SingleStep = 1.;
  70. this->PageStep = 10.;
  71. this->Value = 0.;
  72. }
  73. // --------------------------------------------------------------------------
  74. void ctkDoubleSliderPrivate::init()
  75. {
  76. Q_Q(ctkDoubleSlider);
  77. this->Slider = new ctkSlider(q);
  78. this->Slider->installEventFilter(q);
  79. QHBoxLayout* l = new QHBoxLayout(q);
  80. l->addWidget(this->Slider);
  81. l->setContentsMargins(0,0,0,0);
  82. this->Minimum = this->Slider->minimum();
  83. this->Maximum = this->Slider->maximum();
  84. // this->Slider->singleStep is always 1
  85. this->SingleStep = this->Slider->singleStep();
  86. this->PageStep = this->Slider->pageStep();
  87. this->Value = this->Slider->value();
  88. q->connect(this->Slider, SIGNAL(valueChanged(int)), q, SLOT(onValueChanged(int)));
  89. q->connect(this->Slider, SIGNAL(sliderMoved(int)), q, SLOT(onSliderMoved(int)));
  90. q->connect(this->Slider, SIGNAL(sliderPressed()), q, SIGNAL(sliderPressed()));
  91. q->connect(this->Slider, SIGNAL(sliderReleased()), q, SIGNAL(sliderReleased()));
  92. q->connect(this->Slider, SIGNAL(rangeChanged(int,int)),
  93. q, SLOT(onRangeChanged(int,int)));
  94. q->setSizePolicy(this->Slider->sizePolicy());
  95. q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  96. }
  97. // --------------------------------------------------------------------------
  98. int ctkDoubleSliderPrivate::toInt(double doubleValue)const
  99. {
  100. double tmp = doubleValue / this->SingleStep;
  101. static const double minInt = std::numeric_limits<int>::min();
  102. static const double maxInt = std::numeric_limits<int>::max();
  103. #ifndef QT_NO_DEBUG
  104. if (tmp < minInt || tmp > maxInt)
  105. {
  106. qWarning("ctkDoubleSliderPrivate::toInt value out of bounds !");
  107. }
  108. #endif
  109. tmp = qBound(minInt, tmp, maxInt);
  110. int intValue = qRound(tmp);
  111. //qDebug() << __FUNCTION__ << doubleValue << tmp << intValue;
  112. return intValue;
  113. }
  114. // --------------------------------------------------------------------------
  115. double ctkDoubleSliderPrivate::fromInt(int intValue)const
  116. {
  117. double doubleValue = this->SingleStep * (this->Offset + intValue) ;
  118. //qDebug() << __FUNCTION__ << intValue << doubleValue;
  119. return doubleValue;
  120. }
  121. // --------------------------------------------------------------------------
  122. double ctkDoubleSliderPrivate::safeFromInt(int intValue)const
  123. {
  124. return qBound(this->Minimum, this->fromInt(intValue), this->Maximum);
  125. }
  126. // --------------------------------------------------------------------------
  127. void ctkDoubleSliderPrivate::updateOffset(double value)
  128. {
  129. this->Offset = (value / this->SingleStep) - this->toInt(value);
  130. }
  131. // --------------------------------------------------------------------------
  132. ctkDoubleSlider::ctkDoubleSlider(QWidget* _parent) : Superclass(_parent)
  133. , d_ptr(new ctkDoubleSliderPrivate(*this))
  134. {
  135. Q_D(ctkDoubleSlider);
  136. d->init();
  137. }
  138. // --------------------------------------------------------------------------
  139. ctkDoubleSlider::ctkDoubleSlider(Qt::Orientation _orientation, QWidget* _parent)
  140. : Superclass(_parent)
  141. , d_ptr(new ctkDoubleSliderPrivate(*this))
  142. {
  143. Q_D(ctkDoubleSlider);
  144. d->init();
  145. this->setOrientation(_orientation);
  146. }
  147. // --------------------------------------------------------------------------
  148. ctkDoubleSlider::~ctkDoubleSlider()
  149. {
  150. }
  151. // --------------------------------------------------------------------------
  152. void ctkDoubleSlider::setMinimum(double min)
  153. {
  154. Q_D(ctkDoubleSlider);
  155. this->setRange(min, qMax(min, d->Maximum));
  156. }
  157. // --------------------------------------------------------------------------
  158. void ctkDoubleSlider::setMaximum(double max)
  159. {
  160. Q_D(ctkDoubleSlider);
  161. this->setRange(qMin(d->Minimum, max), max);
  162. }
  163. // --------------------------------------------------------------------------
  164. void ctkDoubleSlider::setRange(double min, double max)
  165. {
  166. Q_D(ctkDoubleSlider);
  167. d->Minimum = min;
  168. d->Maximum = max;
  169. if (d->Minimum >= d->Value)
  170. {
  171. d->updateOffset(d->Minimum);
  172. }
  173. if (d->Maximum <= d->Value)
  174. {
  175. d->updateOffset(d->Maximum);
  176. }
  177. d->SettingRange = true;
  178. d->Slider->setRange(d->toInt(min), d->toInt(max));
  179. d->SettingRange = false;
  180. emit this->rangeChanged(d->Minimum, d->Maximum);
  181. /// In case QSlider::setRange(...) didn't notify the value
  182. /// has changed.
  183. this->setValue(d->Value);
  184. }
  185. // --------------------------------------------------------------------------
  186. double ctkDoubleSlider::minimum()const
  187. {
  188. Q_D(const ctkDoubleSlider);
  189. return d->Minimum;
  190. }
  191. // --------------------------------------------------------------------------
  192. double ctkDoubleSlider::maximum()const
  193. {
  194. Q_D(const ctkDoubleSlider);
  195. return d->Maximum;
  196. }
  197. // --------------------------------------------------------------------------
  198. double ctkDoubleSlider::sliderPosition()const
  199. {
  200. Q_D(const ctkDoubleSlider);
  201. return d->safeFromInt(d->Slider->sliderPosition());
  202. }
  203. // --------------------------------------------------------------------------
  204. void ctkDoubleSlider::setSliderPosition(double newSliderPosition)
  205. {
  206. Q_D(ctkDoubleSlider);
  207. d->Slider->setSliderPosition(d->toInt(newSliderPosition));
  208. }
  209. // --------------------------------------------------------------------------
  210. double ctkDoubleSlider::value()const
  211. {
  212. Q_D(const ctkDoubleSlider);
  213. return d->Value;
  214. }
  215. // --------------------------------------------------------------------------
  216. void ctkDoubleSlider::setValue(double newValue)
  217. {
  218. Q_D(ctkDoubleSlider);
  219. newValue = qBound(d->Minimum, newValue, d->Maximum);
  220. d->updateOffset(newValue);
  221. int newIntValue = d->toInt(newValue);
  222. if (newIntValue != d->Slider->value())
  223. {
  224. // d->Slider will emit a valueChanged signal that is connected to
  225. // ctkDoubleSlider::onValueChanged
  226. d->Slider->setValue(newIntValue);
  227. }
  228. else
  229. {
  230. double oldValue = d->Value;
  231. d->Value = newValue;
  232. // don't emit a valuechanged signal if the new value is quite
  233. // similar to the old value.
  234. if (qAbs(newValue - oldValue) > (d->SingleStep * 0.000000001))
  235. {
  236. emit this->valueChanged(newValue);
  237. }
  238. }
  239. }
  240. // --------------------------------------------------------------------------
  241. double ctkDoubleSlider::singleStep()const
  242. {
  243. Q_D(const ctkDoubleSlider);
  244. return d->SingleStep;
  245. }
  246. // --------------------------------------------------------------------------
  247. void ctkDoubleSlider::setSingleStep(double newStep)
  248. {
  249. Q_D(ctkDoubleSlider);
  250. d->SingleStep = newStep;
  251. d->updateOffset(d->Value);
  252. // update the new values of the QSlider
  253. bool oldBlockSignals = d->Slider->blockSignals(true);
  254. d->Slider->setRange(d->toInt(d->Minimum), d->toInt(d->Maximum));
  255. d->Slider->setValue(d->toInt(d->Value));
  256. d->Slider->setPageStep(d->toInt(d->PageStep));
  257. d->Slider->blockSignals(oldBlockSignals);
  258. Q_ASSERT(qFuzzyCompare(d->Value,d->safeFromInt(d->Slider->value())));
  259. }
  260. // --------------------------------------------------------------------------
  261. double ctkDoubleSlider::pageStep()const
  262. {
  263. Q_D(const ctkDoubleSlider);
  264. return d->PageStep;
  265. }
  266. // --------------------------------------------------------------------------
  267. void ctkDoubleSlider::setPageStep(double newStep)
  268. {
  269. Q_D(ctkDoubleSlider);
  270. d->PageStep = newStep;
  271. d->Slider->setPageStep(d->toInt(d->PageStep));
  272. }
  273. // --------------------------------------------------------------------------
  274. double ctkDoubleSlider::tickInterval()const
  275. {
  276. Q_D(const ctkDoubleSlider);
  277. // No need to apply Offset
  278. return d->SingleStep * d->Slider->tickInterval();
  279. }
  280. // --------------------------------------------------------------------------
  281. void ctkDoubleSlider::setTickInterval(double newTickInterval)
  282. {
  283. Q_D(ctkDoubleSlider);
  284. d->Slider->setTickInterval(d->toInt(newTickInterval));
  285. }
  286. // --------------------------------------------------------------------------
  287. bool ctkDoubleSlider::hasTracking()const
  288. {
  289. Q_D(const ctkDoubleSlider);
  290. return d->Slider->hasTracking();
  291. }
  292. // --------------------------------------------------------------------------
  293. void ctkDoubleSlider::setTracking(bool enable)
  294. {
  295. Q_D(ctkDoubleSlider);
  296. d->Slider->setTracking(enable);
  297. }
  298. // --------------------------------------------------------------------------
  299. void ctkDoubleSlider::triggerAction( QAbstractSlider::SliderAction action)
  300. {
  301. Q_D(ctkDoubleSlider);
  302. d->Slider->triggerAction(action);
  303. }
  304. // --------------------------------------------------------------------------
  305. Qt::Orientation ctkDoubleSlider::orientation()const
  306. {
  307. Q_D(const ctkDoubleSlider);
  308. return d->Slider->orientation();
  309. }
  310. // --------------------------------------------------------------------------
  311. void ctkDoubleSlider::setOrientation(Qt::Orientation newOrientation)
  312. {
  313. Q_D(ctkDoubleSlider);
  314. if (this->orientation() == newOrientation)
  315. {
  316. return;
  317. }
  318. if (!testAttribute(Qt::WA_WState_OwnSizePolicy))
  319. {
  320. QSizePolicy sp = this->sizePolicy();
  321. sp.transpose();
  322. this->setSizePolicy(sp);
  323. this->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  324. }
  325. // d->Slider will take care of calling updateGeometry
  326. d->Slider->setOrientation(newOrientation);
  327. }
  328. // --------------------------------------------------------------------------
  329. QString ctkDoubleSlider::handleToolTip()const
  330. {
  331. Q_D(const ctkDoubleSlider);
  332. return d->HandleToolTip;
  333. }
  334. // --------------------------------------------------------------------------
  335. void ctkDoubleSlider::setHandleToolTip(const QString& toolTip)
  336. {
  337. Q_D(ctkDoubleSlider);
  338. d->HandleToolTip = toolTip;
  339. }
  340. // --------------------------------------------------------------------------
  341. void ctkDoubleSlider::onValueChanged(int newValue)
  342. {
  343. Q_D(ctkDoubleSlider);
  344. double doubleNewValue = d->safeFromInt(newValue);
  345. /*
  346. qDebug() << "onValueChanged: " << newValue << "->"<< d->fromInt(newValue+d->Offset)
  347. << " old: " << d->Value << "->" << d->toInt(d->Value)
  348. << "offset:" << d->Offset << doubleNewValue;
  349. */
  350. if (d->Value == doubleNewValue)
  351. {
  352. return;
  353. }
  354. d->Value = doubleNewValue;
  355. emit this->valueChanged(d->Value);
  356. }
  357. // --------------------------------------------------------------------------
  358. void ctkDoubleSlider::onSliderMoved(int newPosition)
  359. {
  360. Q_D(const ctkDoubleSlider);
  361. emit this->sliderMoved(d->safeFromInt(newPosition));
  362. }
  363. // --------------------------------------------------------------------------
  364. void ctkDoubleSlider::onRangeChanged(int min, int max)
  365. {
  366. Q_D(const ctkDoubleSlider);
  367. if (!d->SettingRange)
  368. {
  369. this->setRange(d->fromInt(min), d->fromInt(max));
  370. }
  371. }
  372. // --------------------------------------------------------------------------
  373. bool ctkDoubleSlider::eventFilter(QObject* watched, QEvent* event)
  374. {
  375. Q_D(ctkDoubleSlider);
  376. if (watched == d->Slider)
  377. {
  378. switch(event->type())
  379. {
  380. case QEvent::ToolTip:
  381. {
  382. QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
  383. QStyleOptionSlider opt;
  384. d->Slider->initStyleOption(&opt);
  385. QStyle::SubControl hoveredControl =
  386. d->Slider->style()->hitTestComplexControl(
  387. QStyle::CC_Slider, &opt, helpEvent->pos(), this);
  388. if (!d->HandleToolTip.isEmpty() &&
  389. hoveredControl == QStyle::SC_SliderHandle)
  390. {
  391. QToolTip::showText(helpEvent->globalPos(), d->HandleToolTip.arg(this->value()));
  392. event->accept();
  393. return true;
  394. }
  395. }
  396. default:
  397. break;
  398. }
  399. }
  400. return this->Superclass::eventFilter(watched, event);
  401. }