ctkSliderWidget.h 9.7 KB

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