KoMo2  1.0.0
A modern ARM emulator GUI.
ControlsModel.cpp
Go to the documentation of this file.
1 
10 #include <iostream>
11 #include <string>
12 #include "../views/ControlsView.h"
13 #include "KoMo2Model.h"
14 
22  const std::string manual,
23  KoMo2Model* const parent)
24  : Model(parent), view(view) {
25  view->getHelpButton()->set_uri(manual);
26 
29 
32 
35 
38 
39  // Set the model & images of the view.
40  view->setModel(this);
41  view->setButtonImages(getParent()->getAbsolutePathToProjectRoot());
42 }
43 
44 // ! Button event handlers
45 
51  getParent()->changeJimulatorState(JimulatorState::UNLOADED);
52 }
53 
60  getParent()->changeJimulatorState(JimulatorState::LOADED);
61 }
62 
70  switch (getJimulatorState()) {
71  case JimulatorState::RUNNING:
73  getParent()->changeJimulatorState(JimulatorState::PAUSED);
74  break;
75  case JimulatorState::PAUSED:
77  getParent()->changeJimulatorState(JimulatorState::RUNNING);
78  break;
79  case JimulatorState::LOADED:
81  getParent()->changeJimulatorState(JimulatorState::RUNNING);
82  break;
83  default:
84  // TODO: Handle error state gracefully
85  break;
86  }
87 }
88 
97 
98  if (getJimulatorState() == JimulatorState::LOADED) {
99  getParent()->changeJimulatorState(JimulatorState::PAUSED);
100  }
101 }
102 
103 // ! Virtual functions
104 
110 const bool ControlsModel::handleKeyPress(const GdkEventKey* const e) {
111  switch (e->keyval) {
112  case GDK_KEY_F5:
113  if (getJimulatorState() != JimulatorState::UNLOADED) {
115  }
116  return true;
117  case GDK_KEY_F6:
118  if (getJimulatorState() == JimulatorState::LOADED ||
119  getJimulatorState() == JimulatorState::PAUSED) {
121  }
122  return true;
123  case GDK_KEY_F1:
124  if (getJimulatorState() == JimulatorState::RUNNING ||
125  getJimulatorState() == JimulatorState::PAUSED) {
127  }
128  return true;
129  case GDK_KEY_F12:
130  view->getHelpButton()->clicked();
131  return true;
132  default:
133  return false;
134  }
135 
136  return false;
137 }
138 
146  switch (newState) {
147  // some unloaded state
148  case JimulatorState::UNLOADED: {
152  view->getPauseResumeButton(), false,
153  new Gtk::Image(getParent()->getAbsolutePathToProjectRoot() +
154  "res/img/commenceSymbol.png"),
155  "Commence execution (F5)");
158 
159  // Set accessibility options for the pause/resume button
160  auto pauseResume = view->getPauseResumeButton()->get_accessible();
161  pauseResume->set_name("Play/pause execution");
162  pauseResume->set_description("Plays and pauses execution of program");
163  return;
164  }
165  // loaded, not yet run state
166  case JimulatorState::LOADED: {
170  view->getPauseResumeButton(), true,
171  new Gtk::Image(getParent()->getAbsolutePathToProjectRoot() +
172  "res/img/commenceSymbol.png"),
173  "Commence execution (F5)");
176 
177  // Set accessibility options for the pause/resume button
178  auto pauseResume = view->getPauseResumeButton();
179  pauseResume->set_tooltip_text("Commence execution (F5)");
180  pauseResume->get_accessible()->set_name("Commence execution");
181  pauseResume->get_accessible()->set_description(
182  "Commence execution of the loaded program");
183  return;
184  }
185  // Currently running
186  case JimulatorState::RUNNING: {
190  view->getPauseResumeButton(), true,
191  new Gtk::Image(getParent()->getAbsolutePathToProjectRoot() +
192  "res/img/pauseSymbol.png"),
193  "Pause execution (F5)");
196 
197  // Set accessibility options for the pause/resume button
198  auto pauseResume = view->getPauseResumeButton();
199  pauseResume->set_tooltip_text("Pause execution (F5)");
200  pauseResume->get_accessible()->set_name("Pause execution");
201  pauseResume->get_accessible()->set_description(
202  "Pause execution of the running program");
203  return;
204  }
205  // Has been running; is paused
206  case JimulatorState::PAUSED: {
210  view->getPauseResumeButton(), true,
211  new Gtk::Image(getParent()->getAbsolutePathToProjectRoot() +
212  "res/img/playSymbol.png"),
213  "Resume execution (F5)");
216 
217  // Set accessibility options for the pause/resume button
218  auto pauseResume = view->getPauseResumeButton();
219  pauseResume->set_tooltip_text("Resume execution (F5)");
220  pauseResume->get_accessible()->set_name("Resume execution");
221  pauseResume->get_accessible()->set_description(
222  "Resume execution of the paused program");
223  return;
224  }
225  // Error state
226  default:
227  // TODO: Handle the error state gracefully
228  return;
229  }
230 }
virtual const bool handleKeyPress(const GdkEventKey *const e) override
Handles a key press event pertaining to this model.
void setButtonState(Gtk::Button *const button, const bool state, Gtk::Image *const img=nullptr, const std::string newTooltip="", const std::string newLabelText="") const
Sets the state of a button to some boolean - the assumption is that if a button is not meant to be se...
Definition: Model.cpp:37
void resetJimulator()
Reset the emulators running.
Definition: kcmd.cpp:363
std::string manual
Manual information read from variables.json is stored here.
Definition: main.cpp:71
void onReloadJimulatorClick()
Handles the reloadJimulatorButton click events - sends a command to Jimulator and changes JimulatorSt...
virtual void changeJimulatorState(const JimulatorState newState) override
Handles the Jimulator state change for this model. For each value of newState, it will update the sta...
Gtk::Button *const getSingleStepExecuteButton()
Gets the singleStepExecuteButton member variable.
KoMo2Model *const getParent() const
Returns the parent pointer.
Definition: Model.cpp:67
ControlsModel(ControlsView *const view, const std::string manual, KoMo2Model *const parent)
Construct a new ControlsModel::ControlsModel object.
JimulatorState getJimulatorState() const
Return the jimulatorState member object.
Definition: Model.cpp:75
void onSingleStepExecuteClick()
Handles the singleStepExecuteButton click events - changes JimulatorState to "JimulatorState::PAUSED"...
ControlsView is the visual aspect of the controls that run along the top of the KoMo2 GUI...
Definition: ControlsView.h:22
ControlsView *const view
A pointer to the view which this model represents.
Definition: ControlsModel.h:33
JimulatorState
Describe the 5 states of Jimulator.
Definition: Model.h:19
void setButtonListener(Gtk::Button *const button, const T1 b, const T2 c)
Connect any button to any member function of.
Definition: Model.h:52
Gtk::Button *const getHaltExecutionButton()
Gets the haltExecutionButton member variable.
void pauseJimulator()
Pauses the emulator running.
Definition: kcmd.cpp:356
void onPauseResumeClick()
Handles the pauseResumeButton click events - changes JimulatorState to "JimulatorState::RUNNING" if c...
A file containing the definition of the KoMo2Model class.
Gtk::Button *const getPauseResumeButton()
Gets the pauseResumeButton member variable.
void startJimulator(const int steps)
Commences running the emulator.
Definition: kcmd.cpp:335
The superclass for all other Model classes. Uses a pure virtual function, so is abastract. Keeps KoMo2Model as a friend so it alone can call setJimulatorState. This class provides basic data that are needed by all other.
Definition: Model.h:35
void onHaltExecutionClick()
Handles the haltExecutionButton click events - changes JimulatorState to "JimulatorState::UNLOADED".
Gtk::Button *const getReloadJimulatorButton()
Gets the reloadJimulatorButton member variable.
The logical model of the entire application. All other models should be member variables of this mode...
Definition: KoMo2Model.h:34
void setModel(ControlsModel *const val)
Set the model member variable.
void continueJimulator()
Continues running Jimulator.
Definition: kcmd.cpp:346
Gtk::LinkButton *const getHelpButton()
Gets the helpButton member variable.
virtual void changeJimulatorState(const JimulatorState newState) override
Changes the Jimulator state and calls each child models own changeJimulatorState function.
Definition: KoMo2Model.cpp:97
void setButtonImages(const std::string projectRoot)
Sets the images for 4 of the buttons.
const bool refreshViews()
Refreshes the views. May be called on a looping timer.
Definition: KoMo2Model.cpp:56