SGL
gevent.h
1 /*
2  * File: gevent.h
3  * --------------
4  *
5  * @author Marty Stepp
6  * @version 2018/09/20
7  * - removed deprecation warning on waitForEvent/Click global functions (for now)
8  * @version 2018/09/07
9  * - added doc comments for new documentation generation
10  * @version 2018/08/23
11  * - renamed to gevent.h to replace Java version
12  * @version 2018/07/06
13  * - initial version
14  */
15 
16 
17 #ifndef _gevent_h
18 #define _gevent_h
19 
20 #include <functional>
21 #include <iostream>
22 #include <string>
23 
24 #include "gtypes.h"
25 
26 class QEvent;
27 class GEvent;
28 class GInteractor;
29 class GObservable;
30 class _Internal_QCanvas;
31 class _Internal_QCheckBox;
32 class _Internal_QPushButton;
33 class _Internal_QWidget;
34 
36 typedef std::function<void(GEvent)> GEventListener;
37 
39 typedef std::function<void()> GEventListenerVoid;
40 
44 enum EventClass {
45  NULL_EVENT = 0x0000,
46  ACTION_EVENT = 0x0010,
47  KEY_EVENT = 0x0020,
48  TIMER_EVENT = 0x0040,
49  WINDOW_EVENT = 0x0080,
50  MOUSE_EVENT = 0x0100,
51  CLICK_EVENT = 0x0200,
52  TABLE_EVENT = 0x0400,
53  SERVER_EVENT = 0x0800,
54  CHANGE_EVENT = 0x1000,
55  HYPERLINK_EVENT = 0x2000,
56  SCROLL_EVENT = 0x4000,
57  ANY_EVENT = ACTION_EVENT | KEY_EVENT | TIMER_EVENT | WINDOW_EVENT
58  | MOUSE_EVENT | CLICK_EVENT | TABLE_EVENT | SERVER_EVENT
59  | CHANGE_EVENT | HYPERLINK_EVENT | SCROLL_EVENT
60 };
61 // Note: If you add any new classes of events, you must also add logic to the
62 // GEvent::classToString function in gevent.cpp.
63 
64 
69 enum EventType {
70  NULL_TYPE = 0,
71 
72  WINDOW_CLOSED = WINDOW_EVENT + 1,
73  WINDOW_RESIZED = WINDOW_EVENT + 2,
74  CONSOLE_CLOSED = WINDOW_EVENT + 3,
75  WINDOW_CLOSING = WINDOW_EVENT + 4,
76  WINDOW_MINIMIZED = WINDOW_EVENT + 5,
77  WINDOW_RESTORED = WINDOW_EVENT + 6,
78  WINDOW_MAXIMIZED = WINDOW_EVENT + 7,
79 
80  ACTION_PERFORMED = ACTION_EVENT + 1,
81  ACTION_MENU = ACTION_EVENT + 2,
82 
83  MOUSE_CLICKED = MOUSE_EVENT + 1,
84  MOUSE_PRESSED = MOUSE_EVENT + 2,
85  MOUSE_RELEASED = MOUSE_EVENT + 3,
86  MOUSE_MOVED = MOUSE_EVENT + 4,
87  MOUSE_DRAGGED = MOUSE_EVENT + 5,
88  MOUSE_ENTERED = MOUSE_EVENT + 6,
89  MOUSE_EXITED = MOUSE_EVENT + 7,
90  MOUSE_WHEEL_DOWN = MOUSE_EVENT + 8,
91  MOUSE_WHEEL_UP = MOUSE_EVENT + 9,
92  MOUSE_DOUBLE_CLICKED = MOUSE_EVENT + 10,
93 
94  KEY_PRESSED = KEY_EVENT + 1,
95  KEY_RELEASED = KEY_EVENT + 2,
96  KEY_TYPED = KEY_EVENT + 3,
97 
98  TIMER_TICKED = TIMER_EVENT + 1,
99 
100  TABLE_UPDATED = TABLE_EVENT + 1, // when a cell's value gets set
101  TABLE_SELECTED = TABLE_EVENT + 2, // cursor moves onto a cell
102  TABLE_EDIT_BEGIN = TABLE_EVENT + 3, // user presses F2 or double clicks to start editing a cell
103  TABLE_REPLACE_BEGIN = TABLE_EVENT + 4, // user starts typing on a cell; like TABLE_EDIT_BEGIN but wipes out previous value
104  TABLE_EDIT_CANCEL = TABLE_EVENT + 5, // user presses Esc or otherwise stops editing a cell
105  TABLE_CUT = TABLE_EVENT + 6, // user cuts cell value to clipboard
106  TABLE_COPY = TABLE_EVENT + 7, // user copies cell value to clipboard
107  TABLE_PASTE = TABLE_EVENT + 8, // user pastes cell value from clipboard
108 
109  SERVER_REQUEST = SERVER_EVENT + 1,
110 
111  STATE_CHANGED = CHANGE_EVENT + 1,
112 
113  HYPERLINK_CLICKED = HYPERLINK_EVENT + 1,
114 
115  SCROLL_SCROLLED = SCROLL_EVENT + 1
116 };
117 // Note: If you add any new classes of events, you must also add logic to the
118 // GEvent::typeToString function in gevent.cpp.
119 
124 enum Modifier {
125  SHIFT_DOWN = 1 << 0,
126  CTRL_DOWN = 1 << 1,
127  META_DOWN = 1 << 2,
128  ALT_DOWN = 1 << 3,
129  ALT_GRAPH_DOWN = 1 << 4,
130  BUTTON1_DOWN = 1 << 5,
131  BUTTON2_DOWN = 1 << 6,
132  BUTTON3_DOWN = 1 << 7
133 };
134 
153 class GEvent {
154 public:
158  static GEventListener EMPTY_EVENT_LISTENER;
159 
164  static GEventListener LOG_EVENT;
165 
166  /*
167  * Type: KeyCode
168  * -------------
169  * This type defines the names of the key codes returned in a key event.
170  */
171  enum KeyCode {
173  TAB_KEY = 9,
174  ENTER_KEY = 10,
175  CLEAR_KEY = 12,
177  SHIFT_KEY = Qt::Key_Shift,
178  CTRL_KEY = Qt::Key_Control,
179  ALT_KEY = Qt::Key_Alt,
180  PAUSE_KEY = 19,
183  PAGE_UP_KEY = Qt::Key_PageUp,
184  PAGE_DOWN_KEY = Qt::Key_PageDown,
185  END_KEY = Qt::Key_End,
186  HOME_KEY = Qt::Key_Home,
187  LEFT_ARROW_KEY = Qt::Key_Left,
188  UP_ARROW_KEY = Qt::Key_Up,
189  RIGHT_ARROW_KEY = Qt::Key_Right,
190  DOWN_ARROW_KEY = Qt::Key_Down,
191  F1_KEY = Qt::Key_F1,
192  F2_KEY = Qt::Key_F2,
193  F3_KEY = Qt::Key_F3,
194  F4_KEY = Qt::Key_F4,
195  F5_KEY = Qt::Key_F5,
196  F6_KEY = Qt::Key_F6,
197  F7_KEY = Qt::Key_F7,
198  F8_KEY = Qt::Key_F8,
199  F9_KEY = Qt::Key_F9,
200  F10_KEY = Qt::Key_F10,
201  F11_KEY = Qt::Key_F11,
202  F12_KEY = Qt::Key_F12,
203  DELETE_KEY = 127,
204  NUM_LOCK_KEY = Qt::Key_NumLock,
205  SCROLL_LOCK_KEY = Qt::Key_ScrollLock,
206  PRINT_SCREEN_KEY = Qt::Key_Print,
207  INSERT_KEY = Qt::Key_Insert,
208  HELP_KEY = Qt::Key_Help,
209  META_KEY = Qt::Key_Meta,
210  WINDOWS_KEY = Qt::Key_Super_L,
211  MENU_KEY = Qt::Key_Menu
212  };
213 
217  GEvent(EventClass eventClass = NULL_EVENT,
218  EventType eventType = NULL_TYPE,
219  const string& eventName = "",
220  GObservable* source = nullptr);
221 
225  virtual ~GEvent();
226 
232  static string classToString(EventClass eventClass);
233 
239  virtual string getActionCommand() const;
240 
245  virtual int getButton() const;
246 
251  virtual EventClass getClass() const;
252 
257  virtual int getColumn() const;
258 
265  static long getCurrentTimeMS();
266 
271  virtual EventClass getEventClass() const;
272 
277  virtual EventType getEventType() const;
278 
282  virtual GInteractor* getInteractor() const;
283 
288  virtual QEvent* getInternalEvent() const;
289 
294  virtual char getKeyChar() const;
295 
301  virtual int getKeyCode() const;
302 
308  virtual GPoint getLocation() const;
309 
314  virtual int getModifiers() const;
315 
319  virtual string getName() const;
320 
325  virtual string getRequestURL() const;
326 
331  virtual int getRow() const;
332 
336  virtual GObservable* getSource() const;
337 
342  virtual long getTime() const;
343 
348  virtual EventType getType() const;
349 
355  virtual double getX() const;
356 
362  virtual double getY() const;
363 
369  virtual void ignore();
370 
375  virtual bool isAltKeyDown() const;
376 
381  virtual bool isCtrlKeyDown() const;
382 
388  virtual bool isCtrlOrCommandKeyDown() const;
389 
394  virtual bool isDoubleClick() const;
395 
400  virtual bool isLeftClick() const;
401 
409  virtual bool isMiddleClick() const;
410 
415  virtual bool isRightClick() const;
416 
421  virtual bool isMetaKeyDown() const;
422 
427  virtual bool isShiftKeyDown() const;
428 
433  static string keyCodeToString(int keyCode);
434 
438  virtual void setActionCommand(const string& actionCommand);
439 
443  virtual void setButton(int button);
444 
448  virtual void setInternalEvent(QEvent* event);
449 
453  virtual void setKeyChar(char keyChar);
454 
458  virtual void setKeyChar(const string& keyCharString);
459 
463  virtual void setKeyCode(int keyCode);
464 
468  virtual void setModifiers(Qt::KeyboardModifiers modifiers);
469 
473  virtual void setRequestURL(const string& requestUrl);
474 
478  virtual void setRowAndColumn(int row, int col);
479 
483  virtual void setSource(GObservable* source);
484 
488  virtual void setX(double x);
489 
493  virtual void setY(double y);
494 
498  virtual string toString() const;
499 
504  static string typeToString(EventType eventType);
505 
506 private:
507  /*
508  * Represents the two types of event listeners.
509  */
510  enum EventListenerType {
511  HANDLER_EVENT,
512  HANDLER_VOID
513  };
514 
515  /*
516  * A wrapper that can hold either of the two types of event listeners.
517  */
518  struct EventListenerWrapper {
519  GEventListener handler;
520  GEventListenerVoid handlerVoid;
521  EventListenerType type;
522 
523  void fireEvent(const GEvent& event) {
524  if (type == HANDLER_EVENT) {
525  handler(event);
526  } else {
527  handlerVoid();
528  }
529  }
530  };
531 
532  // member variables
533  string _actionCommand;
534  int _button;
535  EventClass _class;
536  char _keyChar;
537  int _keyCode;
538  int _modifiers;
539  string _name;
540  string _requestUrl;
541  GObservable* _source;
542  long _time;
543  EventType _type;
544  double _x;
545  double _y;
546  int _row;
547  int _col;
548  QEvent* _internalQtEvent;
549 
550  friend class GInteractor;
551  friend class GObservable;
552  friend class _Internal_QWidget;
553 };
554 
558 std::ostream& operator <<(std::ostream& out, const GEvent& event);
559 
560 // alias GEvent to all event types
561 typedef GEvent GActionEvent;
562 typedef GEvent GChangeEvent;
563 typedef GEvent GHyperlinkEvent;
564 typedef GEvent GKeyEvent;
565 typedef GEvent GMouseEvent;
566 typedef GEvent GScrollEvent;
567 typedef GEvent GServerEvent;
568 typedef GEvent GTableEvent;
569 typedef GEvent GTimerEvent;
570 typedef GEvent GWindowEvent;
571 
572 // global functions for backward compatibility
573 // see geventqueue.cpp for implementation
574 
588 GEvent getNextEvent(int mask = ANY_EVENT) /*(deprecated)*/;
589 
600 GMouseEvent waitForClick() /*(deprecated)*/;
601 
644 GEvent waitForEvent(int mask = ANY_EVENT) /*(deprecated)*/;
645 
646 #endif // _gevent_h
Definition: gevent.h:174
Definition: gevent.h:175
virtual bool isCtrlOrCommandKeyDown() const
Returns true if the Ctrl key, or the Command key (Mac), was held down during this event...
Definition: gevent.cpp:218
Definition: gevent.h:202
virtual int getColumn() const
Returns the column that was interacted with, if this is a table event.
Definition: gevent.cpp:129
virtual GInteractor * getInteractor() const
Returns the source interactor that generated this event.
Definition: gevent.cpp:168
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:188
Definition: gevent.h:173
Definition: gevent.h:172
Definition: gevent.h:211
static string typeToString(EventType eventType)
Converts an event type such as MOUSE_EVENT to a string such as "MOUSE_EVENT".
Definition: gevent.cpp:74
Definition: gevent.h:184
virtual GPoint getLocation() const
Returns an (x, y) point representing the mouse position within the interactor when this event occurre...
Definition: gevent.cpp:176
A GEvent represents a user action that has occurred on a graphical interactor.
Definition: gevent.h:153
virtual double getX() const
Returns the x-coordinate of the mouse position within the interactor when this event occurred...
Definition: gevent.cpp:196
virtual int getModifiers() const
Returns the modifiers active during this event.
Definition: gevent.cpp:156
Definition: gevent.h:176
A GObservable object is one that is able to send out events.
Definition: gobservable.h:36
Definition: gevent.h:197
Definition: gevent.h:195
virtual void ignore()
Instructs the GUI system to ignore or cancel this event.
Definition: gevent.cpp:204
Definition: gevent.h:194
Definition: gevent.h:209
virtual int getButton() const
Returns which mouse button was clicked, if this is a mouse event.
Definition: gevent.cpp:121
Definition: gevent.h:201
virtual char getKeyChar() const
Returns the key character that was typed, if this is a key event.
Definition: gevent.cpp:148
Definition: gevent.h:187
Definition: gevent.h:210
virtual double getY() const
Returns the y-coordinate of the mouse position within the interactor when this event occurred...
Definition: gevent.cpp:200
Definition: gevent.h:188
virtual int getKeyCode() const
Returns the integer key code that was typed, if this is a key event.
Definition: gevent.cpp:152
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
virtual EventType getEventType() const
Returns the event&#39;s type (minor type such as MOUSE_PRESSED).
Definition: gevent.cpp:144
Definition: gevent.h:206
Definition: gevent.h:200
Definition: gevent.h:185
Definition: gevent.h:190
Definition: gevent.h:205
Definition: gevent.h:180
virtual bool isMiddleClick() const
Returns true if the user pressed the middle mouse button.
Definition: gevent.cpp:230
Definition: gevent.h:181
Definition: gevent.h:198
virtual EventClass getEventClass() const
Returns this event&#39;s class (major type such as MOUSE_EVENT).
Definition: gevent.cpp:140
Definition: gevent.h:207
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:30
Definition: gevent.h:193
KeyCode
Definition: gevent.h:171
virtual EventType getType() const
Returns the event&#39;s type (major type such as MOUSE_EVENT).
Definition: gevent.cpp:192
virtual bool isMetaKeyDown() const
Returns true if the Meta/Command key was held down during this event.
Definition: gevent.cpp:238
virtual int getRow() const
Returns the row that was interacted with, if this is a table event.
Definition: gevent.cpp:180
Definition: gevent.h:178
Definition: gevent.h:182
Definition: gevent.h:191
virtual string getName() const
Returns this event&#39;s name such as "click" or "keydown" or "actionperformed".
Definition: gevent.cpp:160
virtual string getActionCommand() const
Returns the action command associated with the event.
Definition: gevent.cpp:117
static GEventListener LOG_EVENT
An event listener that just prints the event that occurred.
Definition: gevent.h:164
virtual string toString() const
Returns a text representation of the event for debugging.
Definition: gevent.cpp:484
static string keyCodeToString(int keyCode)
Converts a key code such as 67 into a string such as "A".
Definition: gevent.cpp:246
virtual ~GEvent()
Frees memory allocated internally by the event.
Definition: gevent.cpp:52
Definition: gevent.h:204
static GEventListener EMPTY_EVENT_LISTENER
An empty event handler that can be passed that does nothing.
Definition: gevent.h:158
Definition: gevent.h:177
Definition: gevent.h:208
virtual string getRequestURL() const
Returns this event&#39;s request URL, if this is a server URL event.
Definition: gevent.cpp:164
virtual bool isLeftClick() const
Returns true if the user pressed the left mouse button.
Definition: gevent.cpp:226
virtual bool isRightClick() const
Returns true if the user pressed the right mouse button.
Definition: gevent.cpp:234
Definition: gevent.h:189
Definition: gevent.h:192
Definition: gevent.h:186
virtual QEvent * getInternalEvent() const
Returns the Qt event being wrapped by this event, if any.
Definition: gevent.cpp:172
Definition: gevent.h:196
virtual bool isDoubleClick() const
Returns true if the user pressed the mouse button multiple times.
Definition: gevent.cpp:222
virtual GObservable * getSource() const
Returns the source object that generated this event.
Definition: gevent.cpp:184
virtual EventClass getClass() const
Returns this event&#39;s class (major type such as MOUSE_EVENT).
Definition: gevent.cpp:125
Definition: gevent.h:179
Definition: gevent.h:199
This struct contains real-valued x and y fields.
Definition: gtypes.h:198
Definition: gevent.h:183
Definition: gevent.h:203
virtual bool isAltKeyDown() const
Returns true if the Alt key was held down during this event.
Definition: gevent.cpp:210
virtual bool isShiftKeyDown() const
Returns true if the Shift key was held down during this event.
Definition: gevent.cpp:242
virtual bool isCtrlKeyDown() const
Returns true if the Ctrl key was held down during this event.
Definition: gevent.cpp:214