SGL
glabel.h
1 /*
2  * File: glabel.h
3  * --------------
4  *
5  * @author Marty Stepp
6  * @version 2019/04/23
7  * - moved some event-handling code to GInteractor superclass
8  * @version 2019/04/22
9  * - added setIcon with QIcon and QPixmap
10  * @version 2018/10/04
11  * - added get/setWordWrap
12  * @version 2018/09/07
13  * - added doc comments for new documentation generation
14  * @version 2018/09/04
15  * - added double-click event support
16  * @version 2018/09/03
17  * - added addActionListener methods for clickable labels
18  * @version 2018/08/23
19  * - renamed to glabel.h to replace Java version
20  * @version 2018/06/25
21  * - initial version
22  */
23 
24 
25 #ifndef _glabel_h
26 #define _glabel_h
27 
28 #include <string>
29 #include <QLabel>
30 
31 #include "ginteractor.h"
32 #include "gobjects.h"
33 
34 // forward declaration
35 class _Internal_QLabel;
36 class GWindow;
37 
46 class GLabel : public GInteractor {
47 public:
51  GLabel(const string& text = "", const string& iconFileName = "", QWidget* parent = nullptr);
52 
56  GLabel(const string& text, const QIcon& icon, QWidget* parent = nullptr);
57 
61  GLabel(const string& text, const QPixmap& icon, QWidget* parent = nullptr);
62 
66  ~GLabel() override;
67 
68  /* @inherit */
69  _Internal_QWidget* getInternalWidget() const override;
70 
75  virtual string getLabel() const;
76 
81  virtual string getText() const;
82 
89 
90  /* @inherit */
91  string getType() const override;
92 
93  /* @inherit */
94  QWidget* getWidget() const override;
95 
100  virtual bool isWordWrap() const;
101 
102  /* @inherit */
103  void setBounds(double x, double y, double width, double height) override;
104 
105  /* @inherit */
106  void setBounds(const GRectangle& size) override;
107 
108  /* @inherit */
109  void setColor(int rgb) override;
110 
111  /* @inherit */
112  void setColor(const string& color) override;
113 
114  /* @inherit */
115  void setFont(const QFont& font) override;
116 
117  /* @inherit */
118  void setFont(const string& font) override;
119 
120  /* @inherit */
121  void setForeground(int rgb) override;
122 
123  /* @inherit */
124  void setForeground(const string& color) override;
125 
126  /* @inherit */
127  void setHeight(double height) override;
128 
129  /* @inherit */
130  void setIcon(const QIcon& icon) override;
131 
132  /* @inherit */
133  void setIcon(const QPixmap& icon) override;
134 
135  /* @inherit */
136  void setIcon(const string& filename, bool retainIconSize = true) override;
137 
142  virtual void setLabel(const string& text);
143 
144  /* @inherit */
145  void setLocation(double x, double y) override;
146 
147  /* @inherit */
148  void setSize(double width, double height) override;
149 
150  /* @inherit */
151  void setSize(const GDimension& size) override;
152 
157  virtual void setText(const string& text);
158 
163  virtual void setTextPosition(GInteractor::TextPosition position);
164 
165  /* @inherit */
166  void setVisible(bool visible) override;
167 
168  /* @inherit */
169  void setWidth(double width) override;
170 
175  virtual void setWordWrap(bool wrap);
176 
177  /* @inherit */
178  void setX(double x) override;
179 
180  /* @inherit */
181  void setY(double y) override;
182 
183 private:
184  Q_DISABLE_COPY(GLabel)
185  _Internal_QLabel* _iqlabel;
186  GText* _gtext;
187 
188  void ensureGText();
189  GText* getGText() const;
190  bool hasGText() const;
191 
192  friend class _Internal_QLabel;
193  friend class GWindow;
194 };
195 
196 // alias GTextLabel for GLabel for backward compatibility
197 typedef GLabel GTextLabel;
198 
199 
204 class _Internal_QLabel : public QLabel, public _Internal_QWidget {
205  Q_OBJECT
206 
207 public:
208  _Internal_QLabel(GLabel* glabel, QWidget* parent = nullptr);
209  void detach() override;
210  QSize sizeHint() const override;
211 
212 signals:
213  void clicked();
214  void doubleClicked();
215 
216 protected:
217  void mouseDoubleClickEvent(QMouseEvent* e) override;
218  void mousePressEvent(QMouseEvent* event) override;
219 
220 private:
221  GLabel* _glabel;
222 
223  friend class GLabel;
224 };
225 
226 #endif // _glabel_h
void setFont(const QFont &font) override
Sets the font used by this widget to the given Qt font.
Definition: glabel.cpp:152
void setVisible(bool visible) override
Returns true if the interactor is visible on the screen.
Definition: glabel.cpp:287
This struct contains real-valued width and height fields.
Definition: gtypes.h:39
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: glabel.cpp:81
void setForeground(int rgb) override
Sets the foreground/text color of the interactor to the color represented by the given RGB integer...
Definition: glabel.cpp:166
void setSize(double width, double height) override
Sets the onscreen width and height of the interactor in pixels.
Definition: glabel.cpp:254
void setBounds(double x, double y, double width, double height) override
Sets the size and location of the widget.
Definition: glabel.cpp:124
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: glabel.cpp:108
virtual void setLabel(string text)
Sets the text on the label to be the given text.
Definition: glabel.cpp:244
This graphical object subclass represents a text string.
Definition: gobjects.h:1442
virtual bool isWordWrap() const
Returns whether the label should wrap if its text is too long.
Definition: glabel.cpp:120
void setX(double x) override
Sets the onscreen x-coordinate of the top-left corner of the interactor relative to its window...
Definition: glabel.cpp:306
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
~GLabel() override
Frees memory allocated internally by the label.
Definition: glabel.cpp:64
virtual string getText() const
Returns the string displayed by the label.
Definition: glabel.cpp:89
void setColor(int rgb) override
Sets the foreground/text color of the interactor to the color represented by the given RGB integer...
Definition: glabel.cpp:138
void setLocation(double x, double y) override
Sets the onscreen x/y-coordinate of the top-left corner of the interactor relative to its window...
Definition: glabel.cpp:248
void setIcon(const QIcon &icon) override
Sets the icon associated with this interactor.
Definition: glabel.cpp:186
void setWidth(double width) override
Sets the onscreen width of the interactor in pixels.
Definition: glabel.cpp:294
GLabel(string text="", string iconFileName="", QWidget* parent=nullptr)
Creates a label with the specified text label and optional icon.
Definition: glabel.cpp:32
virtual string getLabel() const
Returns the string displayed by the label.
Definition: glabel.cpp:85
This class represents a graphics window that supports simple graphics.
Definition: gwindow.h:98
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: glabel.cpp:112
virtual void setText(string text)
Sets the text on the label to be the given text.
Definition: glabel.cpp:266
void setY(double y) override
Sets the onscreen y-coordinate of the top-left corner of the interactor relative to its window...
Definition: glabel.cpp:312
virtual void setWordWrap(bool wrap)
Sets whether the label should wrap if its text is too long.
Definition: glabel.cpp:300
virtual GInteractor::TextPosition getTextPosition() const
Returns the label&#39;s text position relative to its icon.
Definition: glabel.cpp:93
This struct contains real-valued x, y, width, and height fields.
Definition: gtypes.h:289
virtual void setTextPosition(GInteractor::TextPosition position)
Sets the label&#39;s text position relative to its icon.
Definition: glabel.cpp:276
A GLabel represents a text string.
Definition: glabel.h:46
TextPosition
The places where an interactor can place its text relative to its icon.
Definition: ginteractor.h:53
void setHeight(double height) override
Sets the onscreen height of the interactor in pixels.
Definition: glabel.cpp:180