ctkDoubleSpinBox_p.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.h"
  16. // Qt includes
  17. #include <QDoubleSpinBox>
  18. class ctkDoubleSpinBoxPrivate;
  19. //-----------------------------------------------------------------------------
  20. class ctkQDoubleSpinBox: public QDoubleSpinBox
  21. {
  22. Q_OBJECT
  23. /// This property controls whether decreasing the value by the mouse
  24. /// button or mouse wheel increases the value of the widget, and inverts the
  25. /// control similarly in the other way round or not. The property is switched off by
  26. /// default.
  27. /// \sa invertedControls(), setInvertedControls()
  28. Q_PROPERTY(bool invertedControls READ invertedControls WRITE setInvertedControls)
  29. public:
  30. typedef QDoubleSpinBox Superclass;
  31. ctkQDoubleSpinBox(ctkDoubleSpinBoxPrivate* pimpl, QWidget* widget);
  32. void setInvertedControls(bool invertedControls);
  33. bool invertedControls() const;
  34. /// Overrides QDoubleSpinBox::stepBy(int) and negates the step number if the
  35. /// invertedControls property is true.
  36. virtual void stepBy(int steps);
  37. /// Expose lineEdit() publicly.
  38. /// \sa QAbstractSpinBox::lineEdit()
  39. virtual QLineEdit* lineEdit()const;
  40. virtual double valueFromText(const QString &text) const;
  41. virtual QString textFromValue(double value) const;
  42. virtual int decimalsFromText(const QString &text) const;
  43. virtual QValidator::State validate(QString& input, int& pos)const;
  44. protected:
  45. ctkDoubleSpinBoxPrivate* const d_ptr;
  46. /// If the invertedControls property is false (by default) then this function
  47. /// behavesLike QDoubleSpinBox::stepEnabled(). If the property is true then
  48. /// stepping down is allowed if the value is less then the maximum, and
  49. /// stepping up is allowed if the value is greater then the minimum.
  50. virtual StepEnabled stepEnabled () const;
  51. bool InvertedControls;
  52. private:
  53. Q_DECLARE_PRIVATE(ctkDoubleSpinBox);
  54. Q_DISABLE_COPY(ctkQDoubleSpinBox);
  55. };
  56. //-----------------------------------------------------------------------------
  57. class ctkDoubleSpinBoxPrivate: public QObject
  58. {
  59. Q_OBJECT
  60. Q_DECLARE_PUBLIC(ctkDoubleSpinBox);
  61. protected:
  62. ctkDoubleSpinBox* const q_ptr;
  63. public:
  64. ctkDoubleSpinBoxPrivate(ctkDoubleSpinBox& object);
  65. ctkQDoubleSpinBox* SpinBox;
  66. ctkDoubleSpinBox::SetMode Mode;
  67. int DefaultDecimals;
  68. int MinimumDecimals;
  69. ctkDoubleSpinBox::DecimalsOptions DOption;
  70. bool InvertedControls;
  71. mutable QString CachedText;
  72. mutable double CachedValue;
  73. mutable QValidator::State CachedState;
  74. mutable int CachedDecimals;
  75. void init();
  76. /// Compare two double previously rounded according to the number of decimals
  77. bool compare(double x1, double x2) const;
  78. /// Remove prefix and suffix
  79. QString stripped(const QString& text, int* pos)const;
  80. /// Return the number of decimals bounded by the allowed min and max number of
  81. /// decimals.
  82. /// If -1, returns the current number of decimals.
  83. int boundDecimals(int decimals)const;
  84. /// Set the number of decimals of the spinbox and emit the signal
  85. /// No check if they are the same.
  86. void setDecimals(int dec);
  87. /// Set value with a specific number of decimals. -1 means the number of
  88. /// decimals stays the same.
  89. void setValue(double value, int dec = -1);
  90. /// Ensure the spinbox text is meaningful.
  91. /// It is called multiple times when the spinbox line edit is modified,
  92. /// therefore values are cached.
  93. double validateAndInterpret(QString &input, int &pos,
  94. QValidator::State &state, int &decimals) const;
  95. public Q_SLOTS:
  96. void editorTextChanged(const QString& text);
  97. };