VPTissue Reference Manual
ProjectController.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "ProjectController.h"
21 
22 #include "AppController.h"
23 #include "common/PTreeQtState.h"
24 #include "gui/PTreeContainer.h"
26 #include "gui/PTreeMenu.h"
27 #include "util/misc/Exception.h"
28 
29 #include <QAction>
30 
31 using namespace std;
32 using namespace boost::property_tree;
33 using namespace SimPT_Sim::Util;
34 
35 namespace SimShell {
36 namespace Gui {
37 namespace Controller {
38 
39 ProjectController::ProjectController(AppController* m)
40  : QWidget(m),
41  HasUnsavedChangesPrompt("Project"),
42  m_main_controller(m),
43  m_running_controller(m)
44 {
45  InitActions();
46  InitWidgets();
47  InitBaseClasses();
48 }
49 
50 QAction* ProjectController::GetActionClose() const
51 {
52  return m_a_close_project;
53 }
54 
55 QMenu* ProjectController::GetExportMenu() const
56 {
57  return m_m_export;
58 }
59 
60 const std::string& ProjectController::GetOpenedFileName() const
61 {
62  return m_file_name;
63 }
64 
65 const std::string& ProjectController::GetOpenedProjectName() const
66 {
67  return m_project_name;
68 }
69 
70 QDockWidget* ProjectController::GetParametersDock() const
71 {
72  return m_container_parameters->GetDock();
73 }
74 
75 QMenu* ProjectController::GetParametersMenu() const
76 {
77  return m_container_parameters->GetMenu();
78 }
79 
80 QDockWidget* ProjectController::GetPreferencesDock() const
81 {
82  return m_container_preferences->GetDock();
83 }
84 
85 QMenu* ProjectController::GetPreferencesMenu() const
86 {
87  return m_container_preferences->GetMenu();
88 }
89 
90 ptree ProjectController::GetPTreeState() const
91 {
92  ptree state;
93  state.put_child("container_parameters", m_container_parameters->GetPTreeState());
94  state.put_child("container_preferences", m_container_preferences->GetPTreeState());
95  return state;
96 }
97 
98 QMenu* ProjectController::GetSimulationMenu() const
99 {
100  return m_m_simulation;
101 }
102 
103 ProjectController::Timings ProjectController::GetTimings() const
104 {
105  return m_timings;
106 }
107 
108 QMenu* ProjectController::GetViewersMenu() const
109 {
110  return m_m_viewers;
111 }
112 
113 void ProjectController::InitActions()
114 {
115  m_a_close_project = new QAction("&Close", this);
116  m_a_close_project->setShortcut(QKeySequence("Ctrl+W"));
117  m_a_close_project->setEnabled(false);
118  connect(m_a_close_project, SIGNAL(triggered()), this, SLOT(SLOT_Close()));
119  m_enabled_actions.Add(m_a_close_project);
120 }
121 
122 void ProjectController::InitBaseClasses()
123 {
124  SetChildren({
125  m_container_parameters.get(),
126  m_container_preferences.get()
127  });
128 }
129 
130 void ProjectController::InitWidgets()
131 {
132  m_container_parameters = make_shared<PTreeContainer>("Parameters");
133  connect(m_container_parameters.get(), SIGNAL(Applied(const boost::property_tree::ptree&)),
134  this, SLOT(SLOT_ApplyParameters(const boost::property_tree::ptree&)));
135 
136  m_container_preferences = make_shared<PTreeContainerPreferencesObserver>("Project Preferences");
137  m_container_preferences->SetOnlyEditData(true);
138  connect(m_container_preferences.get(), SIGNAL(Applied(const boost::property_tree::ptree&)),
139  this, SLOT(SLOT_ApplyPreferences(const boost::property_tree::ptree&)));
140 
141  m_m_export = new QMenu("&Export", this);
142  m_m_export->setEnabled(false);
143  m_enabled_actions.Add(m_m_export);
144 
145  m_m_viewers = new QMenu("Viewers", this);
146  m_m_viewers->setEnabled(false);
147  m_enabled_actions.Add(m_m_viewers);
148 
149  m_m_simulation = new QMenu("&Simulation", this);
150  m_m_simulation->addAction(m_running_controller.GetActionSingleStep());
151  m_m_simulation->addAction(m_running_controller.GetActionRun());
152  m_m_simulation->addSeparator();
153 }
154 
155 void ProjectController::InternalForceClose()
156 {
157  m_timings.Merge(m_project->Session().GetTimings());
158  m_enabled_actions.Disable();
159  m_running_controller.Disable();
160  m_project->Close();
161  m_project->Subject<Ws::Event::PreferencesChanged,
162  weak_ptr<const void>>::Unregister(m_container_preferences);
163 
164  // Clear menus
165  m_m_export->clear();
166  m_export_actions.reset();
167  m_viewer_actions.reset();
168  m_root_viewer.reset();
169 
170  // No need to Close() m_container_parameters and m_container_preferences,
171  // they are present in the list of children in HasUnsavedChanges base class.
172  m_project.reset();
173  m_file_name.clear();
174  m_project_name.clear();
175 }
176 
177 bool ProjectController::InternalIsClean() const
178 {
179  return true;
180 }
181 
182 void ProjectController::InternalPreForceClose()
183 {
184  m_project->SetUserData("project_controller", GetPTreeState());
185 }
186 
187 bool ProjectController::InternalSave()
188 {
189  return true;
190 }
191 
192 bool ProjectController::IsOpened() const
193 {
194  return (bool) m_project;
195 }
196 
197 bool ProjectController::IsRunning() const
198 {
199  return m_running_controller.IsRunning();
200 }
201 
202 bool ProjectController::Set(const string& project_name, const shared_ptr<Ws::IProject>& project)
203 {
204  if (!PromptClose())
205  return false;
206  try {
207  m_project = project;
208 
209  m_project_name = project_name;
210  m_file_name = project->GetSessionFileName();
211 
212  // Open ptrees in dock windows and menus.
213  m_container_parameters->Open(m_project->Session().GetParameters());
214  m_container_preferences->Open(m_project->GetPreferences());
215 
216  // Create actions for open project (Export, Simulation-specific, Viewers, ...)
217  m_root_viewer = m_project->Session().CreateRootViewer(m_main_controller);
218 
219  // Project actions
220  m_export_actions = make_shared<ProjectActions::ExportActions>(m_project,
221  m_project->Session().GetExporters(), m_main_controller);
222  m_viewer_actions = ProjectActions::ViewerActions::Create(m_root_viewer);
223 
224  // Copy actions into export menu
225  for (auto action : m_export_actions->GetActions())
226  m_m_export->addAction(action);
227 
228  m_m_viewers->clear();
229  // Copy actions into viewer menu
230  for (auto action : m_viewer_actions->GetMenu()->actions())
231  m_m_viewers->addAction(action);
232 
233  if (m_m_viewers->actions().size() == 0) {
234  auto action = m_m_viewers->addAction("No viewers");
235  action->setEnabled(false);
236  }
237 
238  // Register preferences container with PreferencesChanged events coming from workspace project.
239  auto handler = bind(&PTreeContainerPreferencesObserver::Update,
240  m_container_preferences.get(), std::placeholders::_1);
241  m_project->Subject<Ws::Event::PreferencesChanged,
242  weak_ptr<const void>>::Register(m_container_preferences, handler);
243 
244  // Enable RunningController's actions
245  m_running_controller.Enable(m_project);
246 
247  m_enabled_actions.Enable();
248 
249  SetPTreeState(m_project->GetUserData("project_controller"));
250 
251  return true;
252  }
253  catch (Exception&) {
254  return false;
255  }
256 }
257 
258 void ProjectController::SetPTreeState(const ptree& state)
259 {
260  auto container_parameters_state = state.get_child_optional("container_parameters");
261  if (container_parameters_state) {
262  m_container_parameters->SetPTreeState(container_parameters_state.get());
263  }
264 
265  auto container_preferences_state = state.get_child_optional("container_preferences");
266  if (container_preferences_state) {
267  m_container_preferences->SetPTreeState(container_preferences_state.get());
268  }
269 }
270 
271 void ProjectController::SetRunning(bool b)
272 {
273  m_running_controller.SetRunning(b);
274 }
275 
276 ProjectController::operator bool() const
277 {
278  return IsOpened();
279 }
280 
281 void ProjectController::SLOT_ApplyParameters(const ptree& p)
282 {
283  m_project->Session().SetParameters(p);
284 }
285 
286 void ProjectController::SLOT_ApplyPreferences(const ptree& p)
287 {
288  m_project->SetPreferences(p);
289 }
290 
291 void ProjectController::SLOT_Close()
292 {
293  PromptClose();
294 }
295 
296 } // namespace Controller
297 } // namespace Gui
298 } // namespace SimShell
Event used to inform some observer that preferences have changed.
STL namespace.
AppController header.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
Interface for PTreeQtState.
Interface for PTreeContainerPreferencesObserver.
Extremely simple Exception root class.
Definition: Exception.h:28
see the online Qt documentation
Interface for PTreeMenu.
see the online Qt documentation
ProjectController header.
see the online Qt documentation
Header file for Exception class.
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
Interface for PTreeContainer.