JKQtExtras
a library of useful Qt widgets and tools
jkqtecolorslider.h
1 /*
2  Copyright (c) 2019 Jan W. Krieger (<jan@jkrieger.de>)
3 
4  This software is free software: you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License (LGPL) as published by
6  the Free Software Foundation, either version 2.1 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License (LGPL) for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License (LGPL)
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef JKQTECOLORSLIDER_H
19 #define JKQTECOLORSLIDER_H
20 
21 #include <QSlider>
22 #include <QPen>
23 #include <QBrush>
24 #include <QColor>
25 #include "jkqtextras_imexport.h"
26 
27 /*! \brief a QSlider that allows to choose a color hue
28  \ingroup JKQtExtrasWidgetsSliders
29 
30 
31  Horizontal Hue-Slider:
32  \image html JKQTEColorSliderHorizontal.png
33 
34  Vertical Hue Slider:
35  \image html JKQTEColorSliderVertical.png
36 
37  Basic usage is simple:
38 
39  \snippet jkqtextras_test/testmainwindow.cpp Example: JKQTEColorSlider
40 
41  \section JKQTEColorSliderBaseColorMode Base Color Mode
42 
43  Especially when building a color-chooser, you need to combine several sliders.
44  That then define a color together. To support such modes, the slider can use
45  a "base color" i.e. an input color which is then modified and output again.
46  The following functions support this mode:
47  - setBaseColor()
48  - baseColor()
49  - modifiedColor()
50  - Signal: colorChanged()
51  .
52 
53  Here is an example of how to use this feature:
54 
55  \snippet jkqtextras_test/testmainwindow.cpp Example: JKQTEColorSliderRGBGroup
56 
57  \image html JKQTEColorSliderRGBGroup.png
58 
59 */
60 class JKQTEXTRAS_LIB_EXPORT JKQTEColorSlider : public QSlider {
61  Q_OBJECT
62  Q_PROPERTY(QColor baseColor READ baseColor WRITE setBaseColor USER true)
63  Q_PROPERTY(SliderMode sliderMode READ sliderMode WRITE setSliderMode USER true)
64  Q_PROPERTY(QBrush indicatorBrush READ indicatorBrush WRITE setIndicatorBrush USER true)
65  Q_PROPERTY(QPen indicatorPen READ indicatorPen WRITE setIndicatorPen USER true)
66  Q_PROPERTY(IndicatorStyle IndicatorStyle READ indicatorStyle WRITE setIndicatorStyle USER true)
67  public:
68  /** \brief available modes for the slider
69  *
70  * the appearance and the range of the slider depend on this */
71  enum SliderMode {
72  GreySlider=0, /**< the slider modifies the grey color channel \image html JKQTEColorSlider_Grey.png */
73  GraySlider=GreySlider, /**< the slider modifies the grey color channel \image html JKQTEColorSlider_Grey.png */
74  RedSlider, /**< the slider modifies the red color channel \image html JKQTEColorSlider_Red.png */
75  GreenSlider, /**< the slider modifies the green color channel \image html JKQTEColorSlider_Green.png */
76  BlueSlider, /**< the slider modifies the blue color channel \image html JKQTEColorSlider_Blue.png */
77  HueSlider, /**< the slider modifies the hue color channel (from HSV color model) \image html JKQTEColorSlider_Hue.png */
78  SaturationSlider, /**< the slider modifies the saturation color channel (from HSV color model) \image html JKQTEColorSlider_Saturation.png */
79  ValueSlider, /**< the slider modifies the value color channel (from HSV color model) \image html JKQTEColorSlider_Value.png */
80  TransparencySlider, /**< the slider modifies the transparency color channel \image html JKQTEColorSlider_Transparency.png */
81  AlphaSlider=TransparencySlider, /**< the slider modifies the transparency color channel \image html JKQTEColorSlider_Transparency.png */
82  };
83  Q_ENUM(SliderMode)
84 
85  /** \brief available modes for the slider
86  *
87  * the appearance and the range of the slider depend on this */
89  FixedColorDoubleArrowIndicator=0, /**< draws two inward pointing triangles as indicator, filled with indicatorBrush() \image html JKQTEColorSlider_FixedColorDoubleArrowIndicator.png */
90  FixedColorCircleIndicator, /**< draws a circle as indicator, filled with indicatorBrush() \image html JKQTEColorSlider_FixedColorCircleIndicator.png */
91  SelectedColorDoubleArrowIndicator, /**< draws two inward pointing triangles as indicator, filled with the current color \image html JKQTEColorSlider_SelectedColorDoubleArrowIndicator.png */
92  SelectedColorCircleIndicator, /**< draws a circle as indicator, filled with the current color \image html JKQTEColorSlider_SelectedColorCircleIndicator.png */
93  };
94  Q_ENUM(IndicatorStyle)
95 
96  explicit JKQTEColorSlider(QWidget *parent = nullptr);
97  explicit JKQTEColorSlider(Qt::Orientation orientation, QWidget *parent = nullptr);
98  explicit JKQTEColorSlider(SliderMode mode, QWidget *parent = nullptr);
99  explicit JKQTEColorSlider(SliderMode mode, Qt::Orientation orientation, QWidget *parent = nullptr);
100 
101  /** \brief returns the currently set color/slider mode
102  *
103  * The appearance and the range of the slider depend on this
104  *
105  * \see SliderMode setSliderMode() */
106  SliderMode sliderMode() const;
107  /** \brief style of the indicator */
108  IndicatorStyle indicatorStyle() const;
109  /** \brief takes the color \a colorIn and applies the value of the slider to the color */
110  QColor modifiedColor(QColor colorIn) const;
111  /** \brief takes the color baseColor() and applies the value of the slider to the color
112  *
113  * \see \ref JKQTEColorSliderBaseColorMode baseColor(), setBaseColor(), baseColor(), colorChanged()
114  * */
115  QColor modifiedColor() const;
116  /** \brief returns the currently set base color that is modified by the slider
117  *
118  * \see \ref JKQTEColorSliderBaseColorMode baseColor(), setBaseColor(), modifiedColor(), colorChanged()
119  * */
120  QColor baseColor() const;
121 
122  /** \brief returns the <a href="https://doc.qt.io/qt-5/qbrush.html">QBrush</a> used to draw the indicator */
123  const QBrush &indicatorBrush() const;
124  /** \brief returns the <a href="https://doc.qt.io/qt-5/qpen.html">QPen</a> used to draw the indicator */
125  const QPen& indicatorPen() const;
126  /** \brief returns the <a href="https://doc.qt.io/qt-5/qbrush.html">QBrush</a> used to draw the indicator */
127  QBrush& indicatorBrush();
128  /** \brief returns the <a href="https://doc.qt.io/qt-5/qpen.html">QPen</a> used to draw the indicator */
129  QPen& indicatorPen();
130 
131  /** \brief sets the <a href="https://doc.qt.io/qt-5/qbrush.html">QBrush</a> used to draw the indicator */
132  void setIndicatorBrush(const QBrush& b);
133  /** \brief sets the <a href="https://doc.qt.io/qt-5/qpen.html">QPen</a> used to draw the indicator */
134  void setIndicatorPen(const QPen &p);
135  /** \brief sets the style of the indicator */
136  void setIndicatorStyle(IndicatorStyle s) ;
137 
138  virtual QSize minimumSizeHint() const override;
139  virtual QSize sizeHint() const override;
140 
141  signals:
142  /** \brief emitted when the slider moves, the color is based on the baseColor(),
143  * modified by the slider value
144  *
145  * \see \ref JKQTEColorSliderBaseColorMode baseColor(), setBaseColor(), modifiedColor(), colorChanged()
146  * */
147  void colorChanged(QColor color);
148  public slots:
149  /** \brief sets the currently set color/slider mode
150  *
151  * The appearance and the range of the slider depend on this
152  *
153  * \note This also changes the range of the slider!
154  * \see SliderMode sliderMode()
155  */
156  void setSliderMode(SliderMode mode);
157  /** \brief sets the currently set base color that is modified by the slider
158  *
159  * \see \ref JKQTEColorSliderBaseColorMode baseColor(), baseColor(), modifiedColor(), colorChanged()
160  * */
161  void setBaseColor(QColor baseColor);
162  protected slots:
163  void baseSliderChanged(int value);
164  protected:
165  /** \brief currently set color/slider mode */
166  SliderMode m_mode;
167  /** \brief currently set base color that is modified by the slider
168  *
169  * \see \ref JKQTEColorSliderBaseColorMode baseColor(), baseColor(), modifiedColor(), colorChanged() */
170  QColor m_baseColor;
171  /** \brief <a href="https://doc.qt.io/qt-5/qbrush.html">QBrush</a> used to draw the indicator */
172  QBrush m_indicatorBrush;
173  /** \brief <a href="https://doc.qt.io/qt-5/qpen.html">QPen</a> used to draw the indicator */
174  QPen m_indicatorPen;
175  /** \brief style of the indicator */
176  IndicatorStyle m_indicatorStyle;
177  /** \brief returns the default base color for a given slider mode */
178  static QColor defaultBaseColor(SliderMode mode);
179  /** \brief paints the slider */
180  virtual void paintEvent(QPaintEvent *ev) override;
181 
182 
183 };
184 
185 
186 #endif // JKQTECOLORSLIDER_H
JKQTEColorSlider::SliderMode
SliderMode
available modes for the slider
Definition: jkqtecolorslider.h:71
JKQTEColorSlider::SelectedColorCircleIndicator
@ SelectedColorCircleIndicator
Definition: jkqtecolorslider.h:92
JKQTEColorSlider::ValueSlider
@ ValueSlider
Definition: jkqtecolorslider.h:79
JKQTEColorSlider::TransparencySlider
@ TransparencySlider
Definition: jkqtecolorslider.h:80
JKQTEColorSlider::FixedColorCircleIndicator
@ FixedColorCircleIndicator
Definition: jkqtecolorslider.h:90
JKQTEXTRAS_LIB_EXPORT
#define JKQTEXTRAS_LIB_EXPORT
Definition: jkqtextras_imexport.h:95
JKQTEColorSlider::SaturationSlider
@ SaturationSlider
Definition: jkqtecolorslider.h:78
JKQTEColorSlider::GreenSlider
@ GreenSlider
Definition: jkqtecolorslider.h:75
JKQTEColorSlider::IndicatorStyle
IndicatorStyle
available modes for the slider
Definition: jkqtecolorslider.h:88
JKQTEColorSlider::BlueSlider
@ BlueSlider
Definition: jkqtecolorslider.h:76
JKQTEColorSlider
a QSlider that allows to choose a color hue
Definition: jkqtecolorslider.h:60
JKQTEColorSlider::HueSlider
@ HueSlider
Definition: jkqtecolorslider.h:77
JKQTEColorSlider::RedSlider
@ RedSlider
Definition: jkqtecolorslider.h:74
JKQTEColorSlider::SelectedColorDoubleArrowIndicator
@ SelectedColorDoubleArrowIndicator
Definition: jkqtecolorslider.h:91