ctkDoubleSlider.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 __ctkDoubleSlider_h
  15. #define __ctkDoubleSlider_h
  16. // Qt includes
  17. #include <QSlider>
  18. #include <QWidget>
  19. // CTK includes
  20. #include <ctkPimpl.h>
  21. #include "ctkWidgetsExport.h"
  22. class ctkDoubleSliderPrivate;
  23. class ctkValueProxy;
  24. /// \ingroup Widgets
  25. /// ctkDoubleSlider is a QSlider that controls doubles instead of integers.
  26. /// ctkDoubleSlider internally aggregates a QSlider
  27. /// TODO: ctkDoubleSlider tries to represent a double value with integers. It's
  28. /// of course non-optimal and can lead to errors, it would be better if
  29. /// ctkDoubleSlider works like QDoubleSpinBox, where the value is a QVariant
  30. /// and the conversion between double and integer is only done to convert
  31. /// to/from screen coordinates.
  32. /// \sa ctkRangeSlider, ctkDoubleRangeSlider, ctkRangeWidget
  33. class CTK_WIDGETS_EXPORT ctkDoubleSlider : public QWidget
  34. {
  35. Q_OBJECT
  36. Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged USER true)
  37. Q_PROPERTY(double sliderPosition READ sliderPosition WRITE setSliderPosition)
  38. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
  39. Q_PROPERTY(double pageStep READ pageStep WRITE setPageStep)
  40. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  41. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  42. Q_PROPERTY(double tickInterval READ tickInterval WRITE setTickInterval)
  43. Q_PROPERTY(QSlider::TickPosition tickPosition READ tickPosition WRITE setTickPosition)
  44. Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
  45. Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
  46. Q_PROPERTY(QString handleToolTip READ handleToolTip WRITE setHandleToolTip)
  47. Q_PROPERTY(bool invertedAppearance READ invertedAppearance WRITE setInvertedAppearance)
  48. Q_PROPERTY(bool invertedControls READ invertedControls WRITE setInvertedControls)
  49. public:
  50. /// Superclass typedef
  51. typedef QWidget Superclass;
  52. /// Constructors, builds a slider whose default values are the same as
  53. /// QSlider (vertical by default).
  54. explicit ctkDoubleSlider(QWidget* parent = 0);
  55. /// Constructors, builds a slider whose default values are the same as
  56. /// QSlider (vertical by default).
  57. explicit ctkDoubleSlider(Qt::Orientation orient, QWidget* parent = 0);
  58. /// Destructor
  59. virtual ~ctkDoubleSlider();
  60. ///
  61. /// This property holds the sliders's minimum value.
  62. /// When setting this property, the maximum is adjusted if necessary to
  63. /// ensure that the range remains valid. Also the slider's current value
  64. /// is adjusted to be within the new range.
  65. void setMinimum(double min);
  66. double minimum()const;
  67. ///
  68. /// This property holds the slider's maximum value.
  69. /// When setting this property, the minimum is adjusted if necessary to
  70. /// ensure that the range remains valid. Also the slider's current value
  71. /// is adjusted to be within the new range.
  72. void setMaximum(double max);
  73. double maximum()const;
  74. ///
  75. /// Sets the slider's minimum to min and its maximum to max.
  76. /// If max is smaller than min, min becomes the only legal value.
  77. void setRange(double min, double max);
  78. ///
  79. /// This property holds the slider's current value.
  80. /// The slider forces the value to be within the legal range:
  81. /// minimum <= value <= maximum.
  82. /// Changing the value also changes the sliderPosition.
  83. double value()const;
  84. ///
  85. /// This property holds the single step.
  86. /// The smaller of two natural steps that an abstract sliders provides and
  87. /// typically corresponds to the user pressing an arrow key
  88. /// Default value is 1.
  89. /// \sa isValidStep()
  90. void setSingleStep(double step);
  91. double singleStep()const;
  92. /// Return true if the step can be handled by the slider, false otherwise.
  93. /// An invalid step is a step that can't be used to convert from double
  94. /// to int (too large or too small).
  95. /// \sa singleStep
  96. bool isValidStep(double step)const;
  97. ///
  98. /// This property holds the page step.
  99. /// The larger of two natural steps that an abstract slider provides and
  100. /// typically corresponds to the user pressing PageUp or PageDown.
  101. /// Default value is 10.
  102. void setPageStep(double step);
  103. double pageStep()const;
  104. ///
  105. /// This property holds the interval between tickmarks.
  106. /// This is a value interval, not a pixel interval. If it is 0, the slider
  107. /// will choose between lineStep() and pageStep().
  108. /// The default value is 0.
  109. void setTickInterval(double ti);
  110. double tickInterval()const;
  111. ///
  112. /// This property holds the tickmark position for this slider.
  113. /// The valid values are described by the QSlider::TickPosition enum.
  114. /// The default value is QSlider::NoTicks.
  115. void setTickPosition(QSlider::TickPosition position);
  116. QSlider::TickPosition tickPosition()const;
  117. ///
  118. /// This property holds the current slider position.
  119. /// If there is no proxy installed and tracking is enabled (the default),
  120. /// this is identical to value.
  121. /// With a proxy installed, it allows to modify the proxy value.
  122. /// \sa value(), setValue(), setValueProxy(), valueProxy()
  123. double sliderPosition()const;
  124. void setSliderPosition(double);
  125. ///
  126. /// This property holds whether slider tracking is enabled.
  127. /// If tracking is enabled (the default), the slider emits the valueChanged()
  128. /// signal while the slider is being dragged. If tracking is disabled, the
  129. /// slider emits the valueChanged() signal only when the user releases the
  130. /// slider.
  131. void setTracking(bool enable);
  132. bool hasTracking()const;
  133. ///
  134. /// Triggers a slider action. Possible actions are SliderSingleStepAdd,
  135. /// SliderSingleStepSub, SliderPageStepAdd, SliderPageStepSub,
  136. /// SliderToMinimum, SliderToMaximum, and SliderMove.
  137. void triggerAction(QAbstractSlider::SliderAction action);
  138. ///
  139. /// This property holds the orientation of the slider.
  140. /// The orientation must be Qt::Vertical (the default) or Qt::Horizontal.
  141. Qt::Orientation orientation()const;
  142. /// This property holds whether or not a slider shows its values inverted.
  143. /// If this property is false (the default), the minimum and maximum will
  144. /// be shown in its classic position for the inherited widget. If the value
  145. /// is true, the minimum and maximum appear at their opposite location.
  146. /// Note: This property makes most sense for sliders and dials. For scroll
  147. /// bars, the visual effect of the scroll bar subcontrols depends on whether
  148. /// or not the styles understand inverted appearance; most styles ignore this
  149. /// property for scroll bars.
  150. /// \sa invertedControls
  151. void setInvertedAppearance(bool invertedAppearance);
  152. bool invertedAppearance()const;
  153. /// This property holds whether or not the slider inverts its wheel and key
  154. /// events.
  155. /// If this property is false, scrolling the mouse wheel "up" and using keys
  156. /// like page up will increase the slider's value towards its maximum.
  157. /// Otherwise pressing page up will move value towards the slider's minimum.
  158. /// \sa invertedAppearance
  159. void setInvertedControls(bool invertedControls);
  160. bool invertedControls()const;
  161. ///
  162. /// Controls the text to display for the handle tooltip. It is in addition
  163. /// to the widget tooltip.
  164. /// "%1" is replaced by the current value of the slider.
  165. /// Empty string (by default) means no tooltip.
  166. QString handleToolTip()const;
  167. void setHandleToolTip(const QString& toolTip);
  168. /// Reimplemented for internal reasons (handle tooltip).
  169. virtual bool eventFilter(QObject*, QEvent*);
  170. /// Return a pointer to the QSlider used internally.
  171. /// Use with caution.
  172. /// \sa QSlider
  173. QSlider* slider()const;
  174. /// Install or remove a value proxy filter. The value proxy decouples the
  175. /// displayed value from the value retrieved by the value property.
  176. /// For example, the value proxy can allow one to display celsius in the
  177. /// spinbox while the value retrieved from the value property and signals
  178. /// are in farenheit.
  179. /// To remove the proxy, simply install a new empty proxy. The proxy
  180. /// installation/removal is silent.
  181. /// \sa setValueProxy(), valueProxy()
  182. void setValueProxy(ctkValueProxy* proxy);
  183. ctkValueProxy* valueProxy() const;
  184. public Q_SLOTS:
  185. ///
  186. /// This property holds the slider's current value.
  187. /// The slider forces the value to be within the legal range:
  188. /// minimum <= value <= maximum.
  189. /// Changing the value also changes the sliderPosition.
  190. void setValue(double value);
  191. ///
  192. /// This property holds the orientation of the slider.
  193. /// The orientation must be Qt::Vertical (the default) or Qt::Horizontal.
  194. void setOrientation(Qt::Orientation orientation);
  195. Q_SIGNALS:
  196. ///
  197. /// This signal is emitted when the slider value has changed, with the new
  198. /// slider value as argument.
  199. void valueChanged(double value);
  200. ///
  201. /// This signal is emitted when sliderDown is true and the slider moves.
  202. /// This usually happens when the user is dragging the slider. The value
  203. /// is the new slider position.
  204. /// This signal is emitted even when tracking is turned off.
  205. void sliderMoved(double position);
  206. ///
  207. /// This signal is emitted when the user presses the slider with the mouse,
  208. /// or programmatically when setSliderDown(true) is called.
  209. void sliderPressed();
  210. ///
  211. /// This signal is emitted when the user releases the slider with the mouse,
  212. /// or programmatically when setSliderDown(false) is called.
  213. void sliderReleased();
  214. ///
  215. /// This signal is emitted when the slider range has changed, with min being
  216. /// the new minimum, and max being the new maximum.
  217. /// Warning: don't confound with valuesChanged(double, double);
  218. /// \sa QAbstractSlider::rangeChanged()
  219. void rangeChanged(double min, double max);
  220. protected Q_SLOTS:
  221. void onValueChanged(int value);
  222. void onSliderMoved(int position);
  223. void onRangeChanged(int min, int max);
  224. void onValueProxyAboutToBeModified();
  225. void onValueProxyModified();
  226. protected:
  227. QScopedPointer<ctkDoubleSliderPrivate> d_ptr;
  228. private:
  229. Q_DECLARE_PRIVATE(ctkDoubleSlider);
  230. Q_DISABLE_COPY(ctkDoubleSlider);
  231. };
  232. #endif