ctkDoubleSlider.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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() << __FUNCTION__ << ": value " << doubleValue
  107. << " for singleStep " << this->SingleStep
  108. << " is out of integer bounds !";
  109. }
  110. #endif
  111. tmp = qBound(minInt, tmp, maxInt);
  112. int intValue = qRound(tmp);
  113. //qDebug() << __FUNCTION__ << doubleValue << tmp << intValue;
  114. return intValue;
  115. }
  116. // --------------------------------------------------------------------------
  117. double ctkDoubleSliderPrivate::fromInt(int intValue)const
  118. {
  119. double doubleValue = this->SingleStep * (this->Offset + intValue) ;
  120. //qDebug() << __FUNCTION__ << intValue << doubleValue;
  121. return doubleValue;
  122. }
  123. // --------------------------------------------------------------------------
  124. double ctkDoubleSliderPrivate::safeFromInt(int intValue)const
  125. {
  126. return qBound(this->Minimum, this->fromInt(intValue), this->Maximum);
  127. }
  128. // --------------------------------------------------------------------------
  129. void ctkDoubleSliderPrivate::updateOffset(double value)
  130. {
  131. this->Offset = (value / this->SingleStep) - this->toInt(value);
  132. }
  133. // --------------------------------------------------------------------------
  134. ctkDoubleSlider::ctkDoubleSlider(QWidget* _parent) : Superclass(_parent)
  135. , d_ptr(new ctkDoubleSliderPrivate(*this))
  136. {
  137. Q_D(ctkDoubleSlider);
  138. d->init();
  139. }
  140. // --------------------------------------------------------------------------
  141. ctkDoubleSlider::ctkDoubleSlider(Qt::Orientation _orientation, QWidget* _parent)
  142. : Superclass(_parent)
  143. , d_ptr(new ctkDoubleSliderPrivate(*this))
  144. {
  145. Q_D(ctkDoubleSlider);
  146. d->init();
  147. this->setOrientation(_orientation);
  148. }
  149. // --------------------------------------------------------------------------
  150. ctkDoubleSlider::~ctkDoubleSlider()
  151. {
  152. }
  153. // --------------------------------------------------------------------------
  154. void ctkDoubleSlider::setMinimum(double min)
  155. {
  156. Q_D(ctkDoubleSlider);
  157. this->setRange(min, qMax(min, d->Maximum));
  158. }
  159. // --------------------------------------------------------------------------
  160. void ctkDoubleSlider::setMaximum(double max)
  161. {
  162. Q_D(ctkDoubleSlider);
  163. this->setRange(qMin(d->Minimum, max), max);
  164. }
  165. // --------------------------------------------------------------------------
  166. void ctkDoubleSlider::setRange(double min, double max)
  167. {
  168. Q_D(ctkDoubleSlider);
  169. d->Minimum = min;
  170. d->Maximum = max;
  171. if (d->Minimum >= d->Value)
  172. {
  173. d->updateOffset(d->Minimum);
  174. }
  175. if (d->Maximum <= d->Value)
  176. {
  177. d->updateOffset(d->Maximum);
  178. }
  179. d->SettingRange = true;
  180. d->Slider->setRange(d->toInt(min), d->toInt(max));
  181. d->SettingRange = false;
  182. emit this->rangeChanged(d->Minimum, d->Maximum);
  183. /// In case QSlider::setRange(...) didn't notify the value
  184. /// has changed.
  185. this->setValue(d->Value);
  186. }
  187. // --------------------------------------------------------------------------
  188. double ctkDoubleSlider::minimum()const
  189. {
  190. Q_D(const ctkDoubleSlider);
  191. return d->Minimum;
  192. }
  193. // --------------------------------------------------------------------------
  194. double ctkDoubleSlider::maximum()const
  195. {
  196. Q_D(const ctkDoubleSlider);
  197. return d->Maximum;
  198. }
  199. // --------------------------------------------------------------------------
  200. double ctkDoubleSlider::sliderPosition()const
  201. {
  202. Q_D(const ctkDoubleSlider);
  203. return d->safeFromInt(d->Slider->sliderPosition());
  204. }
  205. // --------------------------------------------------------------------------
  206. void ctkDoubleSlider::setSliderPosition(double newSliderPosition)
  207. {
  208. Q_D(ctkDoubleSlider);
  209. d->Slider->setSliderPosition(d->toInt(newSliderPosition));
  210. }
  211. // --------------------------------------------------------------------------
  212. double ctkDoubleSlider::value()const
  213. {
  214. Q_D(const ctkDoubleSlider);
  215. return d->Value;
  216. }
  217. // --------------------------------------------------------------------------
  218. void ctkDoubleSlider::setValue(double newValue)
  219. {
  220. Q_D(ctkDoubleSlider);
  221. newValue = qBound(d->Minimum, newValue, d->Maximum);
  222. d->updateOffset(newValue);
  223. int newIntValue = d->toInt(newValue);
  224. if (newIntValue != d->Slider->value())
  225. {
  226. // d->Slider will emit a valueChanged signal that is connected to
  227. // ctkDoubleSlider::onValueChanged
  228. d->Slider->setValue(newIntValue);
  229. }
  230. else
  231. {
  232. double oldValue = d->Value;
  233. d->Value = newValue;
  234. // don't emit a valuechanged signal if the new value is quite
  235. // similar to the old value.
  236. if (qAbs(newValue - oldValue) > (d->SingleStep * 0.000000001))
  237. {
  238. emit this->valueChanged(newValue);
  239. }
  240. }
  241. }
  242. // --------------------------------------------------------------------------
  243. double ctkDoubleSlider::singleStep()const
  244. {
  245. Q_D(const ctkDoubleSlider);
  246. return d->SingleStep;
  247. }
  248. // --------------------------------------------------------------------------
  249. void ctkDoubleSlider::setSingleStep(double newStep)
  250. {
  251. Q_D(ctkDoubleSlider);
  252. d->SingleStep = newStep;
  253. d->updateOffset(d->Value);
  254. // update the new values of the QSlider
  255. bool oldBlockSignals = d->Slider->blockSignals(true);
  256. d->Slider->setRange(d->toInt(d->Minimum), d->toInt(d->Maximum));
  257. d->Slider->setValue(d->toInt(d->Value));
  258. d->Slider->setPageStep(d->toInt(d->PageStep));
  259. d->Slider->blockSignals(oldBlockSignals);
  260. Q_ASSERT(qFuzzyCompare(d->Value,d->safeFromInt(d->Slider->value())));
  261. }
  262. // --------------------------------------------------------------------------
  263. double ctkDoubleSlider::pageStep()const
  264. {
  265. Q_D(const ctkDoubleSlider);
  266. return d->PageStep;
  267. }
  268. // --------------------------------------------------------------------------
  269. void ctkDoubleSlider::setPageStep(double newStep)
  270. {
  271. Q_D(ctkDoubleSlider);
  272. d->PageStep = newStep;
  273. d->Slider->setPageStep(d->toInt(d->PageStep));
  274. }
  275. // --------------------------------------------------------------------------
  276. double ctkDoubleSlider::tickInterval()const
  277. {
  278. Q_D(const ctkDoubleSlider);
  279. // No need to apply Offset
  280. return d->SingleStep * d->Slider->tickInterval();
  281. }
  282. // --------------------------------------------------------------------------
  283. void ctkDoubleSlider::setTickInterval(double newTickInterval)
  284. {
  285. Q_D(ctkDoubleSlider);
  286. d->Slider->setTickInterval(d->toInt(newTickInterval));
  287. }
  288. // --------------------------------------------------------------------------
  289. QSlider::TickPosition ctkDoubleSlider::tickPosition()const
  290. {
  291. Q_D(const ctkDoubleSlider);
  292. return d->Slider->tickPosition();
  293. }
  294. // --------------------------------------------------------------------------
  295. void ctkDoubleSlider::setTickPosition(QSlider::TickPosition newTickPosition)
  296. {
  297. Q_D(ctkDoubleSlider);
  298. d->Slider->setTickPosition(newTickPosition);
  299. }
  300. // --------------------------------------------------------------------------
  301. bool ctkDoubleSlider::hasTracking()const
  302. {
  303. Q_D(const ctkDoubleSlider);
  304. return d->Slider->hasTracking();
  305. }
  306. // --------------------------------------------------------------------------
  307. void ctkDoubleSlider::setTracking(bool enable)
  308. {
  309. Q_D(ctkDoubleSlider);
  310. d->Slider->setTracking(enable);
  311. }
  312. // --------------------------------------------------------------------------
  313. void ctkDoubleSlider::triggerAction( QAbstractSlider::SliderAction action)
  314. {
  315. Q_D(ctkDoubleSlider);
  316. d->Slider->triggerAction(action);
  317. }
  318. // --------------------------------------------------------------------------
  319. Qt::Orientation ctkDoubleSlider::orientation()const
  320. {
  321. Q_D(const ctkDoubleSlider);
  322. return d->Slider->orientation();
  323. }
  324. // --------------------------------------------------------------------------
  325. void ctkDoubleSlider::setOrientation(Qt::Orientation newOrientation)
  326. {
  327. Q_D(ctkDoubleSlider);
  328. if (this->orientation() == newOrientation)
  329. {
  330. return;
  331. }
  332. if (!testAttribute(Qt::WA_WState_OwnSizePolicy))
  333. {
  334. QSizePolicy sp = this->sizePolicy();
  335. sp.transpose();
  336. this->setSizePolicy(sp);
  337. this->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  338. }
  339. // d->Slider will take care of calling updateGeometry
  340. d->Slider->setOrientation(newOrientation);
  341. }
  342. // --------------------------------------------------------------------------
  343. QString ctkDoubleSlider::handleToolTip()const
  344. {
  345. Q_D(const ctkDoubleSlider);
  346. return d->HandleToolTip;
  347. }
  348. // --------------------------------------------------------------------------
  349. void ctkDoubleSlider::setHandleToolTip(const QString& toolTip)
  350. {
  351. Q_D(ctkDoubleSlider);
  352. d->HandleToolTip = toolTip;
  353. }
  354. // --------------------------------------------------------------------------
  355. void ctkDoubleSlider::onValueChanged(int newValue)
  356. {
  357. Q_D(ctkDoubleSlider);
  358. double doubleNewValue = d->safeFromInt(newValue);
  359. /*
  360. qDebug() << "onValueChanged: " << newValue << "->"<< d->fromInt(newValue+d->Offset)
  361. << " old: " << d->Value << "->" << d->toInt(d->Value)
  362. << "offset:" << d->Offset << doubleNewValue;
  363. */
  364. if (d->Value == doubleNewValue)
  365. {
  366. return;
  367. }
  368. d->Value = doubleNewValue;
  369. emit this->valueChanged(d->Value);
  370. }
  371. // --------------------------------------------------------------------------
  372. void ctkDoubleSlider::onSliderMoved(int newPosition)
  373. {
  374. Q_D(const ctkDoubleSlider);
  375. emit this->sliderMoved(d->safeFromInt(newPosition));
  376. }
  377. // --------------------------------------------------------------------------
  378. void ctkDoubleSlider::onRangeChanged(int min, int max)
  379. {
  380. Q_D(const ctkDoubleSlider);
  381. if (!d->SettingRange)
  382. {
  383. this->setRange(d->fromInt(min), d->fromInt(max));
  384. }
  385. }
  386. // --------------------------------------------------------------------------
  387. bool ctkDoubleSlider::eventFilter(QObject* watched, QEvent* event)
  388. {
  389. Q_D(ctkDoubleSlider);
  390. if (watched == d->Slider)
  391. {
  392. switch(event->type())
  393. {
  394. case QEvent::ToolTip:
  395. {
  396. QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
  397. QStyleOptionSlider opt;
  398. d->Slider->initStyleOption(&opt);
  399. QStyle::SubControl hoveredControl =
  400. d->Slider->style()->hitTestComplexControl(
  401. QStyle::CC_Slider, &opt, helpEvent->pos(), this);
  402. if (!d->HandleToolTip.isEmpty() &&
  403. hoveredControl == QStyle::SC_SliderHandle)
  404. {
  405. QToolTip::showText(helpEvent->globalPos(), d->HandleToolTip.arg(this->value()));
  406. event->accept();
  407. return true;
  408. }
  409. }
  410. default:
  411. break;
  412. }
  413. }
  414. return this->Superclass::eventFilter(watched, event);
  415. }