ctkRangeWidget.h 8.9 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. void range(double minimumAndMaximum[2])const;
  76. ///
  77. /// This property holds the slider and spinbox minimum value.
  78. /// ctkRangeWidget forces the value to be within the
  79. /// legal range: minimum <= minimumValue <= maximumValue <= maximum.
  80. double minimumValue()const;
  81. ///
  82. /// This property holds the slider and spinbox maximum value.
  83. /// ctkRangeWidget forces the value to be within the
  84. /// legal range: minimum <= minimumValue <= maximumValue <= maximum.
  85. double maximumValue()const;
  86. ///
  87. /// Utility function that returns both values at the same time
  88. /// Returns minimumValue and maximumValue
  89. void values(double &minValue, double &maxValue)const;
  90. ///
  91. /// This property holds the single step.
  92. /// The smaller of two natural steps that the
  93. /// slider provides and typically corresponds to the
  94. /// user pressing an arrow key.
  95. double singleStep()const;
  96. void setSingleStep(double step);
  97. ///
  98. /// This property holds the precision of the spin box, in decimals.
  99. int decimals()const;
  100. ///
  101. /// This property holds the spin box's prefix.
  102. /// The prefix is prepended to the start of the displayed value.
  103. /// Typical use is to display a unit of measurement or a currency symbol
  104. QString prefix()const;
  105. void setPrefix(const QString& prefix);
  106. ///
  107. /// This property holds the spin box's suffix.
  108. /// The suffix is appended to the end of the displayed value.
  109. /// Typical use is to display a unit of measurement or a currency symbol
  110. QString suffix()const;
  111. void setSuffix(const QString& suffix);
  112. ///
  113. /// This property holds the interval between tickmarks.
  114. /// This is a value interval, not a pixel interval.
  115. /// If it is 0, the slider will choose between lineStep() and pageStep().
  116. /// The default value is 0.
  117. double tickInterval()const;
  118. void setTickInterval(double ti);
  119. ///
  120. /// This property holds the alignment of the spin boxes.
  121. /// Possible Values are Qt::AlignTop, Qt::AlignBottom, and Qt::AlignVCenter.
  122. /// By default, the alignment is Qt::AlignVCenter
  123. void setSpinBoxAlignment(Qt::Alignment alignment);
  124. Qt::Alignment spinBoxAlignment()const;
  125. ///
  126. /// This property holds the alignment of the text inside the spin boxes.
  127. /// Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter.
  128. /// By default, the alignment is Qt::AlignLeft
  129. void setSpinBoxTextAlignment(Qt::Alignment alignment);
  130. Qt::Alignment spinBoxTextAlignment()const;
  131. ///
  132. /// This property holds whether slider tracking is enabled.
  133. /// If tracking is enabled (the default), the widget emits the valueChanged()
  134. /// signal while the slider or spinbox is being dragged. If tracking is
  135. /// disabled, the widget emits the valueChanged() signal only when the user
  136. /// releases the slider or spinbox.
  137. void setTracking(bool enable);
  138. bool hasTracking()const;
  139. ///
  140. /// Set/Get the auto spinbox width
  141. /// When the autoSpinBoxWidth property is on, the width of the SpinBox is
  142. /// set to the same width of the largest QSpinBox of its
  143. // ctkRangeWidget siblings.
  144. bool isAutoSpinBoxWidth()const;
  145. void setAutoSpinBoxWidth(bool autoWidth);
  146. ///
  147. /// When symmetricMoves is true, moving a handle will move the other handle
  148. /// symmetrically, otherwise the handles are independent. False by default
  149. bool symmetricMoves()const;
  150. void setSymmetricMoves(bool symmetry);
  151. /// Return the slider of the range widget.
  152. /// \sa minimumSpinBox(), maximumSpinBox()
  153. ctkDoubleRangeSlider* slider()const;
  154. /// Return the minimum spinbox.
  155. /// \sa maximumSpinBox(), slider()
  156. ctkDoubleSpinBox* minimumSpinBox()const;
  157. /// Return the maximum spinbox.
  158. /// \sa minimumSpinBox(), slider()
  159. ctkDoubleSpinBox* maximumSpinBox()const;
  160. /// Set/Get the value proxy of the slider and spinboxes.
  161. /// \sa setValueProxy(), valueProxy()
  162. void setValueProxy(ctkValueProxy* proxy);
  163. ctkValueProxy* valueProxy() const;
  164. public Q_SLOTS:
  165. ///
  166. /// Reset the slider and spinbox to zero (value and position)
  167. void reset();
  168. void setMinimumValue(double value);
  169. void setMaximumValue(double value);
  170. ///
  171. /// Utility function that set the min and max values at once
  172. void setValues(double minValue, double maxValue);
  173. /// Sets how many decimals the spinbox will use for displaying and
  174. /// interpreting doubles.
  175. void setDecimals(int decimals);
  176. Q_SIGNALS:
  177. /// Use with care:
  178. /// sliderMoved is emitted only when the user moves the slider
  179. //void sliderMoved(double position);
  180. void minimumValueChanged(double value);
  181. void minimumValueIsChanging(double value);
  182. void maximumValueChanged(double value);
  183. void maximumValueIsChanging(double value);
  184. void valuesChanged(double minValue, double maxValue);
  185. void rangeChanged(double min, double max);
  186. protected Q_SLOTS:
  187. void startChanging();
  188. void stopChanging();
  189. void changeValues(double newMinValue, double newMaxValue);
  190. void changeMinimumValue(double value);
  191. void changeMaximumValue(double value);
  192. /// A spinbox value has been modified, update the slider.
  193. void setSliderValues();
  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