QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskGraduationMetrics.h
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#ifndef QSK_GRADUATION_METRICS_H
7#define QSK_GRADUATION_METRICS_H
8
9#include "QskTickmarks.h"
10#include "QskFunctions.h"
11#include <algorithm>
12#include <qmetatype.h>
13
14class QSK_EXPORT QskGraduationMetrics
15{
16 Q_GADGET
17 Q_PROPERTY( qreal majorTickLength READ majorTickLength WRITE setMajorTickLength )
18 Q_PROPERTY( qreal mediumTickLength READ mediumTickLength WRITE setMediumTickLength )
19 Q_PROPERTY( qreal minorTickLength READ minorTickLength WRITE setMinorTickLength )
20 Q_PROPERTY( qreal tickWidth READ tickWidth WRITE setTickWidth )
21
22 public:
23 using TickType = QskTickmarks::TickType;
24
25 constexpr QskGraduationMetrics() noexcept = default;
26
27 constexpr QskGraduationMetrics( qreal minorTickLength,
28 qreal mediumTickLength, qreal majorTickLength,
29 qreal tickWidth = 1.0 ) noexcept;
30
31 [[nodiscard]] constexpr bool operator==( const QskGraduationMetrics& ) const noexcept;
32 [[nodiscard]] constexpr bool operator!=( const QskGraduationMetrics& ) const noexcept;
33
34 constexpr void setTickWidth( qreal ) noexcept;
35 [[nodiscard]] constexpr qreal tickWidth() const noexcept;
36
37 constexpr void setTickLength( TickType, qreal ) noexcept;
38 [[nodiscard]] constexpr qreal tickLength( TickType ) const noexcept;
39
40 constexpr void setMinorTickLength( qreal ) noexcept;
41 [[nodiscard]] constexpr qreal minorTickLength() const noexcept;
42
43 constexpr void setMediumTickLength( qreal ) noexcept;
44 [[nodiscard]] constexpr qreal mediumTickLength() const noexcept;
45
46 constexpr void setMajorTickLength( qreal ) noexcept;
47 [[nodiscard]] constexpr qreal majorTickLength() const noexcept;
48
49 [[nodiscard]] QskGraduationMetrics interpolated(
50 const QskGraduationMetrics&, qreal progress ) const noexcept;
51
52 [[nodiscard]] static QVariant interpolate(
53 const QskGraduationMetrics&, const QskGraduationMetrics&, qreal progress );
54
55 [[nodiscard]] QskHashValue hash( QskHashValue seed = 0 ) const noexcept;
56
57 [[nodiscard]] constexpr qreal maxLength() const noexcept;
58
59 private:
60 static inline constexpr qreal validated( qreal value )
61 {
62 return std::max( 0.0, value );
63 }
64
65 qreal m_tickLengths[3] = {};
66 qreal m_tickWidth = 1.0;
67};
68
69inline constexpr QskGraduationMetrics::QskGraduationMetrics(
70 qreal minorTickLength, qreal mediumTickLength, qreal majorTickLength,
71 qreal tickWidth ) noexcept
72 : m_tickLengths{ validated( minorTickLength ),
73 validated( mediumTickLength ), validated( majorTickLength ) }
74 , m_tickWidth( tickWidth )
75{
76}
77
78inline constexpr void QskGraduationMetrics::setMajorTickLength( qreal length ) noexcept
79{
80 setTickLength( QskTickmarks::MajorTick, length );
81}
82
83inline constexpr qreal QskGraduationMetrics::majorTickLength() const noexcept
84{
85 return tickLength( QskTickmarks::MajorTick );
86}
87
88inline constexpr void QskGraduationMetrics::setMediumTickLength( qreal length ) noexcept
89{
90 setTickLength( QskTickmarks::MediumTick, length );
91}
92
93inline constexpr qreal QskGraduationMetrics::mediumTickLength() const noexcept
94{
95 return tickLength( QskTickmarks::MediumTick );
96}
97
98inline constexpr void QskGraduationMetrics::setMinorTickLength( qreal length ) noexcept
99{
100 setTickLength( QskTickmarks::MinorTick, length );
101}
102
103inline constexpr qreal QskGraduationMetrics::minorTickLength() const noexcept
104{
105 return tickLength( QskTickmarks::MinorTick );
106}
107
108inline constexpr bool QskGraduationMetrics::operator==(
109 const QskGraduationMetrics& other ) const noexcept
110{
111 return qskFuzzyCompare( m_tickLengths[0], other.m_tickLengths[0] ) &&
112 qskFuzzyCompare( m_tickLengths[1], other.m_tickLengths[1] ) &&
113 qskFuzzyCompare( m_tickLengths[2], other.m_tickLengths[2] &&
114 qskFuzzyCompare( m_tickWidth, other.m_tickWidth ) );
115}
116
117inline constexpr bool QskGraduationMetrics::operator!=(
118 const QskGraduationMetrics& rhs ) const noexcept
119{
120 return !( *this == rhs );
121}
122
123inline constexpr void QskGraduationMetrics::setTickWidth( qreal width ) noexcept
124{
125 m_tickWidth = validated( width );
126}
127
128inline constexpr qreal QskGraduationMetrics::tickWidth() const noexcept
129{
130 return m_tickWidth;
131}
132
133inline constexpr void QskGraduationMetrics::setTickLength(
134 TickType type, qreal length ) noexcept
135{
136 m_tickLengths[ type ] = validated( length );
137}
138
139inline constexpr qreal QskGraduationMetrics::tickLength(
140 const QskTickmarks::TickType type ) const noexcept
141{
142 return m_tickLengths[ type ];
143}
144
145inline constexpr qreal QskGraduationMetrics::maxLength() const noexcept
146{
147 using namespace std;
148 return max( max( m_tickLengths[0], m_tickLengths[1] ), m_tickLengths[2] );
149}
150
151inline QskHashValue QskGraduationMetrics::hash( const QskHashValue seed ) const noexcept
152{
153 auto hash = qHash( m_tickLengths[0], seed );
154 hash = qHash( m_tickLengths[1], hash );
155 hash = qHash( m_tickLengths[2], hash );
156 hash = qHash( m_tickWidth, hash );
157 return hash;
158}
159
160#ifndef QT_NO_DEBUG_STREAM
161
162class QDebug;
163QSK_EXPORT QDebug operator<<( QDebug, const QskGraduationMetrics& );
164
165#endif
166
167Q_DECLARE_TYPEINFO( QskGraduationMetrics, Q_MOVABLE_TYPE );
168Q_DECLARE_METATYPE( QskGraduationMetrics )
169
170#endif