Mantid
Loading...
Searching...
No Matches
ProjectSaveModel.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
13
14#include <sstream>
15#include <unordered_set>
16
17using namespace Mantid::API;
18using namespace MantidQt::API;
19using namespace MantidQt::MantidWidgets;
20
26ProjectSaveModel::ProjectSaveModel(const std::vector<IProjectSerialisable *> &windows,
27 std::vector<std::string> activePythonInterfaces)
28 : m_activePythonInterfaces(std::move(activePythonInterfaces)) {
29 auto workspaces = getWorkspaces();
30 for (auto &ws : workspaces) {
31 std::pair<std::string, std::vector<IProjectSerialisable *>> item(ws->getName(),
32 std::vector<IProjectSerialisable *>());
33 m_workspaceWindows.insert(item);
34 }
35
36 for (auto window : windows) {
37 auto wsNames = window->getWorkspaceNames();
38 // if the window is not associated with any workspaces
39 // then track it so we can always add it to the included
40 // window list
41 if (wsNames.size() == 0) {
42 m_unattachedWindows.emplace_back(window);
43 continue;
44 }
45
46 // otherwise add a reference mapping the window to the
47 // it's various connected workspaces
48 for (auto &name : wsNames) {
49 m_workspaceWindows[name].emplace_back(window);
50 }
51 }
52}
53
59std::vector<IProjectSerialisable *> ProjectSaveModel::getWindows(const std::string &wsName) const {
60 if (hasWindows(wsName)) {
61 return m_workspaceWindows.at(wsName);
62 }
63
64 return std::vector<IProjectSerialisable *>();
65}
66
72std::vector<IProjectSerialisable *> ProjectSaveModel::getUniqueWindows(const std::vector<std::string> &wsNames) const {
73 std::unordered_set<IProjectSerialisable *> uniqueWindows;
74
75 for (auto &name : wsNames) {
76 for (auto window : getWindows(name)) {
77 uniqueWindows.insert(window);
78 }
79 }
80
81 std::vector<IProjectSerialisable *> windows(uniqueWindows.cbegin(), uniqueWindows.cend());
82 std::sort(windows.begin(), windows.end(), [](IProjectSerialisable *lhs, IProjectSerialisable *rhs) {
83 return lhs->getWindowName() < rhs->getWindowName();
84 });
85
86 return windows;
87}
88
96std::vector<std::string> ProjectSaveModel::getWindowNames(const std::vector<std::string> &wsNames) const {
97 std::vector<std::string> names;
98 auto windows = getUniqueWindows(wsNames);
99 names.reserve(windows.size());
100 for (auto window : windows) {
101 names.emplace_back(window->getWindowName());
102 }
103 return names;
104}
105
110std::vector<std::string> ProjectSaveModel::getWorkspaceNames() const {
111 std::vector<std::string> names;
112 names.reserve(m_workspaceWindows.size());
113 for (auto &item : m_workspaceWindows) {
114 names.emplace_back(item.first);
115 }
116
117 std::sort(names.begin(), names.end());
118 return names;
119}
120
125std::vector<std::string> ProjectSaveModel::getAllPythonInterfaces() const { return m_activePythonInterfaces; }
126
134std::vector<WindowInfo> ProjectSaveModel::getWindowInformation(const std::vector<std::string> &wsNames,
135 bool includeUnattached) const {
136 std::vector<WindowInfo> winInfo;
137
138 for (auto window : getUniqueWindows(wsNames)) {
139 auto info = makeWindowInfoObject(window);
140 winInfo.emplace_back(info);
141 }
142
143 if (includeUnattached) {
144 for (const auto window : m_unattachedWindows) {
145 auto info = makeWindowInfoObject(window);
146 winInfo.emplace_back(info);
147 }
148 }
149
150 return winInfo;
151}
152
154 WindowIcons icons;
155 WindowInfo info;
156 info.name = window->getWindowName();
157 info.type = window->getWindowType();
158 info.icon_id = icons.getIconID(window->getWindowType());
159 return info;
160}
161
166std::vector<WorkspaceInfo> ProjectSaveModel::getWorkspaceInformation() const {
167 std::vector<WorkspaceInfo> wsInfo;
168
169 auto items = AnalysisDataService::Instance().topLevelItems();
170 for (auto item : items) {
171 auto ws = item.second;
172 auto info = makeWorkspaceInfoObject(ws);
173
174 if (ws->id() == "WorkspaceGroup") {
175 auto group = std::dynamic_pointer_cast<WorkspaceGroup>(ws);
176 for (int i = 0; i < group->getNumberOfEntries(); ++i) {
177 auto subInfo = makeWorkspaceInfoObject(group->getItem(i));
178 info.subWorkspaces.emplace_back(subInfo);
179 }
180 }
181
182 wsInfo.emplace_back(info);
183 }
184
185 return wsInfo;
186}
187
192std::vector<Workspace_sptr> ProjectSaveModel::getWorkspaces() const {
193 auto &ads = AnalysisDataService::Instance();
194 return ads.getObjects();
195}
196
198 WorkspaceIcons icons;
199 WorkspaceInfo info;
200 info.name = ws->getName();
201 info.numWindows = getWindows(ws->getName()).size();
202 info.size = ws->getMemorySizeAsStr();
203 info.icon_id = icons.getIconID(ws->id());
204 info.type = ws->id();
205 return info;
206}
207
213bool ProjectSaveModel::hasWindows(const std::string &wsName) const {
214 auto item = m_workspaceWindows.find(wsName);
215 if (item != m_workspaceWindows.end()) {
216 return item->second.size() > 0;
217 }
218
219 return false;
220}
221
230bool ProjectSaveModel::needsSizeWarning(const std::vector<std::string> &wsNames) {
231 std::istringstream iss(Mantid::Kernel::ConfigService::Instance().getString("projectSaving.warningSize"));
232 size_t warningSize;
233 iss >> warningSize;
234 const size_t totalSize = getProjectSize(wsNames);
235 return totalSize > warningSize;
236}
237
244size_t ProjectSaveModel::getProjectSize(const std::vector<std::string> &wsNames) {
245 size_t totalSize = 0;
246 for (const auto &ws : wsNames) {
247 totalSize += Mantid::API::AnalysisDataService::Instance().retrieve(ws)->getMemorySize();
248 }
249 return totalSize;
250}
const std::vector< double > & rhs
Defines an interface to a MantidPlot class that can be saved into or loaded from a project.
virtual std::string getWindowType()=0
Returns the type of the window.
virtual std::string getWindowName()=0
Returns the user friendly name of the window.
Defines a mapping between a workspace ID and a pixmap to use for an icon.
Definition: WindowIcons.h:19
std::string getIconID(const std::string &windowID) const
Returns an icon ID for the given window ID.
Definition: WindowIcons.cpp:42
Defines a mapping between a workspace ID and a pixmap to use for an icon.
std::string getIconID(const std::string &workspaceID) const
Returns an icon ID for the given workspace ID.
std::vector< std::string > getWorkspaceNames() const
Get all workspace names.
WindowInfo makeWindowInfoObject(MantidQt::API::IProjectSerialisable *window) const
std::vector< WorkspaceInfo > getWorkspaceInformation() const
Get all workspace information.
WorkspaceInfo makeWorkspaceInfoObject(const Mantid::API::Workspace_const_sptr &ws) const
Create a workspace info object for this workspace.
std::vector< WindowInfo > getWindowInformation(const std::vector< std::string > &wsNames, bool includeUnattached=false) const
Get all window information for a collection of workspaces.
bool hasWindows(const std::string &ws) const
Check if a workspace has any windows attached to it.
std::vector< std::string > getAllPythonInterfaces() const
Return the list of python interfaces that can be saved.
bool needsSizeWarning(const std::vector< std::string > &wsNames)
Check if the size of the project is > than the warning size.
std::unordered_map< std::string, std::vector< MantidQt::API::IProjectSerialisable * > > m_workspaceWindows
Map to hold which windows are associated with a workspace.
ProjectSaveModel(const std::vector< MantidQt::API::IProjectSerialisable * > &windows, std::vector< std::string > activePythonInterfaces=std::vector< std::string >())
Construct a new model instance with vector of window handles.
std::vector< MantidQt::API::IProjectSerialisable * > getUniqueWindows(const std::vector< std::string > &wsNames) const
Get all window handles for a collection of workspace names.
std::vector< std::string > m_activePythonInterfaces
std::vector< Mantid::API::Workspace_sptr > getWorkspaces() const
Get all workspaces from the ADS.
std::vector< std::string > getWindowNames(const std::vector< std::string > &wsNames) const
Get all window names for a collection of workspace names.
std::vector< MantidQt::API::IProjectSerialisable * > getWindows(const std::string &wsName) const
Get all window handles for this workspace.
virtual size_t getProjectSize(const std::vector< std::string > &wsNames)
Find the size of a project from a list of workspace names.
std::vector< MantidQt::API::IProjectSerialisable * > m_unattachedWindows
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< const Workspace > Workspace_const_sptr
shared pointer to Mantid::API::Workspace (const version)
Definition: Workspace_fwd.h:22
STL namespace.