VPTissue Reference Manual
AppController.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 "AppController.h"
21 
22 #include "common/PTreeQtState.h"
23 #include "gui/WorkspaceView.h"
24 #include "gui/WorkspaceWizard.h"
25 
26 #include <QCloseEvent>
27 #include <QDockWidget>
28 #include <QMenuBar>
29 #include <QMessageBox>
30 #include <iostream>
31 #include <string>
32 
33 using namespace std;
34 using namespace boost::property_tree;
35 
36 namespace SimShell {
37 namespace Gui {
38 namespace Controller {
39 
40 AppController::AppController(const shared_ptr<Gui::IFactory>& f, QWidget* p)
41  : QMainWindow(p),
42  m_factory(f),
43  m_settings(QString::fromStdString(f->GetOrganizationName()), QString::fromStdString(f->GetApplicationName())),
44  m_workspace_controller(f->CreateWorkspaceFactory(), this)
45 {
46  InitWidgets();
47  InitMenu();
48  InitWorkspace();
49  InitProject();
50 }
51 
52 AppController::~AppController()
53 {
54 }
55 
56 void AppController::closeEvent(QCloseEvent* event)
57 {
58  if (m_workspace_controller) {
59  m_workspace_controller->SetUserData("recently_opened.project",
60  ptree(m_workspace_controller.Project().GetOpenedProjectName()));
61  m_workspace_controller->SetUserData("recently_opened.file",
62  ptree(m_workspace_controller.Project().GetOpenedFileName()));
63  m_settings.setValue("workspace", QString::fromStdString(m_workspace_controller->GetPath()));
64  } else {
65  m_settings.remove("workspace");
66  }
67 
68  if (m_workspace_controller.PromptClose()) {
69  event->accept();
70  } else {
71  event->ignore();
72  }
73 }
74 
75 ptree AppController::GetPTreeState() const
76 {
77  ptree state;
78  state.put_child("workspace_controller", m_workspace_controller.GetPTreeState());
79  state.put_child("workspace_preferences_dock", PTreeQtState::GetDockWidgetArea(this, m_workspace_controller.GetPreferencesDock()));
80  state.put_child("project_parameters_dock", PTreeQtState::GetDockWidgetArea(this, m_workspace_controller.Project().GetParametersDock()));
81  state.put_child("project_preferences_dock", PTreeQtState::GetDockWidgetArea(this, m_workspace_controller.Project().GetPreferencesDock()));
82  state.put_child("widget", PTreeQtState::GetWidgetState(this));
83  return state;
84 }
85 
86 AppController::Timings AppController::GetTimings() const
87 {
88  return m_workspace_controller.Project().GetTimings();
89 }
90 
91 void AppController::InitMenu()
92 {
93  auto menu = menuBar();
94 
95  // File menu
96  auto menu_file = menu->addMenu("&File");
97  menu_file->addAction(m_workspace_controller.GetActionNewProject());
98  menu_file->addSeparator();
99  menu_file->addAction(m_workspace_controller.GetView()->GetOpenAction());
100  menu_file->addAction(m_workspace_controller.GetView()->GetRenameAction());
101  menu_file->addAction(m_workspace_controller.GetView()->GetRemoveAction());
102  menu_file->addSeparator();
103  menu_file->addMenu(m_workspace_controller.Project().GetExportMenu());
104  menu_file->addSeparator();
105  menu_file->addAction(m_workspace_controller.GetActionRefreshWorkspace());
106  menu_file->addAction("Switch Workspace...", this, SLOT(SLOT_WorkspaceWizard()));
107  menu_file->addSeparator();
108  menu_file->addAction(m_workspace_controller.Project().GetActionClose());
109  auto action_exit = menu_file->addAction("E&xit", this, SLOT(close()));
110  action_exit->setShortcut(QKeySequence("Ctrl+Q"));
111 
112  // Edit menu
113  auto menu_edit = menu->addMenu("&Edit");
114  menu_edit->addMenu(m_workspace_controller.Project().GetParametersMenu());
115  menu_edit->addSeparator();
116  menu_edit->addMenu(m_workspace_controller.Project().GetPreferencesMenu());
117  menu_edit->addMenu(m_workspace_controller.GetPreferencesMenu());
118 
119  // Viewers menu
120  menu->addMenu(m_workspace_controller.Project().GetViewersMenu());
121 
122  // Simulation menu
123  menu->addMenu(m_workspace_controller.Project().GetSimulationMenu());
124 
125  // Help menu
126  auto menu_help = menu->addMenu("&Help");
127  menu_help->addAction("About", this, SLOT(SLOT_AboutDialog()));
128 }
129 
130 void AppController::InitProject()
131 {
132  if (m_workspace_controller) {
133  auto p = m_workspace_controller->Find(
134  m_workspace_controller->GetUserData("recently_opened.project").get_value<string>());
135  if (p != m_workspace_controller->end()) {
136  auto project = p->second.Project();
137  auto f = project->Find(
138  m_workspace_controller->GetUserData("recently_opened.file").get_value<string>());
139  if (f != project->end()) {
140  project->Open(f);
141  m_workspace_controller.Project().Set(p->first, project);
142  }
143  }
144  }
145 }
146 
147 void AppController::InitWidgets()
148 {
149  setWindowTitle(QString::fromStdString(m_factory->GetApplicationName()));
150  setCentralWidget(m_workspace_controller.GetView());
151 
152  m_log_dock = new LogWindow(this);
153  addDockWidget(Qt::BottomDockWidgetArea, m_log_dock);
154 
155  auto workspace_preferences_dock = m_workspace_controller.GetPreferencesDock();
156  addDockWidget(Qt::RightDockWidgetArea, workspace_preferences_dock);
157  workspace_preferences_dock->hide();
158 
159  auto project_preferences_dock = m_workspace_controller.Project().GetPreferencesDock();
160  addDockWidget(Qt::RightDockWidgetArea, project_preferences_dock);
161  project_preferences_dock->hide();
162 
163  auto project_parameters_dock = m_workspace_controller.Project().GetParametersDock();
164  addDockWidget(Qt::RightDockWidgetArea, project_parameters_dock);
165 }
166 
167 void AppController::InitWorkspace()
168 {
169  const string workspace = m_settings.value("workspace").toString().toStdString();
170  m_workspace_controller.Open(workspace);
171  SetWorkspaceWindowTitle(workspace);
172 
173  if (!m_workspace_controller) {
174  SLOT_WorkspaceWizard();
175  }
176 }
177 
178 void AppController::Log(const string& s)
179 {
180  m_log_dock->AddLine(s);
181 }
182 
183 void AppController::SetPTreeState(const ptree& state)
184 {
185  auto widget_state = state.get_child_optional("widget");
186  if (widget_state) {
187  PTreeQtState::SetWidgetState(this, widget_state.get());
188  }
189 
190  auto workspace_preferences_dock_state = state.get_child_optional("workspace_preferences_dock");
191  if (workspace_preferences_dock_state) {
192  PTreeQtState::SetDockWidgetArea(this,
193  m_workspace_controller.GetPreferencesDock(), workspace_preferences_dock_state.get());
194  }
195 
196  auto project_parameters_dock_state = state.get_child_optional("project_parameters_dock");
197  if (project_parameters_dock_state) {
198  PTreeQtState::SetDockWidgetArea(this,
199  m_workspace_controller.Project().GetParametersDock(), project_parameters_dock_state.get());
200  }
201 
202  auto project_preferences_dock_state = state.get_child_optional("project_preferences_dock");
203  if (project_preferences_dock_state) {
204  PTreeQtState::SetDockWidgetArea(this,
205  m_workspace_controller.Project().GetPreferencesDock(), project_preferences_dock_state.get());
206  }
207 
208  auto workspace_controller_state = state.get_child_optional("workspace_controller");
209  if (workspace_controller_state) {
210  m_workspace_controller.SetPTreeState(workspace_controller_state.get());
211  }
212 }
213 
214 void AppController::SetWorkspaceWindowTitle(const string& d)
215 {
216  setWindowTitle(QString::fromStdString(m_factory->GetApplicationName() + ": " + d));
217 }
218 
219 void AppController::SLOT_AboutDialog()
220 {
221  QMessageBox::about(this,
222  QString::fromStdString(m_factory->GetApplicationName()),
223  QString::fromStdString(m_factory->GetAbout()));
224 }
225 
226 void AppController::SLOT_WorkspaceWizard()
227 {
228  auto& p = m_workspace_controller.Project();
229  if (p) {
230  p.SetRunning(false);
231  }
232  WorkspaceWizard w(m_factory->CreateWorkspaceFactory());
233  if (w.exec() == QDialog::Accepted) {
234  m_workspace_controller.Open(w.GetWorkspaceDir());
235  SetWorkspaceWindowTitle(w.GetWorkspaceDir());
236  }
237 }
238 
239 } // namespace Controller
240 } // namespace Gui
241 } // namespace SimShell
Interface for WorkspaceWizard.
STL namespace.
AppController header.
Interface for PTreeQtState.
see the online Qt documentation
Interface for WorkspaceView.
see the online Qt documentation
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32