SGL
gevent.h
1 /*
2  * File: gevent.h
3  * --------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/09
7  * - added sgl namespace
8  * @version 2018/09/20
9  * - removed deprecation warning on waitForEvent/Click global functions (for now)
10  * @version 2018/09/07
11  * - added doc comments for new documentation generation
12  * @version 2018/08/23
13  * - renamed to gevent.h to replace Java version
14  * @version 2018/07/06
15  * - initial version
16  */
17 
18 
19 #ifndef _gevent_h
20 #define _gevent_h
21 
22 #include <functional>
23 #include <iostream>
24 #include <string>
25 
26 #include "gtypes.h"
27 
28 class QEvent;
29 
30 namespace sgl {
31 
32 class GEvent;
33 class GInteractor;
34 class GObservable;
35 class _Internal_QCanvas;
36 class _Internal_QCheckBox;
37 class _Internal_QPushButton;
38 class _Internal_QWidget;
39 
41 typedef std::function<void(GEvent)> GEventListener;
42 
44 typedef std::function<void()> GEventListenerVoid;
45 
49 enum EventClass {
50  NULL_EVENT = 0x0000,
51  ACTION_EVENT = 0x0010,
52  KEY_EVENT = 0x0020,
53  TIMER_EVENT = 0x0040,
54  WINDOW_EVENT = 0x0080,
55  MOUSE_EVENT = 0x0100,
56  CLICK_EVENT = 0x0200,
57  TABLE_EVENT = 0x0400,
58  SERVER_EVENT = 0x0800,
59  CHANGE_EVENT = 0x1000,
60  HYPERLINK_EVENT = 0x2000,
61  SCROLL_EVENT = 0x4000,
65 };
66 // Note: If you add any new classes of events, you must also add logic to the
67 // GEvent::classToString function in gevent.cpp.
68 
69 
74 enum EventType {
75  NULL_TYPE = 0,
76 
84 
87 
98 
102 
104 
105  TABLE_UPDATED = TABLE_EVENT + 1, // when a cell's value gets set
106  TABLE_SELECTED = TABLE_EVENT + 2, // cursor moves onto a cell
107  TABLE_EDIT_BEGIN = TABLE_EVENT + 3, // user presses F2 or double clicks to start editing a cell
108  TABLE_REPLACE_BEGIN = TABLE_EVENT + 4, // user starts typing on a cell; like TABLE_EDIT_BEGIN but wipes out previous value
109  TABLE_EDIT_CANCEL = TABLE_EVENT + 5, // user presses Esc or otherwise stops editing a cell
110  TABLE_CUT = TABLE_EVENT + 6, // user cuts cell value to clipboard
111  TABLE_COPY = TABLE_EVENT + 7, // user copies cell value to clipboard
112  TABLE_PASTE = TABLE_EVENT + 8, // user pastes cell value from clipboard
113 
115 
117 
119 
121 };
122 // Note: If you add any new classes of events, you must also add logic to the
123 // GEvent::typeToString function in gevent.cpp.
124 
129 enum Modifier {
130  SHIFT_DOWN = 1 << 0,
131  CTRL_DOWN = 1 << 1,
132  META_DOWN = 1 << 2,
133  ALT_DOWN = 1 << 3,
134  ALT_GRAPH_DOWN = 1 << 4,
135  BUTTON1_DOWN = 1 << 5,
136  BUTTON2_DOWN = 1 << 6,
137  BUTTON3_DOWN = 1 << 7
138 };
139 
158 class GEvent {
159 public:
164 
170 
171  /*
172  * Type: KeyCode
173  * -------------
174  * This type defines the names of the key codes returned in a key event.
175  */
176  enum KeyCode {
178  TAB_KEY = 9,
179  ENTER_KEY = 10,
180  CLEAR_KEY = 12,
182  SHIFT_KEY = Qt::Key_Shift,
183  CTRL_KEY = Qt::Key_Control,
184  ALT_KEY = Qt::Key_Alt,
185  PAUSE_KEY = 19,
188  PAGE_UP_KEY = Qt::Key_PageUp,
189  PAGE_DOWN_KEY = Qt::Key_PageDown,
190  END_KEY = Qt::Key_End,
191  HOME_KEY = Qt::Key_Home,
192  LEFT_ARROW_KEY = Qt::Key_Left,
193  UP_ARROW_KEY = Qt::Key_Up,
194  RIGHT_ARROW_KEY = Qt::Key_Right,
195  DOWN_ARROW_KEY = Qt::Key_Down,
196  F1_KEY = Qt::Key_F1,
197  F2_KEY = Qt::Key_F2,
198  F3_KEY = Qt::Key_F3,
199  F4_KEY = Qt::Key_F4,
200  F5_KEY = Qt::Key_F5,
201  F6_KEY = Qt::Key_F6,
202  F7_KEY = Qt::Key_F7,
203  F8_KEY = Qt::Key_F8,
204  F9_KEY = Qt::Key_F9,
205  F10_KEY = Qt::Key_F10,
206  F11_KEY = Qt::Key_F11,
207  F12_KEY = Qt::Key_F12,
208  DELETE_KEY = 127,
209  NUM_LOCK_KEY = Qt::Key_NumLock,
210  SCROLL_LOCK_KEY = Qt::Key_ScrollLock,
211  PRINT_SCREEN_KEY = Qt::Key_Print,
212  INSERT_KEY = Qt::Key_Insert,
213  HELP_KEY = Qt::Key_Help,
214  META_KEY = Qt::Key_Meta,
215  WINDOWS_KEY = Qt::Key_Super_L,
216  MENU_KEY = Qt::Key_Menu
217  };
218 
222  GEvent(EventClass eventClass = NULL_EVENT,
223  EventType eventType = NULL_TYPE,
224  const string& eventName = "",
225  GObservable* source = nullptr);
226 
230  virtual ~GEvent();
231 
237  static string classToString(EventClass eventClass);
238 
244  virtual string getActionCommand() const;
245 
250  virtual int getButton() const;
251 
256  virtual EventClass getClass() const;
257 
262  virtual int getColumn() const;
263 
270  static long getCurrentTimeMS();
271 
276  virtual EventClass getEventClass() const;
277 
282  virtual EventType getEventType() const;
283 
287  virtual GInteractor* getInteractor() const;
288 
293  virtual QEvent* getInternalEvent() const;
294 
299  virtual char getKeyChar() const;
300 
306  virtual int getKeyCode() const;
307 
313  virtual GPoint getLocation() const;
314 
319  virtual int getModifiers() const;
320 
324  virtual string getName() const;
325 
330  virtual string getRequestURL() const;
331 
336  virtual int getRow() const;
337 
341  virtual GObservable* getSource() const;
342 
347  virtual long getTime() const;
348 
353  virtual EventType getType() const;
354 
360  virtual double getX() const;
361 
367  virtual double getY() const;
368 
374  virtual void ignore();
375 
380  virtual bool isAltKeyDown() const;
381 
386  virtual bool isCtrlKeyDown() const;
387 
393  virtual bool isCtrlOrCommandKeyDown() const;
394 
399  virtual bool isDoubleClick() const;
400 
405  virtual bool isLeftClick() const;
406 
414  virtual bool isMiddleClick() const;
415 
420  virtual bool isRightClick() const;
421 
426  virtual bool isMetaKeyDown() const;
427 
432  virtual bool isShiftKeyDown() const;
433 
438  static string keyCodeToString(int keyCode);
439 
443  virtual void setActionCommand(const string& actionCommand);
444 
448  virtual void setButton(int button);
449 
453  virtual void setInternalEvent(QEvent* event);
454 
458  virtual void setKeyChar(char keyChar);
459 
463  virtual void setKeyChar(const string& keyCharString);
464 
468  virtual void setKeyCode(int keyCode);
469 
473  virtual void setModifiers(Qt::KeyboardModifiers modifiers);
474 
478  virtual void setRequestURL(const string& requestUrl);
479 
483  virtual void setRowAndColumn(int row, int col);
484 
488  virtual void setSource(GObservable* source);
489 
493  virtual void setX(double x);
494 
498  virtual void setY(double y);
499 
503  virtual string toString() const;
504 
509  static string typeToString(EventType eventType);
510 
511 private:
512  /*
513  * Represents the two types of event listeners.
514  */
515  enum EventListenerType {
516  HANDLER_EVENT,
517  HANDLER_VOID
518  };
519 
520  /*
521  * A wrapper that can hold either of the two types of event listeners.
522  */
523  struct EventListenerWrapper {
524  GEventListener handler;
525  GEventListenerVoid handlerVoid;
526  EventListenerType type;
527 
528  void fireEvent(const GEvent& event) {
529  if (type == HANDLER_EVENT) {
530  handler(event);
531  } else {
532  handlerVoid();
533  }
534  }
535  };
536 
537  // member variables
538  string _actionCommand;
539  int _button;
540  EventClass _class;
541  char _keyChar;
542  int _keyCode;
543  int _modifiers;
544  string _name;
545  string _requestUrl;
546  GObservable* _source;
547  long _time;
548  EventType _type;
549  double _x;
550  double _y;
551  int _row;
552  int _col;
553  QEvent* _internalQtEvent;
554 
555  friend class GInteractor;
556  friend class GObservable;
557  friend class _Internal_QWidget;
558 };
559 
563 std::ostream& operator <<(std::ostream& out, const GEvent& event);
564 
565 // alias GEvent to all event types
576 
577 // global functions for backward compatibility
578 // see geventqueue.cpp for implementation
579 
593 GEvent getNextEvent(int mask = ANY_EVENT) /*(deprecated)*/;
594 
605 GMouseEvent waitForClick() /*(deprecated)*/;
606 
649 GEvent waitForEvent(int mask = ANY_EVENT) /*(deprecated)*/;
650 
651 } // namespace sgl
652 
653 #endif // _gevent_h
Definition: gevent.h:60
virtual EventType getEventType() const
Returns the event&#39;s type (minor type such as MOUSE_PRESSED).
Definition: gevent.cpp:148
Definition: gevent.h:92
Definition: gevent.h:120
Definition: gevent.h:110
virtual EventType getType() const
Returns the event&#39;s type (major type such as MOUSE_EVENT).
Definition: gevent.cpp:196
Definition: gevent.h:207
Definition: gevent.h:101
Definition: gevent.h:79
Definition: gevent.h:114
GEvent GTimerEvent
Definition: gevent.h:574
Definition: gevent.h:182
virtual char getKeyChar() const
Returns the key character that was typed, if this is a key event.
Definition: gevent.cpp:152
virtual int getModifiers() const
Returns the modifiers active during this event.
Definition: gevent.cpp:160
Definition: gevent.h:135
Definition: gevent.h:61
Definition: gevent.h:131
Definition: gevent.h:51
Definition: gevent.h:54
Definition: gevent.h:206
virtual bool isShiftKeyDown() const
Returns true if the Shift key was held down during this event.
Definition: gevent.cpp:246
virtual void ignore()
Instructs the GUI system to ignore or cancel this event.
Definition: gevent.cpp:208
Definition: gevent.h:179
virtual bool isRightClick() const
Returns true if the user pressed the right mouse button.
Definition: gevent.cpp:238
Definition: gevent.h:215
virtual string toString() const
Returns a text representation of the event for debugging.
Definition: gevent.cpp:488
GEvent GMouseEvent
Definition: gevent.h:570
Definition: gevent.h:91
Modifier
A set of constants used to check whether various event modifiers are in effect.
Definition: gevent.h:129
GEvent(EventClass eventClass=NULL_EVENT, EventType eventType=NULL_TYPE, string eventName="", GObservable *source=nullptr)
Creates a new event of the given type.
Definition: gevent.cpp:34
Definition: gevent.h:90
std::ostream & operator<<(std::ostream &out, const GEvent &event)
Writes the given event to the given output stream.
Definition: gevent.cpp:494
Definition: gevent.h:177
virtual bool isCtrlOrCommandKeyDown() const
Returns true if the Ctrl key, or the Command key (Mac), was held down during this event...
Definition: gevent.cpp:222
virtual QEvent * getInternalEvent() const
Returns the Qt event being wrapped by this event, if any.
Definition: gevent.cpp:176
virtual int getKeyCode() const
Returns the integer key code that was typed, if this is a key event.
Definition: gevent.cpp:156
Definition: gevent.h:103
Definition: gevent.h:82
Definition: gevent.h:134
virtual bool isMiddleClick() const
Returns true if the user pressed the middle mouse button.
Definition: gevent.cpp:234
Definition: gevent.h:116
Definition: gevent.h:99
Definition: gevent.h:94
Definition: gevent.h:178
Definition: gevent.h:190
static string keyCodeToString(int keyCode)
Converts a key code such as 67 into a string such as "A".
Definition: gevent.cpp:250
Definition: gevent.h:81
Definition: gevent.h:50
Definition: gevent.h:185
GEvent GTableEvent
Definition: gevent.h:573
Definition: gevent.h:136
Definition: gevent.h:180
Definition: gevent.h:96
GEvent GScrollEvent
Definition: gevent.h:571
GEvent GActionEvent
Definition: gevent.h:566
virtual string getName() const
Returns this event&#39;s name such as "click" or "keydown" or "actionperformed".
Definition: gevent.cpp:164
static string typeToString(EventType eventType)
Converts an event type such as MOUSE_EVENT to a string such as "MOUSE_EVENT".
Definition: gevent.cpp:78
GEvent GKeyEvent
Definition: gevent.h:569
std::function< void(GEvent)> GEventListener
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:38
virtual GInteractor * getInteractor() const
Returns the source interactor that generated this event.
Definition: gevent.cpp:172
Definition: console.h:45
virtual bool isCtrlKeyDown() const
Returns true if the Ctrl key was held down during this event.
Definition: gevent.cpp:218
Definition: gevent.h:109
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
GEvent waitForEvent(int mask=ANY_EVENT)
Dismisses the process until an event occurs whose type is covered by the event mask.
Definition: geventqueue.cpp:176
virtual double getX() const
Returns the x-coordinate of the mouse position within the interactor when this event occurred...
Definition: gevent.cpp:200
Definition: gevent.h:107
Definition: gevent.h:100
GEvent GWindowEvent
Definition: gevent.h:575
Definition: gevent.h:202
Definition: gevent.h:197
virtual string getActionCommand() const
Returns the action command associated with the event.
Definition: gevent.cpp:121
Definition: gevent.h:186
Definition: gevent.h:78
Definition: gevent.h:77
Definition: gevent.h:193
Definition: gevent.h:55
Definition: gevent.h:199
Definition: gevent.h:191
Definition: gevent.h:188
Definition: gevent.h:204
GEvent GChangeEvent
Definition: gevent.h:567
std::function< void()> GEventListenerVoid
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:44
Definition: gevent.h:181
Definition: gevent.h:184
virtual string getRequestURL() const
Returns this event&#39;s request URL, if this is a server URL event.
Definition: gevent.cpp:168
Definition: gevent.h:75
A GObservable object is one that is able to send out events.
Definition: gobservable.h:40
Definition: gevent.h:59
virtual bool isDoubleClick() const
Returns true if the user pressed the mouse button multiple times.
Definition: gevent.cpp:226
Definition: gevent.h:196
Definition: gevent.h:201
virtual GPoint getLocation() const
Returns an (x, y) point representing the mouse position within the interactor when this event occurre...
Definition: gevent.cpp:180
Definition: gevent.h:88
virtual GObservable * getSource() const
Returns the source object that generated this event.
Definition: gevent.cpp:188
virtual bool isAltKeyDown() const
Returns true if the Alt key was held down during this event.
Definition: gevent.cpp:214
virtual int getButton() const
Returns which mouse button was clicked, if this is a mouse event.
Definition: gevent.cpp:125
EventType
Defines the event subtypes for all events.
Definition: gevent.h:74
Definition: gevent.h:213
Definition: gevent.h:85
Definition: gevent.h:211
Definition: gevent.h:130
virtual long getTime() const
Returns this event&#39;s timestamp, as a number of milliseconds elapsed since the epoch of 1970/01/01 12:...
Definition: gevent.cpp:192
Definition: gevent.h:210
virtual EventClass getEventClass() const
Returns this event&#39;s class (major type such as MOUSE_EVENT).
Definition: gevent.cpp:144
Definition: gevent.h:56
Definition: gevent.h:183
static GEventListener EMPTY_EVENT_LISTENER
An empty event handler that can be passed that does nothing.
Definition: gevent.h:163
Definition: gevent.h:62
virtual EventClass getClass() const
Returns this event&#39;s class (major type such as MOUSE_EVENT).
Definition: gevent.cpp:129
EventClass
Represents all major categories of events.
Definition: gevent.h:49
Definition: gevent.h:137
virtual bool isLeftClick() const
Returns true if the user pressed the left mouse button.
Definition: gevent.cpp:230
Definition: gevent.h:212
Definition: gevent.h:58
Definition: gevent.h:205
Definition: gevent.h:108
Definition: gevent.h:57
A GEvent represents a user action that has occurred on a graphical interactor.
Definition: gevent.h:158
virtual ~GEvent()
Frees memory allocated internally by the event.
Definition: gevent.cpp:56
Definition: gevent.h:105
Definition: gevent.h:83
GEvent getNextEvent(int mask=ANY_EVENT)
Checks to see if there are any events of the desired type waiting on the event queue.
Definition: geventqueue.cpp:163
Definition: gevent.h:216
Definition: gevent.h:209
Definition: gevent.h:86
virtual bool isMetaKeyDown() const
Returns true if the Meta/Command key was held down during this event.
Definition: gevent.cpp:242
Definition: gevent.h:111
Definition: gevent.h:208
Definition: gevent.h:93
KeyCode
Definition: gevent.h:176
Definition: gevent.h:106
Definition: gevent.h:187
friend class GObservable
Definition: gevent.h:556
This struct contains real-valued x and y fields.
Definition: gtypes.h:202
Definition: gevent.h:112
Definition: gevent.h:95
Definition: gevent.h:118
Definition: gevent.h:214
virtual double getY() const
Returns the y-coordinate of the mouse position within the interactor when this event occurred...
Definition: gevent.cpp:204
Definition: gevent.h:53
Definition: gevent.h:195
static GEventListener LOG_EVENT
An event listener that just prints the event that occurred.
Definition: gevent.h:169
Definition: gevent.h:203
virtual int getColumn() const
Returns the column that was interacted with, if this is a table event.
Definition: gevent.cpp:133
Definition: gevent.h:89
Definition: gevent.h:189
Definition: gevent.h:97
GEvent GHyperlinkEvent
Definition: gevent.h:568
Definition: gevent.h:133
GMouseEvent waitForClick()
Waits for a mouse click to occur anywhere in any window, returning the event that occurred...
Definition: geventqueue.cpp:167
Definition: gevent.h:200
Definition: gevent.h:192
virtual int getRow() const
Returns the row that was interacted with, if this is a table event.
Definition: gevent.cpp:184
Definition: gevent.h:132
Definition: gevent.h:80
GEvent GServerEvent
Definition: gevent.h:572
Definition: gevent.h:52
Definition: gevent.h:194
Definition: gevent.h:198