QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskGradientDirection.h
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#ifndef QSK_GRDIENT_DIRECTION_H
7#define QSK_GRDIENT_DIRECTION_H
8
9#include "QskGlobal.h"
10
11#include <qmetatype.h>
12#include <qpoint.h>
13#include <qline.h>
14
15class QSK_EXPORT QskLinearDirection
16{
17 Q_GADGET
18
19 Q_PROPERTY( qreal x1 READ x1 WRITE setX1 )
20 Q_PROPERTY( qreal y1 READ y1 WRITE setY1 )
21 Q_PROPERTY( qreal x2 READ x2 WRITE setX2 )
22 Q_PROPERTY( qreal y2 READ y2 WRITE setY2 )
23
24 public:
25 constexpr QskLinearDirection() noexcept = default;
26
27 constexpr QskLinearDirection( Qt::Orientation ) noexcept;
28
29 constexpr QskLinearDirection( const QLineF& ) noexcept;
30 constexpr QskLinearDirection( const QPointF&, const QPointF& ) noexcept;
31 constexpr QskLinearDirection( qreal x1, qreal y1, qreal x2, qreal y2 ) noexcept;
32
33 void setVector( const QLineF& ) noexcept;
34 void setVector( const QPointF&, const QPointF& ) noexcept;
35 void setVector( qreal x1, qreal y1, qreal x2, qreal y2 ) noexcept;
36
37 constexpr QLineF vector() const noexcept;
38
39 void setStart( const QPointF& ) noexcept;
40 void setStart( qreal x, qreal y ) noexcept;
41
42 void setStop(const QPointF& ) noexcept;
43 void setStop( qreal x, qreal y ) noexcept;
44
45 void setInterval( Qt::Orientation, qreal, qreal );
46
47 void setOrientation( Qt::Orientation ) noexcept;
48 constexpr bool isOriented( Qt::Orientation ) const noexcept;
49
50 constexpr bool isHorizontal() const noexcept;
51 constexpr bool isVertical() const noexcept;
52 constexpr bool isTilted() const noexcept;
53
54 constexpr QPointF start() const noexcept;
55 constexpr QPointF stop() const noexcept;
56
57 constexpr qreal x1() const noexcept;
58 void setX1( qreal ) noexcept;
59
60 constexpr qreal y1() const noexcept;
61 void setY1( qreal ) noexcept;
62
63 constexpr qreal x2() const noexcept;
64 void setX2( qreal ) noexcept;
65
66 constexpr qreal y2() const noexcept;
67 void setY2( qreal ) noexcept;
68
69 qreal dx() const noexcept;
70 qreal dy() const noexcept;
71
72 /*
73 In direction of the gradient vector, where 0.0 corresponds to
74 points on the perpendicular at the start and 1.0 to points on
75 the perpendicular of the end point.
76
77 Also corresponds to the positions of the color stops and can be
78 used to calculate the color at a specific position.
79 */
80 qreal valueAt( const QPointF& ) const;
81 qreal valueAt( qreal x, qreal y ) const;
82
83 bool contains( const QRectF& ) const;
84
85 private:
86 void precalculate() const noexcept;
87
88 qreal m_x1 = 0.0;
89 qreal m_y1 = 0.0;
90 qreal m_x2 = 0.0;
91 qreal m_y2 = 1.0;
92
93 mutable qreal m_dx = 0.0;
94 mutable qreal m_dy = 0.0;
95 mutable qreal m_dot = -1.0;
96};
97
98class QSK_EXPORT QskConicDirection
99{
100 Q_GADGET
101
102 Q_PROPERTY( qreal x READ x WRITE setX )
103 Q_PROPERTY( qreal y READ y WRITE setY )
104 Q_PROPERTY( qreal startAngle READ startAngle WRITE setStartAngle )
105 Q_PROPERTY( qreal spanAngle READ spanAngle WRITE setSpanAngle )
106 Q_PROPERTY( qreal aspectRatio READ aspectRatio WRITE setAspectRatio )
107
108 public:
109 // counter-clockwise
110 constexpr QskConicDirection() noexcept = default;
111
112 constexpr QskConicDirection( qreal x, qreal y, qreal startAngle = 0.0 ) noexcept;
113 constexpr QskConicDirection( qreal x, qreal y,
114 qreal startAngle, qreal spanAngle, qreal aspectRatio = 0.0 ) noexcept;
115
116 constexpr QskConicDirection( const QPointF&, qreal startAngle = 0.0 ) noexcept;
117 constexpr QskConicDirection( const QPointF&,
118 qreal startAngle, qreal spanAngle, qreal aspectRatio = 0.0 ) noexcept;
119
120 constexpr QPointF center() const noexcept;
121 void setCenter(const QPointF& center) noexcept;
122 void setCenter(qreal x, qreal y) noexcept;
123
124 void setX( qreal ) noexcept;
125 constexpr qreal x() const noexcept;
126
127 void setY( qreal ) noexcept;
128 constexpr qreal y() const noexcept;
129
130 constexpr qreal startAngle() const noexcept;
131 void setStartAngle( qreal ) noexcept;
132
133 constexpr qreal spanAngle() const noexcept;
134 void setSpanAngle( qreal ) noexcept;
135
136 constexpr qreal aspectRatio() const noexcept;
137 void setAspectRatio( qreal ) noexcept;
138
139 private:
140 qreal m_x = 0.5;
141 qreal m_y = 0.5;
142 qreal m_startAngle = 0.0;
143 qreal m_spanAngle = 360.0;
144 qreal m_aspectRatio = 0.0;
145};
146
147class QSK_EXPORT QskRadialDirection
148{
149 Q_GADGET
150
151 Q_PROPERTY( qreal x READ x WRITE setX )
152 Q_PROPERTY( qreal y READ y WRITE setY )
153 Q_PROPERTY( qreal radiusX READ radiusX WRITE setRadiusX )
154 Q_PROPERTY( qreal radiusY READ radiusY WRITE setRadiusY )
155
156 public:
157 constexpr QskRadialDirection() noexcept = default;
158
159 constexpr QskRadialDirection( const QPointF& center, qreal radius ) noexcept;
160 constexpr QskRadialDirection( qreal cx, qreal cy, qreal radius ) noexcept;
161 constexpr QskRadialDirection( qreal cx, qreal cy, qreal radiusX, qreal radiusY ) noexcept;
162
163 constexpr QPointF center() const noexcept;
164 void setCenter(const QPointF& ) noexcept;
165 void setCenter(qreal x, qreal y) noexcept;
166
167 void setX( qreal ) noexcept;
168 constexpr qreal x() const noexcept;
169
170 void setY( qreal ) noexcept;
171 constexpr qreal y() const noexcept;
172
173 constexpr qreal radiusX() const noexcept;
174 void setRadiusX( qreal ) noexcept;
175
176 constexpr qreal radiusY() const noexcept;
177 void setRadiusY( qreal ) noexcept;
178
179 void setRadius( qreal ) noexcept;
180 void setRadius( qreal, qreal ) noexcept;
181
182 private:
183 qreal m_x = 0.5;
184 qreal m_y = 0.5;
185 qreal m_radiusX = 0.5;
186 qreal m_radiusY = 0.5;
187};
188
189inline constexpr QskLinearDirection::QskLinearDirection(
190 Qt::Orientation orientation ) noexcept
191 : m_x2( ( orientation == Qt::Vertical ) ? 0.0 : 1.0 )
192 , m_y2( ( orientation == Qt::Vertical ) ? 1.0 : 0.0 )
193{
194}
195
196inline constexpr QskLinearDirection::QskLinearDirection( const QLineF& vector ) noexcept
197 : QskLinearDirection( vector.x1(), vector.y1(), vector.x2(), vector.y2() )
198{
199}
200
201inline constexpr QskLinearDirection::QskLinearDirection(
202 const QPointF& start, const QPointF& stop ) noexcept
203 : QskLinearDirection( start.x(), start.y(), stop.x(), stop.y() )
204{
205}
206
207inline constexpr QskLinearDirection::QskLinearDirection(
208 qreal x1, qreal y1, qreal x2, qreal y2 ) noexcept
209 : m_x1( x1 )
210 , m_y1( y1 )
211 , m_x2( x2 )
212 , m_y2( y2 )
213{
214}
215
216inline constexpr qreal QskLinearDirection::x1() const noexcept
217{
218 return m_x1;
219}
220
221inline constexpr qreal QskLinearDirection::y1() const noexcept
222{
223 return m_y1;
224}
225
226inline constexpr qreal QskLinearDirection::x2() const noexcept
227{
228 return m_x2;
229}
230
231inline constexpr qreal QskLinearDirection::y2() const noexcept
232{
233 return m_y2;
234}
235
236inline qreal QskLinearDirection::dx() const noexcept
237{
238 if ( m_dot < 0.0 )
239 precalculate();
240
241 return m_dx;
242}
243
244inline qreal QskLinearDirection::dy() const noexcept
245{
246 if ( m_dot < 0.0 )
247 precalculate();
248
249 return m_dy;
250}
251
252inline constexpr QLineF QskLinearDirection::vector() const noexcept
253{
254 return QLineF( m_x1, m_y1, m_x2, m_y2 );
255}
256
257inline constexpr QPointF QskLinearDirection::start() const noexcept
258{
259 return QPointF( m_x1, m_y1 );
260}
261
262inline constexpr QPointF QskLinearDirection::stop() const noexcept
263{
264 return QPointF( m_x2, m_y2 );
265}
266
267inline constexpr bool QskLinearDirection::isOriented(
268 Qt::Orientation orientation ) const noexcept
269{
270 return ( orientation == Qt::Horizontal ) ? isHorizontal() : isVertical();
271}
272
273inline constexpr bool QskLinearDirection::isHorizontal() const noexcept
274{
275 return !isVertical() && ( m_y1 == m_y2 );
276}
277
278inline constexpr bool QskLinearDirection::isVertical() const noexcept
279{
280 return m_x1 == m_x2;
281}
282
283inline constexpr bool QskLinearDirection::isTilted() const noexcept
284{
285 return ( m_x1 != m_x2 ) && ( m_y1 != m_y2 );
286}
287
288inline qreal QskLinearDirection::valueAt( const QPointF& pos ) const
289{
290 return valueAt( pos.x(), pos.y() );
291}
292
293inline qreal QskLinearDirection::valueAt( qreal x, qreal y ) const
294{
295 if ( m_dot < 0.0 )
296 precalculate();
297
298 return ( ( x - m_x1 ) * m_dx + ( y - m_y1 ) * m_dy ) / m_dot;
299}
300
301inline constexpr QskConicDirection::QskConicDirection(
302 qreal x, qreal y, qreal startAngle ) noexcept
303 : QskConicDirection( x, y, startAngle, 360.0 )
304{
305}
306
307inline constexpr QskConicDirection::QskConicDirection( qreal x, qreal y,
308 qreal startAngle, qreal spanAngle, qreal aspectRatio ) noexcept
309 : m_x( x )
310 , m_y( y )
311 , m_startAngle( startAngle )
312 , m_spanAngle( spanAngle )
313 , m_aspectRatio( aspectRatio )
314{
315}
316
317inline constexpr QskConicDirection::QskConicDirection(
318 const QPointF& pos, qreal startAngle ) noexcept
319 : QskConicDirection( pos.x(), pos.y(), startAngle )
320{
321}
322
323inline constexpr QskConicDirection::QskConicDirection( const QPointF& pos,
324 qreal startAngle, qreal spanAngle, qreal apectRatio ) noexcept
325 : QskConicDirection( pos.x(), pos.y(), startAngle, spanAngle, apectRatio )
326{
327}
328
329inline constexpr QPointF QskConicDirection::center() const noexcept
330{
331 return QPointF( m_x, m_y );
332}
333
334inline constexpr qreal QskConicDirection::startAngle() const noexcept
335{
336 return m_startAngle;
337}
338
339inline constexpr qreal QskConicDirection::spanAngle() const noexcept
340{
341 return m_spanAngle;
342}
343
344inline constexpr qreal QskConicDirection::x() const noexcept
345{
346 return m_x;
347}
348
349inline constexpr qreal QskConicDirection::y() const noexcept
350{
351 return m_y;
352}
353
354inline constexpr qreal QskConicDirection::aspectRatio() const noexcept
355{
356 return m_aspectRatio;
357}
358
359inline constexpr QskRadialDirection::QskRadialDirection(
360 qreal x, qreal y, qreal radius ) noexcept
361 : m_x( x )
362 , m_y( y )
363 , m_radiusX( radius )
364 , m_radiusY( radius )
365{
366}
367
368inline constexpr QskRadialDirection::QskRadialDirection(
369 qreal x, qreal y, qreal radiusX, qreal radiusY ) noexcept
370 : m_x( x )
371 , m_y( y )
372 , m_radiusX( radiusX )
373 , m_radiusY( radiusY )
374{
375}
376
377inline constexpr QskRadialDirection::QskRadialDirection(
378 const QPointF& center, qreal radius ) noexcept
379 : QskRadialDirection( center.x(), center.y(), radius )
380{
381}
382
383inline constexpr QPointF QskRadialDirection::center() const noexcept
384{
385 return QPointF( m_x, m_y );
386}
387
388inline constexpr qreal QskRadialDirection::x() const noexcept
389{
390 return m_x;
391}
392
393inline constexpr qreal QskRadialDirection::y() const noexcept
394{
395 return m_y;
396}
397
398inline constexpr qreal QskRadialDirection::radiusX() const noexcept
399{
400 return m_radiusX;
401}
402
403inline constexpr qreal QskRadialDirection::radiusY() const noexcept
404{
405 return m_radiusY;
406}
407
408#endif