StanfordCPPLib
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 #include "private/init.h" // ensure that Stanford C++ lib is initialized
17 
18 #ifndef INTERNAL_INCLUDE
19 // signal that GUI system is in use (so it will be initialized)
20 #define SPL_QT_GUI_IN_USE 1
21 #include "private/initstudent.h" // insert necessary included code by student
22 #endif // INTERNAL_INCLUDE
23 
24 #ifndef _gevent_h
25 #define _gevent_h
26 
27 #include <functional>
28 #include <iostream>
29 #include <string>
30 #include <QEvent>
31 
32 #define INTERNAL_INCLUDE 1
33 #include "gtypes.h"
34 #undef INTERNAL_INCLUDE
35 
36 class GEvent;
37 class GInteractor;
38 class GObservable;
39 class _Internal_QCanvas;
40 class _Internal_QCheckBox;
41 class _Internal_QPushButton;
42 class _Internal_QWidget;
43 
45 typedef std::function<void(GEvent)> GEventListener;
46 
48 typedef std::function<void()> GEventListenerVoid;
49 
53 enum EventClass {
54  NULL_EVENT = 0x0000,
55  ACTION_EVENT = 0x0010,
56  KEY_EVENT = 0x0020,
57  TIMER_EVENT = 0x0040,
58  WINDOW_EVENT = 0x0080,
59  MOUSE_EVENT = 0x0100,
60  CLICK_EVENT = 0x0200,
61  TABLE_EVENT = 0x0400,
62  SERVER_EVENT = 0x0800,
63  CHANGE_EVENT = 0x1000,
64  HYPERLINK_EVENT = 0x2000,
65  SCROLL_EVENT = 0x4000,
66  ANY_EVENT = ACTION_EVENT | KEY_EVENT | TIMER_EVENT | WINDOW_EVENT
67  | MOUSE_EVENT | CLICK_EVENT | TABLE_EVENT | SERVER_EVENT
68  | CHANGE_EVENT | HYPERLINK_EVENT | SCROLL_EVENT
69 };
70 // Note: If you add any new classes of events, you must also add logic to the
71 // GEvent::classToString function in gevent.cpp.
72 
73 
78 enum EventType {
79  NULL_TYPE = 0,
80 
81  WINDOW_CLOSED = WINDOW_EVENT + 1,
82  WINDOW_RESIZED = WINDOW_EVENT + 2,
83  CONSOLE_CLOSED = WINDOW_EVENT + 3,
84  WINDOW_CLOSING = WINDOW_EVENT + 4,
85  WINDOW_MINIMIZED = WINDOW_EVENT + 5,
86  WINDOW_RESTORED = WINDOW_EVENT + 6,
87  WINDOW_MAXIMIZED = WINDOW_EVENT + 7,
88 
89  ACTION_PERFORMED = ACTION_EVENT + 1,
90  ACTION_MENU = ACTION_EVENT + 2,
91 
92  MOUSE_CLICKED = MOUSE_EVENT + 1,
93  MOUSE_PRESSED = MOUSE_EVENT + 2,
94  MOUSE_RELEASED = MOUSE_EVENT + 3,
95  MOUSE_MOVED = MOUSE_EVENT + 4,
96  MOUSE_DRAGGED = MOUSE_EVENT + 5,
97  MOUSE_ENTERED = MOUSE_EVENT + 6,
98  MOUSE_EXITED = MOUSE_EVENT + 7,
99  MOUSE_WHEEL_DOWN = MOUSE_EVENT + 8,
100  MOUSE_WHEEL_UP = MOUSE_EVENT + 9,
101  MOUSE_DOUBLE_CLICKED = MOUSE_EVENT + 10,
102 
103  KEY_PRESSED = KEY_EVENT + 1,
104  KEY_RELEASED = KEY_EVENT + 2,
105  KEY_TYPED = KEY_EVENT + 3,
106 
107  TIMER_TICKED = TIMER_EVENT + 1,
108 
109  TABLE_UPDATED = TABLE_EVENT + 1, // when a cell's value gets set
110  TABLE_SELECTED = TABLE_EVENT + 2, // cursor moves onto a cell
111  TABLE_EDIT_BEGIN = TABLE_EVENT + 3, // user presses F2 or double clicks to start editing a cell
112  TABLE_REPLACE_BEGIN = TABLE_EVENT + 4, // user starts typing on a cell; like TABLE_EDIT_BEGIN but wipes out previous value
113  TABLE_EDIT_CANCEL = TABLE_EVENT + 5, // user presses Esc or otherwise stops editing a cell
114  TABLE_CUT = TABLE_EVENT + 6, // user cuts cell value to clipboard
115  TABLE_COPY = TABLE_EVENT + 7, // user copies cell value to clipboard
116  TABLE_PASTE = TABLE_EVENT + 8, // user pastes cell value from clipboard
117 
118  SERVER_REQUEST = SERVER_EVENT + 1,
119 
120  STATE_CHANGED = CHANGE_EVENT + 1,
121 
122  HYPERLINK_CLICKED = HYPERLINK_EVENT + 1,
123 
124  SCROLL_SCROLLED = SCROLL_EVENT + 1
125 };
126 // Note: If you add any new classes of events, you must also add logic to the
127 // GEvent::typeToString function in gevent.cpp.
128 
133 enum Modifier {
134  SHIFT_DOWN = 1 << 0,
135  CTRL_DOWN = 1 << 1,
136  META_DOWN = 1 << 2,
137  ALT_DOWN = 1 << 3,
138  ALT_GRAPH_DOWN = 1 << 4,
139  BUTTON1_DOWN = 1 << 5,
140  BUTTON2_DOWN = 1 << 6,
141  BUTTON3_DOWN = 1 << 7
142 };
143 
162 class GEvent {
163 public:
167  static GEventListener EMPTY_EVENT_LISTENER;
168 
173  static GEventListener LOG_EVENT;
174 
175  /*
176  * Type: KeyCode
177  * -------------
178  * This type defines the names of the key codes returned in a key event.
179  */
180  enum KeyCode {
182  TAB_KEY = 9,
183  ENTER_KEY = 10,
184  CLEAR_KEY = 12,
186  SHIFT_KEY = Qt::Key_Shift,
187  CTRL_KEY = Qt::Key_Control,
188  ALT_KEY = Qt::Key_Alt,
189  PAUSE_KEY = 19,
192  PAGE_UP_KEY = Qt::Key_PageUp,
193  PAGE_DOWN_KEY = Qt::Key_PageDown,
194  END_KEY = Qt::Key_End,
195  HOME_KEY = Qt::Key_Home,
196  LEFT_ARROW_KEY = Qt::Key_Left,
197  UP_ARROW_KEY = Qt::Key_Up,
198  RIGHT_ARROW_KEY = Qt::Key_Right,
199  DOWN_ARROW_KEY = Qt::Key_Down,
200  F1_KEY = Qt::Key_F1,
201  F2_KEY = Qt::Key_F2,
202  F3_KEY = Qt::Key_F3,
203  F4_KEY = Qt::Key_F4,
204  F5_KEY = Qt::Key_F5,
205  F6_KEY = Qt::Key_F6,
206  F7_KEY = Qt::Key_F7,
207  F8_KEY = Qt::Key_F8,
208  F9_KEY = Qt::Key_F9,
209  F10_KEY = Qt::Key_F10,
210  F11_KEY = Qt::Key_F11,
211  F12_KEY = Qt::Key_F12,
212  DELETE_KEY = 127,
213  NUM_LOCK_KEY = Qt::Key_NumLock,
214  SCROLL_LOCK_KEY = Qt::Key_ScrollLock,
215  PRINT_SCREEN_KEY = Qt::Key_Print,
216  INSERT_KEY = Qt::Key_Insert,
217  HELP_KEY = Qt::Key_Help,
218  META_KEY = Qt::Key_Meta,
219  WINDOWS_KEY = Qt::Key_Super_L,
220  MENU_KEY = Qt::Key_Menu
221  };
222 
226  GEvent(EventClass eventClass = NULL_EVENT,
227  EventType eventType = NULL_TYPE,
228  const string& eventName = "",
229  GObservable* source = nullptr);
230 
234  virtual ~GEvent();
235 
241  static string classToString(EventClass eventClass);
242 
248  virtual string getActionCommand() const;
249 
254  virtual int getButton() const;
255 
260  virtual EventClass getClass() const;
261 
266  virtual int getColumn() const;
267 
274  static long getCurrentTimeMS();
275 
280  virtual EventClass getEventClass() const;
281 
286  virtual EventType getEventType() const;
287 
291  virtual GInteractor* getInteractor() const;
292 
297  virtual QEvent* getInternalEvent() const;
298 
303  virtual char getKeyChar() const;
304 
310  virtual int getKeyCode() const;
311 
317  virtual GPoint getLocation() const;
318 
323  virtual int getModifiers() const;
324 
328  virtual string getName() const;
329 
334  virtual string getRequestURL() const;
335 
340  virtual int getRow() const;
341 
345  virtual GObservable* getSource() const;
346 
351  virtual long getTime() const;
352 
357  virtual EventType getType() const;
358 
364  virtual double getX() const;
365 
371  virtual double getY() const;
372 
378  virtual void ignore();
379 
384  virtual bool isAltKeyDown() const;
385 
390  virtual bool isCtrlKeyDown() const;
391 
397  virtual bool isCtrlOrCommandKeyDown() const;
398 
403  virtual bool isDoubleClick() const;
404 
409  virtual bool isLeftClick() const;
410 
418  virtual bool isMiddleClick() const;
419 
424  virtual bool isRightClick() const;
425 
430  virtual bool isMetaKeyDown() const;
431 
436  virtual bool isShiftKeyDown() const;
437 
442  static string keyCodeToString(int keyCode);
443 
447  virtual void setActionCommand(const string& actionCommand);
448 
452  virtual void setButton(int button);
453 
457  virtual void setInternalEvent(QEvent* event);
458 
462  virtual void setKeyChar(char keyChar);
463 
467  virtual void setKeyChar(const string& keyCharString);
468 
472  virtual void setKeyCode(int keyCode);
473 
477  virtual void setModifiers(Qt::KeyboardModifiers modifiers);
478 
482  virtual void setRequestURL(const string& requestUrl);
483 
487  virtual void setRowAndColumn(int row, int col);
488 
492  virtual void setSource(GObservable* source);
493 
497  virtual void setX(double x);
498 
502  virtual void setY(double y);
503 
507  virtual string toString() const;
508 
513  static string typeToString(EventType eventType);
514 
515 private:
516  /*
517  * Represents the two types of event listeners.
518  */
519  enum EventListenerType {
520  HANDLER_EVENT,
521  HANDLER_VOID
522  };
523 
524  /*
525  * A wrapper that can hold either of the two types of event listeners.
526  */
527  struct EventListenerWrapper {
528  GEventListener handler;
529  GEventListenerVoid handlerVoid;
530  EventListenerType type;
531 
532  void fireEvent(const GEvent& event) {
533  if (type == HANDLER_EVENT) {
534  handler(event);
535  } else {
536  handlerVoid();
537  }
538  }
539  };
540 
541  // member variables
542  string _actionCommand;
543  int _button;
544  EventClass _class;
545  char _keyChar;
546  int _keyCode;
547  int _modifiers;
548  string _name;
549  string _requestUrl;
550  GObservable* _source;
551  long _time;
552  EventType _type;
553  double _x;
554  double _y;
555  int _row;
556  int _col;
557  QEvent* _internalQtEvent;
558 
559  friend class GInteractor;
560  friend class GObservable;
561  friend class _Internal_QWidget;
562 };
563 
567 std::ostream& operator <<(std::ostream& out, const GEvent& event);
568 
569 // alias GEvent to all event types
570 typedef GEvent GActionEvent;
571 typedef GEvent GChangeEvent;
572 typedef GEvent GHyperlinkEvent;
573 typedef GEvent GKeyEvent;
574 typedef GEvent GMouseEvent;
575 typedef GEvent GScrollEvent;
576 typedef GEvent GServerEvent;
577 typedef GEvent GTableEvent;
578 typedef GEvent GTimerEvent;
579 typedef GEvent GWindowEvent;
580 
581 // global functions for backward compatibility
582 // see geventqueue.cpp for implementation
583 
597 GEvent getNextEvent(int mask = ANY_EVENT) /*(deprecated)*/;
598 
609 GMouseEvent waitForClick() /*(deprecated)*/;
610 
653 GEvent waitForEvent(int mask = ANY_EVENT) /*(deprecated)*/;
654 
655 #endif // _gevent_h
Definition: gevent.h:183
Definition: gevent.h:184
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
Definition: gevent.h:211
virtual int getColumn() const
Returns the column that was interacted with, if this is a table event.
Definition: gevent.cpp:133
virtual GInteractor * getInteractor() const
Returns the source interactor that generated this event.
Definition: gevent.cpp:172
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:182
Definition: gevent.h:181
Definition: gevent.h:220
static string typeToString(EventType eventType)
Converts an event type such as MOUSE_EVENT to a string such as "MOUSE_EVENT".
Definition: gevent.cpp:78
Definition: gevent.h:193
virtual GPoint getLocation() const
Returns an (x, y) point representing the mouse position within the interactor when this event occurre...
Definition: gevent.cpp:180
A GEvent represents a user action that has occurred on a graphical interactor.
Definition: gevent.h:162
virtual double getX() const
Returns the x-coordinate of the mouse position within the interactor when this event occurred...
Definition: gevent.cpp:200
virtual int getModifiers() const
Returns the modifiers active during this event.
Definition: gevent.cpp:160
Definition: gevent.h:185
A GObservable object is one that is able to send out events.
Definition: gobservable.h:54
Definition: gevent.h:206
Definition: gevent.h:204
virtual void ignore()
Instructs the GUI system to ignore or cancel this event.
Definition: gevent.cpp:208
Definition: gevent.h:203
Definition: gevent.h:218
virtual int getButton() const
Returns which mouse button was clicked, if this is a mouse event.
Definition: gevent.cpp:125
Definition: gevent.h:210
virtual char getKeyChar() const
Returns the key character that was typed, if this is a key event.
Definition: gevent.cpp:152
Definition: gevent.h:196
This class contains real-valued x and y fields.
Definition: gtypes.h:216
Definition: gevent.h:219
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:197
virtual int getKeyCode() const
Returns the integer key code that was typed, if this is a key event.
Definition: gevent.cpp:156
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:53
virtual EventType getEventType() const
Returns the event&#39;s type (minor type such as MOUSE_PRESSED).
Definition: gevent.cpp:148
Definition: gevent.h:215
Definition: gevent.h:209
Definition: gevent.h:194
Definition: gevent.h:199
Definition: gevent.h:214
Definition: gevent.h:189
virtual bool isMiddleClick() const
Returns true if the user pressed the middle mouse button.
Definition: gevent.cpp:234
Definition: gevent.h:190
Definition: gevent.h:207
virtual EventClass getEventClass() const
Returns this event&#39;s class (major type such as MOUSE_EVENT).
Definition: gevent.cpp:144
Definition: gevent.h:216
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:202
KeyCode
Definition: gevent.h:180
virtual EventType getType() const
Returns the event&#39;s type (major type such as MOUSE_EVENT).
Definition: gevent.cpp:196
virtual bool isMetaKeyDown() const
Returns true if the Meta/Command key was held down during this event.
Definition: gevent.cpp:242
virtual int getRow() const
Returns the row that was interacted with, if this is a table event.
Definition: gevent.cpp:184
Definition: gevent.h:187
Definition: gevent.h:191
Definition: gevent.h:200
virtual string getName() const
Returns this event&#39;s name such as "click" or "keydown" or "actionperformed".
Definition: gevent.cpp:164
virtual string getActionCommand() const
Returns the action command associated with the event.
Definition: gevent.cpp:121
static GEventListener LOG_EVENT
An event listener that just prints the event that occurred.
Definition: gevent.h:173
virtual string toString() const
Returns a text representation of the event for debugging.
Definition: gevent.cpp:488
static string keyCodeToString(int keyCode)
Converts a key code such as 67 into a string such as "A".
Definition: gevent.cpp:250
virtual ~GEvent()
Frees memory allocated internally by the event.
Definition: gevent.cpp:56
Definition: gevent.h:213
static GEventListener EMPTY_EVENT_LISTENER
An empty event handler that can be passed that does nothing.
Definition: gevent.h:167
Definition: gevent.h:186
Definition: gevent.h:217
virtual string getRequestURL() const
Returns this event&#39;s request URL, if this is a server URL event.
Definition: gevent.cpp:168
virtual bool isLeftClick() const
Returns true if the user pressed the left mouse button.
Definition: gevent.cpp:230
virtual bool isRightClick() const
Returns true if the user pressed the right mouse button.
Definition: gevent.cpp:238
Definition: gevent.h:198
Definition: gevent.h:201
Definition: gevent.h:195
virtual QEvent * getInternalEvent() const
Returns the Qt event being wrapped by this event, if any.
Definition: gevent.cpp:176
Definition: gevent.h:205
virtual bool isDoubleClick() const
Returns true if the user pressed the mouse button multiple times.
Definition: gevent.cpp:226
virtual GObservable * getSource() const
Returns the source object that generated this event.
Definition: gevent.cpp:188
virtual EventClass getClass() const
Returns this event&#39;s class (major type such as MOUSE_EVENT).
Definition: gevent.cpp:129
Definition: gevent.h:188
Definition: gevent.h:208
Definition: gevent.h:192
Definition: gevent.h:212
virtual bool isAltKeyDown() const
Returns true if the Alt key was held down during this event.
Definition: gevent.cpp:214
virtual bool isShiftKeyDown() const
Returns true if the Shift key was held down during this event.
Definition: gevent.cpp:246
virtual bool isCtrlKeyDown() const
Returns true if the Ctrl key was held down during this event.
Definition: gevent.cpp:218