ctkDoubleSpinBox.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 (text.indexOf(this->locale().decimalPoint()) == -1 &&
  110. ((d->DOption & ctkDoubleSpinBox::DecimalsByKey) ||
  111. (d->DOption & ctkDoubleSpinBox::DecimalsByValue)))
  112. {
  113. text += this->locale().decimalPoint();
  114. }
  115. //qDebug() << "textFromValue" << text;
  116. return text;
  117. }
  118. //-----------------------------------------------------------------------------
  119. int ctkQDoubleSpinBox::decimalsFromText(const QString &text) const
  120. {
  121. Q_D(const ctkDoubleSpinBox);
  122. QString copy = text;
  123. int pos = this->lineEdit()->cursorPosition();
  124. int decimals = 0;
  125. QValidator::State state = QValidator::Acceptable;
  126. d->validateAndInterpret(copy, pos, state, decimals);
  127. return decimals;
  128. }
  129. //-----------------------------------------------------------------------------
  130. QValidator::State ctkQDoubleSpinBox::validate(QString &text, int &pos) const
  131. {
  132. Q_D(const ctkDoubleSpinBox);
  133. QValidator::State state = QValidator::Acceptable;
  134. int decimals = 0;
  135. d->validateAndInterpret(text, pos, state, decimals);
  136. return state;
  137. }
  138. //-----------------------------------------------------------------------------
  139. // ctkDoubleSpinBoxPrivate
  140. //-----------------------------------------------------------------------------
  141. ctkDoubleSpinBoxPrivate::ctkDoubleSpinBoxPrivate(ctkDoubleSpinBox& object)
  142. : q_ptr(&object)
  143. {
  144. qRegisterMetaType<ctkDoubleSpinBox::SetMode>("ctkDoubleSpinBox::SetMode");
  145. qRegisterMetaType<ctkDoubleSpinBox::DecimalsOptions>("ctkDoubleSpinBox::DecimalsOption");
  146. this->SpinBox = 0;
  147. this->Mode = ctkDoubleSpinBox::SetIfDifferent;
  148. this->DefaultDecimals = 2;
  149. this->DOption = ctkDoubleSpinBox::DecimalsByShortcuts;
  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. QString text = this->stripped(input, &pos);
  287. //qDebug() << "text: " << text << pos;
  288. state = QValidator::Acceptable;
  289. decimals = 0;
  290. double value = min;
  291. const int dec = text.indexOf(q->locale().decimalPoint());
  292. bool ok = false;
  293. value = q->locale().toDouble(text, &ok);
  294. // could be in an intermediate state
  295. if (!ok && state == QValidator::Acceptable)
  296. {
  297. if (text.isEmpty() ||
  298. text == "." ||
  299. text == "-" ||
  300. text == "+" ||
  301. text == "-." ||
  302. text == "+.")
  303. {
  304. state = QValidator::Intermediate;
  305. }
  306. }
  307. // could be because of group separators:
  308. if (!ok && state == QValidator::Acceptable)
  309. {
  310. if (q->locale().groupSeparator().isPrint())
  311. {
  312. int start = (dec == -1 ? text.size() : dec)- 1;
  313. int lastGroupSeparator = start;
  314. for (int digit = start; digit >= 0; --digit)
  315. {
  316. if (text.at(digit) == q->locale().groupSeparator())
  317. {
  318. if (digit != lastGroupSeparator - 3)
  319. {
  320. state = QValidator::Invalid;
  321. break;
  322. }
  323. text.remove(digit, 1);
  324. lastGroupSeparator = digit;
  325. }
  326. }
  327. }
  328. // try again without the group separators
  329. value = q->locale().toDouble(text, &ok);
  330. }
  331. // test the decimalPoint
  332. if (!ok && state == QValidator::Acceptable)
  333. {
  334. // duplicate decimal points probably means the user typed another decimal points,
  335. // move the cursor pos to the right then
  336. if (dec + 1 < text.size() &&
  337. text.at(dec + 1) == q->locale().decimalPoint() &&
  338. pos == dec + 1)
  339. {
  340. text.remove(dec + 1, 1);
  341. value = q->locale().toDouble(text, &ok);
  342. }
  343. }
  344. if (ok && state != QValidator::Invalid)
  345. {
  346. if (dec != -1)
  347. {
  348. decimals = text.size() - (dec + 1);
  349. if (decimals > q->decimals())
  350. {
  351. // With ReplaceDecimals on, key strokes replace decimal digits
  352. if (this->DOption & ctkDoubleSpinBox::ReplaceDecimals &&
  353. pos > dec && pos < text.size())
  354. {
  355. text.remove(pos, decimals - q->decimals());
  356. decimals = q->decimals();
  357. value = q->locale().toDouble(text, &ok);
  358. }
  359. }
  360. // When DecimalsByKey is set, it is possible to extend the number of decimals
  361. if (decimals > q->decimals() &&
  362. !(this->DOption & ctkDoubleSpinBox::DecimalsByKey) )
  363. {
  364. state = QValidator::Invalid;
  365. }
  366. }
  367. }
  368. if (state == QValidator::Acceptable)
  369. {
  370. //qDebug() << "Acceptable: " << text << value << ok;
  371. if (!ok)
  372. {
  373. state = QValidator::Invalid;
  374. }
  375. else if (value >= min && value <= max)
  376. {
  377. state = QValidator::Acceptable;
  378. }
  379. else if (max == min)
  380. { // when max and min is the same the only non-Invalid input is max (or min)
  381. state = QValidator::Invalid;
  382. }
  383. else if ((value >= 0 && value > max) || (value < 0 && value < min))
  384. {
  385. state = QValidator::Invalid;
  386. }
  387. else
  388. {
  389. state = QValidator::Intermediate;
  390. }
  391. }
  392. if (state != QValidator::Acceptable)
  393. {
  394. value = max > 0 ? min : max;
  395. }
  396. input = q->prefix() + text + q->suffix();
  397. this->CachedText = input;
  398. this->CachedState = state;
  399. this->CachedValue = value;
  400. this->CachedDecimals = decimals;
  401. //qDebug() << "end: text is '" << this->CachedText << "' state is "
  402. // << int(state) << ", value is " << this->CachedValue
  403. // << " decimals is " << decimals
  404. // << " and pos is " << pos;
  405. return value;
  406. }
  407. /*
  408. //-----------------------------------------------------------------------------
  409. double ctkDoubleSpinBoxPrivate
  410. ::validateAndInterpret(QString &input, int &pos,
  411. QValidator::State &state, int &decimals) const
  412. {
  413. Q_Q(const ctkDoubleSpinBox);
  414. qDebug() << "input: " << input << "pos:" << pos;
  415. if (this->CachedText == input && !input.isEmpty())
  416. {
  417. state = this->CachedState;
  418. decimals = this->CachedDecimals;
  419. qDebug() << "cachedText was '" << this->CachedText << "' state was "
  420. << int(state) << " and value was " << this->CachedValue;
  421. return this->CachedValue;
  422. }
  423. const double max = q->maximum();
  424. const double min = q->minimum();
  425. QString copy = this->stripped(input, &pos);
  426. qDebug() << "copy: " << copy << pos;
  427. int len = copy.size();
  428. double num = min;
  429. const bool plus = max >= 0;
  430. const bool minus = min <= 0;
  431. decimals = 0;
  432. switch (len)
  433. {
  434. case 0:
  435. state = max != min ? QValidator::Intermediate : QValidator::Invalid;
  436. goto end;
  437. break;
  438. case 1:
  439. if (copy.at(0) == q->locale().decimalPoint()
  440. || (plus && copy.at(0) == QLatin1Char('+'))
  441. || (minus && copy.at(0) == QLatin1Char('-')))
  442. {
  443. state = QValidator::Intermediate;
  444. goto end;
  445. }
  446. break;
  447. case 2:
  448. if (copy.at(1) == q->locale().decimalPoint()
  449. && ((plus && copy.at(0) == QLatin1Char('+')) || (minus && copy.at(0) == QLatin1Char('-'))))
  450. {
  451. state = QValidator::Intermediate;
  452. goto end;
  453. }
  454. break;
  455. default:
  456. break;
  457. }
  458. if (copy.at(0) == q->locale().groupSeparator())
  459. {
  460. state = QValidator::Invalid;
  461. goto end;
  462. }
  463. else if (len > 1)
  464. {
  465. const int dec = copy.indexOf(q->locale().decimalPoint());
  466. if (dec != -1)
  467. {
  468. if (dec + 1 < copy.size() && copy.at(dec + 1) == q->locale().decimalPoint() && pos == dec + 1)
  469. {
  470. copy.remove(dec + 1, 1); // typing a delimiter when you are on the delimiter
  471. } // should be treated as typing right arrow
  472. // When DecimalsByKey is set, it is possible to extend the number of decimals
  473. if (!(this->DOption & ctkDoubleSpinBox::DecimalsByKey) &&
  474. (copy.size() - dec > q->decimals() + 1))
  475. {
  476. state = QValidator::Invalid;
  477. goto end;
  478. }
  479. for (int i=dec + 1; i<copy.size(); ++i)
  480. {
  481. if (copy.at(i).isSpace() || copy.at(i) == q->locale().groupSeparator())
  482. {
  483. state = QValidator::Invalid;
  484. goto end;
  485. }
  486. }
  487. decimals = copy.size() - dec - 1;
  488. }
  489. else
  490. {
  491. /// Don't accept lack of decimal point.
  492. /// It could change 1.00 into 100 in 1 key stroke (delete or backspace).
  493. if (this->DOption & ctkDoubleSpinBox::DecimalsByKey)
  494. {
  495. state = QValidator::Invalid;
  496. goto end;
  497. }
  498. const QChar &last = copy.at(len - 1);
  499. const QChar &secondLast = copy.at(len - 2);
  500. if ((last == q->locale().groupSeparator() || last.isSpace())
  501. && (secondLast == q->locale().groupSeparator() || secondLast.isSpace()))
  502. {
  503. state = QValidator::Invalid;
  504. goto end;
  505. }
  506. else if (last.isSpace() && (!q->locale().groupSeparator().isSpace() || secondLast.isSpace()))
  507. {
  508. state = QValidator::Invalid;
  509. goto end;
  510. }
  511. }
  512. }
  513. {
  514. bool ok = false;
  515. num = q->locale().toDouble(copy, &ok);
  516. if (!ok) {
  517. if (q->locale().groupSeparator().isPrint())
  518. {
  519. if (max < 1000 && min > -1000 && copy.contains(q->locale().groupSeparator()))
  520. {
  521. state = QValidator::Invalid;
  522. goto end;
  523. }
  524. const int len = copy.size();
  525. for (int i=0; i<len- 1; ++i)
  526. {
  527. if (copy.at(i) == q->locale().groupSeparator() && copy.at(i + 1) == q->locale().groupSeparator())
  528. {
  529. state = QValidator::Invalid;
  530. goto end;
  531. }
  532. }
  533. QString copy2 = copy;
  534. copy2.remove(q->locale().groupSeparator());
  535. num = q->locale().toDouble(copy2, &ok);
  536. if (!ok)
  537. {
  538. state = QValidator::Invalid;
  539. goto end;
  540. }
  541. }
  542. }
  543. if (!ok)
  544. {
  545. state = QValidator::Invalid;
  546. }
  547. else if (num >= min && num <= max)
  548. {
  549. state = QValidator::Acceptable;
  550. }
  551. else if (max == min)
  552. { // when max and min is the same the only non-Invalid input is max (or min)
  553. state = QValidator::Invalid;
  554. }
  555. else
  556. {
  557. if ((num >= 0 && num > max) || (num < 0 && num < min))
  558. {
  559. state = QValidator::Invalid;
  560. }
  561. else
  562. {
  563. state = QValidator::Intermediate;
  564. }
  565. }
  566. }
  567. end:
  568. if (state != QValidator::Acceptable)
  569. {
  570. num = max > 0 ? min : max;
  571. }
  572. input = q->prefix() + copy + q->suffix();
  573. this->CachedText = input;
  574. this->CachedState = state;
  575. this->CachedValue = num;
  576. this->CachedDecimals = decimals;
  577. qDebug() << "end: text is '" << this->CachedText << "' state is "
  578. << int(state) << ", value is " << this->CachedValue
  579. << " decimals is " << decimals
  580. << " and pos is " << pos;
  581. return num;
  582. }
  583. */
  584. //-----------------------------------------------------------------------------
  585. // ctkDoubleSpinBox
  586. //-----------------------------------------------------------------------------
  587. ctkDoubleSpinBox::ctkDoubleSpinBox(QWidget* newParent)
  588. : QWidget(newParent)
  589. , d_ptr(new ctkDoubleSpinBoxPrivate(*this))
  590. {
  591. Q_D(ctkDoubleSpinBox);
  592. d->init();
  593. }
  594. //-----------------------------------------------------------------------------
  595. ctkDoubleSpinBox::ctkDoubleSpinBox(ctkDoubleSpinBox::SetMode mode, QWidget* newParent)
  596. : QWidget(newParent)
  597. , d_ptr(new ctkDoubleSpinBoxPrivate(*this))
  598. {
  599. Q_D(ctkDoubleSpinBox);
  600. d->init();
  601. this->setSetMode(mode);
  602. }
  603. //-----------------------------------------------------------------------------
  604. double ctkDoubleSpinBox::value() const
  605. {
  606. Q_D(const ctkDoubleSpinBox);
  607. return d->SpinBox->value();
  608. }
  609. //-----------------------------------------------------------------------------
  610. double ctkDoubleSpinBox::displayedValue() const
  611. {
  612. return this->round(this->value());
  613. }
  614. //-----------------------------------------------------------------------------
  615. QString ctkDoubleSpinBox::text() const
  616. {
  617. Q_D(const ctkDoubleSpinBox);
  618. return d->SpinBox->text();
  619. }
  620. //-----------------------------------------------------------------------------
  621. QString ctkDoubleSpinBox::cleanText() const
  622. {
  623. Q_D(const ctkDoubleSpinBox);
  624. return d->SpinBox->cleanText();
  625. }
  626. //-----------------------------------------------------------------------------
  627. Qt::Alignment ctkDoubleSpinBox::alignment() const
  628. {
  629. Q_D(const ctkDoubleSpinBox);
  630. return d->SpinBox->alignment();
  631. }
  632. //-----------------------------------------------------------------------------
  633. void ctkDoubleSpinBox::setAlignment(Qt::Alignment flag)
  634. {
  635. Q_D(const ctkDoubleSpinBox);
  636. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && flag == d->SpinBox->alignment())
  637. {
  638. return;
  639. }
  640. d->SpinBox->setAlignment(flag);
  641. }
  642. //-----------------------------------------------------------------------------
  643. void ctkDoubleSpinBox::setFrame(bool frame)
  644. {
  645. Q_D(const ctkDoubleSpinBox);
  646. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && frame == d->SpinBox->hasFrame())
  647. {
  648. return;
  649. }
  650. d->SpinBox->setFrame(frame);
  651. }
  652. //-----------------------------------------------------------------------------
  653. bool ctkDoubleSpinBox::hasFrame() const
  654. {
  655. Q_D(const ctkDoubleSpinBox);
  656. return d->SpinBox->hasFrame();
  657. }
  658. //-----------------------------------------------------------------------------
  659. QString ctkDoubleSpinBox::prefix() const
  660. {
  661. Q_D(const ctkDoubleSpinBox);
  662. return d->SpinBox->prefix();
  663. }
  664. //-----------------------------------------------------------------------------
  665. void ctkDoubleSpinBox::setPrefix(const QString &prefix)
  666. {
  667. Q_D(const ctkDoubleSpinBox);
  668. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && prefix == d->SpinBox->prefix())
  669. {
  670. return;
  671. }
  672. #if QT_VERSION < 0x040800
  673. /// Setting the prefix doesn't recompute the sizehint, do it manually here:
  674. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  675. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  676. #endif
  677. d->SpinBox->setPrefix(prefix);
  678. }
  679. //-----------------------------------------------------------------------------
  680. QString ctkDoubleSpinBox::suffix() const
  681. {
  682. Q_D(const ctkDoubleSpinBox);
  683. return d->SpinBox->suffix();
  684. }
  685. //-----------------------------------------------------------------------------
  686. void ctkDoubleSpinBox::setSuffix(const QString &suffix)
  687. {
  688. Q_D(const ctkDoubleSpinBox);
  689. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && suffix == d->SpinBox->suffix())
  690. {
  691. return;
  692. }
  693. #if QT_VERSION < 0x040800
  694. /// Setting the suffix doesn't recompute the sizehint, do it manually here:
  695. /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
  696. d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
  697. #endif
  698. d->SpinBox->setSuffix(suffix);
  699. }
  700. //-----------------------------------------------------------------------------
  701. double ctkDoubleSpinBox::singleStep() const
  702. {
  703. Q_D(const ctkDoubleSpinBox);
  704. return d->SpinBox->singleStep();
  705. }
  706. //-----------------------------------------------------------------------------
  707. void ctkDoubleSpinBox::setSingleStep(double step)
  708. {
  709. Q_D(ctkDoubleSpinBox);
  710. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  711. && d->compare(step, this->singleStep()))
  712. {
  713. return;
  714. }
  715. d->SpinBox->setSingleStep(step);
  716. }
  717. //-----------------------------------------------------------------------------
  718. double ctkDoubleSpinBox::minimum() const
  719. {
  720. Q_D(const ctkDoubleSpinBox);
  721. return d->SpinBox->minimum();
  722. }
  723. //-----------------------------------------------------------------------------
  724. void ctkDoubleSpinBox::setMinimum(double min)
  725. {
  726. Q_D(ctkDoubleSpinBox);
  727. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  728. && d->compare(min, this->minimum()))
  729. {
  730. return;
  731. }
  732. d->SpinBox->setMinimum(min);
  733. }
  734. //-----------------------------------------------------------------------------
  735. double ctkDoubleSpinBox::maximum() const
  736. {
  737. Q_D(const ctkDoubleSpinBox);
  738. return d->SpinBox->maximum();
  739. }
  740. //-----------------------------------------------------------------------------
  741. void ctkDoubleSpinBox::setMaximum(double max)
  742. {
  743. Q_D(ctkDoubleSpinBox);
  744. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  745. && d->compare(max, this->maximum()))
  746. {
  747. return;
  748. }
  749. d->SpinBox->setMaximum(max);
  750. }
  751. //-----------------------------------------------------------------------------
  752. void ctkDoubleSpinBox::setRange(double min, double max)
  753. {
  754. Q_D(ctkDoubleSpinBox);
  755. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
  756. && d->compare(max, this->maximum()) && d->compare(min, this->minimum()))
  757. {
  758. return;
  759. }
  760. d->SpinBox->setRange(min, max);
  761. }
  762. //-----------------------------------------------------------------------------
  763. int ctkDoubleSpinBox::decimals() const
  764. {
  765. Q_D(const ctkDoubleSpinBox);
  766. return d->SpinBox->decimals();
  767. }
  768. //-----------------------------------------------------------------------------
  769. void ctkDoubleSpinBox::setDecimals(int dec)
  770. {
  771. Q_D(ctkDoubleSpinBox);
  772. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && dec == this->decimals())
  773. {
  774. return;
  775. }
  776. d->DefaultDecimals = dec;
  777. d->setDecimals(d->DefaultDecimals);
  778. }
  779. //-----------------------------------------------------------------------------
  780. double ctkDoubleSpinBox::round(double value) const
  781. {
  782. Q_D(const ctkDoubleSpinBox);
  783. return QString::number(value, 'f', d->SpinBox->decimals()).toDouble();
  784. }
  785. //-----------------------------------------------------------------------------
  786. QDoubleSpinBox* ctkDoubleSpinBox::spinBox() const
  787. {
  788. Q_D(const ctkDoubleSpinBox);
  789. return d->SpinBox;
  790. }
  791. //-----------------------------------------------------------------------------
  792. QLineEdit* ctkDoubleSpinBox::lineEdit() const
  793. {
  794. Q_D(const ctkDoubleSpinBox);
  795. return d->SpinBox->lineEdit();
  796. }
  797. //-----------------------------------------------------------------------------
  798. void ctkDoubleSpinBox::setValue(double value)
  799. {
  800. Q_D(ctkDoubleSpinBox);
  801. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent)
  802. {
  803. this->setValueIfDifferent(value);
  804. }
  805. else
  806. {
  807. this->setValueAlways(value);
  808. }
  809. }
  810. //-----------------------------------------------------------------------------
  811. void ctkDoubleSpinBox::setValueIfDifferent(double value)
  812. {
  813. Q_D(ctkDoubleSpinBox);
  814. if (! d->compare(this->value(), value))
  815. {
  816. this->setValueAlways(value);
  817. }
  818. }
  819. //-----------------------------------------------------------------------------
  820. void ctkDoubleSpinBox::setValueAlways(double value)
  821. {
  822. Q_D(ctkDoubleSpinBox);
  823. if (d->DOption & ctkDoubleSpinBox::DecimalsByValue)
  824. {
  825. d->setValue(value, ctk::significantDecimals(value));
  826. }
  827. else
  828. {
  829. d->setValue(value);
  830. }
  831. }
  832. //-----------------------------------------------------------------------------
  833. void ctkDoubleSpinBox::stepUp()
  834. {
  835. Q_D(const ctkDoubleSpinBox);
  836. d->SpinBox->stepUp();
  837. }
  838. //-----------------------------------------------------------------------------
  839. void ctkDoubleSpinBox::stepDown()
  840. {
  841. Q_D(const ctkDoubleSpinBox);
  842. d->SpinBox->stepDown();
  843. }
  844. //-----------------------------------------------------------------------------
  845. ctkDoubleSpinBox::SetMode ctkDoubleSpinBox::setMode() const
  846. {
  847. Q_D(const ctkDoubleSpinBox);
  848. return d->Mode;
  849. }
  850. //-----------------------------------------------------------------------------
  851. void ctkDoubleSpinBox::setSetMode(ctkDoubleSpinBox::SetMode newMode)
  852. {
  853. Q_D(ctkDoubleSpinBox);
  854. d->Mode = newMode;
  855. }
  856. //-----------------------------------------------------------------------------
  857. ctkDoubleSpinBox::DecimalsOptions ctkDoubleSpinBox::decimalsOption()
  858. {
  859. Q_D(const ctkDoubleSpinBox);
  860. return d->DOption;
  861. }
  862. //-----------------------------------------------------------------------------
  863. void ctkDoubleSpinBox::setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions option)
  864. {
  865. Q_D(ctkDoubleSpinBox);
  866. if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && option == d->DOption)
  867. {
  868. return;
  869. }
  870. d->DOption = option;
  871. }
  872. //----------------------------------------------------------------------------
  873. void ctkDoubleSpinBox::setInvertedControls(bool invertedControls)
  874. {
  875. Q_D(ctkDoubleSpinBox);
  876. d->InvertedControls = invertedControls;
  877. d->SpinBox->setInvertedControls(d->InvertedControls);
  878. }
  879. //----------------------------------------------------------------------------
  880. bool ctkDoubleSpinBox::invertedControls() const
  881. {
  882. Q_D(const ctkDoubleSpinBox);
  883. return d->InvertedControls;
  884. }
  885. //-----------------------------------------------------------------------------
  886. bool ctkDoubleSpinBox::eventFilter(QObject* obj, QEvent* event)
  887. {
  888. Q_D(ctkDoubleSpinBox);
  889. if (d->DOption & ctkDoubleSpinBox::DecimalsByShortcuts &&
  890. obj == d->SpinBox && event->type() == QEvent::KeyPress)
  891. {
  892. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
  893. Q_ASSERT(keyEvent);
  894. if (keyEvent->modifiers() & Qt::ControlModifier)
  895. {
  896. if (keyEvent->key() == Qt::Key_Plus
  897. || keyEvent->key() == Qt::Key_Equal)
  898. {
  899. d->setDecimals(this->decimals() + 1);
  900. return true;
  901. }
  902. else if (keyEvent->key() == Qt::Key_Minus)
  903. {
  904. d->setDecimals(this->decimals() - 1);
  905. return true;
  906. }
  907. else if (keyEvent->key() == Qt::Key_0)
  908. {
  909. d->setDecimals(d->DefaultDecimals);
  910. return true;
  911. }
  912. }
  913. return QWidget::eventFilter(obj, event);
  914. }
  915. else
  916. {
  917. // pass the event on to the parent class
  918. return QWidget::eventFilter(obj, event);
  919. }
  920. }