QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskGradientDirection.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskGradientDirection.h"
7#include <qrect.h>
8
9static void qskRegisterGradientDirection()
10{
11 qRegisterMetaType< QskLinearDirection >();
12 qRegisterMetaType< QskConicDirection >();
13 qRegisterMetaType< QskRadialDirection >();
14}
15
16Q_CONSTRUCTOR_FUNCTION( qskRegisterGradientDirection )
17
18// -- QskLinearDirection
19
20void QskLinearDirection::setOrientation( Qt::Orientation orientation ) noexcept
21{
22 m_x1 = m_y1 = 0.0;
23 setStart( 0.0, 0.0 );
24
25 if ( orientation == Qt::Vertical )
26 {
27 m_x2 = 0.0;
28 m_y2 = 1.0;
29 }
30 else
31 {
32 m_x2 = 1.0;
33 m_y2 = 0.0;
34 }
35}
36
37void QskLinearDirection::setVector( const QLineF& vector ) noexcept
38{
39 m_x1 = vector.x1();
40 m_y1 = vector.y1();
41 m_x2 = vector.x2();
42 m_y2 = vector.y2();
43 m_dot = -1.0;
44}
45
46void QskLinearDirection::setVector( const QPointF& start, const QPointF& stop ) noexcept
47{
48 m_x1 = start.x();
49 m_y1 = start.y();
50 m_x2 = stop.x();
51 m_y2 = stop.y();
52 m_dot = -1.0;
53}
54
55void QskLinearDirection::setVector( qreal x1, qreal y1, qreal x2, qreal y2 ) noexcept
56{
57 m_x1 = x1;
58 m_y1 = y1;
59 m_x2 = x2;
60 m_y2 = y2;
61 m_dot = -1.0;
62}
63
64void QskLinearDirection::setStart( const QPointF& pos ) noexcept
65{
66 m_x1 = pos.x();
67 m_y1 = pos.y();
68 m_dot = -1.0;
69}
70
71void QskLinearDirection::setStart( qreal x, qreal y ) noexcept
72{
73 m_x1 = x;
74 m_y1 = y;
75 m_dot = -1.0;
76}
77
78void QskLinearDirection::setStop( const QPointF& pos ) noexcept
79{
80 m_x2 = pos.x();
81 m_y2 = pos.y();
82 m_dot = -1.0;
83}
84
85void QskLinearDirection::setStop( qreal x, qreal y ) noexcept
86{
87 m_x2 = x;
88 m_y2 = y;
89 m_dot = -1.0;
90}
91
92void QskLinearDirection::setX1( qreal x ) noexcept
93{
94 m_x1 = x;
95 m_dot = -1.0;
96}
97
98void QskLinearDirection::setY1( qreal y ) noexcept
99{
100 m_y1 = y;
101 m_dot = -1.0;
102}
103
104void QskLinearDirection::setX2( qreal x ) noexcept
105{
106 m_x2 = x;
107 m_dot = -1.0;
108}
109
110void QskLinearDirection::setY2( qreal y ) noexcept
111{
112 m_y2 = y;
113 m_dot = -1.0;
114}
115
116void QskLinearDirection::setInterval( Qt::Orientation orientation, qreal from, qreal to )
117{
118 if ( orientation == Qt::Vertical )
119 {
120 m_y1 = from;
121 m_y2 = to;
122 }
123 else
124 {
125 m_x1 = from;
126 m_x2 = to;
127 }
128 m_dot = -1.0;
129}
130
131void QskLinearDirection::precalculate() const noexcept
132{
133 m_dx = m_x2 - m_x1;
134 m_dy = m_y2 - m_y1;
135 m_dot = m_dx * m_dx + m_dy * m_dy;
136}
137
138static inline bool qskIntersectsTop(
139 qreal vx, qreal vy, qreal m, const QRectF& rect )
140{
141 const auto cx = vx - ( vy - rect.top() ) / m;
142 return cx > rect.left() && cx < rect.right();
143}
144
145static inline bool qskIntersectsBottom(
146 qreal vx, qreal vy, qreal m, const QRectF& rect )
147{
148 const auto cx = vx - ( vy - rect.bottom() ) / m;
149 return cx > rect.left() && cx < rect.right();
150}
151
152static inline bool qskIntersectsLeft(
153 qreal vx, qreal vy, qreal m, const QRectF& rect )
154{
155 const auto cy = vy - ( vx - rect.left() ) * m;
156 return ( cy > rect.top() && cy < rect.bottom() );
157}
158
159static inline bool qskIntersectsRight(
160 qreal vx, qreal vy, qreal m, const QRectF& rect )
161{
162 const auto cy = vy - ( vx - rect.right() ) * m;
163 return ( cy > rect.top() && cy < rect.bottom() );
164}
165
166bool QskLinearDirection::contains( const QRectF& rect ) const
167{
168 if ( m_y1 == m_y2 )
169 {
170 return ( m_x1 <= rect.left() && m_x2 >= rect.right() )
171 || ( m_x1 >= rect.right() && m_x2 <= rect.left() );
172 }
173
174 if ( m_x1 == m_x2 )
175 {
176 return ( m_y1 <= rect.top() && m_y2 >= rect.bottom() )
177 || ( m_y1 >= rect.bottom() && m_y2 <= rect.top() );
178 }
179
180 // are the normal vectors intersecting ?
181
182 const auto m = ( m_x2 - m_x1 ) / ( m_y1 - m_y2 ); // slope of the normal vectors
183
184 const bool intersecting =
185 qskIntersectsLeft( m_x1, m_y1, m, rect ) ||
186 qskIntersectsRight( m_x1, m_y1, m, rect ) ||
187 qskIntersectsTop( m_x1, m_y1, m, rect ) ||
188 qskIntersectsBottom( m_x1, m_y1, m, rect ) ||
189 qskIntersectsLeft( m_x2, m_y2, m, rect ) ||
190 qskIntersectsRight( m_x2, m_y2, m, rect ) ||
191 qskIntersectsTop( m_x2, m_y2, m, rect ) ||
192 qskIntersectsBottom( m_x2, m_y2, m, rect );
193
194 return !intersecting;
195}
196
197// -- QskConicDirection
198
199void QskConicDirection::setCenter( const QPointF& center ) noexcept
200{
201 m_x = center.x();
202 m_y = center.y();
203}
204
205void QskConicDirection::setCenter( qreal x, qreal y ) noexcept
206{
207 m_x = x;
208 m_y = y;
209}
210
211void QskConicDirection::setX( qreal x ) noexcept
212{
213 m_x = x;
214}
215
216void QskConicDirection::setY( qreal y ) noexcept
217{
218 m_y = y;
219}
220
221void QskConicDirection::setStartAngle( qreal degrees ) noexcept
222{
223 m_startAngle = degrees;
224}
225
226void QskConicDirection::setSpanAngle( qreal degrees ) noexcept
227{
228 m_spanAngle = qBound( -360.0, degrees, 360.0 );
229}
230
231void QskConicDirection::setAspectRatio( qreal ratio ) noexcept
232{
233 m_aspectRatio = qMax( ratio, 0.0 );
234}
235
236// -- QskRadialDirection
237
238void QskRadialDirection::setCenter( const QPointF& center ) noexcept
239{
240 m_x = center.x();
241 m_y = center.y();
242}
243
244void QskRadialDirection::setCenter( qreal x, qreal y ) noexcept
245{
246 m_x = x;
247 m_y = y;
248}
249
250void QskRadialDirection::setX( qreal x ) noexcept
251{
252 m_x = x;
253}
254
255void QskRadialDirection::setY( qreal y ) noexcept
256{
257 m_y = y;
258}
259
260void QskRadialDirection::setRadiusX( qreal radius ) noexcept
261{
262 m_radiusX = radius;
263}
264
265void QskRadialDirection::setRadiusY( qreal radius ) noexcept
266{
267 m_radiusY = radius;
268}
269
270void QskRadialDirection::setRadius( qreal radius ) noexcept
271{
272 m_radiusX = m_radiusY = radius;
273}
274
275void QskRadialDirection::setRadius( qreal radiusX, qreal radiusY ) noexcept
276{
277 m_radiusX = radiusX;
278 m_radiusY = radiusY;
279}
280
281#include "moc_QskGradientDirection.cpp"