ctkRangeWidget.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. Q_PROPERTY(double customSpinBoxesLimitsMin READ customSpinBoxesLimitsMin)
  48. Q_PROPERTY(double customSpinBoxesLimitsMax READ customSpinBoxesLimitsMax)
  49. public:
  50. /// Superclass typedef
  51. typedef QWidget Superclass;
  52. /// Constructor
  53. /// If \li parent is null, ctkRangeWidget will be a top-leve widget
  54. /// \note The \li parent can be set later using QWidget::setParent()
  55. explicit ctkRangeWidget(QWidget* parent = 0);
  56. /// Destructor
  57. virtual ~ctkRangeWidget();
  58. ///
  59. /// This property holds the sliders and spinbox minimum value.
  60. /// FIXME: Test following specs.
  61. /// When setting this property, the maximum is adjusted if necessary
  62. /// to ensure that the range remains valid.
  63. /// Also the slider's current value is adjusted to be within the new range.
  64. virtual double minimum()const;
  65. virtual void setMinimum(double minimum);
  66. ///
  67. /// This property holds the sliders and spinbox minimum value.
  68. /// FIXME: Test following specs.
  69. /// When setting this property, the maximum is adjusted if necessary
  70. /// to ensure that the range remains valid.
  71. /// Also the slider's current value is adjusted to be within the new range.
  72. virtual double maximum()const;
  73. virtual void setMaximum(double maximum);
  74. /// Description
  75. /// Utility function that set the min/max in once
  76. Q_INVOKABLE virtual void setRange(double min, double max);
  77. virtual void range(double minimumAndMaximum[2])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. virtual 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. virtual double maximumValue()const;
  88. ///
  89. /// Utility function that returns both values at the same time
  90. /// Returns minimumValue and maximumValue
  91. virtual 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. virtual double singleStep()const;
  98. virtual void setSingleStep(double step);
  99. ///
  100. /// This property holds the precision of the spin box, in decimals.
  101. virtual 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. virtual QString prefix()const;
  107. virtual 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. virtual QString suffix()const;
  113. virtual 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. virtual double tickInterval()const;
  120. virtual 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. virtual void setSpinBoxAlignment(Qt::Alignment alignment);
  126. virtual 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. virtual void setSpinBoxTextAlignment(Qt::Alignment alignment);
  132. virtual 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. virtual void setTracking(bool enable);
  140. virtual 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. virtual bool isAutoSpinBoxWidth()const;
  147. virtual 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. virtual bool symmetricMoves()const;
  152. virtual void setSymmetricMoves(bool symmetry);
  153. ///
  154. /// This property sets custom limits for spin boxes.
  155. virtual void setCustomSpinBoxesLimits(double min, double max);
  156. virtual double customSpinBoxesLimitsMin()const;
  157. virtual double customSpinBoxesLimitsMax()const;
  158. /// Return the slider of the range widget.
  159. /// \sa minimumSpinBox(), maximumSpinBox()
  160. virtual ctkDoubleRangeSlider* slider()const;
  161. /// Return the minimum spinbox.
  162. /// \sa maximumSpinBox(), slider()
  163. virtual ctkDoubleSpinBox* minimumSpinBox()const;
  164. /// Return the maximum spinbox.
  165. /// \sa minimumSpinBox(), slider()
  166. virtual ctkDoubleSpinBox* maximumSpinBox()const;
  167. /// Set/Get the value proxy of the slider and spinboxes.
  168. /// \sa setValueProxy(), valueProxy()
  169. virtual void setValueProxy(ctkValueProxy* proxy);
  170. virtual ctkValueProxy* valueProxy() const;
  171. public Q_SLOTS:
  172. ///
  173. /// Reset the slider and spinbox to zero (value and position)
  174. virtual void reset();
  175. virtual void setMinimumValue(double value);
  176. virtual void setMaximumValue(double value);
  177. ///
  178. /// Utility function that set the min and max values at once
  179. virtual void setValues(double minValue, double maxValue);
  180. /// Sets how many decimals the spinbox will use for displaying and
  181. /// interpreting doubles.
  182. virtual void setDecimals(int decimals);
  183. Q_SIGNALS:
  184. /// Use with care:
  185. /// sliderMoved is emitted only when the user moves the slider
  186. //void sliderMoved(double position);
  187. void minimumValueChanged(double value);
  188. void minimumValueIsChanging(double value);
  189. void maximumValueChanged(double value);
  190. void maximumValueIsChanging(double value);
  191. void valuesChanged(double minValue, double maxValue);
  192. void rangeChanged(double min, double max);
  193. protected Q_SLOTS:
  194. virtual void startChanging();
  195. virtual void stopChanging();
  196. virtual void changeValues(double newMinValue, double newMaxValue);
  197. virtual void changeMinimumValue(double value);
  198. virtual void changeMaximumValue(double value);
  199. /// A spinbox value has been modified, update the slider.
  200. virtual void setSliderValues();
  201. virtual void setMinimumToMaximumSpinBox(double minimum);
  202. virtual void setMaximumToMinimumSpinBox(double maximum);
  203. virtual void onSliderRangeChanged(double min, double max);
  204. virtual void onValueProxyAboutToBeModified();
  205. virtual void onValueProxyModified();
  206. protected:
  207. virtual bool eventFilter(QObject *obj, QEvent *event);
  208. /// can be used to change the slider by a custom one
  209. virtual void setSlider(ctkDoubleRangeSlider* slider);
  210. protected:
  211. QScopedPointer<ctkRangeWidgetPrivate> d_ptr;
  212. private:
  213. Q_DECLARE_PRIVATE(ctkRangeWidget);
  214. Q_DISABLE_COPY(ctkRangeWidget);
  215. };
  216. #endif