SGL
gtextarea.h
1 /*
2  * File: gtextarea.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 listener code to GInteractor superclass
10  * @version 2018/09/10
11  * - added doc comments for new documentation generation
12  * @version 2018/08/23
13  * - renamed to gtextarea.h to replace Java version
14  * @version 2018/06/25
15  * - initial version
16  */
17 
18 
19 #ifndef _gtextarea_h
20 #define _gtextarea_h
21 
22 #include <string>
23 #include <QTextEdit>
24 
25 #include "ginteractor.h"
26 
27 namespace sgl {
28 
29 class _Internal_QTextEdit;
30 
37 class GTextArea : public GInteractor {
38 public:
44  GTextArea(int rows, int columns, QWidget* parent = nullptr);
45 
49  GTextArea(const string& text = "", QWidget* parent = nullptr);
50 
54  ~GTextArea() override;
55 
60  virtual void appendFormattedText(const string& text, const string& color = "", const string& font = "");
61 
65  virtual void appendHtml(const string& html);
66 
70  virtual void appendText(const string& text);
71 
75  virtual void clearSelection();
76 
80  virtual void clearText();
81 
85  virtual int getColumns() const;
86 
91  virtual int getCursorPosition() const;
92 
97  virtual string getHtml() const;
98 
99  /* @inherit */
100  _Internal_QWidget* getInternalWidget() const override;
101 
108  virtual string getPlaceholder() const;
109 
115  virtual int getRows() const;
116 
121  virtual string getSelectedText() const;
122 
128  virtual int getSelectionEnd() const;
129 
134  virtual int getSelectionLength() const;
135 
141  virtual int getSelectionStart() const;
142 
146  virtual string getText() const;
147 
148  /* @inherit */
149  string getType() const override;
150 
151  /* @inherit */
152  QWidget* getWidget() const override;
153 
160  virtual bool isContextMenuEnabled() const;
161 
166  virtual bool isEditable() const;
167 
172  virtual bool isLineWrap() const;
173 
178  virtual void moveCursorToEnd();
179 
184  virtual void moveCursorToStart();
185 
190  virtual void removeTextChangeListener();
191 
196  virtual void scrollToBottom();
197 
202  virtual void scrollToTop();
203 
210  virtual void select(int startIndex, int length);
211 
215  virtual void selectAll();
216 
222  virtual void setColumns(int columns);
223 
229  virtual void setContextMenuEnabled(bool enabled);
230 
236  virtual void setCursorPosition(int index, bool keepAnchor = false);
237 
242  virtual void setEditable(bool value);
243 
249  virtual void setHtml(const string& html);
250 
255  virtual void setLineWrap(bool wrap);
256 
262  void setMouseListener(GEventListener func) override;
263 
269  void setMouseListener(GEventListenerVoid func) override;
270 
277  virtual void setPlaceholder(const string& text);
278 
284  virtual void setRows(int rows);
285 
291  virtual void setRowsColumns(int rows, int columns);
292 
297  virtual void setText(const string& text);
298 
313  virtual void setTextChangeListener(GEventListener func);
314 
329  virtual void setTextChangeListener(GEventListenerVoid func);
330 
331 private:
332  Q_DISABLE_COPY(GTextArea)
333 
334  _Internal_QTextEdit* _iqtextedit;
335  bool _contextMenuEnabled;
336 
337  // helper used by getRows, getColumns, etc.
338  GDimension getRowColumnSize() const;
339 
340  friend class _Internal_QTextEdit;
341 };
342 
347 class _Internal_QTextEdit : public QTextEdit, public _Internal_QWidget {
348  Q_OBJECT
349 
350 public:
351  _Internal_QTextEdit(GTextArea* gtextArea, QWidget* parent = nullptr);
352  void contextMenuEvent(QContextMenuEvent* event) override;
353  void detach() override;
354  void keyPressEvent(QKeyEvent* event) override;
355  void keyReleaseEvent(QKeyEvent* event) override;
356  void mousePressEvent(QMouseEvent* event) override;
357  void mouseReleaseEvent(QMouseEvent* event) override;
358  QSize sizeHint() const override;
359 
360 public slots:
361  void handleScroll(int value);
362  void handleTextChange();
363 
364 private:
365  GTextArea* _gtextarea;
366 
367  friend class GTextArea;
368 };
369 
370 } // namespace sgl
371 
372 #endif // _gtextarea_h
virtual void selectAll()
Selects the entire text of the text area.
Definition: gtextarea.cpp:253
virtual int getRows() const
Returns the number of visible rows (lines tall) in the text area.
Definition: gtextarea.cpp:135
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:314
virtual void clearText()
Sets the text in the text area to be empty.
Definition: gtextarea.cpp:100
virtual void appendHtml(string html)
Adds HTML-formatted text to the end of the text area.
Definition: gtextarea.cpp:80
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextarea.cpp:188
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:169
virtual string getHtml() const
Returns the text area&#39;s current text as HTML.
Definition: gtextarea.cpp:114
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:222
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:270
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:242
std::function< void(GEvent)> GEventListener
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:38
virtual bool isLineWrap() const
Returns whether the text area wraps its text when a line becomes too long.
Definition: gtextarea.cpp:200
Definition: console.h:45
virtual void setEditable(bool value)
Sets whether the text area allows the user to modify its text.
Definition: gtextarea.cpp:280
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
virtual void appendFormattedText(string text, string color="", string font="")
Adds formatted text to the end of the text area.
Definition: gtextarea.cpp:53
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:321
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gtextarea.cpp:184
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextarea.cpp:118
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:234
~GTextArea() override
Frees memory allocated internally by the text area.
Definition: gtextarea.cpp:47
std::function< void()> GEventListenerVoid
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:44
virtual string getText() const
Returns the text area&#39;s current text.
Definition: gtextarea.cpp:180
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:110
This struct contains real-valued width and height fields.
Definition: gtypes.h:43
virtual int getSelectionLength() const
Returns the number of characters that are currently selected.
Definition: gtextarea.cpp:162
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:335
virtual void setContextMenuEnabled(bool enabled)
Sets whether a context menu will pop up when the user right-clicks the text area. ...
Definition: gtextarea.cpp:266
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:29
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:308
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:226
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:150
virtual void appendText(string text)
Adds the given plain text to the end of the text area.
Definition: gtextarea.cpp:85
virtual void moveCursorToEnd()
Sets the text area&#39;s keyboard cursor position to the end of the current text.
Definition: gtextarea.cpp:204
A GTextArea is a multi-line editable text box.
Definition: gtextarea.h:37
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:298
virtual void moveCursorToStart()
Sets the text area&#39;s keyboard cursor position to the start of the current text.
Definition: gtextarea.cpp:213
virtual void clearSelection()
Deselects any text that is currently selected in the text area.
Definition: gtextarea.cpp:92
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:259
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:122
virtual bool isEditable() const
Returns whether the text area allows the user to modify its text.
Definition: gtextarea.cpp:196
virtual void setHtml(string html)
Sets the text area&#39;s current text to the given HTML string.
Definition: gtextarea.cpp:286
virtual int getColumns() const
Returns the number of visible columns (characters wide) in the text area.
Definition: gtextarea.cpp:106
virtual void setLineWrap(bool wrap)
Sets whether the text area wraps its text when a line becomes too long.
Definition: gtextarea.cpp:292
virtual void setText(string text)
Sets the text area&#39;s current text to the given string, replacing any existing text.
Definition: gtextarea.cpp:329
virtual string getSelectedText() const
Returns the text that is currently selected in the text area.
Definition: gtextarea.cpp:139
virtual bool isContextMenuEnabled() const
Returns true if a context menu will pop up when the user right-clicks the text area.
Definition: gtextarea.cpp:192