1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#ifndef GRADIENTS_H
52#define GRADIENTS_H
53
54#include "arthurwidgets.h"
55
56QT_BEGIN_NAMESPACE
57class QRadioButton;
58QT_END_NAMESPACE
59
60class HoverPoints;
61
62
63class ShadeWidget : public QWidget
64{
65 Q_OBJECT
66
67public:
68 enum ShadeType {
69 RedShade,
70 GreenShade,
71 BlueShade,
72 ARGBShade
73 };
74
75 ShadeWidget(ShadeType type, QWidget *parent);
76
77 void setGradientStops(const QGradientStops &stops);
78
79 void paintEvent(QPaintEvent *e) override;
80
81 QSize sizeHint() const override { return QSize(150, 40); }
82 QPolygonF points() const;
83
84 HoverPoints *hoverPoints() const { return m_hoverPoints; }
85
86 uint colorAt(int x);
87
88signals:
89 void colorsChanged();
90
91private:
92 void generateShade();
93
94 ShadeType m_shade_type;
95 QImage m_shade;
96 HoverPoints *m_hoverPoints;
97 QLinearGradient m_alpha_gradient;
98};
99
100class GradientEditor : public QWidget
101{
102 Q_OBJECT
103
104public:
105 GradientEditor(QWidget *parent);
106
107 void setGradientStops(const QGradientStops &stops);
108
109public slots:
110 void pointsUpdated();
111
112signals:
113 void gradientStopsChanged(const QGradientStops &stops);
114
115private:
116 ShadeWidget *m_red_shade;
117 ShadeWidget *m_green_shade;
118 ShadeWidget *m_blue_shade;
119 ShadeWidget *m_alpha_shade;
120};
121
122class GradientRenderer : public ArthurFrame
123{
124 Q_OBJECT
125
126public:
127 GradientRenderer(QWidget *parent);
128 void paint(QPainter *p) override;
129
130 QSize sizeHint() const override { return QSize(400, 400); }
131
132 HoverPoints *hoverPoints() const { return m_hoverPoints; }
133 void mousePressEvent(QMouseEvent *e) override;
134
135public slots:
136 void setGradientStops(const QGradientStops &stops);
137
138 void setPadSpread() { m_spread = QGradient::PadSpread; update(); }
139 void setRepeatSpread() { m_spread = QGradient::RepeatSpread; update(); }
140 void setReflectSpread() { m_spread = QGradient::ReflectSpread; update(); }
141
142 void setLinearGradient() { m_gradientType = Qt::LinearGradientPattern; update(); }
143 void setRadialGradient() { m_gradientType = Qt::RadialGradientPattern; update(); }
144 void setConicalGradient() { m_gradientType = Qt::ConicalGradientPattern; update(); }
145
146
147private:
148 QGradientStops m_stops;
149 HoverPoints *m_hoverPoints;
150
151 QGradient::Spread m_spread;
152 Qt::BrushStyle m_gradientType;
153};
154
155class GradientWidget : public QWidget
156{
157 Q_OBJECT
158
159public:
160 GradientWidget(QWidget *parent = nullptr);
161
162public slots:
163 void setDefault1() { setDefault(1); }
164 void setDefault2() { setDefault(2); }
165 void setDefault3() { setDefault(3); }
166 void setDefault4() { setDefault(4); }
167 void setPreset() { changePresetBy(indexOffset: 0); }
168 void setPrevPreset() { changePresetBy(indexOffset: -1); }
169 void setNextPreset() { changePresetBy(indexOffset: 1); }
170
171private:
172 void setDefault(int i);
173 void updatePresetName();
174 void changePresetBy(int indexOffset);
175
176 GradientRenderer *m_renderer;
177 GradientEditor *m_editor;
178
179 QRadioButton *m_linearButton;
180 QRadioButton *m_radialButton;
181 QRadioButton *m_conicalButton;
182 QRadioButton *m_padSpreadButton;
183 QRadioButton *m_reflectSpreadButton;
184 QRadioButton *m_repeatSpreadButton;
185 QPushButton *m_presetButton;
186
187 int m_presetIndex = 0;
188};
189
190#endif // GRADIENTS_H
191

source code of qtbase/examples/widgets/painting/gradients/gradients.h