ctkDoubleSpinBox.cpp 30 KB

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