ctkDoubleSlider.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. #include "ctkValueProxy.h"
  24. // STD includes
  25. #include <limits>
  26. //-----------------------------------------------------------------------------
  27. // ctkSlider
  28. //-----------------------------------------------------------------------------
  29. class ctkSlider: public QSlider
  30. {
  31. public:
  32. ctkSlider(QWidget* parent);
  33. using QSlider::initStyleOption;
  34. };
  35. //-----------------------------------------------------------------------------
  36. ctkSlider::ctkSlider(QWidget* parent): QSlider(parent)
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. // ctkDoubleSliderPrivate
  41. //-----------------------------------------------------------------------------
  42. class ctkDoubleSliderPrivate
  43. {
  44. Q_DECLARE_PUBLIC(ctkDoubleSlider);
  45. protected:
  46. ctkDoubleSlider* const q_ptr;
  47. public:
  48. ctkDoubleSliderPrivate(ctkDoubleSlider& object);
  49. int toInt(double value)const;
  50. double fromInt(int value)const;
  51. double safeFromInt(int value)const;
  52. void init();
  53. void updateOffset(double value);
  54. ctkSlider* Slider;
  55. QString HandleToolTip;
  56. double Minimum;
  57. double Maximum;
  58. bool SettingRange;
  59. // we should have a Offset and SliderPositionOffset (and MinimumOffset?)
  60. double Offset;
  61. double SingleStep;
  62. double PageStep;
  63. double Value;
  64. /// Converts input value with displayed value
  65. QWeakPointer<ctkValueProxy> Proxy;
  66. };
  67. // --------------------------------------------------------------------------
  68. ctkDoubleSliderPrivate::ctkDoubleSliderPrivate(ctkDoubleSlider& object)
  69. :q_ptr(&object)
  70. {
  71. this->Slider = 0;
  72. this->Minimum = 0.;
  73. this->Maximum = 100.;
  74. this->SettingRange = false;
  75. this->Offset = 0.;
  76. this->SingleStep = 1.;
  77. this->PageStep = 10.;
  78. this->Value = 0.;
  79. }
  80. // --------------------------------------------------------------------------
  81. void ctkDoubleSliderPrivate::init()
  82. {
  83. Q_Q(ctkDoubleSlider);
  84. this->Slider = new ctkSlider(q);
  85. this->Slider->installEventFilter(q);
  86. QHBoxLayout* l = new QHBoxLayout(q);
  87. l->addWidget(this->Slider);
  88. l->setContentsMargins(0,0,0,0);
  89. this->Minimum = this->Slider->minimum();
  90. this->Maximum = this->Slider->maximum();
  91. // this->Slider->singleStep is always 1
  92. this->SingleStep = this->Slider->singleStep();
  93. this->PageStep = this->Slider->pageStep();
  94. this->Value = this->Slider->value();
  95. q->connect(this->Slider, SIGNAL(valueChanged(int)), q, SLOT(onValueChanged(int)));
  96. q->connect(this->Slider, SIGNAL(sliderMoved(int)), q, SLOT(onSliderMoved(int)));
  97. q->connect(this->Slider, SIGNAL(sliderPressed()), q, SIGNAL(sliderPressed()));
  98. q->connect(this->Slider, SIGNAL(sliderReleased()), q, SIGNAL(sliderReleased()));
  99. q->connect(this->Slider, SIGNAL(rangeChanged(int,int)),
  100. q, SLOT(onRangeChanged(int,int)));
  101. q->setSizePolicy(this->Slider->sizePolicy());
  102. q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  103. }
  104. // --------------------------------------------------------------------------
  105. int ctkDoubleSliderPrivate::toInt(double doubleValue)const
  106. {
  107. double tmp = doubleValue / this->SingleStep;
  108. static const double minInt = std::numeric_limits<int>::min();
  109. static const double maxInt = std::numeric_limits<int>::max();
  110. #ifndef QT_NO_DEBUG
  111. if (tmp < minInt || tmp > maxInt)
  112. {
  113. qWarning() << __FUNCTION__ << ": value " << doubleValue
  114. << " for singleStep " << this->SingleStep
  115. << " is out of integer bounds !";
  116. }
  117. #endif
  118. tmp = qBound(minInt, tmp, maxInt);
  119. int intValue = qRound(tmp);
  120. //qDebug() << __FUNCTION__ << doubleValue << tmp << intValue;
  121. return intValue;
  122. }
  123. // --------------------------------------------------------------------------
  124. double ctkDoubleSliderPrivate::fromInt(int intValue)const
  125. {
  126. double doubleValue = this->SingleStep * (this->Offset + intValue) ;
  127. //qDebug() << __FUNCTION__ << intValue << doubleValue;
  128. return doubleValue;
  129. }
  130. // --------------------------------------------------------------------------
  131. double ctkDoubleSliderPrivate::safeFromInt(int intValue)const
  132. {
  133. return qBound(this->Minimum, this->fromInt(intValue), this->Maximum);
  134. }
  135. // --------------------------------------------------------------------------
  136. void ctkDoubleSliderPrivate::updateOffset(double value)
  137. {
  138. this->Offset = (value / this->SingleStep) - this->toInt(value);
  139. }
  140. //-----------------------------------------------------------------------------
  141. // ctkDoubleSlider
  142. // --------------------------------------------------------------------------
  143. ctkDoubleSlider::ctkDoubleSlider(QWidget* _parent) : Superclass(_parent)
  144. , d_ptr(new ctkDoubleSliderPrivate(*this))
  145. {
  146. Q_D(ctkDoubleSlider);
  147. d->init();
  148. }
  149. // --------------------------------------------------------------------------
  150. ctkDoubleSlider::ctkDoubleSlider(Qt::Orientation _orientation, QWidget* _parent)
  151. : Superclass(_parent)
  152. , d_ptr(new ctkDoubleSliderPrivate(*this))
  153. {
  154. Q_D(ctkDoubleSlider);
  155. d->init();
  156. this->setOrientation(_orientation);
  157. }
  158. // --------------------------------------------------------------------------
  159. ctkDoubleSlider::~ctkDoubleSlider()
  160. {
  161. }
  162. // --------------------------------------------------------------------------
  163. void ctkDoubleSlider::setMinimum(double min)
  164. {
  165. this->setRange(min, this->maximum());
  166. }
  167. // --------------------------------------------------------------------------
  168. void ctkDoubleSlider::setMaximum(double max)
  169. {
  170. this->setRange(this->minimum(), max);
  171. }
  172. // --------------------------------------------------------------------------
  173. void ctkDoubleSlider::setRange(double newMin, double newMax)
  174. {
  175. Q_D(ctkDoubleSlider);
  176. if (d->Proxy)
  177. {
  178. newMin = d->Proxy.data()->proxyValueFromValue(newMin);
  179. newMax = d->Proxy.data()->proxyValueFromValue(newMax);
  180. }
  181. if (newMin > newMax)
  182. {
  183. qSwap(newMin, newMax);
  184. }
  185. d->Minimum = newMin;
  186. d->Maximum = newMax;
  187. if (d->Minimum >= d->Value)
  188. {
  189. d->updateOffset(d->Minimum);
  190. }
  191. if (d->Maximum <= d->Value)
  192. {
  193. d->updateOffset(d->Maximum);
  194. }
  195. d->SettingRange = true;
  196. d->Slider->setRange(d->toInt(newMin), d->toInt(newMax));
  197. d->SettingRange = false;
  198. emit this->rangeChanged(d->Minimum, d->Maximum);
  199. /// In case QSlider::setRange(...) didn't notify the value
  200. /// has changed.
  201. this->setValue(this->value());
  202. }
  203. // --------------------------------------------------------------------------
  204. double ctkDoubleSlider::minimum()const
  205. {
  206. Q_D(const ctkDoubleSlider);
  207. double min = d->Minimum;
  208. if (d->Proxy)
  209. {
  210. min = d->Proxy.data()->valueFromProxyValue(min);
  211. }
  212. return min;
  213. }
  214. // --------------------------------------------------------------------------
  215. double ctkDoubleSlider::maximum()const
  216. {
  217. Q_D(const ctkDoubleSlider);
  218. double max = d->Maximum;
  219. if (d->Proxy)
  220. {
  221. max = d->Proxy.data()->valueFromProxyValue(max);
  222. }
  223. return max;
  224. }
  225. // --------------------------------------------------------------------------
  226. double ctkDoubleSlider::sliderPosition()const
  227. {
  228. Q_D(const ctkDoubleSlider);
  229. int intPosition = d->Slider->sliderPosition();
  230. double position = d->safeFromInt(intPosition);
  231. if (d->Proxy)
  232. {
  233. position = d->Proxy.data()->valueFromProxyValue(position);
  234. }
  235. return position;
  236. }
  237. // --------------------------------------------------------------------------
  238. void ctkDoubleSlider::setSliderPosition(double newPosition)
  239. {
  240. Q_D(ctkDoubleSlider);
  241. if (d->Proxy)
  242. {
  243. newPosition = d->Proxy.data()->proxyValueFromValue(newPosition);
  244. }
  245. int newIntPosition = d->toInt(newPosition);
  246. d->Slider->setSliderPosition(newIntPosition);
  247. }
  248. // --------------------------------------------------------------------------
  249. double ctkDoubleSlider::value()const
  250. {
  251. Q_D(const ctkDoubleSlider);
  252. double val = d->Value;
  253. if (d->Proxy)
  254. {
  255. val = d->Proxy.data()->valueFromProxyValue(val);
  256. }
  257. return val;
  258. }
  259. // --------------------------------------------------------------------------
  260. void ctkDoubleSlider::setValue(double newValue)
  261. {
  262. Q_D(ctkDoubleSlider);
  263. if (d->Proxy)
  264. {
  265. newValue = d->Proxy.data()->proxyValueFromValue(newValue);
  266. }
  267. newValue = qBound(d->Minimum, newValue, d->Maximum);
  268. d->updateOffset(newValue);
  269. int newIntValue = d->toInt(newValue);
  270. if (newIntValue != d->Slider->value())
  271. {
  272. // d->Slider will emit a valueChanged signal that is connected to
  273. // ctkDoubleSlider::onValueChanged
  274. d->Slider->setValue(newIntValue);
  275. }
  276. else
  277. {
  278. double oldValue = d->Value;
  279. d->Value = newValue;
  280. // don't emit a valuechanged signal if the new value is quite
  281. // similar to the old value.
  282. if (qAbs(newValue - oldValue) > (d->SingleStep * 0.000000001))
  283. {
  284. emit this->valueChanged(this->value());
  285. }
  286. }
  287. }
  288. // --------------------------------------------------------------------------
  289. double ctkDoubleSlider::singleStep()const
  290. {
  291. Q_D(const ctkDoubleSlider);
  292. double step = d->SingleStep;
  293. return step;
  294. }
  295. // --------------------------------------------------------------------------
  296. void ctkDoubleSlider::setSingleStep(double newStep)
  297. {
  298. Q_D(ctkDoubleSlider);
  299. if (!this->isValidStep(newStep))
  300. {
  301. qWarning() << "ctkDoubleSlider::setSingleStep("<< newStep <<")"
  302. << "is outside of valid bounds.";
  303. return;
  304. }
  305. d->SingleStep = newStep;
  306. d->updateOffset(d->Value);
  307. // update the new values of the QSlider
  308. bool oldBlockSignals = d->Slider->blockSignals(true);
  309. d->Slider->setRange(d->toInt(d->Minimum), d->toInt(d->Maximum));
  310. d->Slider->setValue(d->toInt(d->Value));
  311. d->Slider->setPageStep(d->toInt(d->PageStep));
  312. d->Slider->blockSignals(oldBlockSignals);
  313. Q_ASSERT(qFuzzyCompare(d->Value,d->safeFromInt(d->Slider->value())));
  314. }
  315. // --------------------------------------------------------------------------
  316. bool ctkDoubleSlider::isValidStep(double step)const
  317. {
  318. Q_D(const ctkDoubleSlider);
  319. const double minStep = qMax(d->Maximum / std::numeric_limits<double>::max(),
  320. std::numeric_limits<double>::epsilon());
  321. const double maxStep = qMin(d->Maximum - d->Minimum,
  322. static_cast<double>(std::numeric_limits<int>::max()));
  323. return step >= minStep && step <= maxStep;
  324. }
  325. // --------------------------------------------------------------------------
  326. double ctkDoubleSlider::pageStep()const
  327. {
  328. Q_D(const ctkDoubleSlider);
  329. return d->PageStep;
  330. }
  331. // --------------------------------------------------------------------------
  332. void ctkDoubleSlider::setPageStep(double newStep)
  333. {
  334. Q_D(ctkDoubleSlider);
  335. d->PageStep = newStep;
  336. int intPageStep = d->toInt(d->PageStep);
  337. d->Slider->setPageStep(intPageStep);
  338. }
  339. // --------------------------------------------------------------------------
  340. double ctkDoubleSlider::tickInterval()const
  341. {
  342. Q_D(const ctkDoubleSlider);
  343. // No need to apply Offset
  344. return d->SingleStep * d->Slider->tickInterval();
  345. }
  346. // --------------------------------------------------------------------------
  347. void ctkDoubleSlider::setTickInterval(double newTickInterval)
  348. {
  349. Q_D(ctkDoubleSlider);
  350. d->Slider->setTickInterval(d->toInt(newTickInterval));
  351. }
  352. // --------------------------------------------------------------------------
  353. QSlider::TickPosition ctkDoubleSlider::tickPosition()const
  354. {
  355. Q_D(const ctkDoubleSlider);
  356. return d->Slider->tickPosition();
  357. }
  358. // --------------------------------------------------------------------------
  359. void ctkDoubleSlider::setTickPosition(QSlider::TickPosition newTickPosition)
  360. {
  361. Q_D(ctkDoubleSlider);
  362. d->Slider->setTickPosition(newTickPosition);
  363. }
  364. // --------------------------------------------------------------------------
  365. bool ctkDoubleSlider::hasTracking()const
  366. {
  367. Q_D(const ctkDoubleSlider);
  368. return d->Slider->hasTracking();
  369. }
  370. // --------------------------------------------------------------------------
  371. void ctkDoubleSlider::setTracking(bool enable)
  372. {
  373. Q_D(ctkDoubleSlider);
  374. d->Slider->setTracking(enable);
  375. }
  376. // --------------------------------------------------------------------------
  377. bool ctkDoubleSlider::invertedAppearance()const
  378. {
  379. Q_D(const ctkDoubleSlider);
  380. return d->Slider->invertedAppearance();
  381. }
  382. // --------------------------------------------------------------------------
  383. void ctkDoubleSlider::setInvertedAppearance(bool invertedAppearance)
  384. {
  385. Q_D(ctkDoubleSlider);
  386. d->Slider->setInvertedAppearance(invertedAppearance);
  387. }
  388. // --------------------------------------------------------------------------
  389. bool ctkDoubleSlider::invertedControls()const
  390. {
  391. Q_D(const ctkDoubleSlider);
  392. return d->Slider->invertedControls();
  393. }
  394. // --------------------------------------------------------------------------
  395. void ctkDoubleSlider::setInvertedControls(bool invertedControls)
  396. {
  397. Q_D(ctkDoubleSlider);
  398. d->Slider->setInvertedControls(invertedControls);
  399. }
  400. // --------------------------------------------------------------------------
  401. void ctkDoubleSlider::triggerAction( QAbstractSlider::SliderAction action)
  402. {
  403. Q_D(ctkDoubleSlider);
  404. d->Slider->triggerAction(action);
  405. }
  406. // --------------------------------------------------------------------------
  407. Qt::Orientation ctkDoubleSlider::orientation()const
  408. {
  409. Q_D(const ctkDoubleSlider);
  410. return d->Slider->orientation();
  411. }
  412. // --------------------------------------------------------------------------
  413. void ctkDoubleSlider::setOrientation(Qt::Orientation newOrientation)
  414. {
  415. Q_D(ctkDoubleSlider);
  416. if (this->orientation() == newOrientation)
  417. {
  418. return;
  419. }
  420. if (!testAttribute(Qt::WA_WState_OwnSizePolicy))
  421. {
  422. QSizePolicy sp = this->sizePolicy();
  423. sp.transpose();
  424. this->setSizePolicy(sp);
  425. this->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
  426. }
  427. // d->Slider will take care of calling updateGeometry
  428. d->Slider->setOrientation(newOrientation);
  429. }
  430. // --------------------------------------------------------------------------
  431. QString ctkDoubleSlider::handleToolTip()const
  432. {
  433. Q_D(const ctkDoubleSlider);
  434. return d->HandleToolTip;
  435. }
  436. // --------------------------------------------------------------------------
  437. void ctkDoubleSlider::setHandleToolTip(const QString& toolTip)
  438. {
  439. Q_D(ctkDoubleSlider);
  440. d->HandleToolTip = toolTip;
  441. }
  442. // --------------------------------------------------------------------------
  443. void ctkDoubleSlider::onValueChanged(int newValue)
  444. {
  445. Q_D(ctkDoubleSlider);
  446. double doubleNewValue = d->safeFromInt(newValue);
  447. /*
  448. qDebug() << "onValueChanged: " << newValue << "->"<< d->fromInt(newValue+d->Offset)
  449. << " old: " << d->Value << "->" << d->toInt(d->Value)
  450. << "offset:" << d->Offset << doubleNewValue;
  451. */
  452. if (d->Value == doubleNewValue)
  453. {
  454. return;
  455. }
  456. d->Value = doubleNewValue;
  457. emit this->valueChanged(this->value());
  458. }
  459. // --------------------------------------------------------------------------
  460. void ctkDoubleSlider::onSliderMoved(int newPosition)
  461. {
  462. Q_D(const ctkDoubleSlider);
  463. emit this->sliderMoved(d->safeFromInt(newPosition));
  464. }
  465. // --------------------------------------------------------------------------
  466. void ctkDoubleSlider::onRangeChanged(int min, int max)
  467. {
  468. Q_D(const ctkDoubleSlider);
  469. if (!d->SettingRange)
  470. {
  471. this->setRange(d->fromInt(min), d->fromInt(max));
  472. }
  473. }
  474. // --------------------------------------------------------------------------
  475. bool ctkDoubleSlider::eventFilter(QObject* watched, QEvent* event)
  476. {
  477. Q_D(ctkDoubleSlider);
  478. if (watched == d->Slider)
  479. {
  480. switch(event->type())
  481. {
  482. case QEvent::ToolTip:
  483. {
  484. QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
  485. QStyleOptionSlider opt;
  486. d->Slider->initStyleOption(&opt);
  487. QStyle::SubControl hoveredControl =
  488. d->Slider->style()->hitTestComplexControl(
  489. QStyle::CC_Slider, &opt, helpEvent->pos(), this);
  490. if (!d->HandleToolTip.isEmpty() &&
  491. hoveredControl == QStyle::SC_SliderHandle)
  492. {
  493. QToolTip::showText(helpEvent->globalPos(), d->HandleToolTip.arg(this->value()));
  494. event->accept();
  495. return true;
  496. }
  497. }
  498. default:
  499. break;
  500. }
  501. }
  502. return this->Superclass::eventFilter(watched, event);
  503. }
  504. //----------------------------------------------------------------------------
  505. void ctkDoubleSlider::setValueProxy(ctkValueProxy* proxy)
  506. {
  507. Q_D(ctkDoubleSlider);
  508. if (proxy == d->Proxy.data())
  509. {
  510. return;
  511. }
  512. this->onValueProxyAboutToBeModified();
  513. if (d->Proxy.data())
  514. {
  515. disconnect(d->Proxy.data(), 0, this, 0);
  516. }
  517. d->Proxy = proxy;
  518. if (d->Proxy)
  519. {
  520. connect(d->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  521. this, SLOT(onValueProxyAboutToBeModified()));
  522. connect(d->Proxy.data(), SIGNAL(proxyModified()),
  523. this, SLOT(onValueProxyModified()));
  524. }
  525. this->onValueProxyModified();
  526. }
  527. //----------------------------------------------------------------------------
  528. ctkValueProxy* ctkDoubleSlider::valueProxy() const
  529. {
  530. Q_D(const ctkDoubleSlider);
  531. return d->Proxy.data();
  532. }
  533. // --------------------------------------------------------------------------
  534. void ctkDoubleSlider::onValueProxyAboutToBeModified()
  535. {
  536. Q_D(ctkDoubleSlider);
  537. d->Slider->setProperty("inputValue", this->value());
  538. d->Slider->setProperty("inputMinimum", this->minimum());
  539. d->Slider->setProperty("inputMaximum", this->maximum());
  540. }
  541. // --------------------------------------------------------------------------
  542. void ctkDoubleSlider::onValueProxyModified()
  543. {
  544. Q_D(ctkDoubleSlider);
  545. this->setRange(d->Slider->property("inputMinimum").toDouble(),
  546. d->Slider->property("inputMaximum").toDouble());
  547. this->setValue(d->Slider->property("inputValue").toDouble());
  548. }