QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskBoxBorderColors.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskBoxBorderColors.h"
7#include "QskRgbValue.h"
8
9#include <qhashfunctions.h>
10#include <qvariant.h>
11
12static void qskRegisterBoxBorderColors()
13{
14 qRegisterMetaType< QskBoxBorderColors >();
15
16#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
17 QMetaType::registerEqualsComparator< QskBoxBorderColors >();
18#endif
19
20 QMetaType::registerConverter< QColor, QskBoxBorderColors >(
21 []( const QColor& color ) { return QskBoxBorderColors( color ); } );
22
23 QMetaType::registerConverter< QskGradient, QskBoxBorderColors >(
24 []( const QskGradient& gradient ) { return QskBoxBorderColors( gradient ); } );
25}
26
27Q_CONSTRUCTOR_FUNCTION( qskRegisterBoxBorderColors )
28
29QskBoxBorderColors::QskBoxBorderColors()
30{
31}
32
33QskBoxBorderColors::QskBoxBorderColors(
34 const QskGradient& left, const QskGradient& top,
35 const QskGradient& right, const QskGradient& bottom )
36 : m_gradients { top, left, right, bottom }
37{
38}
39
40QskBoxBorderColors::QskBoxBorderColors( const QskGradient& gradient )
41 : m_gradients { gradient, gradient, gradient, gradient }
42{
43}
44
45QskBoxBorderColors::~QskBoxBorderColors()
46{
47}
48
49bool QskBoxBorderColors::operator==( const QskBoxBorderColors& other ) const
50{
51 return ( m_gradients[ 0 ] == other.m_gradients[ 0 ] ) &&
52 ( m_gradients[ 1 ] == other.m_gradients[ 1 ] ) &&
53 ( m_gradients[ 2 ] == other.m_gradients[ 2 ] ) &&
54 ( m_gradients[ 3 ] == other.m_gradients[ 3 ] );
55}
56
57void QskBoxBorderColors::setAlpha( int alpha )
58{
59 for ( int i = 0; i < 4; i++ )
60 {
61 if ( m_gradients[ i ].isValid() )
62 m_gradients[ i ].setAlpha( alpha );
63 }
64}
65
66void QskBoxBorderColors::setGradients( const QskGradient& gradient )
67{
68 m_gradients[ 0 ] = m_gradients[ 1 ] = m_gradients[ 2 ] = m_gradients[ 3 ] = gradient;
69}
70
71void QskBoxBorderColors::setGradients( const QskGradient& left, const QskGradient& top,
72 const QskGradient& right, const QskGradient& bottom )
73{
74 m_gradients[ Top ] = top;
75 m_gradients[ Left ] = left;
76 m_gradients[ Right ] = right;
77 m_gradients[ Bottom ] = bottom;
78}
79
80void QskBoxBorderColors::setGradientAt( Qt::Edges edges, const QskGradient& gradient )
81{
82 if ( edges & Qt::TopEdge )
83 m_gradients[ Top ] = gradient;
84
85 if ( edges & Qt::LeftEdge )
86 m_gradients[ Left ] = gradient;
87
88 if ( edges & Qt::RightEdge )
89 m_gradients[ Right ] = gradient;
90
91 if ( edges & Qt::BottomEdge )
92 m_gradients[ Bottom ] = gradient;
93}
94
95void QskBoxBorderColors::setLeft( const QskGradient& gradient )
96{
97 m_gradients[ Left ] = gradient;
98}
99
100void QskBoxBorderColors::setTop( const QskGradient& gradient )
101{
102 m_gradients[ Top ] = gradient;
103}
104
105void QskBoxBorderColors::setRight( const QskGradient& gradient )
106{
107 m_gradients[ Right ] = gradient;
108}
109
110void QskBoxBorderColors::setBottom( const QskGradient& gradient )
111{
112 m_gradients[ Bottom ] = gradient;
113}
114
115const QskGradient& QskBoxBorderColors::gradientAt( Qt::Edge edge ) const
116{
117 switch ( edge )
118 {
119 case Qt::TopEdge:
120 return m_gradients[ Top ];
121
122 case Qt::LeftEdge:
123 return m_gradients[ Left ];
124
125 case Qt::RightEdge:
126 return m_gradients[ Right ];
127
128 case Qt::BottomEdge:
129 return m_gradients[ Bottom ];
130 }
131
132 static QskGradient noGradient;
133 return noGradient;
134}
135
136bool QskBoxBorderColors::isVisible() const
137{
138 if ( isValid() )
139 {
140 if ( m_gradients[ 0 ].isVisible() )
141 return true;
142
143 if ( m_gradients[ 1 ].isVisible() )
144 return true;
145
146 if ( m_gradients[ 2 ].isVisible() )
147 return true;
148
149 if ( m_gradients[ 3 ].isVisible() )
150 return true;
151 }
152
153 return false;
154}
155
156bool QskBoxBorderColors::isMonochrome() const
157{
158 if ( m_gradients[ 0 ].isMonochrome()
159 && m_gradients[ 1 ].isMonochrome()
160 && m_gradients[ 2 ].isMonochrome()
161 && m_gradients[ 3 ].isMonochrome() )
162 {
163 if ( m_gradients[ 1 ].rgbStart() != m_gradients[ 0 ].rgbStart() )
164 return false;
165
166 if ( m_gradients[ 2 ].rgbStart() != m_gradients[ 1 ].rgbStart() )
167 return false;
168
169 if ( m_gradients[ 3 ].rgbStart() != m_gradients[ 2 ].rgbStart() )
170 return false;
171
172 return true;
173 }
174
175 return false;
176}
177
178bool QskBoxBorderColors::isValid() const
179{
180 return m_gradients[ 0 ].isValid()
181 && m_gradients[ 1 ].isValid()
182 && m_gradients[ 2 ].isValid()
183 && m_gradients[ 3 ].isValid();
184}
185
186QskBoxBorderColors QskBoxBorderColors::interpolated(
187 const QskBoxBorderColors& to, qreal ratio ) const
188{
189 QskBoxBorderColors colors;
190
191 for ( size_t i = 0; i < 4; i++ )
192 {
193#if 1
194 /*
195 When one border has a width of 0 we would prefer to ignore
196 the color and use always use the other color. TODO ...
197 */
198#endif
199 colors.m_gradients[ i ] =
200 m_gradients[ i ].interpolated( to.m_gradients[ i ], ratio );
201 }
202
203 return colors;
204}
205
206QVariant QskBoxBorderColors::interpolate(
207 const QskBoxBorderColors& from, const QskBoxBorderColors& to, qreal ratio )
208{
209 return QVariant::fromValue( from.interpolated( to, ratio ) );
210}
211
212QskHashValue QskBoxBorderColors::hash( QskHashValue seed ) const
213{
214 auto h = m_gradients[ 0 ].hash( seed );
215 h = m_gradients[ 1 ].hash( h );
216 h = m_gradients[ 2 ].hash( h );
217 h = m_gradients[ 3 ].hash( h );
218
219 return h;
220}
221
222#ifndef QT_NO_DEBUG_STREAM
223
224#include <qdebug.h>
225
226QDebug operator<<( QDebug debug, const QskBoxBorderColors& colors )
227{
228 QDebugStateSaver saver( debug );
229 debug.nospace();
230
231 debug << "BoxBorderColors";
232
233 if ( !colors.isValid() )
234 {
235 debug << "()";
236 }
237 else
238 {
239 debug << "( ";
240
241 if ( colors.isMonochrome() )
242 {
243 const auto& gradient = colors.gradientAt( Qt::LeftEdge );
244 QskRgb::debugColor( debug, gradient.rgbStart() );
245 }
246 else
247 {
248 using namespace Qt;
249
250 const char prompts[] = { 'L', 'T', 'R', 'B' };
251 const Edge edges[] = { LeftEdge, TopEdge, RightEdge, BottomEdge };
252
253 for ( int i = 0; i < 4; i++ )
254 {
255 if ( i != 0 )
256 debug << ", ";
257
258 const auto& gradient = colors.gradientAt( edges[i] );
259
260 debug << prompts[ i ] << ": ";
261
262 if ( gradient.isValid() && gradient.isMonochrome() )
263 QskRgb::debugColor( debug, gradient.rgbStart() );
264 else
265 debug << gradient;
266 }
267 }
268
269 debug << " )";
270 }
271
272 return debug;
273}
274
275#endif
276
277#include "moc_QskBoxBorderColors.cpp"