SGL
gobservable.h
1 /*
2  * File: gobservable.h
3  * --------------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/09
7  * - added sgl namespace
8  * @version 2021/04/03
9  * - removed dependency on custom collections
10  * @version 2018/09/08
11  * - added doc comments for new documentation generation
12  * @version 2018/08/23
13  * - renamed to gobservable.h to replace Java version
14  * @version 2018/07/11
15  * - initial version
16  */
17 
18 
19 #ifndef _gobservable_h
20 #define _gobservable_h
21 
22 #include <functional>
23 #include <map>
24 #include <string>
25 #include <QtEvents>
26 
27 #include "gevent.h"
28 
29 namespace sgl {
30 
31 class GInteractor;
32 class _Internal_QWidget;
33 
40 class GObservable {
41 public:
45  GObservable();
46 
50  virtual ~GObservable();
51 
57  virtual bool eventsEnabled() const;
58 
64  virtual string getType() const = 0;
65 
70  virtual void setEventsEnabled(bool eventsEnabled);
71 
76  virtual string toString() const;
77 
78 protected:
82  virtual void clearEventListeners();
83 
87  virtual void ensureThreadSafety(const string& memberName = "");
88 
92  virtual void fireEvent(GEvent& event);
93 
98  virtual void fireGEvent(QEvent* event, EventType eventType, const string& eventName);
99 
104  virtual void fireGEvent(QCloseEvent* event, EventType eventType, const string& eventName);
105 
110  virtual void fireGEvent(QKeyEvent* event, EventType eventType, const string& eventName);
111 
116  virtual void fireGEvent(QMouseEvent* event, EventType eventType, const string& eventName, const string& actionCommand = "");
117 
122  virtual void fireGEvent(QResizeEvent* event, EventType eventType, const string& eventName);
123 
128  virtual void fireGEvent(QTimerEvent* event, EventType eventType, const string& eventName);
129 
134  virtual void fireGEvent(QWheelEvent* event, EventType eventType, const string& eventName);
135 
140  virtual void fireGEvent(QWindowStateChangeEvent* event, EventType eventType, const string& eventName);
141 
146  virtual bool hasEventListener(const string& eventName) const;
147 
153  virtual bool isAcceptingEvent(int eventMask) const;
154 
159  virtual bool isAcceptingEvent(const GEvent& event) const;
160 
165  virtual bool isAcceptingEvent(const string& eventType) const;
166 
171  virtual void removeEventListener(const string& eventName);
172 
177  virtual void removeEventListeners(std::initializer_list<string> eventNames);
178 
184  virtual void setEventListener(const string& eventName, GEventListener func);
185 
191  virtual void setEventListener(const string& eventName, GEventListenerVoid func);
192 
198  virtual void setEventListeners(std::initializer_list<string> eventNames, GEventListener func);
199 
205  virtual void setEventListeners(std::initializer_list<string> eventNames, GEventListenerVoid func);
206 
207 private:
208  std::map<string, GEvent::EventListenerWrapper> _eventMap;
209  bool _eventsEnabled;
210 
211  // allow all interactors and their inner QWidgets to call observable methods
212  friend class GInteractor;
213  friend class _Internal_QWidget;
214 };
215 
216 } // namespace sgl
217 
218 #endif // _gobservable_h
virtual string getType() const =0
Returns the concrete type of the object as a string, such as "GButton" or "GWindow".
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:59
virtual string toString() const
Returns a string representation of this observable object&#39;s state.
Definition: gobservable.cpp:264
virtual bool isAcceptingEvent(int eventMask) const
Returns true if the observable object has a listener for the given type of event. ...
Definition: gobservable.cpp:220
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:234
virtual void fireEvent(GEvent &event)
Sends out the given event to any attached listeners.
Definition: gobservable.cpp:47
std::function< void(GEvent)> GEventListener
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:38
Definition: console.h:45
GObservable()
Initializes a newly created observable object.
Definition: gobservable.cpp:26
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
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:228
virtual void clearEventListeners()
Removes all event listeners from this object.
Definition: gobservable.cpp:35
virtual bool eventsEnabled() const
Returns true if the object is currently allowing itself to fire events.
Definition: gobservable.cpp:43
virtual ~GObservable()
Frees any memory used internally by the observable object.
Definition: gobservable.cpp:31
virtual void ensureThreadSafety(string memberName="")
Ensures that we are currently in the Qt GUI thread.
Definition: gobservable.cpp:39
std::function< void()> GEventListenerVoid
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:44
A GObservable object is one that is able to send out events.
Definition: gobservable.h:40
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:224
virtual bool hasEventListener(string eventName) const
Returns true if the observable object has a listener for the given type of event. ...
Definition: gobservable.cpp:216
EventType
Defines the event subtypes for all events.
Definition: gevent.h:74
virtual void setEventsEnabled(bool eventsEnabled)
Sets whether the object is currently allowing itself to fire events.
Definition: gobservable.cpp:260
A GEvent represents a user action that has occurred on a graphical interactor.
Definition: gevent.h:158
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:248