QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskShadowMetrics.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskShadowMetrics.h"
7
8#include <qhashfunctions.h>
9#include <qrect.h>
10#include <qvariant.h>
11
12static void qskRegisterShadowMetrics()
13{
14 qRegisterMetaType< QskShadowMetrics >();
15
16#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
17 QMetaType::registerEqualsComparator< QskShadowMetrics >();
18#endif
19}
20
21Q_CONSTRUCTOR_FUNCTION( qskRegisterShadowMetrics )
22
23static inline qreal qskInterpolated( qreal from, qreal to, qreal ratio )
24{
25 return from + ( to - from ) * ratio;
26}
27
28static inline qreal qskToAbsolute( qreal length, qreal percentage )
29{
30 percentage = qBound( 0.0, percentage, 100.0 );
31 return percentage / 100.0 * length;
32}
33
34QskShadowMetrics QskShadowMetrics::toAbsolute( const QSizeF& size ) const noexcept
35{
36 if ( m_sizeMode != Qt::RelativeSize )
37 return *this;
38
39 if ( size.isEmpty() )
40 return QskShadowMetrics( 0.0, 0.0, QPointF() );
41
42 const qreal length = std::max( size.width(), size.height() );
43
44 const qreal spreadRadius = qskToAbsolute( length, m_spreadRadius );
45 const qreal blurRadius = qskToAbsolute( length, m_spreadRadius );
46 const qreal dx = qskToAbsolute( size.width(), m_offset.x() );
47 const qreal dy = qskToAbsolute( size.height(), m_offset.x() );
48
49 return QskShadowMetrics( spreadRadius, blurRadius, QPointF( dx, dy ) );
50}
51
52QskShadowMetrics QskShadowMetrics::interpolated(
53 const QskShadowMetrics& to, qreal ratio ) const noexcept
54{
55 if ( ( *this == to ) || ( m_sizeMode != to.m_sizeMode ) )
56 return to;
57
58 const QPointF offset(
59 qskInterpolated( m_offset.x(), to.m_offset.x(), ratio ),
60 qskInterpolated( m_offset.y(), to.m_offset.y(), ratio ) );
61
62 QskShadowMetrics metrics(
63 qskInterpolated( m_spreadRadius, to.m_spreadRadius, ratio ),
64 qskInterpolated( m_blurRadius, to.m_blurRadius, ratio ),
65 offset );
66 metrics.m_sizeMode = m_sizeMode;
67
68 return metrics;
69}
70
71QVariant QskShadowMetrics::interpolate(
72 const QskShadowMetrics& from, const QskShadowMetrics& to,
73 qreal progress )
74{
75 return QVariant::fromValue( from.interpolated( to, progress ) );
76}
77
78QRectF QskShadowMetrics::shadowRect( const QRectF& sourceRect ) const
79{
80 const auto metrics = toAbsolute( sourceRect.size() );
81 const auto extent = metrics.m_spreadRadius + metrics.m_blurRadius;
82
83 return QRectF(
84 sourceRect.x() + metrics.m_offset.x() - extent,
85 sourceRect.y() + metrics.m_offset.y() - extent,
86 sourceRect.width() + 2 * extent,
87 sourceRect.height() + 2 * extent );
88}
89
90QskHashValue QskShadowMetrics::hash( QskHashValue seed ) const noexcept
91{
92 QskHashValue hash;
93
94 hash = qHash( m_offset.x(), seed );
95 hash = qHash( m_offset.y(), seed );
96 hash = qHash( m_spreadRadius, hash );
97 hash = qHash( m_blurRadius, hash );
98 hash = qHash( m_sizeMode, hash );
99 hash = qHash( m_shapeMode, hash );
100
101 return hash;
102}
103
104#ifndef QT_NO_DEBUG_STREAM
105
106#include <qdebug.h>
107
108QDebug operator<<( QDebug debug, const QskShadowMetrics& metrics )
109{
110 QDebugStateSaver saver( debug );
111 debug.nospace();
112
113 debug << "Shadow" << '(';
114 debug << " spread:" << metrics.spreadRadius();
115 debug << " blur:" << metrics.blurRadius();
116 debug << " dx:" << metrics.offset().x();
117 debug << " dy:" << metrics.offset().y();
118 debug << " )";
119
120 return debug;
121}
122
123#endif
124
125#include "moc_QskShadowMetrics.cpp"