common/QskArcMetrics.cpp

Functions

  Name
void qskRegisterArcMetrics()
qreal qskInterpolated(qreal from, qreal to, qreal ratio)
qreal qskAbsoluted(qreal length, qreal percentage)
QDebug operator«(QDebug debug, const QskArcMetrics & metrics)

Functions Documentation

function qskRegisterArcMetrics

static void qskRegisterArcMetrics()

function qskInterpolated

static inline qreal qskInterpolated(
    qreal from,
    qreal to,
    qreal ratio
)

function qskAbsoluted

static inline qreal qskAbsoluted(
    qreal length,
    qreal percentage
)

function operator«

QDebug operator<<(
    QDebug debug,
    const QskArcMetrics & metrics
)

Source code

/******************************************************************************
 * QSkinny - Copyright (C) 2021 Uwe Rathmann
 * This file may be used under the terms of the QSkinny License, Version 1.0
 *****************************************************************************/

#include "QskArcMetrics.h"

#include <qhashfunctions.h>
#include <qvariant.h>

static void qskRegisterArcMetrics()
{
    qRegisterMetaType< QskArcMetrics >();
}

Q_CONSTRUCTOR_FUNCTION( qskRegisterArcMetrics )

static inline qreal qskInterpolated( qreal from, qreal to, qreal ratio )
{
    return from + ( to - from ) * ratio;
}

static inline qreal qskAbsoluted( qreal length, qreal percentage )
{
    // 100% means -> 0.5 of length
    percentage = qBound( 0.0, percentage, 100.0 );
    return percentage / 100.0 * 0.5 * length;
}

void QskArcMetrics::setWidth( qreal width ) noexcept
{
    m_width = width;
}

void QskArcMetrics::setStartAngle( qreal startAngle ) noexcept
{
    m_startAngle = startAngle;
}

void QskArcMetrics::setSpanAngle( qreal spanAngle ) noexcept
{
    m_spanAngle = spanAngle;
}

void QskArcMetrics::setSizeMode( Qt::SizeMode sizeMode ) noexcept
{
    m_sizeMode = sizeMode;
}

QskArcMetrics QskArcMetrics::interpolated(
    const QskArcMetrics& to, qreal ratio ) const noexcept
{
    if ( ( *this == to ) || ( m_sizeMode != to.m_sizeMode ) )
        return to;

    const qreal width = qskInterpolated( m_width, to.m_width, ratio );

    const qreal s1 = qskInterpolated( m_startAngle, to.m_startAngle, ratio );
    const qreal s2 = qskInterpolated( endAngle(), to.endAngle(), ratio );

    return QskArcMetrics( width, s1, s2 - s1, m_sizeMode );
}

QVariant QskArcMetrics::interpolate(
    const QskArcMetrics& from, const QskArcMetrics& to,
    qreal progress )
{
    return QVariant::fromValue( from.interpolated( to, progress ) );
}

QskArcMetrics QskArcMetrics::toAbsolute( const QSizeF& size ) const noexcept
{
    if ( m_sizeMode != Qt::RelativeSize )
        return *this;

    QskArcMetrics absoluted = *this;

    const auto l = qMin( size.width(), size.height() );
    if ( l <= 0.0 )
        absoluted.m_width = 0.0;
    else
        absoluted.m_width = qskAbsoluted( l, absoluted.m_width );

    absoluted.m_sizeMode = Qt::AbsoluteSize;

    return absoluted;
}

uint QskArcMetrics::hash( uint seed ) const noexcept
{
    uint hash = qHash( m_width, seed );
    hash = qHash( m_startAngle, hash );
    hash = qHash( m_spanAngle, hash );

    const int mode = m_sizeMode;
    return qHashBits( &mode, sizeof( mode ), hash );
}

#ifndef QT_NO_DEBUG_STREAM

#include <qdebug.h>

QDebug operator<<( QDebug debug, const QskArcMetrics& metrics )
{
    QDebugStateSaver saver( debug );
    debug.nospace();

    debug << "QskArcMetrics" << '(';
    debug << metrics.width() << ',' << metrics.sizeMode();
    debug << ",[" << metrics.startAngle() << ',' << metrics.spanAngle() << ']';
    debug << ')';

    return debug;
}

#endif

#include "moc_QskArcMetrics.cpp"

Updated on 28 July 2023 at 14:02:29 CEST