ctkSliderWidget.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 __ctkSliderWidget_h
  15. #define __ctkSliderWidget_h
  16. // Qt includes
  17. #include <QSlider>
  18. class QDoubleSpinBox;
  19. // CTK includes
  20. #include <ctkPimpl.h>
  21. #include "ctkWidgetsExport.h"
  22. class ctkDoubleSlider;
  23. class ctkPopupWidget;
  24. class ctkSliderWidgetPrivate;
  25. ///
  26. /// ctkSliderWidget is a wrapper around a ctkDoubleSlider and a QDoubleSpinBox
  27. /// where the slider value and the spinbox value are synchronized.
  28. /// \sa ctkRangeWidget, ctkDoubleRangeSlider, QSpinBox
  29. class CTK_WIDGETS_EXPORT ctkSliderWidget : public QWidget
  30. {
  31. Q_OBJECT
  32. Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
  33. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
  34. Q_PROPERTY(double pageStep READ pageStep WRITE setPageStep)
  35. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  36. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  37. Q_PROPERTY(double value READ value WRITE setValue)
  38. Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
  39. Q_PROPERTY(QString suffix READ suffix WRITE setSuffix)
  40. Q_PROPERTY(double tickInterval READ tickInterval WRITE setTickInterval)
  41. Q_PROPERTY(bool autoSpinBoxWidth READ isAutoSpinBoxWidth WRITE setAutoSpinBoxWidth)
  42. Q_PROPERTY(Qt::Alignment spinBoxAlignment READ spinBoxAlignment WRITE setSpinBoxAlignment)
  43. Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
  44. Q_PROPERTY(bool spinBoxVisible READ isSpinBoxVisible WRITE setSpinBoxVisible);
  45. Q_PROPERTY(bool popupSlider READ hasPopupSlider WRITE setPopupSlider);
  46. public:
  47. /// Superclass typedef
  48. typedef QWidget Superclass;
  49. /// Constructors
  50. explicit ctkSliderWidget(QWidget* parent = 0);
  51. virtual ~ctkSliderWidget();
  52. ///
  53. /// This property holds the sliders and spinbox minimum value.
  54. /// FIXME: Test following specs.
  55. /// When setting this property, the maximum is adjusted if necessary
  56. /// to ensure that the range remains valid.
  57. /// Also the slider's current value is adjusted to be within the new range.
  58. double minimum()const;
  59. void setMinimum(double minimum);
  60. ///
  61. /// This property holds the sliders and spinbox minimum value.
  62. /// FIXME: Test following specs.
  63. /// When setting this property, the maximum is adjusted if necessary
  64. /// to ensure that the range remains valid.
  65. /// Also the slider's current value is adjusted to be within the new range.
  66. double maximum()const;
  67. void setMaximum(double maximum);
  68. /// Description
  69. /// Utility function that set the min/max in once
  70. void setRange(double min, double max);
  71. ///
  72. /// This property holds the current slider position.
  73. /// If tracking is enabled (the default), this is identical to value.
  74. //double sliderPosition()const;
  75. //void setSliderPosition(double position);
  76. ///
  77. /// This property holds the slider and spinbox current value.
  78. /// ctkSliderWidget forces the value to be within the
  79. /// legal range: minimum <= value <= maximum.
  80. double value()const;
  81. ///
  82. /// This property holds the single step.
  83. /// The smaller of two natural steps that the
  84. /// slider provides and typically corresponds to the
  85. /// user pressing an arrow key.
  86. double singleStep()const;
  87. void setSingleStep(double step);
  88. ///
  89. /// This property holds the page step.
  90. /// The larger of two natural steps that an abstract slider provides and
  91. /// typically corresponds to the user pressing PageUp or PageDown.
  92. double pageStep()const;
  93. void setPageStep(double step);
  94. ///
  95. /// This property holds the precision of the spin box, in decimals.
  96. /// Sets how many decimals the spinbox will use for displaying and interpreting doubles.
  97. int decimals()const;
  98. void setDecimals(int decimals);
  99. ///
  100. /// This property holds the spin box's prefix.
  101. /// The prefix is prepended to the start of the displayed value.
  102. /// Typical use is to display a unit of measurement or a currency symbol
  103. QString prefix()const;
  104. void setPrefix(const QString& prefix);
  105. ///
  106. /// This property holds the spin box's suffix.
  107. /// The suffix is appended to the end of the displayed value.
  108. /// Typical use is to display a unit of measurement or a currency symbol
  109. QString suffix()const;
  110. void setSuffix(const QString& suffix);
  111. ///
  112. /// This property holds the interval between tickmarks.
  113. /// This is a value interval, not a pixel interval.
  114. /// If it is 0, the slider will choose between lineStep() and pageStep().
  115. /// The default value is 0.
  116. double tickInterval()const;
  117. void setTickInterval(double ti);
  118. ///
  119. /// This property holds the alignment of the spin box.
  120. /// Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter.
  121. /// By default, the alignment is Qt::AlignLeft
  122. void setSpinBoxAlignment(Qt::Alignment alignment);
  123. Qt::Alignment spinBoxAlignment()const;
  124. ///
  125. /// This property holds whether slider tracking is enabled.
  126. /// If tracking is enabled (the default), the widget emits the valueChanged()
  127. /// signal while the slider or spinbox is being dragged. If tracking is
  128. /// disabled, the widget emits the valueChanged() signal only when the user
  129. /// releases the slider or spinbox.
  130. void setTracking(bool enable);
  131. bool hasTracking()const;
  132. ///
  133. /// Set/Get the auto spinbox width
  134. /// When the autoSpinBoxWidth property is on, the width of the SpinBox is
  135. /// set to the same width of the largest QSpinBox of its
  136. // ctkSliderWidget siblings.
  137. bool isAutoSpinBoxWidth()const;
  138. void setAutoSpinBoxWidth(bool autoWidth);
  139. ///
  140. /// The Spinbox visibility can be controlled using setSpinBoxVisible() and
  141. /// isSpinBoxVisible().
  142. bool isSpinBoxVisible()const;
  143. ///
  144. /// The slider can be handled as a popup for the spinbox. The location where
  145. /// the popup appears is controlled by \sa alignement.
  146. /// False by default.
  147. /// Note: some sizing issues in the popup can happen if the ctkSliderWidget
  148. /// has already parent. You might want to consider setting this property
  149. /// before setting a parent to ctkSliderWidget.
  150. bool hasPopupSlider()const;
  151. void setPopupSlider(bool popup);
  152. ///
  153. /// Return the popup if ctkSliderWidget hasPopupSlider() is true, 0 otherwise.
  154. /// It can be useful to control where the popup shows up relative to the
  155. /// spinbox the popup \sa ctkPopupWidget::baseWidget.
  156. ctkPopupWidget* popup()const;
  157. ///
  158. /// Returns the spinbox synchronized with the slider. Be careful
  159. /// with what you do with the spinbox as the slider might change
  160. /// properties automatically.
  161. QDoubleSpinBox* spinBox();
  162. ///
  163. /// Returns the slider synchronized with the spinbox. Be careful
  164. /// with what you do with the slider as the spinbox might change
  165. /// properties automatically.
  166. ctkDoubleSlider* slider();
  167. public Q_SLOTS:
  168. ///
  169. /// Reset the slider and spinbox to zero (value and position)
  170. void reset();
  171. void setValue(double value);
  172. void setSpinBoxVisible(bool);
  173. Q_SIGNALS:
  174. /// When tracking is on (default), valueChanged is emitted when the
  175. /// user drags the slider.
  176. /// If tracking is off, valueChanged() is emitted only when the user
  177. /// releases the mouse.
  178. /// \sa valueIsChanging QAbstractSlider::valueChanged
  179. void valueChanged(double value);
  180. /// valueIsChanging() is emitted whenever the slider is dragged and tracking
  181. /// is turned off. You might want to use valueChanged instead.
  182. /// It behaves the same way than QAbstractSlider::sliderMoved()
  183. /// \sa valueChanged QAbstractSlider::sliderMoved
  184. void valueIsChanging(double value);
  185. protected Q_SLOTS:
  186. void startChanging();
  187. void stopChanging();
  188. void changeValue(double value);
  189. protected:
  190. virtual bool eventFilter(QObject *obj, QEvent *event);
  191. protected:
  192. QScopedPointer<ctkSliderWidgetPrivate> d_ptr;
  193. private:
  194. Q_DECLARE_PRIVATE(ctkSliderWidget);
  195. Q_DISABLE_COPY(ctkSliderWidget);
  196. };
  197. #endif