VPTissue Reference Manual
SessionController.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 "SessionController.h"
21 
22 #include "util/misc/Exception.h"
23 
24 #include <QAction>
25 #include <QApplication>
26 #include <QMessageBox>
27 #include <QTimer>
28 
29 using namespace std;
30 using namespace SimPT_Sim::Util;
31 
32 namespace SimShell {
33 namespace Gui {
34 namespace Controller {
35 
36 SessionController::SessionController(QWidget* parent)
37  : QWidget(parent)
38 {
39  qRegisterMetaType< std::string >();
40 
41  m_action_run = new QAction("Run", this);
42  m_action_run->setCheckable(true);
43  m_action_run->setEnabled(false);
44  m_action_run->setShortcut(QKeySequence("R"));
45  m_action_run->setShortcutContext(Qt::ApplicationShortcut);
46  connect(m_action_run, SIGNAL(toggled(bool)), this, SLOT(SLOT_ToggleRunning(bool)));
47 
48  m_action_single_step = new QAction("Single Step", this);
49  m_action_single_step->setEnabled(false);
50  m_action_single_step->setShortcut(QKeySequence("S"));
51  m_action_single_step->setShortcutContext(Qt::ApplicationShortcut);
52  connect(m_action_single_step, SIGNAL(triggered()), this, SLOT(SLOT_TimeStep()));
53 
54  m_enabled_actions.Add(m_action_run);
55  m_enabled_actions.Add(m_action_single_step);
56 
57  // For the shortcut keys to work the action has to be added to active widget also.
58  addAction(m_action_run);
59  addAction(m_action_single_step);
60 }
61 
62 void SessionController::Disable()
63 {
64  m_project.reset();
65  m_action_run->setChecked(false);
66  m_enabled_actions.Disable();
67 }
68 
69 void SessionController::Enable(const shared_ptr<Ws::IProject>& project)
70 {
71  Disable();
72  m_project = project;
73  if (m_project->IsOpened()) {
74  connect(&m_project->Session(),
75  SIGNAL(InfoMessage(const std::string&, const InfoMessageReason&)),
76  this, SLOT(SimulationInfo(const std::string&, const InfoMessageReason&)));
77  connect(&m_project->Session(),
78  SIGNAL(ErrorMessage(const std::string&)),
79  this, SLOT(SimulationError(const std::string&)));
80  m_enabled_actions.Enable();
81  }
82 }
83 
84 QAction* SessionController::GetActionRun() const
85 {
86  return m_action_run;
87 }
88 
89 QAction* SessionController::GetActionSingleStep() const
90 {
91  return m_action_single_step;
92 }
93 
94 bool SessionController::IsEnabled() const
95 {
96  return (bool) m_project;
97 }
98 
99 bool SessionController::IsRunning() const
100 {
101  return m_action_run->isChecked();
102 }
103 
104 void SessionController::SetRunning(bool b)
105 {
106  if (m_action_run->isEnabled())
107  m_action_run->setChecked(b);
108 }
109 
110 void SessionController::SimulationError(const std::string& error)
111 {
112  std::cerr << "Exception> " + error << std::endl;
113  QString qmess = QString("Exception:\n%1\n").arg(error.c_str());
114  QMessageBox::information(0, "Critical Error", qmess);
115 }
116 
117 void SessionController::SimulationInfo(const std::string& ,
118  const Session::ISession::InfoMessageReason& reason)
119 {
120  switch (reason) {
121  case Session::ISession::InfoMessageReason::Stopped:
122  SetRunning(false);
123  break;
124  case Session::ISession::InfoMessageReason::Terminated:
125  Disable();
126  break;
127  default:
128  break;
129  }
130 }
131 
132 void SessionController::SLOT_ToggleRunning(bool b)
133 {
134  m_action_single_step->setEnabled(!b);
135 
136  if (m_project) {
137  if (m_project->IsOpened()) {
138  if (b) {
139  m_project->Session().StartSimulation();
140  } else {
141  m_project->Session().StopSimulation();
142  }
143  }
144  }
145 }
146 
147 void SessionController::SLOT_TimeStep()
148 {
149  if (m_project->IsOpened()) {
150  m_project->Session().TimeStep();
151  }
152 }
153 
154 } // namespace Controller
155 } // namespace Gui
156 } // namespace SimShell
STL namespace.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
SessionController header.
see the online Qt documentation
Header file for Exception class.
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32