ctkDoubleSpinBox.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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. // CTK includes
  15. #include "ctkDoubleSpinBox_p.h"
  16. #include "ctkUtils.h"
  17. #include "ctkValueProxy.h"
  18. #include "ctkPimpl.h"
  19. // Qt includes
  20. #include <QApplication>
  21. #include <QDebug>
  22. #include <QEvent>
  23. #include <QHBoxLayout>
  24. #include <QKeyEvent>
  25. #include <QLineEdit>
  26. #include <QShortcut>
  27. #include <QSizePolicy>
  28. #include <QStyle>
  29. #include <QStyleOptionSpinBox>
  30. #include <QVariant>
  31. //-----------------------------------------------------------------------------
  32. // ctkQDoubleSpinBox
  33. //----------------------------------------------------------------------------
  34. ctkQDoubleSpinBox::ctkQDoubleSpinBox(ctkDoubleSpinBoxPrivate* pimpl,
  35. QWidget* spinBoxParent)
  36. : QDoubleSpinBox(spinBoxParent)
  37. , d_ptr(pimpl)
  38. {
  39. this->InvertedControls = false;
  40. }
  41. //----------------------------------------------------------------------------
  42. QLineEdit* ctkQDoubleSpinBox::lineEdit()const
  43. {
  44. return this->QDoubleSpinBox::lineEdit();
  45. }
  46. //----------------------------------------------------------------------------
  47. void ctkQDoubleSpinBox::initStyleOptionSpinBox(QStyleOptionSpinBox* option)
  48. {
  49. this->initStyleOption(option);
  50. }
  51. //----------------------------------------------------------------------------
  52. void ctkQDoubleSpinBox::setInvertedControls(bool invertedControls)
  53. {
  54. this->InvertedControls = invertedControls;
  55. }
  56. //----------------------------------------------------------------------------
  57. bool ctkQDoubleSpinBox::invertedControls() const
  58. {
  59. return this->InvertedControls;
  60. }
  61. //----------------------------------------------------------------------------
  62. void ctkQDoubleSpinBox::stepBy(int steps)
  63. {
  64. if (this->InvertedControls)
  65. {
  66. steps = -steps;
  67. }
  68. this->Superclass::stepBy(steps);
  69. }
  70. //----------------------------------------------------------------------------
  71. QAbstractSpinBox::StepEnabled ctkQDoubleSpinBox::stepEnabled() const
  72. {
  73. if (!this->InvertedControls)
  74. {
  75. return this->Superclass::stepEnabled();
  76. }
  77. if (this->isReadOnly())
  78. {
  79. return StepNone;
  80. }
  81. if (this->wrapping())
  82. {
  83. return StepEnabled(StepUpEnabled | StepDownEnabled);
  84. }
  85. StepEnabled ret = StepNone;
  86. double value = this->value();
  87. if (value < this->maximum())
  88. {
  89. ret |= StepDownEnabled;
  90. }
  91. if (value > this->minimum())
  92. {
  93. ret |= StepUpEnabled;
  94. }
  95. return ret;
  96. }
  97. //-----------------------------------------------------------------------------
  98. double ctkQDoubleSpinBox::valueFromText(const QString &text) const
  99. {
  100. Q_D(const ctkDoubleSpinBox);
  101. QString copy = text;
  102. int pos = this->lineEdit()->cursorPosition();
  103. QValidator::State state = QValidator::Acceptable;
  104. int decimals = 0;
  105. double value = d->validateAndInterpret(copy, pos, state, decimals);
  106. return value;
  107. }
  108. //-----------------------------------------------------------------------------
  109. QString ctkQDoubleSpinBox::textFromValue(double value) const
  110. {
  111. Q_D(const ctkDoubleSpinBox);
  112. QString text = this->QDoubleSpinBox::textFromValue(value);
  113. if (text.isEmpty())
  114. {
  115. text = "0";
  116. }
  117. // If there is no decimal, it does not mean there won't be any.
  118. if (d->DOption & ctkDoubleSpinBox::DecimalPointAlwaysVisible &&
  119. text.indexOf(this->locale().decimalPoint()) == -1)
  120. {
  121. text += this->locale().decimalPoint();
  122. }
  123. return text;
  124. }
  125. //-----------------------------------------------------------------------------
  126. int ctkQDoubleSpinBox::decimalsFromText(const QString &text) const
  127. {
  128. Q_D(const ctkDoubleSpinBox);
  129. QString copy = text;
  130. int pos = this->lineEdit()->cursorPosition();
  131. int decimals = 0;
  132. QValidator::State state = QValidator::Acceptable;
  133. d->validateAndInterpret(copy, pos, state, decimals);
  134. return decimals;
  135. }
  136. //-----------------------------------------------------------------------------
  137. QValidator::State ctkQDoubleSpinBox::validate(QString &text, int &pos) const
  138. {
  139. Q_D(const ctkDoubleSpinBox);
  140. QValidator::State state = QValidator::Acceptable;
  141. int decimals = 0;
  142. d->validateAndInterpret(text, pos, state, decimals);
  143. return state;
  144. }
  145. //-----------------------------------------------------------------------------
  146. // ctkDoubleSpinBoxPrivate
  147. //-----------------------------------------------------------------------------
  148. ctkDoubleSpinBoxPrivate::ctkDoubleSpinBoxPrivate(ctkDoubleSpinBox& object)
  149. : q_ptr(&object)
  150. {
  151. qRegisterMetaType<ctkDoubleSpinBox::SetMode>("ctkDoubleSpinBox::SetMode");
  152. qRegisterMetaType<ctkDoubleSpinBox::DecimalsOptions>("ctkDoubleSpinBox::DecimalsOption");
  153. this->SpinBox = 0;
  154. this->Mode = ctkDoubleSpinBox::SetIfDifferent;
  155. this->DefaultDecimals = 2;
  156. // InsertDecimals is not a great default, but it is QDoubleSpinBox's default.
  157. this->DOption = ctkDoubleSpinBox::DecimalsByShortcuts
  158. | ctkDoubleSpinBox::InsertDecimals;
  159. this->InvertedControls = false;
  160. this->SizeHintPolicy = ctkDoubleSpinBox::SizeHintByMinMax;
  161. this->InputValue = 0.;
  162. this->InputRange[0] = 0.;
  163. this->InputRange[1] = 99.99;
  164. this->ForceInputValueUpdate = false;
  165. }
  166. //-----------------------------------------------------------------------------
  167. void ctkDoubleSpinBoxPrivate::init()
  168. {
  169. Q_Q(ctkDoubleSpinBox);
  170. this->SpinBox = new ctkQDoubleSpinBox(this, q);
  171. this->SpinBox->setInvertedControls(this->InvertedControls);
  172. // ctkQDoubleSpinBox needs to be first to receive textChanged() signals.
  173. QLineEdit* lineEdit = new QLineEdit(q);
  174. QObject::connect(lineEdit, SIGNAL(textChanged(QString)),
  175. this, SLOT(editorTextChanged(QString)));
  176. this->SpinBox->setLineEdit(lineEdit);
  177. lineEdit->setObjectName(QLatin1String("qt_spinbox_lineedit"));
  178. this->InputValue = this->SpinBox->value();
  179. this->InputRange[0] = this->SpinBox->minimum();
  180. this->InputRange[1] = this->SpinBox->maximum();
  181. QObject::connect(this->SpinBox, SIGNAL(valueChanged(double)),
  182. this, SLOT(onValueChanged()));
  183. QObject::connect(this->SpinBox, SIGNAL(editingFinished()),
  184. q, SIGNAL(editingFinished()));
  185. QHBoxLayout* l = new QHBoxLayout(q);
  186. l->addWidget(this->SpinBox);
  187. l->setContentsMargins(0,0,0,0);
  188. q->setLayout(l);
  189. q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,
  190. QSizePolicy::Fixed, QSizePolicy::ButtonBox));
  191. this->SpinBox->installEventFilter(q);
  192. }
  193. //-----------------------------------------------------------------------------
  194. bool ctkDoubleSpinBoxPrivate::compare(double x1, double x2) const
  195. {
  196. Q_Q(const ctkDoubleSpinBox);
  197. return q->round(x1) == q->round(x2);
  198. }
  199. //-----------------------------------------------------------------------------
  200. double ctkDoubleSpinBoxPrivate::round(double value, int decimals) const
  201. {
  202. return QString::number(value, 'f', decimals).toDouble();
  203. }
  204. //-----------------------------------------------------------------------------
  205. QString ctkDoubleSpinBoxPrivate::stripped(const QString& text, int* pos) const
  206. {
  207. Q_Q(const ctkDoubleSpinBox);
  208. QString strip(text);
  209. if (strip.startsWith(q->prefix()))
  210. {
  211. strip.remove(0, q->prefix().size());
  212. }
  213. if (strip.endsWith(q->suffix()))
  214. {
  215. strip.chop(q->suffix().size());
  216. }
  217. strip = strip.trimmed();
  218. if (pos)
  219. {
  220. int stripInText = text.indexOf(strip);
  221. *pos = qBound(0, *pos - stripInText, strip.size());
  222. }
  223. return strip;
  224. }
  225. //-----------------------------------------------------------------------------
  226. int ctkDoubleSpinBoxPrivate::boundDecimals(int dec)const
  227. {
  228. Q_Q(const ctkDoubleSpinBox);
  229. if (dec == -1)
  230. {
  231. return q->decimals();
  232. }
  233. int min = (this->DOption & ctkDoubleSpinBox::DecimalsAsMin) ?
  234. this->DefaultDecimals : 0;
  235. int max = (this->DOption & ctkDoubleSpinBox::DecimalsAsMax) ?
  236. this->DefaultDecimals : 323; // see QDoubleSpinBox::decimals doc
  237. return qBound(min, dec, max);
  238. }
  239. //-----------------------------------------------------------------------------
  240. int ctkDoubleSpinBoxPrivate::decimalsForValue(double value) const
  241. {
  242. int decimals = this->DefaultDecimals;
  243. if (this->DOption & ctkDoubleSpinBox::DecimalsByValue)
  244. {
  245. decimals = ctk::significantDecimals(value, decimals);
  246. }
  247. return this->boundDecimals(decimals);
  248. }
  249. //-----------------------------------------------------------------------------
  250. void ctkDoubleSpinBoxPrivate::setValue(double value, int dec)
  251. {
  252. Q_Q(ctkDoubleSpinBox);
  253. dec = this->boundDecimals(dec);
  254. bool changeDecimals = dec != q->decimals();
  255. if (changeDecimals)
  256. {
  257. // don't fire decimalsChanged signal yet, wait for the value to be
  258. // up-to-date.
  259. this->SpinBox->setDecimals(dec);
  260. }
  261. this->SpinBox->setValue(value); // re-do the text (calls textFromValue())
  262. if (changeDecimals)
  263. {
  264. emit q->decimalsChanged(dec);
  265. }
  266. if (this->SizeHintPolicy == ctkDoubleSpinBox::SizeHintByValue)
  267. {
  268. this->CachedSizeHint = QSize();
  269. q->updateGeometry();
  270. }
  271. }
  272. //-----------------------------------------------------------------------------
  273. void ctkDoubleSpinBoxPrivate::setDecimals(int dec)
  274. {
  275. Q_Q(ctkDoubleSpinBox);
  276. dec = this->boundDecimals(dec);
  277. this->SpinBox->setDecimals(dec);
  278. emit q->decimalsChanged(dec);
  279. }
  280. //-----------------------------------------------------------------------------
  281. void ctkDoubleSpinBoxPrivate::editorTextChanged(const QString& text)
  282. {
  283. if (this->SpinBox->keyboardTracking())
  284. {
  285. QString tmp = text;
  286. int pos = this->SpinBox->lineEdit()->cursorPosition();
  287. QValidator::State state = QValidator::Invalid;
  288. int decimals = 0;
  289. this->validateAndInterpret(tmp, pos, state, decimals);
  290. if (state == QValidator::Acceptable)
  291. {
  292. double newValue = this->SpinBox->valueFromText(tmp);
  293. int decimals = this->boundDecimals(this->SpinBox->decimalsFromText(tmp));
  294. bool changeDecimals = this->DOption & ctkDoubleSpinBox::DecimalsByKey &&
  295. decimals != this->SpinBox->decimals();
  296. if (changeDecimals)
  297. {
  298. this->ForceInputValueUpdate = true;
  299. this->setValue(newValue, decimals);
  300. this->ForceInputValueUpdate = false;
  301. }
  302. // else, let QDoubleSpinBox process the validation.
  303. }
  304. }
  305. }
  306. //-----------------------------------------------------------------------------
  307. double ctkDoubleSpinBoxPrivate
  308. ::validateAndInterpret(QString &input, int &pos,
  309. QValidator::State &state, int &decimals) const
  310. {
  311. Q_Q(const ctkDoubleSpinBox);
  312. if (this->CachedText == input)
  313. {
  314. state = this->CachedState;
  315. decimals = this->CachedDecimals;
  316. return this->CachedValue;
  317. }
  318. const double max = this->SpinBox->maximum();
  319. const double min = this->SpinBox->minimum();
  320. int posInValue = pos;
  321. QString text = this->stripped(input, &posInValue);
  322. // posInValue can change, track the offset.
  323. const int oldPosInValue = posInValue;
  324. state = QValidator::Acceptable;
  325. decimals = 0;
  326. double value = min;
  327. const int dec = text.indexOf(q->locale().decimalPoint());
  328. bool ok = false;
  329. value = q->locale().toDouble(text, &ok);
  330. // could be in an intermediate state
  331. if (!ok && state == QValidator::Acceptable)
  332. {
  333. if (text.isEmpty() ||
  334. text == "." ||
  335. text == "-" ||
  336. text == "+" ||
  337. text == "-." ||
  338. text == "+.")
  339. {
  340. state = QValidator::Intermediate;
  341. }
  342. }
  343. // could be because of group separators:
  344. if (!ok && state == QValidator::Acceptable)
  345. {
  346. if (q->locale().groupSeparator().isPrint())
  347. {
  348. int start = (dec == -1 ? text.size() : dec)- 1;
  349. int lastGroupSeparator = start;
  350. for (int digit = start; digit >= 0; --digit)
  351. {
  352. if (text.at(digit) == q->locale().groupSeparator())
  353. {
  354. if (digit != lastGroupSeparator - 3)
  355. {
  356. state = QValidator::Invalid;
  357. break;
  358. }
  359. text.remove(digit, 1);
  360. lastGroupSeparator = digit;
  361. }
  362. }
  363. }
  364. // try again without the group separators
  365. value = q->locale().toDouble(text, &ok);
  366. }
  367. // test the decimalPoint
  368. if (!ok && state == QValidator::Acceptable)
  369. {
  370. // duplicate decimal points probably means the user typed another decimal points,
  371. // move the cursor pos to the right then
  372. if (dec + 1 < text.size() &&
  373. text.at(dec + 1) == q->locale().decimalPoint() &&
  374. posInValue == dec + 1)
  375. {
  376. text.remove(dec + 1, 1);
  377. value = q->locale().toDouble(text, &ok);
  378. }
  379. }
  380. if (ok && state != QValidator::Invalid)
  381. {
  382. if (dec != -1)
  383. {
  384. decimals = text.size() - (dec + 1);
  385. if (decimals > q->decimals())
  386. {
  387. // With ReplaceDecimals on, key strokes replace decimal digits
  388. if (posInValue > dec && posInValue < text.size())
  389. {
  390. const int extraDecimals = decimals - q->decimals();
  391. if (this->DOption & ctkDoubleSpinBox::ReplaceDecimals)
  392. {
  393. text.remove(posInValue, extraDecimals);
  394. decimals = q->decimals();
  395. value = q->locale().toDouble(text, &ok);
  396. }
  397. else if (!(this->DOption & ctkDoubleSpinBox::InsertDecimals))
  398. {
  399. text.remove(text.size() - extraDecimals, extraDecimals);
  400. decimals = q->decimals();
  401. value = q->locale().toDouble(text, &ok);
  402. }
  403. }
  404. }
  405. // When DecimalsByKey is set, it is possible to extend the number of decimals
  406. if (decimals > q->decimals() &&
  407. !(this->DOption & ctkDoubleSpinBox::DecimalsByKey) )
  408. {
  409. state = QValidator::Invalid;
  410. }
  411. }
  412. }
  413. if (state == QValidator::Acceptable)
  414. {
  415. if (!ok)
  416. {
  417. state = QValidator::Invalid;
  418. }
  419. else if (value >= min && value <= max)
  420. {
  421. state = QValidator::Acceptable;
  422. }
  423. else if (max == min)
  424. { // when max and min is the same the only non-Invalid input is max (or min)
  425. state = QValidator::Invalid;
  426. }
  427. else if ((value >= 0 && value > max) || (value < 0 && value < min))
  428. {
  429. state = QValidator::Invalid;
  430. }
  431. else
  432. {
  433. state = QValidator::Intermediate;
  434. }
  435. }
  436. if (state != QValidator::Acceptable)
  437. {
  438. value = max > 0 ? min : max;
  439. }
  440. pos += posInValue - oldPosInValue;
  441. input = q->prefix() + text + q->suffix();
  442. this->CachedText = input;
  443. this->CachedState = state;
  444. this->CachedValue = value;
  445. this->CachedDecimals = decimals;
  446. return value;
  447. }
  448. //-----------------------------------------------------------------------------
  449. void ctkDoubleSpinBoxPrivate::onValueChanged()
  450. {
  451. Q_Q(ctkDoubleSpinBox);
  452. double newValue = this->SpinBox->value();
  453. double oldValue = q->value();
  454. if (this->Proxy)
  455. {
  456. oldValue = this->Proxy.data()->proxyValueFromValue(oldValue);
  457. }
  458. // Don't trigger value changed signal if the difference only happened on the
  459. // precision.
  460. if (this->compare(oldValue, newValue) && !this->ForceInputValueUpdate)
  461. {
  462. return;
  463. }
  464. // Force it only once (when the user typed a new number that could have change
  465. // the number of decimals which could have make the compare test always pass.
  466. this->ForceInputValueUpdate = false;
  467. double minimum = q->minimum();
  468. double maximum = q->maximum();
  469. if (this->Proxy)
  470. {
  471. minimum = this->Proxy.data()->proxyValueFromValue(minimum);
  472. maximum = this->Proxy.data()->proxyValueFromValue(maximum);
  473. }
  474. // Special case to return max precision
  475. if (this->compare(minimum, newValue))
  476. {
  477. newValue = q->minimum();
  478. }
  479. else if (this->compare(maximum, newValue))
  480. {
  481. newValue = q->maximum();
  482. }
  483. else if (this->Proxy)
  484. {
  485. newValue = this->Proxy.data()->valueFromProxyValue(newValue);
  486. }
  487. this->InputValue = newValue;
  488. emit q->valueChanged(newValue);
  489. // \tbd The string might not make much sense when using proxies.
  490. emit q->valueChanged(
  491. QString::number(newValue, 'f', this->SpinBox->decimals()));
  492. }
  493. //-----------------------------------------------------------------------------
  494. void ctkDoubleSpinBoxPrivate::onValueProxyAboutToBeModified()
  495. {
  496. }
  497. //-----------------------------------------------------------------------------
  498. void ctkDoubleSpinBoxPrivate::onValueProxyModified()
  499. {
  500. Q_Q(ctkDoubleSpinBox);
  501. int oldDecimals = q->decimals();
  502. double oldValue = this->InputValue;
  503. ctkDoubleSpinBox::SetMode oldSetMode = this->Mode;
  504. // Only the display is changed, not the programatic value, no need to trigger
  505. // signals
  506. bool wasBlocking = q->blockSignals(true);
  507. // Enforce a refresh. Signals are blocked so it should not trigger unwanted
  508. // signals
  509. this->Mode = ctkDoubleSpinBox::SetAlways;
  510. q->setRange(this->InputRange[0], this->InputRange[1]);
  511. q->setValue(oldValue);
  512. this->Mode = oldSetMode;
  513. q->blockSignals(wasBlocking);
  514. // Decimals might change when value proxy is modified.
  515. if (oldDecimals != q->decimals())
  516. {
  517. emit q->decimalsChanged(q->decimals());
  518. }
  519. }
  520. //-----------------------------------------------------------------------------
  521. // ctkDoubleSpinBox
  522. //-----------------------------------------------------------------------------
  523. ctkDoubleSpinBox::ctkDoubleSpinBox(QWidget* newParent)
  524. : QWidget(newParent)
  525. , d_ptr(new ctkDoubleSpinBoxPrivate(*this))
  526. {
  527. Q_D(ctkDoubleSpinBox);
  528. d->init();
  529. }
  530. //-----------------------------------------------------------------------------
  531. ctkDoubleSpinBox::ctkDoubleSpinBox(ctkDoubleSpinBox::SetMode mode, QWidget* newParent)
  532. : QWidget(newParent)
  533. , d_ptr(new ctkDoubleSpinBoxPrivate(*this))
  534. {
  535. Q_D(ctkDoubleSpinBox);
  536. d->init();
  537. this->setSetMode(mode);
  538. }
  539. //-----------------------------------------------------------------------------
  540. double ctkDoubleSpinBox::value() const
  541. {
  542. Q_D(const ctkDoubleSpinBox);
  543. return d->InputValue;
  544. }
  545. //-----------------------------------------------------------------------------
  546. double ctkDoubleSpinBox::displayedValue() const
  547. {
  548. Q_D(const ctkDoubleSpinBox);
  549. return d->SpinBox->value();
  550. }
  551. //----------------------------------------------------------------------------
  552. void ctkDoubleSpinBox::setDisplayedValue(double value)
  553. {
  554. Q_D(ctkDoubleSpinBox);
  555. d->SpinBox->setValue(value);
  556. }
  557. //-----------------------------------------------------------------------------
  558. QString ctkDoubleSpinBox::text() const
  559. {
  560. Q_D(const ctkDoubleSpinBox);
  561. return d->SpinBox->text();
  562. }
  563. //-----------------------------------------------------------------------------
  564. QString ctkDoubleSpinBox::cleanText() const
  565. {
  566. Q_D(const ctkDoubleSpinBox);
  567. return d->SpinBox->cleanText();
  568. }
  569. //-----------------------------------------------------------------------------
  570. Qt::Alignment ctkDoubleSpinBox::alignment() const
  571. {
  572. Q_D(const ctkDoubleSpinBox);
  573. return d->SpinBox->alignment();
  574. }
  575. //-----------------------------------------------------------------------------
  576. void ctkDoubleSpinBox::setAlignment(Qt::Alignment flag)
  577. {
  578. Q_D(const ctkDoubleSpinBox);
  579. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && flag == d->SpinBox->alignment())
  580. {
  581. return;
  582. }
  583. d->SpinBox->setAlignment(flag);
  584. }
  585. //-----------------------------------------------------------------------------
  586. void ctkDoubleSpinBox::setFrame(bool frame)
  587. {
  588. Q_D(const ctkDoubleSpinBox);
  589. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && frame == d->SpinBox->hasFrame())
  590. {
  591. return;
  592. }
  593. d->SpinBox->setFrame(frame);
  594. }
  595. //-----------------------------------------------------------------------------
  596. bool ctkDoubleSpinBox::hasFrame() const
  597. {
  598. Q_D(const ctkDoubleSpinBox);
  599. return d->SpinBox->hasFrame();
  600. }
  601. //-----------------------------------------------------------------------------
  602. QString ctkDoubleSpinBox::prefix() const
  603. {
  604. Q_D(const ctkDoubleSpinBox);
  605. return d->SpinBox->prefix();
  606. }
  607. //-----------------------------------------------------------------------------
  608. void ctkDoubleSpinBox::setPrefix(const QString &prefix)
  609. {
  610. Q_D(const ctkDoubleSpinBox);
  611. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && prefix == d->SpinBox->prefix())
  612. {
  613. return;
  614. }
  615. #if QT_VERSION < 0x040800
  616. /// Setting the prefix doesn't recompute the sizehint, do it manually here:
  617. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  618. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  619. #endif
  620. d->SpinBox->setPrefix(prefix);
  621. }
  622. //-----------------------------------------------------------------------------
  623. QString ctkDoubleSpinBox::suffix() const
  624. {
  625. Q_D(const ctkDoubleSpinBox);
  626. return d->SpinBox->suffix();
  627. }
  628. //-----------------------------------------------------------------------------
  629. void ctkDoubleSpinBox::setSuffix(const QString &suffix)
  630. {
  631. Q_D(const ctkDoubleSpinBox);
  632. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && suffix == d->SpinBox->suffix())
  633. {
  634. return;
  635. }
  636. #if QT_VERSION < 0x040800
  637. /// Setting the suffix doesn't recompute the sizehint, do it manually here:
  638. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  639. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  640. #endif
  641. d->SpinBox->setSuffix(suffix);
  642. }
  643. //-----------------------------------------------------------------------------
  644. double ctkDoubleSpinBox::singleStep() const
  645. {
  646. Q_D(const ctkDoubleSpinBox);
  647. double step = d->SpinBox->singleStep();
  648. return step;
  649. }
  650. //-----------------------------------------------------------------------------
  651. void ctkDoubleSpinBox::setSingleStep(double newStep)
  652. {
  653. Q_D(ctkDoubleSpinBox);
  654. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  655. && d->compare(newStep, this->singleStep()))
  656. {
  657. return;
  658. }
  659. d->SpinBox->setSingleStep(newStep);
  660. }
  661. //-----------------------------------------------------------------------------
  662. double ctkDoubleSpinBox::minimum() const
  663. {
  664. Q_D(const ctkDoubleSpinBox);
  665. return d->InputRange[0];
  666. }
  667. //-----------------------------------------------------------------------------
  668. void ctkDoubleSpinBox::setMinimum(double newMin)
  669. {
  670. this->setRange(newMin, qMax(newMin, this->maximum()));
  671. }
  672. //-----------------------------------------------------------------------------
  673. double ctkDoubleSpinBox::maximum() const
  674. {
  675. Q_D(const ctkDoubleSpinBox);
  676. return d->InputRange[1];
  677. }
  678. //-----------------------------------------------------------------------------
  679. void ctkDoubleSpinBox::setMaximum(double newMax)
  680. {
  681. this->setRange(qMin(newMax, this->minimum()), newMax);
  682. }
  683. //-----------------------------------------------------------------------------
  684. void ctkDoubleSpinBox::setRange(double newMin, double newMax)
  685. {
  686. Q_D(ctkDoubleSpinBox);
  687. if (newMin > newMax)
  688. {
  689. qSwap(newMin, newMax);
  690. }
  691. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  692. && newMin == d->InputRange[0]
  693. && newMax == d->InputRange[1])
  694. {
  695. return;
  696. }
  697. d->InputRange[0] = newMin;
  698. d->InputRange[1] = newMax;
  699. if (d->Proxy)
  700. {
  701. newMin = d->Proxy.data()->proxyValueFromValue(newMin);
  702. newMax = d->Proxy.data()->proxyValueFromValue(newMax);
  703. if (newMin > newMax)
  704. {
  705. qSwap(newMin, newMax);
  706. }
  707. }
  708. d->SpinBox->setRange(newMin, newMax);
  709. }
  710. //-----------------------------------------------------------------------------
  711. int ctkDoubleSpinBox::decimals() const
  712. {
  713. Q_D(const ctkDoubleSpinBox);
  714. return d->SpinBox->decimals();
  715. }
  716. //-----------------------------------------------------------------------------
  717. void ctkDoubleSpinBox::setDecimals(int dec)
  718. {
  719. Q_D(ctkDoubleSpinBox);
  720. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  721. && dec == this->decimals()
  722. && dec == d->DefaultDecimals)
  723. {
  724. return;
  725. }
  726. d->DefaultDecimals = dec;
  727. // The number of decimals may or may not depend on the value. Recompute the
  728. // new number of decimals.
  729. double currentValue = this->value();
  730. if (d->Proxy)
  731. {
  732. currentValue = d->Proxy.data()->proxyValueFromValue(currentValue);
  733. }
  734. int newDecimals = d->decimalsForValue(currentValue);
  735. d->setValue(currentValue, newDecimals);
  736. }
  737. //-----------------------------------------------------------------------------
  738. double ctkDoubleSpinBox::round(double value) const
  739. {
  740. Q_D(const ctkDoubleSpinBox);
  741. return QString::number(value, 'f', d->SpinBox->decimals()).toDouble();
  742. }
  743. //-----------------------------------------------------------------------------
  744. QDoubleSpinBox* ctkDoubleSpinBox::spinBox() const
  745. {
  746. Q_D(const ctkDoubleSpinBox);
  747. return d->SpinBox;
  748. }
  749. //-----------------------------------------------------------------------------
  750. QLineEdit* ctkDoubleSpinBox::lineEdit() const
  751. {
  752. Q_D(const ctkDoubleSpinBox);
  753. return d->SpinBox->lineEdit();
  754. }
  755. //-----------------------------------------------------------------------------
  756. void ctkDoubleSpinBox::setValue(double value)
  757. {
  758. Q_D(ctkDoubleSpinBox);
  759. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent)
  760. {
  761. this->setValueIfDifferent(value);
  762. }
  763. else
  764. {
  765. this->setValueAlways(value);
  766. }
  767. }
  768. //-----------------------------------------------------------------------------
  769. void ctkDoubleSpinBox::setValueIfDifferent(double newValue)
  770. {
  771. Q_D(ctkDoubleSpinBox);
  772. if (newValue == d->InputValue)
  773. {
  774. return;
  775. }
  776. this->setValueAlways(newValue);
  777. }
  778. //-----------------------------------------------------------------------------
  779. void ctkDoubleSpinBox::setValueAlways(double newValue)
  780. {
  781. Q_D(ctkDoubleSpinBox);
  782. newValue = qBound(d->InputRange[0], newValue, d->InputRange[1]);
  783. const bool valueModified = d->InputValue != newValue;
  784. d->InputValue = newValue;
  785. double newValueToDisplay = newValue;
  786. if (d->Proxy)
  787. {
  788. newValueToDisplay = d->Proxy.data()->proxyValueFromValue(newValueToDisplay);
  789. }
  790. const int decimals = d->decimalsForValue(newValueToDisplay);
  791. d->setValue(newValueToDisplay, decimals);
  792. const bool signalsEmitted = (newValue != d->InputValue);
  793. if (valueModified && !signalsEmitted)
  794. {
  795. emit valueChanged(d->InputValue);
  796. emit valueChanged(QString::number(d->InputValue, 'f', d->SpinBox->decimals()));
  797. }
  798. }
  799. //-----------------------------------------------------------------------------
  800. void ctkDoubleSpinBox::stepUp()
  801. {
  802. Q_D(const ctkDoubleSpinBox);
  803. d->SpinBox->stepUp();
  804. }
  805. //-----------------------------------------------------------------------------
  806. void ctkDoubleSpinBox::stepDown()
  807. {
  808. Q_D(const ctkDoubleSpinBox);
  809. d->SpinBox->stepDown();
  810. }
  811. //-----------------------------------------------------------------------------
  812. ctkDoubleSpinBox::SetMode ctkDoubleSpinBox::setMode() const
  813. {
  814. Q_D(const ctkDoubleSpinBox);
  815. return d->Mode;
  816. }
  817. //-----------------------------------------------------------------------------
  818. void ctkDoubleSpinBox::setSetMode(ctkDoubleSpinBox::SetMode newMode)
  819. {
  820. Q_D(ctkDoubleSpinBox);
  821. d->Mode = newMode;
  822. }
  823. //-----------------------------------------------------------------------------
  824. ctkDoubleSpinBox::DecimalsOptions ctkDoubleSpinBox::decimalsOption()
  825. {
  826. Q_D(const ctkDoubleSpinBox);
  827. return d->DOption;
  828. }
  829. //-----------------------------------------------------------------------------
  830. void ctkDoubleSpinBox::setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions option)
  831. {
  832. Q_D(ctkDoubleSpinBox);
  833. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && option == d->DOption)
  834. {
  835. return;
  836. }
  837. d->DOption = option;
  838. this->setValueAlways(this->value());
  839. }
  840. //----------------------------------------------------------------------------
  841. void ctkDoubleSpinBox::setInvertedControls(bool invertedControls)
  842. {
  843. Q_D(ctkDoubleSpinBox);
  844. d->InvertedControls = invertedControls;
  845. d->SpinBox->setInvertedControls(d->InvertedControls);
  846. }
  847. //----------------------------------------------------------------------------
  848. bool ctkDoubleSpinBox::invertedControls() const
  849. {
  850. Q_D(const ctkDoubleSpinBox);
  851. return d->InvertedControls;
  852. }
  853. //----------------------------------------------------------------------------
  854. void ctkDoubleSpinBox
  855. ::setSizeHintPolicy(ctkDoubleSpinBox::SizeHintPolicy newSizeHintPolicy)
  856. {
  857. Q_D(ctkDoubleSpinBox);
  858. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  859. && newSizeHintPolicy == d->SizeHintPolicy)
  860. {
  861. return;
  862. }
  863. d->SizeHintPolicy = newSizeHintPolicy;
  864. d->CachedSizeHint = QSize();
  865. this->updateGeometry();
  866. }
  867. //----------------------------------------------------------------------------
  868. ctkDoubleSpinBox::SizeHintPolicy ctkDoubleSpinBox::sizeHintPolicy() const
  869. {
  870. Q_D(const ctkDoubleSpinBox);
  871. return d->SizeHintPolicy;
  872. }
  873. //----------------------------------------------------------------------------
  874. void ctkDoubleSpinBox::setValueProxy(ctkValueProxy* proxy)
  875. {
  876. Q_D(ctkDoubleSpinBox);
  877. if (proxy == d->Proxy.data())
  878. {
  879. return;
  880. }
  881. d->onValueProxyAboutToBeModified();
  882. if (d->Proxy)
  883. {
  884. disconnect(d->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  885. d, SLOT(onValueProxyAboutToBeModified()));
  886. disconnect(d->Proxy.data(), SIGNAL(proxyModified()),
  887. d, SLOT(onValueProxyModified()));
  888. }
  889. d->Proxy = proxy;
  890. if (d->Proxy)
  891. {
  892. connect(d->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  893. d, SLOT(onValueProxyAboutToBeModified()));
  894. connect(d->Proxy.data(), SIGNAL(proxyModified()),
  895. d, SLOT(onValueProxyModified()));
  896. }
  897. d->onValueProxyModified();
  898. }
  899. //----------------------------------------------------------------------------
  900. ctkValueProxy* ctkDoubleSpinBox::valueProxy() const
  901. {
  902. Q_D(const ctkDoubleSpinBox);
  903. return d->Proxy.data();
  904. }
  905. //----------------------------------------------------------------------------
  906. QSize ctkDoubleSpinBox::sizeHint() const
  907. {
  908. Q_D(const ctkDoubleSpinBox);
  909. if (d->SizeHintPolicy == ctkDoubleSpinBox::SizeHintByMinMax)
  910. {
  911. return this->Superclass::sizeHint();
  912. }
  913. if (!d->CachedSizeHint.isEmpty())
  914. {
  915. return d->CachedSizeHint;
  916. }
  917. QSize newSizeHint;
  918. newSizeHint.setHeight(this->lineEdit()->sizeHint().height());
  919. QString extraString = " "; // give some room
  920. QString s = this->text() + extraString;
  921. s.truncate(18);
  922. int extraWidth = 2; // cursor width
  923. this->ensurePolished(); // ensure we are using the right font
  924. const QFontMetrics fm(this->fontMetrics());
  925. newSizeHint.setWidth(fm.width(s + extraString) + extraWidth);
  926. QStyleOptionSpinBox opt;
  927. d->SpinBox->initStyleOptionSpinBox(&opt);
  928. QSize extraSize(35, 6);
  929. opt.rect.setSize(newSizeHint + extraSize);
  930. extraSize += newSizeHint - this->style()->subControlRect(
  931. QStyle::CC_SpinBox, &opt,
  932. QStyle::SC_SpinBoxEditField, this).size();
  933. // Converging size hint...
  934. opt.rect.setSize(newSizeHint + extraSize);
  935. extraSize += newSizeHint - this->style()->subControlRect(
  936. QStyle::CC_SpinBox, &opt,
  937. QStyle::SC_SpinBoxEditField, this).size();
  938. newSizeHint += extraSize;
  939. opt.rect = this->rect();
  940. d->CachedSizeHint = this->style()->sizeFromContents(
  941. QStyle::CT_SpinBox, &opt, newSizeHint, this)
  942. .expandedTo(QApplication::globalStrut());
  943. return d->CachedSizeHint;
  944. }
  945. //----------------------------------------------------------------------------
  946. QSize ctkDoubleSpinBox::minimumSizeHint() const
  947. {
  948. // For some reasons, Superclass::minimumSizeHint() returns the spinbox
  949. // sizeHint()
  950. return this->spinBox()->minimumSizeHint();
  951. }
  952. //-----------------------------------------------------------------------------
  953. void ctkDoubleSpinBox::keyPressEvent(QKeyEvent* event)
  954. {
  955. Q_D(ctkDoubleSpinBox);
  956. const bool accept = this->eventFilter(d->SpinBox, event);
  957. event->setAccepted(accept);
  958. }
  959. //-----------------------------------------------------------------------------
  960. bool ctkDoubleSpinBox::eventFilter(QObject* obj, QEvent* event)
  961. {
  962. Q_D(ctkDoubleSpinBox);
  963. if (d->DOption & ctkDoubleSpinBox::DecimalsByShortcuts &&
  964. obj == d->SpinBox && event->type() == QEvent::KeyPress)
  965. {
  966. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  967. Q_ASSERT(keyEvent);
  968. int newDecimals = -1;
  969. if (keyEvent->modifiers() & Qt::ControlModifier)
  970. {
  971. if (keyEvent->key() == Qt::Key_Plus
  972. || keyEvent->key() == Qt::Key_Equal)
  973. {
  974. newDecimals = this->decimals() + 1;
  975. }
  976. else if (keyEvent->key() == Qt::Key_Minus)
  977. {
  978. newDecimals = this->decimals() - 1;
  979. }
  980. else if (keyEvent->key() == Qt::Key_0)
  981. {
  982. newDecimals = d->DefaultDecimals;
  983. }
  984. }
  985. if (newDecimals != -1)
  986. {
  987. double currentValue = this->value();
  988. if (d->Proxy)
  989. {
  990. currentValue = d->Proxy.data()->proxyValueFromValue(currentValue);
  991. }
  992. // increasing the number of decimals should restore lost precision
  993. d->setValue(currentValue, newDecimals);
  994. return true;
  995. }
  996. return QWidget::eventFilter(obj, event);
  997. }
  998. else
  999. {
  1000. // pass the event on to the parent class
  1001. return QWidget::eventFilter(obj, event);
  1002. }
  1003. }