-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproject.cpp
119 lines (97 loc) · 2.79 KB
/
project.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
116
117
118
119
#include "project.h"
#include "timeline.h"
#include "timelineevent.h"
#include "util.h"
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QDir>
Project* Project::gCurrentProject = NULL;
void Project::open(const QString& path)
{
close();
gCurrentProject = new Project();
gCurrentProject->mSavePath = path;
gCurrentProject->fromJSON(Util::readJSON(path));
}
void Project::init(const QString& directory, const QString &name)
{
close();
bool ok = QDir().mkpath(directory);
Q_ASSERT(ok);
ok = QDir().mkpath(directory + "/scripts");
Q_ASSERT(ok);
ok = QDir().mkpath(directory + "/timelines");
Q_ASSERT(ok);
gCurrentProject = new Project();
gCurrentProject->mName = name;
gCurrentProject->mSavePath = directory + "/" + name + ".bgp";
gCurrentProject->save();
}
Project* Project::get()
{
return gCurrentProject;
}
void Project::close()
{
if (gCurrentProject) {
delete gCurrentProject;
gCurrentProject = NULL;
}
}
Project::Project()
{
auto placeholderType = std::make_shared<EventType>("placeholder");
placeholderType->properties["note"] = EventProperty(QMetaType::Int, QVariant(0));
mEventTypes.push_back(placeholderType);
}
QString Project::root() const
{
return mSavePath.path();
}
void Project::save() const
{
Util::writeJSON(toJSON(), mSavePath.filePath());
}
QJsonObject Project::toJSON() const
{
QJsonArray eventTypes;
for (auto it = mEventTypes.begin(); it != mEventTypes.end(); it++) {
eventTypes.push_back(it->get()->toJSON());
}
QJsonObject project;
project["name"] = mName;
project["lastOpenedTimeline"] = QDir(root()).relativeFilePath(mLastOpenedTimeline);
project["eventTypes"] = eventTypes;
return project;
}
void Project::fromJSON(const QJsonObject& project)
{
mEventTypes.clear();
mName = project["name"].toString();
if (!project["lastOpenedTimeline"].toString().isEmpty())
mLastOpenedTimeline = QDir(root()).filePath(project["lastOpenedTimeline"].toString());
const QJsonArray& eventTypes = project["eventTypes"].toArray();
for (auto it = eventTypes.begin(); it != eventTypes.end(); it++) {
auto eventType = std::make_shared<EventType>();
eventType->fromJSON(it->toObject());
mEventTypes.push_back(eventType);
}
}
void Project::setEventTypes(const EventTypeVector& eventTypes)
{
mEventTypes = eventTypes;
save();
}
void Project::setLastOpenedTimeline(const QString &timelinePath)
{
mLastOpenedTimeline = timelinePath;
save();
}
std::shared_ptr<EventType> Project::eventType(const QString &name)
{
return *std::find_if(mEventTypes.begin(), mEventTypes.end(),
[&](const std::shared_ptr<EventType>& et) -> bool {
return et->name.c_str() == name;
});
}