ctkDoubleSpinBox.cpp 30 KB

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