SGL
ginteractor.h
1 /*
2  * File: ginteractor.h
3  * -------------------
4  *
5  * @author Marty Stepp
6  * @version 2019/04/23
7  * - added set/removeActionListener
8  * - added set/removeClickListener
9  * - added set/removeDoubleClickListener
10  * - added set/removeKeyListener
11  * - added set/removeMouseListener
12  * @version 2019/04/22
13  * - added setIcon with QIcon and QPixmap
14  * @version 2018/09/20
15  * - added read/write lock to avoid race conditions
16  * @version 2018/09/05
17  * - added getContainer and is/setVisible logic
18  * @version 2018/08/23
19  * - renamed to ginteractor.h to replace Java version
20  * @version 2018/06/25
21  * - initial version
22  */
23 
24 
25 #ifndef _ginteractor_h
26 #define _ginteractor_h
27 
28 #include <string>
29 #include <QReadWriteLock>
30 #include <QWidget>
31 
32 #include "gevent.h"
33 #include "gobservable.h"
34 
35 class QPixmap;
36 class QIcon;
37 
38 class GContainer;
39 class GDiffGui;
40 class GWindow;
41 class _Internal_QWidget;
42 
48 class GInteractor : public GObservable {
49 public:
53  enum TextPosition {
57  };
58 
64  GInteractor();
65 
69  virtual ~GInteractor();
70 
77  virtual void addActionListener() (deprecated); // does nothing
78 
84  bool eventsEnabled() const override;
85 
91  virtual std::string getAccelerator() const;
92 
98  virtual std::string getActionCommand() const;
99 
104  virtual std::string getBackground() const;
105 
110  virtual int getBackgroundInt() const;
111 
115  virtual GRectangle getBounds() const;
116 
122  virtual std::string getColor() const;
123 
129  virtual int getColorInt() const;
130 
141  virtual GContainer* getContainer() const;
142 
148  virtual std::string getFont() const;
149 
155  virtual std::string getForeground() const;
156 
162  virtual int getForegroundInt() const;
163 
167  virtual double getHeight() const;
168 
174  virtual int getID() const;
175 
181  virtual std::string getIcon() const;
182 
189  virtual _Internal_QWidget* getInternalWidget() const = 0;
190 
195  virtual GPoint getLocation() const;
196 
201  virtual double getMinimumHeight() const;
202 
207  virtual GDimension getMinimumSize() const;
208 
213  virtual double getMinimumWidth() const;
214 
222  virtual char getMnemonic() const (deprecated);
223 
230  virtual std::string getName() const;
231 
236  virtual double getPreferredHeight() const;
237 
243  virtual GDimension getPreferredSize() const;
244 
249  virtual double getPreferredWidth() const;
250 
255  virtual GDimension getSize() const;
256 
263  std::string getType() const override = 0;
264 
271  virtual QWidget* getWidget() const = 0;
272 
276  virtual double getWidth() const;
277 
282  virtual double getX() const;
283 
288  virtual double getY() const;
289 
293  virtual bool inBounds(double x, double y) const;
294 
298  virtual bool inBounds(int x, int y) const;
299 
305  virtual bool isEnabled() const;
306 
312  virtual bool isVisible() const;
313 
318  virtual void removeActionListener();
319 
324  virtual void removeClickListener();
325 
330  virtual void removeDoubleClickListener();
331 
336  virtual void removeKeyListener();
337 
342  virtual void removeMouseListener();
343 
347  virtual void requestFocus();
348 
354  virtual void setAccelerator(const std::string& accelerator);
355 
366  virtual void setActionCommand(const std::string& actionCommand);
367 
374  virtual void setActionListener(GEventListener func);
375 
382  virtual void setActionListener(GEventListenerVoid func);
383 
389  virtual void setBackground(int rgb);
390 
396  virtual void setBackground(const std::string& color);
397 
404  virtual void setBounds(double x, double y, double width, double height);
405 
412  virtual void setBounds(const GRectangle& size);
413 
419  virtual void setClickListener(GEventListener func);
420 
426  virtual void setClickListener(GEventListenerVoid func);
427 
434  virtual void setColor(int rgb);
435 
442  virtual void setColor(const std::string& color);
443 
449  virtual void setDoubleClickListener(GEventListener func);
450 
456  virtual void setDoubleClickListener(GEventListenerVoid func);
457 
463  virtual void setEnabled(bool value);
464 
469  virtual void setFont(const QFont& font);
470 
476  virtual void setFont(const std::string& font);
477 
484  virtual void setForeground(int rgb);
485 
492  virtual void setForeground(const std::string& color);
493 
498  virtual void setHeight(double height);
499 
505  virtual void setIcon(const QIcon& icon);
506 
512  virtual void setIcon(const QPixmap& icon);
513 
522  virtual void setIcon(const std::string& filename, bool retainIconSize = true);
523 
529  virtual void setKeyListener(GEventListener func);
530 
536  virtual void setKeyListener(GEventListenerVoid func);
537 
544  virtual void setLocation(double x, double y);
545 
551  virtual void setMinimumSize(double width, double height);
552 
558  virtual void setMinimumSize(const GDimension& size);
559 
567  virtual void setMnemonic(char mnemonic) (deprecated);
568 
574  virtual void setMouseListener(GEventListener func);
575 
581  virtual void setMouseListener(GEventListenerVoid func);
582 
589  virtual void setName(const std::string& name);
590 
599  virtual void setPreferredHeight(double height);
600 
609  virtual void setPreferredSize(double width, double height);
610 
611 
617  virtual void setPreferredSize(const GDimension& size);
618 
619 
625  virtual void setPreferredWidth(double width);
626 
631  virtual void setSize(double width, double height);
632 
637  virtual void setSize(const GDimension& size);
638 
644  virtual void setTooltip(const std::string& tooltipText);
645 
653  virtual void setVisible(bool visible);
654 
659  virtual void setWidth(double width);
660 
667  virtual void setX(double x);
668 
675  virtual void setY(double y);
676 
677 protected:
681  std::string _actionCommand;
682 
686  std::string _actionEventType;
687 
691  std::string _icon;
692 
696  std::string _name;
697 
701  int _id;
702 
706  GContainer* _container;
707 
711  QReadWriteLock _lock; // avoid thread race conditions
712 
716  static QWidget* getInternalParent(QWidget* parent);
717 
721  virtual std::string getActionEventType() const;
722 
726  virtual std::string getDefaultInteractorName() const;
727 
731  virtual void lockForRead();
732 
736  virtual void lockForReadConst() const;
737 
741  virtual void lockForWrite();
742 
746  virtual void lockForWriteConst() const;
747 
751  static std::string normalizeAccelerator(const std::string& accelerator);
752 
756  virtual void setContainer(GContainer* container);
757 
761  virtual void unlock();
762 
766  virtual void unlockConst() const;
767 
768  friend class GContainer;
769  friend class GDiffGui;
770  friend class GWindow;
771  friend class _Internal_QWidget;
772 
773 private:
774  static int _interactorCount;
775 };
776 
777 
785 class _Internal_QWidget {
786 public:
787  _Internal_QWidget();
788  virtual ~_Internal_QWidget();
789  virtual void detach();
790  virtual QSize getMinimumSize() const;
791  virtual bool hasMinimumSize() const;
792  virtual QSize getPreferredSize() const;
793  virtual bool hasPreferredSize() const;
794  virtual void setMinimumSize(double width, double height);
795  virtual void setMinimumSize(const QSize& size);
796  virtual void setPreferredSize(double width, double height);
797  virtual void setPreferredSize(const QSize& size);
798 
799 private:
800  GDimension _minimumSize;
801  GDimension _preferredSize;
802 
803  friend class GInteractor;
804 };
805 
812 template <typename T>
813 class GGenericInteractor : public GInteractor {
814 public:
815  GGenericInteractor(T* widget)
816  : _widget(widget) {
817  _iqwidget = new _Internal_QWidget(); // dummy
818  }
819 
820  _Internal_QWidget* getInternalWidget() const override {
821  return _iqwidget;
822  }
823 
824  string getType() const override {
825  string typeName = typeid(T).name();
826  return string("GGenericInteractor<") + typeName + ">";
827  }
828 
829  QWidget* getWidget() const override {
830  return _widget;
831  }
832 
833 private:
834  _Internal_QWidget* _iqwidget;
835  T* _widget;
836 };
837 
838 #endif // _ginteractor_h
virtual GRectangle getBounds() const
Returns a rectangle representing the x/y position and size of this interactor.
Definition: ginteractor.cpp:85
This struct contains real-valued width and height fields.
Definition: gtypes.h:39
Definition: ginteractor.h:54
virtual void removeClickListener()
Removes the click listener from this interactor so that it will no longer call it when events occur...
Definition: ginteractor.cpp:248
virtual void setPreferredSize(double width, double height)
Sets the width and height in pixels that this interactor would prefer to be.
Definition: ginteractor.cpp:521
virtual double getX() const
Returns the x-coordinate of the top-left pixel of this interactor within its onscreen window...
Definition: ginteractor.cpp:193
virtual void setPreferredWidth(double width)
Sets the width in pixels that this interactor would prefer to be.
Definition: ginteractor.cpp:535
virtual double getHeight() const
Returns the current onscreen height of this interactor in pixels.
Definition: ginteractor.cpp:121
virtual string getActionCommand() const
Returns an action command for this interactor, which is a semi-unique string you can use to identify ...
Definition: ginteractor.cpp:67
Definition: ginteractor.h:55
virtual GDimension getMinimumSize() const
Returns a GDimension structure representing the minimum size in pixels that this interactor will perm...
Definition: ginteractor.cpp:145
virtual double getY() const
Returns the y-coordinate of the top-left pixel of this interactor within its onscreen window...
Definition: ginteractor.cpp:197
virtual void setMinimumSize(double width, double height)
Sets the minimum size in pixels that this interactor will permit itself to be resized to...
Definition: ginteractor.cpp:472
virtual void setTooltip(string tooltipText)
Sets a "tooltip" that will appear if the user hovers their mouse over the interactor.
Definition: ginteractor.cpp:554
virtual void removeDoubleClickListener()
Removes the double-click listener from this interactor so that it will no longer call it when events ...
Definition: ginteractor.cpp:252
STL namespace.
virtual void addActionListener() (deprecated)
Adds an event listener to be notified when this interactor is clicked or generally interacted with...
Definition: ginteractor.cpp:54
virtual GDimension getPreferredSize() const
Returns a GDimension structure storing the width and height in pixels that this interactor would pref...
Definition: ginteractor.cpp:171
A GObservable object is one that is able to send out events.
Definition: gobservable.h:36
virtual GDimension getSize() const
Returns a GDimension structure storing the current onscreen width and height of this interactor in pi...
Definition: ginteractor.cpp:185
virtual void setForeground(int rgb)
Sets the foreground/text color of the interactor to the color represented by the given RGB integer...
Definition: ginteractor.cpp:399
virtual void setEnabled(bool value)
Sets whether this interactor is currently enabled.
Definition: ginteractor.cpp:393
virtual string getFont() const
Returns the font of this interactor&#39;s text as a font string such as "Helvetica-12-Bold".
Definition: ginteractor.cpp:107
virtual ~GInteractor()
Frees memory allocated internally by the interactor.
Definition: ginteractor.cpp:50
virtual void setName(string name)
Sets a string representing a unique name for this interactor.
Definition: ginteractor.cpp:512
GInteractor()
Initializes a newly created interactor.
Definition: ginteractor.cpp:38
virtual GContainer * getContainer() const
Returns a pointer to the onscreen container holding this interactor.
Definition: ginteractor.cpp:99
virtual void setAccelerator(string accelerator)
Sets an accelerator hotkey for this interactor, such as "Ctrl-S".
Definition: ginteractor.cpp:284
virtual bool isEnabled() const
Returns true if this interactor is currently enabled.
Definition: ginteractor.cpp:209
virtual int getBackgroundInt() const
Returns the background color of the interactor as an RGB integer.
Definition: ginteractor.cpp:80
virtual int getColorInt() const
Returns the foreground/text color of the interactor as an RGB integer.
Definition: ginteractor.cpp:94
virtual double getMinimumHeight() const
Returns the minimum height in pixels that this interactor will permit itself to be resized to...
Definition: ginteractor.cpp:141
virtual void setSize(double width, double height)
Sets the onscreen width and height of the interactor in pixels.
Definition: ginteractor.cpp:539
virtual bool isVisible() const
Returns true if the interactor is visible on the screen.
Definition: ginteractor.cpp:213
virtual int getID() const
Returns a globally unique identifier for this interactor, which is set when the interactor is constru...
Definition: ginteractor.cpp:125
virtual void setY(double y)
Sets the onscreen y-coordinate of the top-left corner of the interactor relative to its window...
Definition: ginteractor.cpp:579
bool eventsEnabled() const override
Returns true if this interactor is currently accepting events.
Definition: ginteractor.cpp:58
virtual void setBounds(double x, double y, double width, double height)
Sets the size and location of the widget.
Definition: ginteractor.cpp:345
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
virtual void setHeight(double height)
Sets the onscreen height of the interactor in pixels.
Definition: ginteractor.cpp:433
virtual void setActionListener(GEventListener func)
Sets an action listener on this interactor so that it will be called when it is interacted with in it...
Definition: ginteractor.cpp:288
virtual void removeMouseListener()
Removes the mouse listener from this interactor so that it will no longer call it when events occur...
Definition: ginteractor.cpp:262
string getType() const override=0
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
virtual void setLocation(double x, double y)
Sets the onscreen x/y-coordinate of the top-left corner of the interactor relative to its window...
Definition: ginteractor.cpp:466
virtual double getWidth() const
Returns the current onscreen width of this interactor in pixels.
Definition: ginteractor.cpp:189
virtual void setDoubleClickListener(GEventListener func)
Sets a mouse listener on this interactor so that it will be called when the mouse is double-clicked o...
Definition: ginteractor.cpp:385
virtual string getColor() const
Returns the foreground/text color of the interactor as a string.
Definition: ginteractor.cpp:89
virtual void setIcon(const QIcon &icon)
Sets the icon associated with this interactor.
Definition: ginteractor.cpp:440
virtual void setWidth(double width)
Sets the onscreen width of the interactor in pixels.
Definition: ginteractor.cpp:569
virtual void setClickListener(GEventListener func)
Sets a mouse listener on this interactor so that it will be called when the mouse is clicked on it...
Definition: ginteractor.cpp:356
virtual string getAccelerator() const
Returns a string representing a hotkey for this interactor, or an empty string if no accelerator has ...
Definition: ginteractor.cpp:62
virtual void setActionCommand(string actionCommand)
Sets the action command for this interactor.
Definition: ginteractor.cpp:280
virtual double getMinimumWidth() const
Returns the minimum width in pixels that this interactor will permit itself to be resized to...
Definition: ginteractor.cpp:150
virtual double getPreferredHeight() const
Returns the height in pixels that this interactor would prefer to be, which would exactly fit its con...
Definition: ginteractor.cpp:167
virtual void setMouseListener(GEventListener func)
Sets a mouse listener on this interactor so that it will be called when the mouse is moved or clicked...
Definition: ginteractor.cpp:488
This class represents a graphics window that supports simple graphics.
Definition: gwindow.h:98
virtual int getForegroundInt() const
Returns the foreground/text color of the interactor as an RGB integer.
Definition: ginteractor.cpp:116
virtual void setVisible(bool visible)
Returns true if the interactor is visible on the screen.
Definition: ginteractor.cpp:560
virtual string getIcon() const
Returns the file name of the icon associated with this interactor, or an empty string if no icon has ...
Definition: ginteractor.cpp:129
virtual string getForeground() const
Returns the foreground/text color of the interactor as a string.
Definition: ginteractor.cpp:111
virtual void setPreferredHeight(double height)
Sets the height in pixels that this interactor would prefer to be.
Definition: ginteractor.cpp:517
virtual void removeKeyListener()
Removes the key listener from this interactor so that it will no longer call it when key events occur...
Definition: ginteractor.cpp:256
virtual void setFont(const QFont &font)
Sets the font used by this widget to the given Qt font.
Definition: ginteractor.cpp:423
virtual void requestFocus()
Transfers keyboard focus to this interactor.
Definition: ginteractor.cpp:274
virtual void setKeyListener(GEventListener func)
Sets a key listener on this interactor so that it will be called when the user presses any key...
Definition: ginteractor.cpp:454
virtual void setColor(int rgb)
Sets the foreground/text color of the interactor to the color represented by the given RGB integer...
Definition: ginteractor.cpp:364
virtual double getPreferredWidth() const
Returns the height in pixels that this interactor would prefer to be, which would exactly fit its con...
Definition: ginteractor.cpp:181
friend class GDiffGui
Definition: ginteractor.h:769
virtual string getBackground() const
Returns the background color of the interactor as a string.
Definition: ginteractor.cpp:75
This struct contains real-valued x, y, width, and height fields.
Definition: gtypes.h:289
virtual string getName() const
Returns a string representing a unique name for this interactor.
Definition: ginteractor.cpp:159
TextPosition
The places where an interactor can place its text relative to its icon.
Definition: ginteractor.h:53
Definition: ginteractor.h:56
virtual GPoint getLocation() const
Returns an (x, y) point representing the onscreen location of the top-left corner of this interactor ...
Definition: ginteractor.cpp:133
This struct contains real-valued x and y fields.
Definition: gtypes.h:198
virtual void removeActionListener()
Removes the action listener from this interactor so that it will no longer call it when events occur...
Definition: ginteractor.cpp:244
A GContainer is a logical grouping for interactors.
Definition: gcontainer.h:69
virtual bool inBounds(double x, double y) const
Returns true if the given x/y pixel is within the bounds of this interactor.
Definition: ginteractor.cpp:201
virtual void setBackground(int rgb)
Sets the background color of the interactor to the color represented by the given RGB integer...
Definition: ginteractor.cpp:296
virtual void setX(double x)
Sets the onscreen x-coordinate of the top-left corner of the interactor relative to its window...
Definition: ginteractor.cpp:575