-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProject.cpp
More file actions
218 lines (167 loc) · 7.01 KB
/
Copy pathProject.cpp
File metadata and controls
218 lines (167 loc) · 7.01 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// SPDX-License-Identifier: LGPL-3.0-or-later
// A corresponding LICENSE file is located in the root directory of this source tree
// Copyright (C) 2023 BioVault (Biomedical Visual Analytics Unit LUMC - TU Delft)
#include "Project.h"
#include "AbstractProjectManager.h"
#include "CoreInterface.h"
#include "Set.h"
#include "util/Serialization.h"
#include <stdlib.h>
using namespace mv::gui;
using namespace mv::util;
namespace mv {
Project::Project(QObject* parent /*= nullptr*/) :
QObject(parent),
Serializable("Project"),
_filePath(),
_startupProject(false),
_applicationVersion(Application::current()->getVersion()),
_projectMetaAction(this)
{
initialize();
}
Project::Project(const QString& filePath, QObject* parent /*= nullptr*/) :
Project(parent)
{
setFilePath(filePath);
initialize();
try {
if (!QFileInfo(_filePath).exists())
throw std::runtime_error("File does not exist");
QFile projectJsonFile(_filePath);
if (!projectJsonFile.open(QIODevice::ReadOnly))
throw std::runtime_error("Unable to open file for reading");
QByteArray projectByteArray = projectJsonFile.readAll();
QJsonDocument jsonDocument;
jsonDocument = QJsonDocument::fromJson(projectByteArray);
if (jsonDocument.isNull() || jsonDocument.isEmpty())
throw std::runtime_error("JSON document is invalid");
fromVariantMap(jsonDocument.toVariant().toMap()["Project"].toMap());
}
catch (std::exception& e)
{
qDebug() << "Unable to load project from file:" << e.what();
}
catch (...)
{
qDebug() << "Unable to load project from file";
}
}
QString Project::getFilePath() const
{
return _filePath;
}
void Project::setFilePath(const QString& filePath)
{
_filePath = filePath;
_startupProject = filePath == Application::current()->getStartupProjectFilePath();
emit filePathChanged(_filePath);
}
bool Project::isStartupProject() const
{
return _startupProject;
}
void Project::fromVariantMap(const mv::VariantMap& variantMap)
{
Serializable::fromVariantMap(variantMap);
projects().getProjectSerializationTask().setName("Load project");
_projectMetaAction.getApplicationVersionAction().fromParentVariantMap(variantMap);
_projectMetaAction.getProjectVersionAction().fromParentVariantMap(variantMap);
_projectMetaAction.getReadOnlyAction().fromParentVariantMap(variantMap);
_projectMetaAction.getTitleAction().fromParentVariantMap(variantMap);
_projectMetaAction.getDescriptionAction().fromParentVariantMap(variantMap);
_projectMetaAction.getTagsAction().fromParentVariantMap(variantMap);
_projectMetaAction.getCommentsAction().fromParentVariantMap(variantMap);
_projectMetaAction.getContributorsAction().fromParentVariantMap(variantMap);
_projectMetaAction.getCompressionAction().fromParentVariantMap(variantMap);
_projectMetaAction.getSplashScreenAction().fromParentVariantMap(variantMap);
_projectMetaAction.getStudioModeAction().fromParentVariantMap(variantMap);
dataHierarchy().fromParentVariantMap(variantMap);
actions().fromParentVariantMap(variantMap);
plugins().fromParentVariantMap(variantMap);
}
mv::VariantMap Project::toVariantMap() const
{
projects().getProjectSerializationTask().setName("Save project");
auto variantMap = Serializable::toVariantMap();
_projectMetaAction.getApplicationVersionAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getProjectVersionAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getReadOnlyAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getTitleAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getDescriptionAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getTagsAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getCommentsAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getContributorsAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getCompressionAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getSplashScreenAction().insertIntoVariantMap(variantMap);
_projectMetaAction.getStudioModeAction().insertIntoVariantMap(variantMap);
plugins().insertIntoVariantMap(variantMap);
dataHierarchy().insertIntoVariantMap(variantMap);
actions().insertIntoVariantMap(variantMap);
return variantMap;
}
util::Version Project::getVersion() const
{
return _applicationVersion;
}
void Project::updateContributors()
{
QString currentUserName;
#ifdef __APPLE__
currentUserName = qgetenv("USER");
#else
currentUserName = qgetenv("USERNAME");
#endif
if (!currentUserName.isEmpty() && !_projectMetaAction.getContributorsAction().getStrings().contains(currentUserName))
_projectMetaAction.getContributorsAction().addString(currentUserName);
}
void Project::setStudioMode(bool studioMode)
{
auto plugins = mv::plugins().getPluginsByTypes(); // by default gets all plugin types
auto datasets = mv::data().getAllDatasets();
if (studioMode) {
for (auto plugin : plugins)
plugin->cacheConnectionPermissions(true);
for (auto plugin : plugins)
plugin->setConnectionPermissionsToAll(true);
for (auto& dataset : datasets)
{
for (auto& action : dataset.get()->getActions())
action->cacheConnectionPermissions(true);
for (auto& action : dataset.get()->getActions())
action->setConnectionPermissionsToAll(true);
}
}
else {
for (auto plugin : plugins)
plugin->restoreConnectionPermissions(true);
for (auto& dataset : datasets)
for (auto& action : dataset.get()->getActions())
action->restoreConnectionPermissions(true);
}
}
ProjectMetaAction& Project::getProjectMetaAction()
{
return _projectMetaAction;
}
QSharedPointer<ProjectMetaAction> Project::getProjectMetaActionFromProjectFilePath(const QString& projectFilePath)
{
const auto projectMetaJsonFilePath = projects().extractFileFromManiVaultProject(projectFilePath, Application::current()->getTemporaryDir(), "meta.json");
if (projectMetaJsonFilePath.isEmpty())
return {};
return QSharedPointer<ProjectMetaAction>::create(projectMetaJsonFilePath);
}
void Project::initialize()
{
getProjectMetaAction().getSplashScreenAction().setMayCloseSplashScreenWidget(true);
updateContributors();
getStudioModeAction().setIcon(Application::getIconFont("FontAwesome").getIcon("pencil-ruler"));
connect(&getStudioModeAction(), &ToggleAction::toggled, this, &Project::setStudioMode);
const auto updateStudioModeActionReadOnly = [&]() -> void {
getStudioModeAction().setEnabled(projects().hasProject());
};
updateStudioModeActionReadOnly();
connect(&projects(), &AbstractProjectManager::projectCreated, this, updateStudioModeActionReadOnly);
connect(&projects(), &AbstractProjectManager::projectDestroyed, this, updateStudioModeActionReadOnly);
}
}