ctkSliderWidget.h 12 KB

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