1 /*
2     This file is part of the KDE libraries
3     SPDX-FileCopyrightText: 1997 Martin Jones <mjones@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 //-----------------------------------------------------------------------------
8 // Selector widgets for KDE Color Selector, but probably useful for other
9 // stuff also.
10 
11 #ifndef KSELECTOR_H
12 #define KSELECTOR_H
13 
14 #include <kwidgetsaddons_export.h>
15 
16 #include <QAbstractSlider>
17 #include <QGradient>
18 #include <QWidget>
19 #include <memory>
20 
21 /**
22  * @class KSelector kselector.h KSelector
23  *
24  * KSelector is the base class for other widgets which
25  * provides the ability to choose from a one-dimensional
26  * range of values. An example is the KGradientSelector
27  * which allows to choose from a range of colors.
28  *
29  * A custom drawing routine for the widget surface has
30  * to be provided by the subclass.
31  */
32 class KWIDGETSADDONS_EXPORT KSelector : public QAbstractSlider
33 {
34     Q_OBJECT
35     Q_PROPERTY(int value READ value WRITE setValue)
36     Q_PROPERTY(int minValue READ minimum WRITE setMinimum)
37     Q_PROPERTY(int maxValue READ maximum WRITE setMaximum)
38     Q_PROPERTY(bool indent READ indent WRITE setIndent)
39     Q_PROPERTY(Qt::ArrowType arrowDirection READ arrowDirection WRITE setArrowDirection)
40 public:
41     /**
42      * Constructs a horizontal one-dimensional selection widget.
43      */
44     explicit KSelector(QWidget *parent = nullptr);
45     /**
46      * Constructs a one-dimensional selection widget with
47      * a given orientation.
48      */
49     explicit KSelector(Qt::Orientation o, QWidget *parent = nullptr);
50     /*
51      * Destructs the widget.
52      */
53     ~KSelector() override;
54 
55     /**
56      * @return the rectangle on which subclasses should draw.
57      */
58     QRect contentsRect() const;
59 
60     /**
61      * Sets the indent option of the widget to i.
62      * This determines whether a shaded frame is drawn.
63      */
64     void setIndent(bool i);
65 
66     /**
67      * @return whether the indent option is set.
68      */
69     bool indent() const;
70 
71     /**
72      * Sets the arrow direction.
73      */
74     void setArrowDirection(Qt::ArrowType direction);
75 
76     /**
77      * @return the current arrow direction
78      */
79     Qt::ArrowType arrowDirection() const;
80 
81 protected:
82     /**
83      * Override this function to draw the contents of the control.
84      * The default implementation does nothing.
85      *
86      * Draw only within contentsRect().
87      */
88     virtual void drawContents(QPainter *);
89     /**
90      * Override this function to draw the cursor which
91      * indicates the current value.
92      */
93     virtual void drawArrow(QPainter *painter, const QPoint &pos);
94 
95     void paintEvent(QPaintEvent *) override;
96     void mousePressEvent(QMouseEvent *e) override;
97     void mouseMoveEvent(QMouseEvent *e) override;
98     void mouseReleaseEvent(QMouseEvent *e) override;
99     void wheelEvent(QWheelEvent *) override;
100 
101 private:
102     QPoint calcArrowPos(int val);
103     void moveArrow(const QPoint &pos);
104 
105 private:
106     friend class KSelectorPrivate;
107     std::unique_ptr<class KSelectorPrivate> const d;
108 
109     Q_DISABLE_COPY(KSelector)
110 };
111 
112 /**
113  * @class KGradientSelector kselector.h KGradientSelector
114  *
115  * The KGradientSelector widget allows the user to choose
116  * from a one-dimensional range of colors which is given as a
117  * gradient between two colors provided by the programmer.
118  *
119  * \image html kgradientselector.png "KGradientSelector Widget"
120  */
121 class KWIDGETSADDONS_EXPORT KGradientSelector : public KSelector
122 {
123     Q_OBJECT
124 
125     Q_PROPERTY(QColor firstColor READ firstColor WRITE setFirstColor)
126     Q_PROPERTY(QColor secondColor READ secondColor WRITE setSecondColor)
127     Q_PROPERTY(QString firstText READ firstText WRITE setFirstText)
128     Q_PROPERTY(QString secondText READ secondText WRITE setSecondText)
129 
130 public:
131     /**
132      * Constructs a horizontal color selector which
133      * contains a gradient between white and black.
134      */
135     explicit KGradientSelector(QWidget *parent = nullptr);
136     /**
137      * Constructs a colors selector with orientation o which
138      * contains a gradient between white and black.
139      */
140     explicit KGradientSelector(Qt::Orientation o, QWidget *parent = nullptr);
141     /**
142      * Destructs the widget.
143      */
144     ~KGradientSelector() override;
145 
146     /**
147      * Sets the colors that make up the gradient. Any previously set colors
148      * are removed.
149      * @since 4.5
150      */
151     void setStops(const QGradientStops &stops);
152 
153     /**
154      * Get the colors that make up the gradient.
155      * @since 4.5
156      */
157     QGradientStops stops() const;
158 
159     /**
160      * Sets the two colors which span the gradient.
161      */
162     void setColors(const QColor &col1, const QColor &col2);
163     void setText(const QString &t1, const QString &t2);
164 
165     /**
166      * Set each color on its own.
167      */
168     void setFirstColor(const QColor &col);
169     void setSecondColor(const QColor &col);
170 
171     /**
172      * Set each description on its own
173      */
174     void setFirstText(const QString &t);
175     void setSecondText(const QString &t);
176 
177     QColor firstColor() const;
178     QColor secondColor() const;
179 
180     QString firstText() const;
181     QString secondText() const;
182 
183 protected:
184     void drawContents(QPainter *) override;
185     virtual QSize minimumSize() const;
186 
187 private:
188     friend class KGradientSelectorPrivate;
189     std::unique_ptr<class KGradientSelectorPrivate> const d;
190 
191     Q_DISABLE_COPY(KGradientSelector)
192 };
193 
194 #endif // KSELECTOR_H
195