ctkSliderWidget.h 12 KB

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