SGL
ginteractor.h
1 /*
2  * File: ginteractor.h
3  * -------------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/09
7  * - added sgl namespace
8  * @version 2019/04/23
9  * - added set/removeActionListener
10  * - added set/removeClickListener
11  * - added set/removeDoubleClickListener
12  * - added set/removeKeyListener
13  * - added set/removeMouseListener
14  * @version 2019/04/22
15  * - added setIcon with QIcon and QPixmap
16  * @version 2018/09/20
17  * - added read/write lock to avoid race conditions
18  * @version 2018/09/05
19  * - added getContainer and is/setVisible logic
20  * @version 2018/08/23
21  * - renamed to ginteractor.h to replace Java version
22  * @version 2018/06/25
23  * - initial version
24  */
25 
26 
27 #ifndef _ginteractor_h
28 #define _ginteractor_h
29 
30 #include <string>
31 #include <QReadWriteLock>
32 #include <QWidget>
33 
34 #include "gevent.h"
35 #include "gobservable.h"
36 
37 class QPixmap;
38 class QIcon;
39 
40 namespace sgl {
41 
42 class GContainer;
43 class GDiffGui;
44 class GWindow;
45 class _Internal_QWidget;
46 
52 class GInteractor : public GObservable {
53 public:
57  enum TextPosition {
61  };
62 
68  GInteractor();
69 
73  virtual ~GInteractor();
74 
81  virtual void addActionListener() (deprecated); // does nothing
82 
88  bool eventsEnabled() const override;
89 
95  virtual std::string getAccelerator() const;
96 
102  virtual std::string getActionCommand() const;
103 
108  virtual std::string getBackground() const;
109 
114  virtual int getBackgroundInt() const;
115 
119  virtual GRectangle getBounds() const;
120 
126  virtual std::string getColor() const;
127 
133  virtual int getColorInt() const;
134 
145  virtual GContainer* getContainer() const;
146 
152  virtual std::string getFont() const;
153 
159  virtual std::string getForeground() const;
160 
166  virtual int getForegroundInt() const;
167 
171  virtual double getHeight() const;
172 
178  virtual int getID() const;
179 
185  virtual std::string getIcon() const;
186 
193  virtual _Internal_QWidget* getInternalWidget() const = 0;
194 
199  virtual GPoint getLocation() const;
200 
205  virtual double getMinimumHeight() const;
206 
211  virtual GDimension getMinimumSize() const;
212 
217  virtual double getMinimumWidth() const;
218 
226  virtual char getMnemonic() const (deprecated);
227 
234  virtual std::string getName() const;
235 
240  virtual double getPreferredHeight() const;
241 
247  virtual GDimension getPreferredSize() const;
248 
253  virtual double getPreferredWidth() const;
254 
259  virtual GDimension getSize() const;
260 
267  std::string getType() const override = 0;
268 
275  virtual QWidget* getWidget() const = 0;
276 
280  virtual double getWidth() const;
281 
286  virtual double getX() const;
287 
292  virtual double getY() const;
293 
297  virtual bool inBounds(double x, double y) const;
298 
302  virtual bool inBounds(int x, int y) const;
303 
309  virtual bool isEnabled() const;
310 
316  virtual bool isVisible() const;
317 
322  virtual void removeActionListener();
323 
328  virtual void removeClickListener();
329 
334  virtual void removeDoubleClickListener();
335 
340  virtual void removeKeyListener();
341 
346  virtual void removeMouseListener();
347 
351  virtual void requestFocus();
352 
358  virtual void setAccelerator(const std::string& accelerator);
359 
370  virtual void setActionCommand(const std::string& actionCommand);
371 
378  virtual void setActionListener(GEventListener func);
379 
386  virtual void setActionListener(GEventListenerVoid func);
387 
393  virtual void setBackground(int rgb);
394 
400  virtual void setBackground(const std::string& color);
401 
408  virtual void setBounds(double x, double y, double width, double height);
409 
416  virtual void setBounds(const GRectangle& size);
417 
423  virtual void setClickListener(GEventListener func);
424 
430  virtual void setClickListener(GEventListenerVoid func);
431 
438  virtual void setColor(int rgb);
439 
446  virtual void setColor(const std::string& color);
447 
453  virtual void setDoubleClickListener(GEventListener func);
454 
460  virtual void setDoubleClickListener(GEventListenerVoid func);
461 
467  virtual void setEnabled(bool value);
468 
473  virtual void setFont(const QFont& font);
474 
480  virtual void setFont(const std::string& font);
481 
488  virtual void setForeground(int rgb);
489 
496  virtual void setForeground(const std::string& color);
497 
502  virtual void setHeight(double height);
503 
509  virtual void setIcon(const QIcon& icon);
510 
516  virtual void setIcon(const QPixmap& icon);
517 
526  virtual void setIcon(const std::string& filename, bool retainIconSize = true);
527 
533  virtual void setKeyListener(GEventListener func);
534 
540  virtual void setKeyListener(GEventListenerVoid func);
541 
548  virtual void setLocation(double x, double y);
549 
555  virtual void setMinimumSize(double width, double height);
556 
562  virtual void setMinimumSize(const GDimension& size);
563 
571  virtual void setMnemonic(char mnemonic) (deprecated);
572 
578  virtual void setMouseListener(GEventListener func);
579 
585  virtual void setMouseListener(GEventListenerVoid func);
586 
593  virtual void setName(const std::string& name);
594 
603  virtual void setPreferredHeight(double height);
604 
613  virtual void setPreferredSize(double width, double height);
614 
615 
621  virtual void setPreferredSize(const GDimension& size);
622 
623 
629  virtual void setPreferredWidth(double width);
630 
635  virtual void setSize(double width, double height);
636 
641  virtual void setSize(const GDimension& size);
642 
648  virtual void setTooltip(const std::string& tooltipText);
649 
657  virtual void setVisible(bool visible);
658 
663  virtual void setWidth(double width);
664 
671  virtual void setX(double x);
672 
679  virtual void setY(double y);
680 
681 protected:
685  std::string _actionCommand;
686 
690  std::string _actionEventType;
691 
695  std::string _icon;
696 
700  std::string _name;
701 
705  int _id;
706 
710  GContainer* _container;
711 
715  QReadWriteLock _lock; // avoid thread race conditions
716 
720  static QWidget* getInternalParent(QWidget* parent);
721 
725  virtual std::string getActionEventType() const;
726 
730  virtual std::string getDefaultInteractorName() const;
731 
735  virtual void lockForRead();
736 
740  virtual void lockForReadConst() const;
741 
745  virtual void lockForWrite();
746 
750  virtual void lockForWriteConst() const;
751 
755  static std::string normalizeAccelerator(const std::string& accelerator);
756 
760  virtual void setContainer(GContainer* container);
761 
765  virtual void unlock();
766 
770  virtual void unlockConst() const;
771 
772  friend class GContainer;
773  friend class GDiffGui;
774  friend class GWindow;
775  friend class _Internal_QWidget;
776 
777 private:
778  static int _interactorCount;
779 };
780 
781 
789 class _Internal_QWidget {
790 public:
791  _Internal_QWidget();
792  virtual ~_Internal_QWidget();
793  virtual void detach();
794  virtual QSize getMinimumSize() const;
795  virtual bool hasMinimumSize() const;
796  virtual QSize getPreferredSize() const;
797  virtual bool hasPreferredSize() const;
798  virtual void setMinimumSize(double width, double height);
799  virtual void setMinimumSize(const QSize& size);
800  virtual void setPreferredSize(double width, double height);
801  virtual void setPreferredSize(const QSize& size);
802 
803 private:
804  GDimension _minimumSize;
805  GDimension _preferredSize;
806 
807  friend class GInteractor;
808 };
809 
816 template <typename T>
817 class GGenericInteractor : public GInteractor {
818 public:
819  GGenericInteractor(T* widget)
820  : _widget(widget) {
821  _iqwidget = new _Internal_QWidget(); // dummy
822  }
823 
824  _Internal_QWidget* getInternalWidget() const override {
825  return _iqwidget;
826  }
827 
828  string getType() const override {
829  string typeName = typeid(T).name();
830  return string("GGenericInteractor<") + typeName + ">";
831  }
832 
833  QWidget* getWidget() const override {
834  return _widget;
835  }
836 
837 private:
838  _Internal_QWidget* _iqwidget;
839  T* _widget;
840 };
841 
842 } // namespace sgl
843 
844 #endif // _ginteractor_h
A GContainer is a logical grouping for interactors.
Definition: gcontainer.h:73
This class represents a graphics window that supports simple graphics.
Definition: gwindow.h:102
bool eventsEnabled() const override
Returns true if this interactor is currently accepting events.
Definition: ginteractor.cpp:63
This struct contains real-valued x, y, width, and height fields.
Definition: gtypes.h:293
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:112
TextPosition
The places where an interactor can place its text relative to its icon.
Definition: ginteractor.h:57
virtual void requestFocus()
Transfers keyboard focus to 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:155
virtual GDimension getMinimumSize() const
Returns a GDimension structure representing the minimum size in pixels that this interactor will perm...
Definition: ginteractor.cpp:150
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:585
virtual void setSize(double width, double height)
Sets the onscreen width and height of the interactor in pixels.
Definition: ginteractor.cpp:545
virtual int getBackgroundInt() const
Returns the background color of the interactor as an RGB integer.
Definition: ginteractor.cpp:85
GInteractor()
Initializes a newly created interactor.
Definition: ginteractor.cpp:43
virtual void setTooltip(string tooltipText)
Sets a "tooltip" that will appear if the user hovers their mouse over the interactor.
Definition: ginteractor.cpp:560
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:405
friend class GDiffGui
Definition: ginteractor.h:773
virtual void setFont(const QFont &font)
Sets the font used by this widget to the given Qt font.
Definition: ginteractor.cpp:429
virtual void removeDoubleClickListener()
Removes the double-click listener from this interactor so that it will no longer call it when events ...
Definition: ginteractor.cpp:258
virtual void setPreferredHeight(double height)
Sets the height in pixels that this interactor would prefer to be.
Definition: ginteractor.cpp:523
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:460
virtual void addActionListener() (deprecated)
Adds an event listener to be notified when this interactor is clicked or generally interacted with...
Definition: ginteractor.cpp:59
virtual int getForegroundInt() const
Returns the foreground/text color of the interactor as an RGB integer.
Definition: ginteractor.cpp:121
virtual void setIcon(const QIcon &icon)
Sets the icon associated with this interactor.
Definition: ginteractor.cpp:446
virtual double getHeight() const
Returns the current onscreen height of this interactor in pixels.
Definition: ginteractor.cpp:126
virtual bool isEnabled() const
Returns true if this interactor is currently enabled.
Definition: ginteractor.cpp:214
virtual string getForeground() const
Returns the foreground/text color of the interactor as a string.
Definition: ginteractor.cpp:116
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:186
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:72
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:362
STL namespace.
virtual GDimension getSize() const
Returns a GDimension structure storing the current onscreen width and height of this interactor in pi...
Definition: ginteractor.cpp:190
virtual void removeMouseListener()
Removes the mouse listener from this interactor so that it will no longer call it when events occur...
Definition: ginteractor.cpp:268
virtual double getMinimumHeight() const
Returns the minimum height in pixels that this interactor will permit itself to be resized to...
Definition: ginteractor.cpp:146
virtual double getWidth() const
Returns the current onscreen width of this interactor in pixels.
Definition: ginteractor.cpp:194
virtual void setHeight(double height)
Sets the onscreen height of the interactor in pixels.
Definition: ginteractor.cpp:439
virtual GDimension getPreferredSize() const
Returns a GDimension structure storing the width and height in pixels that this interactor would pref...
Definition: ginteractor.cpp:176
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
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
virtual void setPreferredWidth(double width)
Sets the width in pixels that this interactor would prefer to be.
Definition: ginteractor.cpp:541
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:134
virtual void setName(string name)
Sets a string representing a unique name for this interactor.
Definition: ginteractor.cpp:518
virtual ~GInteractor()
Frees memory allocated internally by the interactor.
Definition: ginteractor.cpp:55
virtual int getColorInt() const
Returns the foreground/text color of the interactor as an RGB integer.
Definition: ginteractor.cpp:99
virtual string getName() const
Returns a string representing a unique name for this interactor.
Definition: ginteractor.cpp:164
string getType() const override=0
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
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:172
virtual void setBackground(int rgb)
Sets the background color of the interactor to the color represented by the given RGB integer...
Definition: ginteractor.cpp:302
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:494
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:581
virtual void setEnabled(bool value)
Sets whether this interactor is currently enabled.
Definition: ginteractor.cpp:399
virtual GPoint getLocation() const
Returns an (x, y) point representing the onscreen location of the top-left corner of this interactor ...
Definition: ginteractor.cpp:138
std::function< void()> GEventListenerVoid
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:44
virtual GRectangle getBounds() const
Returns a rectangle representing the x/y position and size of this interactor.
Definition: ginteractor.cpp:90
virtual bool isVisible() const
Returns true if the interactor is visible on the screen.
Definition: ginteractor.cpp:218
virtual GContainer * getContainer() const
Returns a pointer to the onscreen container holding this interactor.
Definition: ginteractor.cpp:104
A GObservable object is one that is able to send out events.
Definition: gobservable.h:40
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:262
virtual string getBackground() const
Returns the background color of the interactor as a string.
Definition: ginteractor.cpp:80
This struct contains real-valued width and height fields.
Definition: gtypes.h:43
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:391
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:206
Definition: ginteractor.h:59
virtual double getY() const
Returns the y-coordinate of the top-left pixel of this interactor within its onscreen window...
Definition: ginteractor.cpp:202
virtual int getID() const
Returns a globally unique identifier for this interactor, which is set when the interactor is constru...
Definition: ginteractor.cpp:130
virtual void setPreferredSize(double width, double height)
Sets the width and height in pixels that this interactor would prefer to be.
Definition: ginteractor.cpp:527
virtual void setAccelerator(string accelerator)
Sets an accelerator hotkey for this interactor, such as "Ctrl-S".
Definition: ginteractor.cpp:290
Definition: ginteractor.h:60
virtual void setBounds(double x, double y, double width, double height)
Sets the size and location of the widget.
Definition: ginteractor.cpp:351
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:294
virtual void setActionCommand(string actionCommand)
Sets the action command for this interactor.
Definition: ginteractor.cpp:286
virtual double getX() const
Returns the x-coordinate of the top-left pixel of this interactor within its onscreen window...
Definition: ginteractor.cpp: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:250
This struct contains real-valued x and y fields.
Definition: gtypes.h:202
virtual string getAccelerator() const
Returns a string representing a hotkey for this interactor, or an empty string if no accelerator has ...
Definition: ginteractor.cpp:67
Definition: ginteractor.h:58
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:370
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:478
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:472
virtual void setVisible(bool visible)
Returns true if the interactor is visible on the screen.
Definition: ginteractor.cpp:566
virtual void setWidth(double width)
Sets the onscreen width of the interactor in pixels.
Definition: ginteractor.cpp:575
virtual void removeClickListener()
Removes the click listener from this interactor so that it will no longer call it when events occur...
Definition: ginteractor.cpp:254
virtual string getColor() const
Returns the foreground/text color of the interactor as a string.
Definition: ginteractor.cpp:94