ctkDoubleSpinBox_p.h 5.0 KB

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