ctkDoubleSpinBox.cpp 30 KB

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