ctkDoubleSlider.cpp 16 KB

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