-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtimelineevent.h
78 lines (60 loc) · 2.2 KB
/
timelineevent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef TIMELINEEVENT_H
#define TIMELINEEVENT_H
#include <QMetaType>
#include <QVariant>
#include <QJsonObject>
#include <memory>
#include "bugelexception.h"
struct EventProperty
{
QMetaType::Type type;
QVariant defaultValue;
EventProperty() : type(QMetaType::UnknownType) {}
EventProperty(QMetaType::Type t, const QVariant& def) : type(t), defaultValue(def) {}
};
struct EventType
{
// This is std::string because ScriptableEvent::type()
// returns a const char*, and QString.toUtf8().constData() will go
// out of scope immediately after returning - but std::string.c_str() doesn't.
// (Although, it's probably not the best idea to rely on that either.)
std::string name;
QMap<QString, EventProperty> properties;
EventType() {}
EventType(const std::string& n, QMap<QString, EventProperty> props = QMap<QString, EventProperty>())
: name(n), properties(props) {}
QJsonObject toJSON() const;
void fromJSON(const QJsonObject& type);
};
class TimelineEvent
{
public:
TimelineEvent(const std::shared_ptr<EventType>& type, double time);
virtual ~TimelineEvent() {}
// sort by time
inline bool operator<(const TimelineEvent& rhs) {
return time() < rhs.time();
}
inline double time() const { return mTime; }
// properties
void setProperty(const QString& name, const QVariant& value);
template<typename T>
T getProperty(const QString& name) {
auto it = mProperties.find(name);
if (it == mProperties.end())
throw EventException() << "Property " << name << " does not exist for event type " << mType->name;
if (!it->canConvert<T>())
throw EventException() << "Cannot convert property " << name << " of " << mType->name;
return it->value<T>();
}
inline const QMap<QString, QVariant>& properties() const { return mProperties; }
inline const char* typeName() const { return mType->name.c_str(); }
// saving/loading
QJsonObject toJSON() const;
static std::shared_ptr<TimelineEvent> createFromJSON(const QJsonObject& ev);
private:
double mTime;
std::shared_ptr<EventType> mType;
QMap<QString, QVariant> mProperties;
};
#endif // TIMELINEEVENT_H