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
23 * \inmodule KWidgetsAddons
24 *
25 * \brief Base class for other widgets which
26 * provides the ability to choose from a one-dimensional
27 * range of values.
28 *
29 * An example is the KGradientSelector
30 * which allows to choose from a range of colors.
31 *
32 * A custom drawing routine for the widget surface has
33 * to be provided by the subclass.
34 */
35class KWIDGETSADDONS_EXPORT KSelector : public QAbstractSlider
36{
37 Q_OBJECT
38 /*!
39 * \property KSelector::value
40 */
41 Q_PROPERTY(int value READ value WRITE setValue)
42
43 /*!
44 * \property KSelector::minValue
45 */
46 Q_PROPERTY(int minValue READ minimum WRITE setMinimum)
47
48 /*!
49 * \property KSelector::maxValue
50 */
51 Q_PROPERTY(int maxValue READ maximum WRITE setMaximum)
52
53 /*!
54 * \property KSelector::indent
55 */
56 Q_PROPERTY(bool indent READ indent WRITE setIndent)
57
58 /*!
59 * \property KSelector::arrowDirection
60 */
61 Q_PROPERTY(Qt::ArrowType arrowDirection READ arrowDirection WRITE setArrowDirection)
62public:
63 /*!
64 * Constructs a horizontal one-dimensional selection widget.
65 */
66 explicit KSelector(QWidget *parent = nullptr);
67
68 /*!
69 * Constructs a one-dimensional selection widget with
70 * a given orientation.
71 */
72 explicit KSelector(Qt::Orientation o, QWidget *parent = nullptr);
73
74 ~KSelector() override;
75
76 /*!
77 * Returns the rectangle on which subclasses should draw.
78 */
79 QRect contentsRect() const;
80
81 /*!
82 * Sets the indent option of the widget to i.
83 *
84 * This determines whether a shaded frame is drawn.
85 */
86 void setIndent(bool i);
87
88 /*!
89 * Returns whether the indent option is set.
90 */
91 bool indent() const;
92
93 /*!
94 * Sets the arrow direction.
95 */
96 void setArrowDirection(Qt::ArrowType direction);
97
98 /*!
99 * Returns the current arrow direction
100 */
101 Qt::ArrowType arrowDirection() const;
102
103protected:
104 /*!
105 * Override this function to draw the contents of the control.
106 * The default implementation does nothing.
107 *
108 * Draw only within contentsRect().
109 */
110 virtual void drawContents(QPainter *);
111
112 /*!
113 * Override this function to draw the cursor which
114 * indicates the current value.
115 */
116 virtual void drawArrow(QPainter *painter, const QPoint &pos);
117
118 void paintEvent(QPaintEvent *) override;
119 void mousePressEvent(QMouseEvent *e) override;
120 void mouseMoveEvent(QMouseEvent *e) override;
121 void mouseReleaseEvent(QMouseEvent *e) override;
122 void wheelEvent(QWheelEvent *) override;
123
124private:
125 KWIDGETSADDONS_NO_EXPORT QPoint calcArrowPos(int val);
126 KWIDGETSADDONS_NO_EXPORT void moveArrow(const QPoint &pos);
127
128private:
129 friend class KSelectorPrivate;
130 std::unique_ptr<class KSelectorPrivate> const d;
131
132 Q_DISABLE_COPY(KSelector)
133};
134
135/*!
136 * \class KGradientSelector
137 * \inmodule KWidgetsAddons
138 *
139 * \brief Allows the user to choose
140 * from a one-dimensional range of colors which is given as a
141 * gradient between two colors provided by the programmer.
142 *
143 * \image kgradientselector.png "KGradientSelector Widget"
144 */
145class KWIDGETSADDONS_EXPORT KGradientSelector : public KSelector
146{
147 Q_OBJECT
148
149 /*!
150 * \property KGradientSelector::firstColor
151 */
152 Q_PROPERTY(QColor firstColor READ firstColor WRITE setFirstColor)
153
154 /*!
155 * \property KGradientSelector::secondColor
156 */
157 Q_PROPERTY(QColor secondColor READ secondColor WRITE setSecondColor)
158
159 /*!
160 * \property KGradientSelector::firstText
161 */
162 Q_PROPERTY(QString firstText READ firstText WRITE setFirstText)
163
164 /*!
165 * \property KGradientSelector::secondText
166 */
167 Q_PROPERTY(QString secondText READ secondText WRITE setSecondText)
168
169public:
170 /*!
171 * Constructs a horizontal color selector which
172 * contains a gradient between white and black.
173 */
174 explicit KGradientSelector(QWidget *parent = nullptr);
175 /*!
176 * Constructs a colors selector with orientation \a o which
177 * contains a gradient between white and black.
178 */
179 explicit KGradientSelector(Qt::Orientation o, QWidget *parent = nullptr);
180
181 ~KGradientSelector() override;
182
183 /*!
184 * Sets the colors that make up the gradient. Any previously set colors
185 * are removed.
186 * \since 4.5
187 */
188 void setStops(const QGradientStops &stops);
189
190 /*!
191 * Get the colors that make up the gradient.
192 * \since 4.5
193 */
194 QGradientStops stops() const;
195
196 /*!
197 * Sets the two colors which span the gradient.
198 */
199 void setColors(const QColor &col1, const QColor &col2);
200
201 /*!
202 *
203 */
204 void setText(const QString &t1, const QString &t2);
205
206 /*!
207 * Set each color on its own.
208 */
209 void setFirstColor(const QColor &col);
210
211 /*!
212 *
213 */
214 void setSecondColor(const QColor &col);
215
216 /*!
217 * Set each description on its own
218 */
219 void setFirstText(const QString &t);
220
221 /*!
222 *
223 */
224 void setSecondText(const QString &t);
225
226 /*!
227 *
228 */
229 QColor firstColor() const;
230
231 /*!
232 *
233 */
234 QColor secondColor() const;
235
236 /*!
237 *
238 */
239 QString firstText() const;
240
241 /*!
242 *
243 */
244 QString secondText() const;
245
246protected:
247 void drawContents(QPainter *) override;
248
249 /*!
250 *
251 */
252 virtual QSize minimumSize() const;
253
254private:
255 friend class KGradientSelectorPrivate;
256 std::unique_ptr<class KGradientSelectorPrivate> const d;
257
258 Q_DISABLE_COPY(KGradientSelector)
259};
260
261#endif // KSELECTOR_H
262

source code of kwidgetsaddons/src/kselector.h