qskinny/src/common/QskGraduationMetrics.h

171 lines
5.3 KiB
C
Raw Normal View History

2023-11-25 17:04:06 +01:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#ifndef QSK_GRADUATION_METRICS_H
#define QSK_GRADUATION_METRICS_H
2023-11-28 10:46:03 +01:00
#include "QskTickmarks.h"
2023-11-25 17:04:06 +01:00
#include "QskFunctions.h"
#include <algorithm>
#include <qmetatype.h>
class QSK_EXPORT QskGraduationMetrics
{
Q_GADGET
Q_PROPERTY( qreal majorTickLength READ majorTickLength WRITE setMajorTickLength )
Q_PROPERTY( qreal mediumTickLength READ mediumTickLength WRITE setMediumTickLength )
Q_PROPERTY( qreal minorTickLength READ minorTickLength WRITE setMinorTickLength )
Q_PROPERTY( qreal tickWidth READ tickWidth WRITE setTickWidth )
2023-11-25 17:04:06 +01:00
public:
2023-11-28 10:46:03 +01:00
using TickType = QskTickmarks::TickType;
2023-11-25 17:04:06 +01:00
constexpr QskGraduationMetrics() noexcept = default;
2023-11-25 17:04:06 +01:00
constexpr QskGraduationMetrics( qreal minorTickLength,
qreal mediumTickLength, qreal majorTickLength,
qreal tickWidth = 1.0 ) noexcept;
2023-11-25 17:04:06 +01:00
[[nodiscard]] constexpr bool operator==( const QskGraduationMetrics& ) const noexcept;
[[nodiscard]] constexpr bool operator!=( const QskGraduationMetrics& ) const noexcept;
2023-11-25 17:04:06 +01:00
constexpr void setTickWidth( qreal ) noexcept;
[[nodiscard]] constexpr qreal tickWidth() const noexcept;
2023-11-25 17:04:06 +01:00
constexpr void setTickLength( TickType, qreal ) noexcept;
[[nodiscard]] constexpr qreal tickLength( TickType ) const noexcept;
constexpr void setMinorTickLength( qreal ) noexcept;
[[nodiscard]] constexpr qreal minorTickLength() const noexcept;
constexpr void setMediumTickLength( qreal ) noexcept;
[[nodiscard]] constexpr qreal mediumTickLength() const noexcept;
constexpr void setMajorTickLength( qreal ) noexcept;
[[nodiscard]] constexpr qreal majorTickLength() const noexcept;
[[nodiscard]] QskGraduationMetrics interpolated(
const QskGraduationMetrics&, qreal progress ) const noexcept;
[[nodiscard]] static QVariant interpolate(
const QskGraduationMetrics&, const QskGraduationMetrics&, qreal progress );
[[nodiscard]] QskHashValue hash( QskHashValue seed = 0 ) const noexcept;
2023-11-28 14:35:28 +01:00
[[nodiscard]] constexpr qreal maxLength() const noexcept;
2023-11-25 17:04:06 +01:00
private:
static inline constexpr qreal validated( qreal value )
2023-11-25 17:04:06 +01:00
{
return std::max( 0.0, value );
2023-11-25 17:04:06 +01:00
}
qreal m_tickLengths[3] = {};
qreal m_tickWidth = 1.0;
2023-11-25 17:04:06 +01:00
};
inline constexpr QskGraduationMetrics::QskGraduationMetrics(
qreal minorTickLength, qreal mediumTickLength, qreal majorTickLength,
qreal tickWidth ) noexcept
: m_tickLengths{ validated( minorTickLength ),
validated( mediumTickLength ), validated( majorTickLength ) }
, m_tickWidth( tickWidth )
2023-11-25 17:04:06 +01:00
{
}
inline constexpr void QskGraduationMetrics::setMajorTickLength( qreal length ) noexcept
2023-11-25 17:04:06 +01:00
{
setTickLength( QskTickmarks::MajorTick, length );
2023-11-25 17:04:06 +01:00
}
inline constexpr qreal QskGraduationMetrics::majorTickLength() const noexcept
2023-11-25 17:04:06 +01:00
{
return tickLength( QskTickmarks::MajorTick );
2023-11-25 17:04:06 +01:00
}
inline constexpr void QskGraduationMetrics::setMediumTickLength( qreal length ) noexcept
2023-11-25 17:04:06 +01:00
{
setTickLength( QskTickmarks::MediumTick, length );
2023-11-25 17:04:06 +01:00
}
inline constexpr qreal QskGraduationMetrics::mediumTickLength() const noexcept
2023-11-25 17:04:06 +01:00
{
return tickLength( QskTickmarks::MediumTick );
2023-11-25 17:04:06 +01:00
}
inline constexpr void QskGraduationMetrics::setMinorTickLength( qreal length ) noexcept
2023-11-25 17:04:06 +01:00
{
setTickLength( QskTickmarks::MinorTick, length );
2023-11-25 17:04:06 +01:00
}
inline constexpr qreal QskGraduationMetrics::minorTickLength() const noexcept
2023-11-25 17:04:06 +01:00
{
return tickLength( QskTickmarks::MinorTick );
2023-11-25 17:04:06 +01:00
}
inline constexpr bool QskGraduationMetrics::operator==(
const QskGraduationMetrics& other ) const noexcept
{
return qskFuzzyCompare( m_tickLengths[0], other.m_tickLengths[0] ) &&
qskFuzzyCompare( m_tickLengths[1], other.m_tickLengths[1] ) &&
qskFuzzyCompare( m_tickLengths[2], other.m_tickLengths[2] &&
qskFuzzyCompare( m_tickWidth, other.m_tickWidth ) );
2023-11-25 17:04:06 +01:00
}
inline constexpr bool QskGraduationMetrics::operator!=(
const QskGraduationMetrics& rhs ) const noexcept
{
return !( *this == rhs );
}
inline constexpr void QskGraduationMetrics::setTickWidth( qreal width ) noexcept
2023-11-25 17:04:06 +01:00
{
m_tickWidth = validated( width );
}
inline constexpr qreal QskGraduationMetrics::tickWidth() const noexcept
{
return m_tickWidth;
2023-11-25 17:04:06 +01:00
}
inline constexpr void QskGraduationMetrics::setTickLength(
TickType type, qreal length ) noexcept
{
m_tickLengths[ type ] = validated( length );
}
inline constexpr qreal QskGraduationMetrics::tickLength(
const QskTickmarks::TickType type ) const noexcept
{
return m_tickLengths[ type ];
2023-11-25 17:04:06 +01:00
}
2023-11-28 14:35:28 +01:00
inline constexpr qreal QskGraduationMetrics::maxLength() const noexcept
{
using namespace std;
return max( max( m_tickLengths[0], m_tickLengths[1] ), m_tickLengths[2] );
}
2023-11-25 17:04:06 +01:00
inline QskHashValue QskGraduationMetrics::hash( const QskHashValue seed ) const noexcept
{
auto hash = qHash( m_tickLengths[0], seed );
hash = qHash( m_tickLengths[1], hash );
hash = qHash( m_tickLengths[2], hash );
hash = qHash( m_tickWidth, hash );
2023-11-25 17:04:06 +01:00
return hash;
}
#ifndef QT_NO_DEBUG_STREAM
class QDebug;
QSK_EXPORT QDebug operator<<( QDebug, const QskGraduationMetrics& );
#endif
Q_DECLARE_TYPEINFO( QskGraduationMetrics, Q_MOVABLE_TYPE );
Q_DECLARE_METATYPE( QskGraduationMetrics )
#endif