SGL
gobservable.h
1 /*
2  * File: gobservable.h
3  * --------------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/03
7  * - removed dependency on custom collections
8  * @version 2018/09/08
9  * - added doc comments for new documentation generation
10  * @version 2018/08/23
11  * - renamed to gobservable.h to replace Java version
12  * @version 2018/07/11
13  * - initial version
14  */
15 
16 
17 #ifndef _gobservable_h
18 #define _gobservable_h
19 
20 #include <functional>
21 #include <map>
22 #include <string>
23 #include <QtEvents>
24 
25 #include "gevent.h"
26 
27 class GInteractor;
28 class _Internal_QWidget;
29 
36 class GObservable {
37 public:
41  GObservable();
42 
46  virtual ~GObservable();
47 
53  virtual bool eventsEnabled() const;
54 
60  virtual string getType() const = 0;
61 
66  virtual void setEventsEnabled(bool eventsEnabled);
67 
72  virtual string toString() const;
73 
74 protected:
78  virtual void clearEventListeners();
79 
83  virtual void ensureThreadSafety(const string& memberName = "");
84 
88  virtual void fireEvent(GEvent& event);
89 
94  virtual void fireGEvent(QEvent* event, EventType eventType, const string& eventName);
95 
100  virtual void fireGEvent(QCloseEvent* event, EventType eventType, const string& eventName);
101 
106  virtual void fireGEvent(QKeyEvent* event, EventType eventType, const string& eventName);
107 
112  virtual void fireGEvent(QMouseEvent* event, EventType eventType, const string& eventName, const string& actionCommand = "");
113 
118  virtual void fireGEvent(QResizeEvent* event, EventType eventType, const string& eventName);
119 
124  virtual void fireGEvent(QTimerEvent* event, EventType eventType, const string& eventName);
125 
130  virtual void fireGEvent(QWheelEvent* event, EventType eventType, const string& eventName);
131 
136  virtual void fireGEvent(QWindowStateChangeEvent* event, EventType eventType, const string& eventName);
137 
142  virtual bool hasEventListener(const string& eventName) const;
143 
149  virtual bool isAcceptingEvent(int eventMask) const;
150 
155  virtual bool isAcceptingEvent(const GEvent& event) const;
156 
161  virtual bool isAcceptingEvent(const string& eventType) const;
162 
167  virtual void removeEventListener(const string& eventName);
168 
173  virtual void removeEventListeners(std::initializer_list<string> eventNames);
174 
180  virtual void setEventListener(const string& eventName, GEventListener func);
181 
187  virtual void setEventListener(const string& eventName, GEventListenerVoid func);
188 
194  virtual void setEventListeners(std::initializer_list<string> eventNames, GEventListener func);
195 
201  virtual void setEventListeners(std::initializer_list<string> eventNames, GEventListenerVoid func);
202 
203 private:
204  std::map<string, GEvent::EventListenerWrapper> _eventMap;
205  bool _eventsEnabled;
206 
207  // allow all interactors and their inner QWidgets to call observable methods
208  friend class GInteractor;
209  friend class _Internal_QWidget;
210 };
211 
212 #endif // _gobservable_h
virtual bool isAcceptingEvent(int eventMask) const
Returns true if the observable object has a listener for the given type of event. ...
Definition: gobservable.cpp:216
virtual void removeEventListeners(std::initializer_list< string > eventNames)
Removes any event listener from this observable object that would respond to the given types of event...
Definition: gobservable.cpp:224
virtual void clearEventListeners()
Removes all event listeners from this object.
Definition: gobservable.cpp:31
A GEvent represents a user action that has occurred on a graphical interactor.
Definition: gevent.h:153
virtual void removeEventListener(string eventName)
Removes any event listener from this observable object that would respond to the given type of event...
Definition: gobservable.cpp:220
virtual bool hasEventListener(string eventName) const
Returns true if the observable object has a listener for the given type of event. ...
Definition: gobservable.cpp:212
A GObservable object is one that is able to send out events.
Definition: gobservable.h:36
virtual void fireGEvent(QEvent *event, EventType eventType, string eventName)
Creates an event of the given type, then sends it out to any attached listeners.
Definition: gobservable.cpp:55
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
virtual void fireEvent(GEvent &event)
Sends out the given event to any attached listeners.
Definition: gobservable.cpp:43
virtual void setEventListeners(std::initializer_list< string > eventNames, GEventListener func)
Adds an event listener from this observable object to respond to the given types of events...
Definition: gobservable.cpp:244
virtual bool eventsEnabled() const
Returns true if the object is currently allowing itself to fire events.
Definition: gobservable.cpp:39
virtual void setEventListener(string eventName, GEventListener func)
Adds an event listener from this observable object to respond to the given type of event...
Definition: gobservable.cpp:230
virtual void ensureThreadSafety(string memberName="")
Ensures that we are currently in the Qt GUI thread.
Definition: gobservable.cpp:35
virtual string getType() const =0
Returns the concrete type of the object as a string, such as "GButton" or "GWindow".
virtual string toString() const
Returns a string representation of this observable object&#39;s state.
Definition: gobservable.cpp:260
virtual void setEventsEnabled(bool eventsEnabled)
Sets whether the object is currently allowing itself to fire events.
Definition: gobservable.cpp:256
GObservable()
Initializes a newly created observable object.
Definition: gobservable.cpp:22
virtual ~GObservable()
Frees any memory used internally by the observable object.
Definition: gobservable.cpp:27