SGL
gtextfield.h
1 /*
2  * File: gtextfield.h
3  * ------------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/03
7  * - removed dependency on custom collections
8  * @version 2019/04/23
9  * - added key events
10  * @version 2018/09/08
11  * - added doc comments for new documentation generation
12  * @version 2018/08/23
13  * - renamed to gtextfield.h to replace Java version
14  * @version 2018/06/29
15  * - added textChange event
16  * @version 2018/06/25
17  * - initial version
18  */
19 
20 
21 #ifndef _gtextfield_h
22 #define _gtextfield_h
23 
24 #include <initializer_list>
25 #include <string>
26 #include <QLineEdit>
27 #include <QSpinBox>
28 #include <QDoubleSpinBox>
29 #include <QString>
30 
31 #include "ginteractor.h"
32 
33 class _Internal_QLineEdit;
34 class _Internal_QSpinBox;
35 class _Internal_QDoubleSpinBox;
36 
41 class GTextField : public GInteractor {
42 public:
46  enum InputType {
50  };
51 
57  GTextField(const string& text = "", int charsWide = 0, QWidget* parent = nullptr);
58 
62  GTextField(int charsWide, QWidget* parent = nullptr);
63 
70  GTextField(int value, int min, int max, int step = 1, QWidget* parent = nullptr);
71 
78  GTextField(double value, double min, double max, double step, QWidget* parent = nullptr);
79 
83  ~GTextField() override;
84 
89  virtual int getCharsWide() const;
90 
97  virtual InputType getInputType() const;
98 
99  /* @inherit */
100  _Internal_QWidget* getInternalWidget() const override;
101 
106  virtual int getMaxLength() const;
107 
113  virtual string getPlaceholder() const;
114 
119  virtual string getText() const;
120 
121  /* @inherit */
122  string getType() const override;
123 
128  virtual string getValue() const;
129 
137  virtual bool getValueAsBool() const;
138 
144  virtual char getValueAsChar() const;
145 
153  virtual double getValueAsDouble() const;
154 
163  virtual int getValueAsInt() const;
164 
173  virtual int getValueAsInteger() const;
174 
175  /* @inherit */
176  QWidget* getWidget() const override;
177 
184  virtual bool isAutocompleteEnabled() const;
185 
191  virtual bool isEditable() const;
192 
197  virtual void removeTextChangeListener();
198 
207  virtual void setAutocompleteList(std::initializer_list<string> strings);
208 
217  virtual void setAutocompleteList(const std::vector<string>& strings);
218 
224  virtual void setAutocompleteEnabled(bool enabled);
225 
230  virtual void setCharsWide(int charsWide);
231 
236  virtual void setEditable(bool value);
237 
241  virtual void setMaxLength(int maxLength);
242 
248  virtual void setPlaceholder(const string& text);
249 
253  virtual void setText(const string& text);
254 
260  virtual void setTextChangeListener(GEventListener func);
261 
267  virtual void setTextChangeListener(GEventListenerVoid func);
268 
273  virtual void setValue(bool value);
274 
279  virtual void setValue(char value);
280 
285  virtual void setValue(double value);
286 
291  virtual void setValue(int value);
292 
298  virtual void setValue(const string& value);
299 
305  virtual bool valueIsBool() const;
306 
313  virtual bool valueIsChar() const;
314 
321  virtual bool valueIsDouble() const;
322 
329  virtual bool valueIsInt() const;
330 
337  virtual bool valueIsInteger() const;
338 
345  virtual bool valueIsReal() const;
346 
347 protected:
351  string getActionEventType() const override;
352 
353 private:
354  Q_DISABLE_COPY(GTextField)
355 
356  // pointers to the internal Qt text field;
357  // at most one of these will be non-null for a given instance
358  _Internal_QLineEdit* _iqlineedit;
359  _Internal_QSpinBox* _iqspinbox;
360  _Internal_QDoubleSpinBox* _iqdoublespinbox;
361 
362  // type of text field; helps tell us which of the above internal Qt widgets
363  // will be non-null
364  InputType _inputType;
365 
366  friend class _Internal_QLineEdit;
367  friend class _Internal_QSpinBox;
368  friend class _Internal_QDoubleSpinBox;
369 };
370 
375 class _Internal_QLineEdit : public QLineEdit, public _Internal_QWidget {
376  Q_OBJECT
377 
378 public:
379  _Internal_QLineEdit(GTextField* gtextField, QWidget* parent = nullptr);
380  void detach() override;
381  void keyPressEvent(QKeyEvent* event) override;
382  void keyReleaseEvent(QKeyEvent* event) override;
383  QSize sizeHint() const override;
384 
385 public slots:
386  void handleTextChange(const QString&);
387 
388 private:
389  GTextField* _gtextfield;
390 
391  friend class GTextField;
392 };
393 
398 class _Internal_QSpinBox : public QSpinBox, public _Internal_QWidget {
399  Q_OBJECT
400 
401 public:
402  _Internal_QSpinBox(GTextField* qgtextField, int min, int max, int step = 1, QWidget* parent = nullptr);
403  void detach() override;
404  void keyPressEvent(QKeyEvent* event) override;
405  void keyReleaseEvent(QKeyEvent* event) override;
406  virtual QLineEdit* lineEdit() const;
407  QSize sizeHint() const override;
408 
409 public slots:
410  void handleTextChange(const QString&);
411 
412 private:
413  GTextField* _gtextfield;
414 
415  friend class GTextField;
416 };
417 
422 class _Internal_QDoubleSpinBox : public QDoubleSpinBox, public _Internal_QWidget {
423  Q_OBJECT
424 
425 public:
426  _Internal_QDoubleSpinBox(GTextField* qgtextField, double min, double max, double step = 0.1, QWidget* parent = nullptr);
427  void detach() override;
428  void keyPressEvent(QKeyEvent* event) override;
429  void keyReleaseEvent(QKeyEvent* event) override;
430  virtual QLineEdit* lineEdit() const;
431  QSize sizeHint() const override;
432 
433 public slots:
434  void handleTextChange(const QString&);
435 
436 private:
437  GTextField* _gtextfield;
438 
439  friend class GTextField;
440 };
441 
442 #endif // _gtextfield_h
virtual void setCharsWide(int charsWide)
Sets the width of this text field to be exactly wide enough to display the given number of characters...
Definition: gtextfield.cpp:283
virtual bool valueIsInteger() const
Returns true if the currently typed value in the text field can be interpreted as an integer...
Definition: gtextfield.cpp:389
virtual string getText() const
Returns the text field&#39;s current text.
Definition: gtextfield.cpp:159
virtual char getValueAsChar() const
Returns the currently typed value in the text field as a char value.
Definition: gtextfield.cpp:182
virtual string getValue() const
Returns the text field&#39;s current text.
Definition: gtextfield.cpp:173
virtual bool getValueAsBool() const
Returns the currently typed value in the text field, interpreted as a bool value of true or false...
Definition: gtextfield.cpp:177
This interactor subclass represents a text field for entering short text strings. ...
Definition: gtextfield.h:41
InputType
Constants for the valid types of text field input.
Definition: gtextfield.h:46
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextfield.cpp:120
virtual void setEditable(bool value)
Sets whether the value in the text box can be edited.
Definition: gtextfield.cpp:297
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gtextfield.cpp:169
Definition: gtextfield.h:48
virtual void setMaxLength(int maxLength)
Sets the maximum number of characters that can be typed into the field.
Definition: gtextfield.cpp:309
virtual bool valueIsDouble() const
Returns true if the currently typed value in the text field can be interpreted as a real number...
Definition: gtextfield.cpp:381
virtual bool isAutocompleteEnabled() const
Returns true if this text field has an autocompletion list of options that will pop up as the user be...
Definition: gtextfield.cpp:215
virtual bool valueIsReal() const
Returns true if the currently typed value in the text field can be interpreted as a real number...
Definition: gtextfield.cpp:393
virtual void setAutocompleteEnabled(bool enabled)
Sets whether the autocompletion feature is enabled for this text field.
Definition: gtextfield.cpp:268
Definition: gtextfield.h:49
virtual void removeTextChangeListener()
Removes the text change listener from this text field so that it will no longer call it when the user...
Definition: gtextfield.cpp:235
virtual double getValueAsDouble() const
Returns the currently typed value in the text field, interpreted as a real number value...
Definition: gtextfield.cpp:191
virtual void setPlaceholder(string text)
Sets a gray message that is displayed in the background of the text field before the user has typed a...
Definition: gtextfield.cpp:321
virtual string getPlaceholder() const
Returns the text field&#39;s placeholder text, which is usually displayed as a light gray text in the fie...
Definition: gtextfield.cpp:149
virtual int getMaxLength() const
Returns the maximum length of string allowed in the text field.
Definition: gtextfield.cpp:130
virtual int getCharsWide() const
Returns the number of characters that can fit in the visible area of this text field.
Definition: gtextfield.cpp:106
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
virtual InputType getInputType() const
Returns the type of input accepted by this text field.
Definition: gtextfield.cpp:116
Definition: gtextfield.h:47
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextfield.cpp:205
virtual bool valueIsBool() const
Returns true if the currently typed value in the text field can be interpreted as a bool value of tru...
Definition: gtextfield.cpp:373
GTextField(string text="", int charsWide=0, QWidget* parent=nullptr)
Creates a text field with the given initial text.
Definition: gtextfield.cpp:33
virtual void setValue(bool value)
Sets the current text value in the text field to the string representation of the given value...
Definition: gtextfield.cpp:353
virtual bool valueIsChar() const
Returns true if the currently typed value in the text field can be interpreted as a char value...
Definition: gtextfield.cpp:377
virtual int getValueAsInt() const
Returns the currently typed value in the text field, interpreted as an integer value.
Definition: gtextfield.cpp:196
virtual void setText(string text)
Sets the current text value in the text field.
Definition: gtextfield.cpp:333
virtual bool isEditable() const
Returns true if the text field&#39;s value can be edited.
Definition: gtextfield.cpp:225
virtual int getValueAsInteger() const
Returns the currently typed value in the text field, interpreted as an integer value.
Definition: gtextfield.cpp:200
~GTextField() override
Frees memory allocated internally by the text field.
Definition: gtextfield.cpp:86
virtual void setTextChangeListener(GEventListener func)
Sets a text-change listener on this text field so that it will be called when the value in the field ...
Definition: gtextfield.cpp:345
virtual bool valueIsInt() const
Returns true if the currently typed value in the text field can be interpreted as an integer...
Definition: gtextfield.cpp:385
virtual void setAutocompleteList(std::initializer_list< string > strings)
Sets the given list of strings to be used as an autocompletion list for this text field...
Definition: gtextfield.cpp:239