qskinny/src/common/QskShadowMetrics.cpp

125 lines
3.5 KiB
C++
Raw Normal View History

2020-10-09 08:21:30 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskShadowMetrics.h"
#include <qhashfunctions.h>
2020-10-20 17:45:07 +02:00
#include <qrect.h>
2020-10-09 08:21:30 +02:00
#include <qvariant.h>
static void qskRegisterShadowMetrics()
{
qRegisterMetaType< QskShadowMetrics >();
2022-03-30 18:30:22 +02:00
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
QMetaType::registerEqualsComparator< QskShadowMetrics >();
#endif
2020-10-09 08:21:30 +02:00
}
Q_CONSTRUCTOR_FUNCTION( qskRegisterShadowMetrics )
static inline qreal qskInterpolated( qreal from, qreal to, qreal ratio )
{
return from + ( to - from ) * ratio;
}
static inline qreal qskToAbsolute( qreal length, qreal percentage )
{
percentage = qBound( 0.0, percentage, 100.0 );
return percentage / 100.0 * length;
}
QskShadowMetrics QskShadowMetrics::toAbsolute( const QSizeF& size ) const noexcept
{
if ( m_sizeMode != Qt::RelativeSize )
return *this;
if ( size.isEmpty() )
return QskShadowMetrics( 0.0, 0.0, QPointF() );
const qreal length = std::max( size.width(), size.height() );
const qreal spreadRadius = qskToAbsolute( length, m_spreadRadius );
const qreal blurRadius = qskToAbsolute( length, m_spreadRadius );
const qreal dx = qskToAbsolute( size.width(), m_offset.x() );
const qreal dy = qskToAbsolute( size.height(), m_offset.x() );
2020-12-05 15:09:31 +01:00
2020-10-09 08:21:30 +02:00
return QskShadowMetrics( spreadRadius, blurRadius, QPointF( dx, dy ) );
}
QskShadowMetrics QskShadowMetrics::interpolated(
const QskShadowMetrics& to, qreal ratio ) const noexcept
{
if ( ( *this == to ) || ( m_sizeMode != to.m_sizeMode ) )
return to;
const QPointF offset(
qskInterpolated( m_offset.x(), to.m_offset.x(), ratio ),
qskInterpolated( m_offset.y(), to.m_offset.y(), ratio ) );
2020-12-05 15:09:31 +01:00
QskShadowMetrics metrics(
2020-10-09 08:21:30 +02:00
qskInterpolated( m_spreadRadius, to.m_spreadRadius, ratio ),
qskInterpolated( m_blurRadius, to.m_blurRadius, ratio ),
offset );
metrics.m_sizeMode = m_sizeMode;
return metrics;
}
QVariant QskShadowMetrics::interpolate(
const QskShadowMetrics& from, const QskShadowMetrics& to,
qreal progress )
{
return QVariant::fromValue( from.interpolated( to, progress ) );
}
2020-10-20 17:45:07 +02:00
QRectF QskShadowMetrics::shadowRect( const QRectF& sourceRect ) const
{
2021-09-18 14:48:25 +02:00
const auto metrics = toAbsolute( sourceRect.size() );
const auto extent = metrics.m_spreadRadius + metrics.m_blurRadius;
2020-10-20 17:45:07 +02:00
return QRectF(
2021-09-18 14:48:25 +02:00
sourceRect.x() + metrics.m_offset.x() - extent,
sourceRect.y() + metrics.m_offset.y() - extent,
sourceRect.width() + 2 * extent,
sourceRect.height() + 2 * extent );
2020-10-20 17:45:07 +02:00
}
QskHashValue QskShadowMetrics::hash( QskHashValue seed ) const noexcept
2020-10-09 08:21:30 +02:00
{
QskHashValue hash;
2020-10-09 08:21:30 +02:00
hash = qHash( m_offset.x(), seed );
hash = qHash( m_offset.y(), seed );
hash = qHash( m_spreadRadius, hash );
hash = qHash( m_blurRadius, hash );
hash = qHash( static_cast< int >( m_sizeMode ), hash );
return hash;
}
#ifndef QT_NO_DEBUG_STREAM
#include <qdebug.h>
QDebug operator<<( QDebug debug, const QskShadowMetrics& metrics )
{
QDebugStateSaver saver( debug );
debug.nospace();
debug << "Shadow" << '(';
debug << " spread:" << metrics.spreadRadius();
debug << " blur:" << metrics.blurRadius();
debug << " dx:" << metrics.offset().x();
debug << " dy:" << metrics.offset().y();
debug << " )";
return debug;
}
#endif
2021-09-18 14:54:25 +02:00
#include "moc_QskShadowMetrics.cpp"