-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathProjectReader.cpp
More file actions
189 lines (162 loc) · 5.53 KB
/
Copy pathProjectReader.cpp
File metadata and controls
189 lines (162 loc) · 5.53 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* ProjectReader.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ProjectReader.h"
#include <iostream>
namespace mmp {
ProjectReader::ProjectReader(MainWindow *window) : _window(window)
{
}
bool ProjectReader::isValidVersion(const QString& versionString)
{
QRegularExpression re(MM::SUPPORTED_FILE_VERSIONS);
QRegularExpressionMatch match = re.match(versionString);
return match.hasMatch();
}
bool ProjectReader::readFile(QIODevice *device)
{
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(device->readAll(), &parseError);
if (doc.isNull()) {
_errorString = QString("Parse error at offset %1: %2")
.arg(parseError.offset).arg(parseError.errorString());
return false;
}
QJsonObject root = doc.object();
QString projectVersion = root["version"].toString();
if (projectVersion.isEmpty()) {
_errorString = QObject::tr("The contents of this file does not look like a MapMap project.");
return false;
} else if (!isValidVersion(projectVersion)) {
_errorString = QObject::tr("The version of MapMap %1 used to save this file is not readable by this MapMap version %2.")
.arg(projectVersion, MM::VERSION);
return false;
}
parseProject(root);
return _errorString.isEmpty();
}
QString ProjectReader::errorString() const
{
return _errorString;
}
void ProjectReader::parseProject(const QJsonObject& project)
{
MappingManager& manager = _window->getMappingManager();
manager.clearAll();
QJsonArray sources = project[ProjectLabels::SOURCES].toArray();
QJsonArray layers = project[ProjectLabels::LAYERS].toArray();
// Parse sources (formerly sources).
for (const auto& val : sources)
{
Source::ptr source = parseSource(val.toObject());
if (source.isNull())
{
qDebug() << "Problem creating source." << Qt::endl;
}
else
{
manager.addSource(source);
_window->addSourceItem(source->getId(), source->getIcon(), source->getName());
// Locate media file if not found
if (source->getSourceType() == Source::SourceType::Video)
{
QSharedPointer<Video> media = qSharedPointerCast<Video>(source);
Q_CHECK_PTR(media);
if (!_window->fileExists(media->getUri()))
media->setUri(_window->locateMediaFile(media->getUri(), false));
}
if (source->getSourceType() == Source::SourceType::Image)
{
QSharedPointer<Image> image = qSharedPointerCast<Image>(source);
Q_CHECK_PTR(image);
if (!_window->fileExists(image->getUri()))
image->setUri(_window->locateMediaFile(image->getUri(), true));
}
}
}
// Parse layers (formerly mappings).
QVector<Layer::ptr> allLayers;
for (const auto& val : layers)
{
Layer::ptr layer = parseLayer(val.toObject());
if (layer.isNull())
{
qDebug() << "Problem creating layer." << Qt::endl;
}
else
{
allLayers.push_back(layer);
}
}
// Add all mappings in reverse order.
for (QVector<Layer::ptr>::const_reverse_iterator it = allLayers.rbegin();
it != allLayers.rend(); ++it)
{
manager.addLayer(*it);
_window->addLayerItem((*it)->getId());
}
}
Source::ptr ProjectReader::parseSource(const QJsonObject& obj)
{
QString className = Serializable::classNameCleanToReal(obj[ProjectLabels::CLASS_NAME].toString());
int id = obj[ProjectLabels::ID].toInt(NULL_UID);
qDebug() << "Found source with classname: " << className << Qt::endl;
const QMetaObject* metaObject = MetaObjectRegistry::instance().getMetaObject(className);
if (metaObject)
{
Source::ptr source(qobject_cast<Source*>(metaObject->newInstance(Q_ARG(int, id))));
if (source.isNull())
{
qDebug() << QObject::tr("Problem at creation of source.") << Qt::endl;
}
else
qDebug() << "Created new instance with id: " << source->getId();
source->read(obj);
return source;
}
else
{
// Unknown source type (e.g. a macOS-only Syphon source opened on another
// platform). Skip it with a warning rather than failing the whole load.
qWarning() << "Skipping unsupported source of type" << className;
return Source::ptr();
}
}
Layer::ptr ProjectReader::parseLayer(const QJsonObject& obj)
{
QString className = Serializable::classNameCleanToReal(obj[ProjectLabels::CLASS_NAME].toString());
int id = obj[ProjectLabels::ID].toInt(NULL_UID);
const QMetaObject* metaObject = MetaObjectRegistry::instance().getMetaObject(className);
if (metaObject)
{
Layer::ptr layer(qobject_cast<Layer*>(metaObject->newInstance(Q_ARG(int, id))));
if (layer.isNull())
{
qDebug() << QObject::tr("Problem at creation of layer.") << Qt::endl;
}
layer->read(obj);
return layer;
}
else
{
_errorString = QObject::tr("Unable to create layer of type '%1'.").arg(className);
return Layer::ptr();
}
}
}