ctkDoubleSpinBox.cpp 33 KB

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