SGL
gtextarea.h
1 /*
2  * File: gtextarea.h
3  * -----------------
4  *
5  * @author Marty Stepp
6  * @version 2019/04/23
7  * - moved some event listener code to GInteractor superclass
8  * @version 2018/09/10
9  * - added doc comments for new documentation generation
10  * @version 2018/08/23
11  * - renamed to gtextarea.h to replace Java version
12  * @version 2018/06/25
13  * - initial version
14  */
15 
16 
17 #ifndef _gtextarea_h
18 #define _gtextarea_h
19 
20 #include <string>
21 #include <QTextEdit>
22 
23 #include "ginteractor.h"
24 
25 class _Internal_QTextEdit;
26 
33 class GTextArea : public GInteractor {
34 public:
40  GTextArea(int rows, int columns, QWidget* parent = nullptr);
41 
45  GTextArea(const string& text = "", QWidget* parent = nullptr);
46 
50  ~GTextArea() override;
51 
56  virtual void appendFormattedText(const string& text, const string& color = "", const string& font = "");
57 
61  virtual void appendHtml(const string& html);
62 
66  virtual void appendText(const string& text);
67 
71  virtual void clearSelection();
72 
76  virtual void clearText();
77 
81  virtual int getColumns() const;
82 
87  virtual int getCursorPosition() const;
88 
93  virtual string getHtml() const;
94 
95  /* @inherit */
96  _Internal_QWidget* getInternalWidget() const override;
97 
104  virtual string getPlaceholder() const;
105 
111  virtual int getRows() const;
112 
117  virtual string getSelectedText() const;
118 
124  virtual int getSelectionEnd() const;
125 
130  virtual int getSelectionLength() const;
131 
137  virtual int getSelectionStart() const;
138 
142  virtual string getText() const;
143 
144  /* @inherit */
145  string getType() const override;
146 
147  /* @inherit */
148  QWidget* getWidget() const override;
149 
156  virtual bool isContextMenuEnabled() const;
157 
162  virtual bool isEditable() const;
163 
168  virtual bool isLineWrap() const;
169 
174  virtual void moveCursorToEnd();
175 
180  virtual void moveCursorToStart();
181 
186  virtual void removeTextChangeListener();
187 
192  virtual void scrollToBottom();
193 
198  virtual void scrollToTop();
199 
206  virtual void select(int startIndex, int length);
207 
211  virtual void selectAll();
212 
218  virtual void setColumns(int columns);
219 
225  virtual void setContextMenuEnabled(bool enabled);
226 
232  virtual void setCursorPosition(int index, bool keepAnchor = false);
233 
238  virtual void setEditable(bool value);
239 
245  virtual void setHtml(const string& html);
246 
251  virtual void setLineWrap(bool wrap);
252 
258  void setMouseListener(GEventListener func) override;
259 
265  void setMouseListener(GEventListenerVoid func) override;
266 
273  virtual void setPlaceholder(const string& text);
274 
280  virtual void setRows(int rows);
281 
287  virtual void setRowsColumns(int rows, int columns);
288 
293  virtual void setText(const string& text);
294 
309  virtual void setTextChangeListener(GEventListener func);
310 
325  virtual void setTextChangeListener(GEventListenerVoid func);
326 
327 private:
328  Q_DISABLE_COPY(GTextArea)
329 
330  _Internal_QTextEdit* _iqtextedit;
331  bool _contextMenuEnabled;
332 
333  // helper used by getRows, getColumns, etc.
334  GDimension getRowColumnSize() const;
335 
336  friend class _Internal_QTextEdit;
337 };
338 
343 class _Internal_QTextEdit : public QTextEdit, public _Internal_QWidget {
344  Q_OBJECT
345 
346 public:
347  _Internal_QTextEdit(GTextArea* gtextArea, QWidget* parent = nullptr);
348  void contextMenuEvent(QContextMenuEvent* event) override;
349  void detach() override;
350  void keyPressEvent(QKeyEvent* event) override;
351  void keyReleaseEvent(QKeyEvent* event) override;
352  void mousePressEvent(QMouseEvent* event) override;
353  void mouseReleaseEvent(QMouseEvent* event) override;
354  QSize sizeHint() const override;
355 
356 public slots:
357  void handleScroll(int value);
358  void handleTextChange();
359 
360 private:
361  GTextArea* _gtextarea;
362 
363  friend class GTextArea;
364 };
365 
366 #endif // _gtextarea_h
virtual void setColumns(int columns)
Sets the width of the text area to be wide enough to fit the given number of characters (columns) of ...
Definition: gtextarea.cpp:255
This struct contains real-valued width and height fields.
Definition: gtypes.h:39
virtual void setTextChangeListener(GEventListener func)
Sets a text change listener on this text area so that it will be called when the user modifies the cu...
Definition: gtextarea.cpp:331
virtual int getRows() const
Returns the number of visible rows (lines tall) in the text area.
Definition: gtextarea.cpp:131
virtual void scrollToBottom()
Moves the visible scroll region of the text area so that the bottom part of the text is visible...
Definition: gtextarea.cpp:222
virtual void appendText(string text)
Adds the given plain text to the end of the text area.
Definition: gtextarea.cpp:81
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gtextarea.cpp:180
virtual int getSelectionLength() const
Returns the number of characters that are currently selected.
Definition: gtextarea.cpp:158
virtual string getSelectedText() const
Returns the text that is currently selected in the text area.
Definition: gtextarea.cpp:135
virtual void setHtml(string html)
Sets the text area&#39;s current text to the given HTML string.
Definition: gtextarea.cpp:282
virtual int getColumns() const
Returns the number of visible columns (characters wide) in the text area.
Definition: gtextarea.cpp:102
virtual void clearText()
Sets the text in the text area to be empty.
Definition: gtextarea.cpp:96
virtual void setText(string text)
Sets the text area&#39;s current text to the given string, replacing any existing text.
Definition: gtextarea.cpp:325
virtual int getSelectionStart() const
Returns the index of the start of the current selection of text as a 0-based character index within t...
Definition: gtextarea.cpp:165
virtual void select(int startIndex, int length)
Sets the given range of text to be selected, beginning with the given start index as a 0-based charac...
Definition: gtextarea.cpp:238
GTextArea(int rows, int columns, QWidget* parent=nullptr)
Creates a new text area large enough to display the given number of rows and columns of text...
Definition: gtextarea.cpp:25
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
virtual void setCursorPosition(int index, bool keepAnchor=false)
Moves the keyboard cursor to the given 0-based character index within the text.
Definition: gtextarea.cpp:266
virtual int getSelectionEnd() const
Returns the index just past the end of the current selection of text as a 0-based character index wit...
Definition: gtextarea.cpp:146
virtual bool isContextMenuEnabled() const
Returns true if a context menu will pop up when the user right-clicks the text area.
Definition: gtextarea.cpp:188
virtual int getCursorPosition() const
Returns the keyboard cursor&#39;s current position in the text area as a 0-based character index within t...
Definition: gtextarea.cpp:106
A GTextArea is a multi-line editable text box.
Definition: gtextarea.h:33
virtual void removeTextChangeListener()
Removes the text change listener from this text area so that it will no longer call it when the user ...
Definition: gtextarea.cpp:218
void setMouseListener(GEventListener func) override
Sets a mouse listener on this text area so that it will be called when the user moves or clicks the m...
Definition: gtextarea.cpp:294
virtual void moveCursorToStart()
Sets the text area&#39;s keyboard cursor position to the start of the current text.
Definition: gtextarea.cpp:209
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextarea.cpp:184
virtual void clearSelection()
Deselects any text that is currently selected in the text area.
Definition: gtextarea.cpp:88
virtual string getText() const
Returns the text area&#39;s current text.
Definition: gtextarea.cpp:176
virtual void setPlaceholder(string text)
Sets the text area&#39;s placeholder text, which is usually displayed as a light gray text in the text ar...
Definition: gtextarea.cpp:304
virtual bool isLineWrap() const
Returns whether the text area wraps its text when a line becomes too long.
Definition: gtextarea.cpp:196
virtual bool isEditable() const
Returns whether the text area allows the user to modify its text.
Definition: gtextarea.cpp:192
virtual void setRows(int rows)
Sets the height of the text area to be wide enough to fit the given number of lines (rows) of text...
Definition: gtextarea.cpp:310
virtual void scrollToTop()
Moves the visible scroll region of the text area so that the top part of the text is visible...
Definition: gtextarea.cpp:230
virtual void selectAll()
Selects the entire text of the text area.
Definition: gtextarea.cpp:249
~GTextArea() override
Frees memory allocated internally by the text area.
Definition: gtextarea.cpp:43
virtual string getHtml() const
Returns the text area&#39;s current text as HTML.
Definition: gtextarea.cpp:110
virtual void setLineWrap(bool wrap)
Sets whether the text area wraps its text when a line becomes too long.
Definition: gtextarea.cpp:288
virtual void setEditable(bool value)
Sets whether the text area allows the user to modify its text.
Definition: gtextarea.cpp:276
virtual void appendHtml(string html)
Adds HTML-formatted text to the end of the text area.
Definition: gtextarea.cpp:76
virtual void moveCursorToEnd()
Sets the text area&#39;s keyboard cursor position to the end of the current text.
Definition: gtextarea.cpp:200
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextarea.cpp:114
virtual void appendFormattedText(string text, string color="", string font="")
Adds formatted text to the end of the text area.
Definition: gtextarea.cpp:49
virtual void setRowsColumns(int rows, int columns)
Sets the size of the text area to be wide enough to fit the given number of lines (rows) and characte...
Definition: gtextarea.cpp:317
virtual string getPlaceholder() const
Returns the text area&#39;s placeholder text, which is usually displayed as a light gray text in the text...
Definition: gtextarea.cpp:118
virtual void setContextMenuEnabled(bool enabled)
Sets whether a context menu will pop up when the user right-clicks the text area. ...
Definition: gtextarea.cpp:262