SGL
glabel.h
1 /*
2  * File: glabel.h
3  * --------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/09
7  * - added sgl namespace
8  * @version 2019/04/23
9  * - moved some event-handling code to GInteractor superclass
10  * @version 2019/04/22
11  * - added setIcon with QIcon and QPixmap
12  * @version 2018/10/04
13  * - added get/setWordWrap
14  * @version 2018/09/07
15  * - added doc comments for new documentation generation
16  * @version 2018/09/04
17  * - added double-click event support
18  * @version 2018/09/03
19  * - added addActionListener methods for clickable labels
20  * @version 2018/08/23
21  * - renamed to glabel.h to replace Java version
22  * @version 2018/06/25
23  * - initial version
24  */
25 
26 
27 #ifndef _glabel_h
28 #define _glabel_h
29 
30 #include <string>
31 #include <QLabel>
32 
33 #include "ginteractor.h"
34 #include "gobjects.h"
35 
36 namespace sgl {
37 
38 // forward declaration
39 class _Internal_QLabel;
40 class GWindow;
41 
50 class GLabel : public GInteractor {
51 public:
55  GLabel(const string& text = "", const string& iconFileName = "", QWidget* parent = nullptr);
56 
60  GLabel(const string& text, const QIcon& icon, QWidget* parent = nullptr);
61 
65  GLabel(const string& text, const QPixmap& icon, QWidget* parent = nullptr);
66 
70  ~GLabel() override;
71 
72  /* @inherit */
73  _Internal_QWidget* getInternalWidget() const override;
74 
79  virtual string getLabel() const;
80 
85  virtual string getText() const;
86 
93 
94  /* @inherit */
95  string getType() const override;
96 
97  /* @inherit */
98  QWidget* getWidget() const override;
99 
104  virtual bool isWordWrap() const;
105 
106  /* @inherit */
107  void setBounds(double x, double y, double width, double height) override;
108 
109  /* @inherit */
110  void setBounds(const GRectangle& size) override;
111 
112  /* @inherit */
113  void setColor(int rgb) override;
114 
115  /* @inherit */
116  void setColor(const string& color) override;
117 
118  /* @inherit */
119  void setFont(const QFont& font) override;
120 
121  /* @inherit */
122  void setFont(const string& font) override;
123 
124  /* @inherit */
125  void setForeground(int rgb) override;
126 
127  /* @inherit */
128  void setForeground(const string& color) override;
129 
130  /* @inherit */
131  void setHeight(double height) override;
132 
133  /* @inherit */
134  void setIcon(const QIcon& icon) override;
135 
136  /* @inherit */
137  void setIcon(const QPixmap& icon) override;
138 
139  /* @inherit */
140  void setIcon(const string& filename, bool retainIconSize = true) override;
141 
146  virtual void setLabel(const string& text);
147 
148  /* @inherit */
149  void setLocation(double x, double y) override;
150 
151  /* @inherit */
152  void setSize(double width, double height) override;
153 
154  /* @inherit */
155  void setSize(const GDimension& size) override;
156 
161  virtual void setText(const string& text);
162 
167  virtual void setTextPosition(GInteractor::TextPosition position);
168 
169  /* @inherit */
170  void setVisible(bool visible) override;
171 
172  /* @inherit */
173  void setWidth(double width) override;
174 
179  virtual void setWordWrap(bool wrap);
180 
181  /* @inherit */
182  void setX(double x) override;
183 
184  /* @inherit */
185  void setY(double y) override;
186 
187 private:
188  Q_DISABLE_COPY(GLabel)
189  _Internal_QLabel* _iqlabel;
190  GText* _gtext;
191 
192  void ensureGText();
193  GText* getGText() const;
194  bool hasGText() const;
195 
196  friend class _Internal_QLabel;
197  friend class GWindow;
198 };
199 
200 // alias GTextLabel for GLabel for backward compatibility
202 
203 
208 class _Internal_QLabel : public QLabel, public _Internal_QWidget {
209  Q_OBJECT
210 
211 public:
212  _Internal_QLabel(GLabel* glabel, QWidget* parent = nullptr);
213  void detach() override;
214  QSize sizeHint() const override;
215 
216 signals:
217  void clicked();
218  void doubleClicked();
219 
220 protected:
221  void mouseDoubleClickEvent(QMouseEvent* e) override;
222  void mousePressEvent(QMouseEvent* event) override;
223 
224 private:
225  GLabel* _glabel;
226 
227  friend class GLabel;
228 };
229 
230 } // namespace sgl
231 
232 #endif // _glabel_h
This class represents a graphics window that supports simple graphics.
Definition: gwindow.h:102
void setWidth(double width) override
Sets the onscreen width of the interactor in pixels.
Definition: glabel.cpp:298
This struct contains real-valued x, y, width, and height fields.
Definition: gtypes.h:293
void setFont(const QFont &font) override
Sets the font used by this widget to the given Qt font.
Definition: glabel.cpp:156
void setHeight(double height) override
Sets the onscreen height of the interactor in pixels.
Definition: glabel.cpp:184
TextPosition
The places where an interactor can place its text relative to its icon.
Definition: ginteractor.h:57
A GLabel represents a text string.
Definition: glabel.h:50
void setBounds(double x, double y, double width, double height) override
Sets the size and location of the widget.
Definition: glabel.cpp:128
virtual void setTextPosition(GInteractor::TextPosition position)
Sets the label&#39;s text position relative to its icon.
Definition: glabel.cpp:280
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:252
void setSize(double width, double height) override
Sets the onscreen width and height of the interactor in pixels.
Definition: glabel.cpp:258
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: glabel.cpp:85
GLabel(string text="", string iconFileName="", QWidget* parent=nullptr)
Creates a label with the specified text label and optional icon.
Definition: glabel.cpp:36
virtual void setLabel(string text)
Sets the text on the label to be the given text.
Definition: glabel.cpp:248
virtual string getText() const
Returns the string displayed by the label.
Definition: glabel.cpp:93
virtual void setWordWrap(bool wrap)
Sets whether the label should wrap if its text is too long.
Definition: glabel.cpp:304
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:170
Definition: console.h:45
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
GLabel GTextLabel
Definition: glabel.h:201
void setIcon(const QIcon &icon) override
Sets the icon associated with this interactor.
Definition: glabel.cpp:190
This graphical object subclass represents a text string.
Definition: gobjects.h:1446
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:142
virtual void setText(string text)
Sets the text on the label to be the given text.
Definition: glabel.cpp:270
virtual string getLabel() const
Returns the string displayed by the label.
Definition: glabel.cpp:89
This struct contains real-valued width and height fields.
Definition: gtypes.h:43
virtual GInteractor::TextPosition getTextPosition() const
Returns the label&#39;s text position relative to its icon.
Definition: glabel.cpp:97
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: glabel.cpp:116
void setVisible(bool visible) override
Returns true if the interactor is visible on the screen.
Definition: glabel.cpp:291
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:310
virtual bool isWordWrap() const
Returns whether the label should wrap if its text is too long.
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:112
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:316
~GLabel() override
Frees memory allocated internally by the label.
Definition: glabel.cpp:68