ctkSliderWidget.h 8.7 KB

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