ctkRangeWidget.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifndef __ctkRangeWidget_h
  15. #define __ctkRangeWidget_h
  16. // Qt includes
  17. #include <QSlider>
  18. // CTK includes
  19. #include <ctkPimpl.h>
  20. #include "ctkWidgetsExport.h"
  21. class ctkDoubleRangeSlider;
  22. class ctkDoubleSpinBox;
  23. class ctkRangeWidgetPrivate;
  24. class ctkValueProxy;
  25. /// \ingroup Widgets
  26. ///
  27. /// ctkRangeWidget is a wrapper around a ctkDoubleRangeSlider and 2 QSpinBoxes
  28. /// \image html http://www.commontk.org/images/1/14/CtkRangeWidget.png
  29. /// \sa ctkSliderSpinBoxWidget, ctkDoubleRangeSlider, QSpinBox
  30. class CTK_WIDGETS_EXPORT ctkRangeWidget : public QWidget
  31. {
  32. Q_OBJECT
  33. Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
  34. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
  35. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  36. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  37. Q_PROPERTY(double minimumValue READ minimumValue WRITE setMinimumValue)
  38. Q_PROPERTY(double maximumValue READ maximumValue WRITE setMaximumValue)
  39. Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
  40. Q_PROPERTY(QString suffix READ suffix WRITE setSuffix)
  41. Q_PROPERTY(double tickInterval READ tickInterval WRITE setTickInterval)
  42. Q_PROPERTY(bool autoSpinBoxWidth READ isAutoSpinBoxWidth WRITE setAutoSpinBoxWidth)
  43. Q_PROPERTY(Qt::Alignment spinBoxTextAlignment READ spinBoxTextAlignment WRITE setSpinBoxTextAlignment)
  44. Q_PROPERTY(Qt::Alignment spinBoxAlignment READ spinBoxAlignment WRITE setSpinBoxAlignment)
  45. Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
  46. Q_PROPERTY(bool symmetricMoves READ symmetricMoves WRITE setSymmetricMoves)
  47. public:
  48. /// Superclass typedef
  49. typedef QWidget Superclass;
  50. /// Constructor
  51. /// If \li parent is null, ctkRangeWidget will be a top-leve widget
  52. /// \note The \li parent can be set later using QWidget::setParent()
  53. explicit ctkRangeWidget(QWidget* parent = 0);
  54. /// Destructor
  55. virtual ~ctkRangeWidget();
  56. ///
  57. /// This property holds the sliders and spinbox minimum value.
  58. /// FIXME: Test following specs.
  59. /// When setting this property, the maximum is adjusted if necessary
  60. /// to ensure that the range remains valid.
  61. /// Also the slider's current value is adjusted to be within the new range.
  62. double minimum()const;
  63. void setMinimum(double minimum);
  64. ///
  65. /// This property holds the sliders and spinbox minimum value.
  66. /// FIXME: Test following specs.
  67. /// When setting this property, the maximum is adjusted if necessary
  68. /// to ensure that the range remains valid.
  69. /// Also the slider's current value is adjusted to be within the new range.
  70. double maximum()const;
  71. void setMaximum(double maximum);
  72. /// Description
  73. /// Utility function that set the min/max in once
  74. void setRange(double min, double max);
  75. /// Description
  76. /// Return the range of the slider
  77. void range(double* range)const;
  78. ///
  79. /// This property holds the slider and spinbox minimum value.
  80. /// ctkRangeWidget forces the value to be within the
  81. /// legal range: minimum <= minimumValue <= maximumValue <= maximum.
  82. double minimumValue()const;
  83. ///
  84. /// This property holds the slider and spinbox maximum value.
  85. /// ctkRangeWidget forces the value to be within the
  86. /// legal range: minimum <= minimumValue <= maximumValue <= maximum.
  87. double maximumValue()const;
  88. ///
  89. /// Utility function that returns both values at the same time
  90. /// Returns minimumValue and maximumValue
  91. void values(double &minValue, double &maxValue)const;
  92. ///
  93. /// This property holds the single step.
  94. /// The smaller of two natural steps that the
  95. /// slider provides and typically corresponds to the
  96. /// user pressing an arrow key.
  97. double singleStep()const;
  98. void setSingleStep(double step);
  99. ///
  100. /// This property holds the precision of the spin box, in decimals.
  101. int decimals()const;
  102. ///
  103. /// This property holds the spin box's prefix.
  104. /// The prefix is prepended to the start of the displayed value.
  105. /// Typical use is to display a unit of measurement or a currency symbol
  106. QString prefix()const;
  107. void setPrefix(const QString& prefix);
  108. ///
  109. /// This property holds the spin box's suffix.
  110. /// The suffix is appended to the end of the displayed value.
  111. /// Typical use is to display a unit of measurement or a currency symbol
  112. QString suffix()const;
  113. void setSuffix(const QString& suffix);
  114. ///
  115. /// This property holds the interval between tickmarks.
  116. /// This is a value interval, not a pixel interval.
  117. /// If it is 0, the slider will choose between lineStep() and pageStep().
  118. /// The default value is 0.
  119. double tickInterval()const;
  120. void setTickInterval(double ti);
  121. ///
  122. /// This property holds the alignment of the spin boxes.
  123. /// Possible Values are Qt::AlignTop, Qt::AlignBottom, and Qt::AlignVCenter.
  124. /// By default, the alignment is Qt::AlignVCenter
  125. void setSpinBoxAlignment(Qt::Alignment alignment);
  126. Qt::Alignment spinBoxAlignment()const;
  127. ///
  128. /// This property holds the alignment of the text inside the spin boxes.
  129. /// Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter.
  130. /// By default, the alignment is Qt::AlignLeft
  131. void setSpinBoxTextAlignment(Qt::Alignment alignment);
  132. Qt::Alignment spinBoxTextAlignment()const;
  133. ///
  134. /// This property holds whether slider tracking is enabled.
  135. /// If tracking is enabled (the default), the widget emits the valueChanged()
  136. /// signal while the slider or spinbox is being dragged. If tracking is
  137. /// disabled, the widget emits the valueChanged() signal only when the user
  138. /// releases the slider or spinbox.
  139. void setTracking(bool enable);
  140. bool hasTracking()const;
  141. ///
  142. /// Set/Get the auto spinbox width
  143. /// When the autoSpinBoxWidth property is on, the width of the SpinBox is
  144. /// set to the same width of the largest QSpinBox of its
  145. // ctkRangeWidget siblings.
  146. bool isAutoSpinBoxWidth()const;
  147. void setAutoSpinBoxWidth(bool autoWidth);
  148. ///
  149. /// When symmetricMoves is true, moving a handle will move the other handle
  150. /// symmetrically, otherwise the handles are independent. False by default
  151. bool symmetricMoves()const;
  152. void setSymmetricMoves(bool symmetry);
  153. /// Return the slider of the range widget.
  154. /// \sa minimumSpinBox(), maximumSpinBox()
  155. ctkDoubleRangeSlider* slider()const;
  156. /// Return the minimum spinbox.
  157. /// \sa maximumSpinBox(), slider()
  158. ctkDoubleSpinBox* minimumSpinBox()const;
  159. /// Return the maximum spinbox.
  160. /// \sa minimumSpinBox(), slider()
  161. ctkDoubleSpinBox* maximumSpinBox()const;
  162. /// Set/Get the value proxy of the slider and spinboxes.
  163. /// \sa setValueProxy(), valueProxy()
  164. void setValueProxy(ctkValueProxy* proxy);
  165. ctkValueProxy* valueProxy() const;
  166. public Q_SLOTS:
  167. ///
  168. /// Reset the slider and spinbox to zero (value and position)
  169. void reset();
  170. void setMinimumValue(double value);
  171. void setMaximumValue(double value);
  172. ///
  173. /// Utility function that set the min and max values at once
  174. void setValues(double minValue, double maxValue);
  175. /// Sets how many decimals the spinbox will use for displaying and
  176. /// interpreting doubles.
  177. void setDecimals(int decimals);
  178. Q_SIGNALS:
  179. /// Use with care:
  180. /// sliderMoved is emitted only when the user moves the slider
  181. //void sliderMoved(double position);
  182. void minimumValueChanged(double value);
  183. void minimumValueIsChanging(double value);
  184. void maximumValueChanged(double value);
  185. void maximumValueIsChanging(double value);
  186. void valuesChanged(double minValue, double maxValue);
  187. void rangeChanged(double min, double max);
  188. protected Q_SLOTS:
  189. void startChanging();
  190. void stopChanging();
  191. void changeValues(double newMinValue, double newMaxValue);
  192. void changeMinimumValue(double value);
  193. void changeMaximumValue(double value);
  194. void setMinimumToMaximumSpinBox(double minimum);
  195. void setMaximumToMinimumSpinBox(double maximum);
  196. void onSliderRangeChanged(double min, double max);
  197. void onValueProxyAboutToBeModified();
  198. void onValueProxyModified();
  199. protected:
  200. virtual bool eventFilter(QObject *obj, QEvent *event);
  201. /// can be used to change the slider by a custom one
  202. void setSlider(ctkDoubleRangeSlider* slider);
  203. protected:
  204. QScopedPointer<ctkRangeWidgetPrivate> d_ptr;
  205. private:
  206. Q_DECLARE_PRIVATE(ctkRangeWidget);
  207. Q_DISABLE_COPY(ctkRangeWidget);
  208. };
  209. #endif