Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
PhosphorProfile.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
8#include <PhosphorAnimation/phosphoranimation_export.h>
10
11#include <QtCore/QJsonObject>
12#include <QtCore/QObject>
13#include <QtCore/QString>
14#include <QtCore/QtMath>
15#include <QtQml/qqmlregistration.h>
16
17#include <cmath>
18
19namespace PhosphorAnimation {
20
49class PHOSPHORANIMATION_EXPORT PhosphorProfile
50{
51 Q_GADGET
52 QML_VALUE_TYPE(phosphorProfile)
53 QML_STRUCTURED_VALUE
54
55 Q_PROPERTY(PhosphorCurve curve READ curve WRITE setCurve)
56 Q_PROPERTY(qreal duration READ duration WRITE setDuration)
57 Q_PROPERTY(int minDistance READ minDistance WRITE setMinDistance)
58 Q_PROPERTY(SequenceMode sequenceMode READ sequenceMode WRITE setSequenceMode)
59 Q_PROPERTY(int staggerInterval READ staggerInterval WRITE setStaggerInterval)
60 Q_PROPERTY(QString presetName READ presetName WRITE setPresetName)
61
62public:
63 // Parallel to `PhosphorAnimation::SequenceMode` so QML enum
64 // references work without touching the C++ scope. Integer values
65 // match for `static_cast` round-trip — matches decision O's "do
66 // not rename enum values" convention used by `PhosphorEasing::Type`.
71 Q_ENUM(SequenceMode)
72
73 PhosphorProfile() = default;
75 explicit PhosphorProfile(const Profile& value)
76 : m_value(value)
77 {
78 }
79
85 const Profile& value() const
86 {
87 return m_value;
88 }
89
90 // ─── Property delegates ───
91 //
92 // Setters mirror the validation in `Profile::fromJson`: NaN/inf
93 // and out-of-range values are silently rejected (the field stays
94 // unset, so `effective*()` substitutes the library default).
95 // QML scripts that pass garbage get the same fault-tolerant
96 // behaviour as a malformed profile JSON file rather than landing
97 // pathological values into a QQuickPropertyAnimation downstream.
98
100 {
101 return PhosphorCurve(m_value.curve);
102 }
103 void setCurve(const PhosphorCurve& c)
104 {
105 m_value.curve = c.curve();
106 }
107
108 qreal duration() const
109 {
110 return m_value.effectiveDuration();
111 }
112 void setDuration(qreal ms)
113 {
114 if (!std::isfinite(ms) || ms <= 0.0 || ms > Profile::MaxDurationMs) {
115 m_value.duration.reset();
116 return;
117 }
118 m_value.duration = ms;
119 }
120
121 int minDistance() const
122 {
123 return m_value.effectiveMinDistance();
124 }
125 void setMinDistance(int px)
126 {
127 if (px < 0) {
128 m_value.minDistance.reset();
129 return;
130 }
131 m_value.minDistance = px;
132 }
133
135 {
136 return static_cast<SequenceMode>(static_cast<int>(m_value.effectiveSequenceMode()));
137 }
139 {
140 m_value.sequenceMode = static_cast<PhosphorAnimation::SequenceMode>(static_cast<int>(mode));
141 }
142
143 int staggerInterval() const
144 {
145 return m_value.effectiveStaggerInterval();
146 }
148 {
149 if (ms < 0 || ms > Profile::MaxStaggerIntervalMs) {
150 m_value.staggerInterval.reset();
151 return;
152 }
153 m_value.staggerInterval = ms;
154 }
155
156 QString presetName() const
157 {
158 return m_value.presetName.value_or(QString());
159 }
160 void setPresetName(const QString& name)
161 {
162 m_value.presetName = name;
163 }
164
165 // ─── Serialization ───
166
170 Q_INVOKABLE QJsonObject toJson() const
171 {
172 return m_value.toJson();
173 }
174
188 Q_INVOKABLE static PhosphorProfile fromJson(const QJsonObject& obj)
189 {
190 static CurveRegistry sFallback;
191 CurveRegistry* registry = PhosphorCurve::defaultRegistry();
192 return PhosphorProfile(Profile::fromJson(obj, registry ? *registry : sFallback));
193 }
194
195 // ─── Equality ───
196
197 bool operator==(const PhosphorProfile& other) const
198 {
199 return m_value == other.m_value;
200 }
201 bool operator!=(const PhosphorProfile& other) const
202 {
203 return !(*this == other);
204 }
205
206private:
207 Profile m_value;
208};
209
210} // namespace PhosphorAnimation
211
212Q_DECLARE_METATYPE(PhosphorAnimation::PhosphorProfile)
String-id <-> curve factory registry.
Definition CurveRegistry.h:22
Opaque QML value-type wrapper around shared_ptr<const Curve>.
Definition PhosphorCurve.h:47
std::shared_ptr< const Curve > curve() const
The wrapped pointer. May be null on a default-constructed handle.
Definition PhosphorCurve.h:64
QML value-type wrapper around PhosphorAnimation::Profile.
Definition PhosphorProfile.h:50
void setCurve(const PhosphorCurve &c)
Definition PhosphorProfile.h:103
QString presetName() const
Definition PhosphorProfile.h:156
SequenceMode sequenceMode() const
Definition PhosphorProfile.h:134
QJsonObject toJson() const
Serialize to a JSON object via Profile::toJson.
Definition PhosphorProfile.h:170
void setPresetName(const QString &name)
Definition PhosphorProfile.h:160
void setDuration(qreal ms)
Definition PhosphorProfile.h:112
PhosphorCurve curve() const
Definition PhosphorProfile.h:99
SequenceMode
Definition PhosphorProfile.h:67
bool operator!=(const PhosphorProfile &other) const
Definition PhosphorProfile.h:201
void setMinDistance(int px)
Definition PhosphorProfile.h:125
static PhosphorProfile fromJson(const QJsonObject &obj)
Parse from a JSON object.
Definition PhosphorProfile.h:188
int minDistance() const
Definition PhosphorProfile.h:121
qreal duration() const
Definition PhosphorProfile.h:108
int staggerInterval() const
Definition PhosphorProfile.h:143
bool operator==(const PhosphorProfile &other) const
Definition PhosphorProfile.h:197
void setSequenceMode(SequenceMode mode)
Definition PhosphorProfile.h:138
void setStaggerInterval(int ms)
Definition PhosphorProfile.h:147
const Profile & value() const
Read-only access to the underlying value.
Definition PhosphorProfile.h:85
Configuration for a single animation event.
Definition Profile.h:33
Definition AnimatedValue.h:31
SequenceMode
How a batch of animations starts. Numeric values match the historical wire format.
Definition Profile.h:21