-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtimelineevent.cpp
115 lines (94 loc) · 3.79 KB
/
timelineevent.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "timelineevent.h"
#include <QDebug>
#include <QJsonArray>
#include "project.h"
QJsonObject EventType::toJSON() const
{
QJsonArray props;
for (auto it = properties.begin(); it != properties.end(); it++) {
QJsonObject prop;
prop["name"] = it.key();
prop["type"] = QMetaType::typeName(it->type);
prop["defaultValue"] = it->defaultValue.toString();
props.push_back(prop);
}
QJsonObject type;
type["name"] = name.c_str();
type["properties"] = props;
return type;
}
void EventType::fromJSON(const QJsonObject& type)
{
name = type["name"].toString().toUtf8().constData();
const QJsonArray& props = type["properties"].toArray();
for (auto it = props.begin(); it != props.end(); it++) {
const QJsonObject& prop = it->toObject();
QMetaType::Type type = (QMetaType::Type)QMetaType::type(prop["type"].toString().toUtf8().constData());
properties[prop["name"].toString()] =
EventProperty(type, prop["defaultValue"].toString());
}
}
TimelineEvent::TimelineEvent(const std::shared_ptr<EventType>& type, double time)
: mTime(time), mType(type)
{
// initialize properties to defaults
if (mType) {
const auto& props = mType->properties;
for (auto it = props.begin(); it != props.end(); it++) {
mProperties[it.key()] = it->defaultValue;
// make sure the default value is the right type
bool conv = mProperties[it.key()].convert(it->type);
// Q_ASSERT(conv);
}
}
}
QJsonObject TimelineEvent::toJSON() const
{
QJsonObject event;
event["type"] = typeName();
event["time"] = time();
// write properties
for (auto it = mProperties.begin(); it != mProperties.end(); it++) {
event[it.key()] = it->toString();
}
return event;
}
std::shared_ptr<TimelineEvent> TimelineEvent::createFromJSON(const QJsonObject& event)
{
const QString typeName = event["type"].toString();
const double time = event["time"].toDouble();
std::shared_ptr<EventType> type = Project::get()->eventType(typeName);
std::shared_ptr<TimelineEvent> ev = std::make_shared<TimelineEvent>(type, time);
// read properties
for (auto it = ev->mProperties.begin(); it != ev->mProperties.end(); it++) {
*it = event[it.key()].toString();
bool conv = it->convert(type->properties.find(it.key())->type);
Q_ASSERT(conv);
}
return ev;
}
void TimelineEvent::setProperty(const QString &name, const QVariant &value) {
// Do we have a property by this name?
auto it = mProperties.find(name);
if (it == mProperties.end())
throw EventException() << "Property \"" << name <<
"\" does not exist for " << mType->name <<
" events!";
// Can whatever we were given be converted to the type we require?
const auto propType = mType->properties.find(name)->type;
if (!value.canConvert(propType))
throw EventException() << "Cannot set property \"" << name <<
" to type " << value.typeName() << "!";
// Set it, but keep it as whatever type it was given.
it->setValue(value);
// Force a conversion to the type we require.
// This can fail even if canConvert() returns true - for example,
// imagine converting a string to a date. That conversion is possible,
// but if the string is "loldicks," we can't convert it to a date.
// So, we do this to make sure it's a well-formed version of the type we require.
if (!it->convert(propType)) {
*it = mType->properties.find(name)->defaultValue;
throw EventException() << "Invalid data for property \"" << name <<
"\" to convert to " << QMetaType::typeName(propType);
}
}