ctkDoubleSpinBox.cpp 33 KB

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