QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskMargins.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskMargins.h"
7#include <qvariant.h>
8
9static void qskRegisterMargins()
10{
11 qRegisterMetaType< QskMargins >();
12
13 QMetaType::registerConverter< int, QskMargins >(
14 []( int value ) { return QskMargins( value ); } );
15
16 QMetaType::registerConverter< qreal, QskMargins >(
17 []( qreal value ) { return QskMargins( value ); } );
18
19#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
20 QMetaType::registerEqualsComparator< QskMargins >();
21#endif
22}
23
24Q_CONSTRUCTOR_FUNCTION( qskRegisterMargins )
25
26static inline qreal qskInterpolated( qreal from, qreal to, qreal ratio )
27{
28 return from + ( to - from ) * ratio;
29}
30
31static inline QskMargins qskInterpolateMargins(
32 const QskMargins& m1, const QskMargins& m2, qreal progress )
33{
34 const qreal left = qskInterpolated( m1.left(), m2.left(), progress );
35 const qreal top = qskInterpolated( m1.top(), m2.top(), progress );
36 const qreal right = qskInterpolated( m1.right(), m2.right(), progress );
37 const qreal bottom = qskInterpolated( m1.bottom(), m2.bottom(), progress );
38
39 return QskMargins( left, top, right, bottom );
40}
41
42QskMargins QskMargins::interpolated(
43 const QskMargins& to, qreal progress ) const noexcept
44{
45 return qskInterpolateMargins( *this, to, progress );
46}
47
48QVariant QskMargins::interpolate(
49 const QskMargins& m1, const QskMargins& m2, qreal progress ) noexcept
50{
51 return QVariant::fromValue( qskInterpolateMargins( m1, m2, progress ) );
52}
53
54void QskMargins::setMarginsAt( Qt::Edges edges, qreal value ) noexcept
55{
56 if ( edges & Qt::LeftEdge )
57 setLeft( value );
58
59 if ( edges & Qt::TopEdge )
60 setTop( value );
61
62 if ( edges & Qt::RightEdge )
63 setRight( value );
64
65 if ( edges & Qt::BottomEdge )
66 setBottom( value );
67}
68
69qreal QskMargins::marginAt( Qt::Edge edge ) const noexcept
70{
71 switch ( edge )
72 {
73 case Qt::LeftEdge:
74 return left();
75
76 case Qt::TopEdge:
77 return top();
78
79 case Qt::RightEdge:
80 return right();
81
82 case Qt::BottomEdge:
83 return bottom();
84 }
85
86 return 0.0;
87}
88
89#include "moc_QskMargins.cpp"