ctkSliderWidget.h 8.3 KB

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