SGL
gtextfield.h
1 /*
2  * File: gtextfield.h
3  * ------------------
4  *
5  * @author Marty Stepp
6  * @version 2021/04/09
7  * - added sgl namespace
8  * @version 2021/04/03
9  * - removed dependency on custom collections
10  * @version 2019/04/23
11  * - added key events
12  * @version 2018/09/08
13  * - added doc comments for new documentation generation
14  * @version 2018/08/23
15  * - renamed to gtextfield.h to replace Java version
16  * @version 2018/06/29
17  * - added textChange event
18  * @version 2018/06/25
19  * - initial version
20  */
21 
22 
23 #ifndef _gtextfield_h
24 #define _gtextfield_h
25 
26 #include <initializer_list>
27 #include <string>
28 #include <QLineEdit>
29 #include <QSpinBox>
30 #include <QDoubleSpinBox>
31 #include <QString>
32 
33 #include "ginteractor.h"
34 
35 namespace sgl {
36 
37 class _Internal_QLineEdit;
38 class _Internal_QSpinBox;
39 class _Internal_QDoubleSpinBox;
40 
45 class GTextField : public GInteractor {
46 public:
50  enum InputType {
54  };
55 
61  GTextField(const string& text = "", int charsWide = 0, QWidget* parent = nullptr);
62 
66  GTextField(int charsWide, QWidget* parent = nullptr);
67 
74  GTextField(int value, int min, int max, int step = 1, QWidget* parent = nullptr);
75 
82  GTextField(double value, double min, double max, double step, QWidget* parent = nullptr);
83 
87  ~GTextField() override;
88 
93  virtual int getCharsWide() const;
94 
101  virtual InputType getInputType() const;
102 
103  /* @inherit */
104  _Internal_QWidget* getInternalWidget() const override;
105 
110  virtual int getMaxLength() const;
111 
117  virtual string getPlaceholder() const;
118 
123  virtual string getText() const;
124 
125  /* @inherit */
126  string getType() const override;
127 
132  virtual string getValue() const;
133 
141  virtual bool getValueAsBool() const;
142 
148  virtual char getValueAsChar() const;
149 
157  virtual double getValueAsDouble() const;
158 
167  virtual int getValueAsInt() const;
168 
177  virtual int getValueAsInteger() const;
178 
179  /* @inherit */
180  QWidget* getWidget() const override;
181 
188  virtual bool isAutocompleteEnabled() const;
189 
195  virtual bool isEditable() const;
196 
201  virtual void removeTextChangeListener();
202 
211  virtual void setAutocompleteList(std::initializer_list<string> strings);
212 
221  virtual void setAutocompleteList(const std::vector<string>& strings);
222 
228  virtual void setAutocompleteEnabled(bool enabled);
229 
234  virtual void setCharsWide(int charsWide);
235 
240  virtual void setEditable(bool value);
241 
245  virtual void setMaxLength(int maxLength);
246 
252  virtual void setPlaceholder(const string& text);
253 
257  virtual void setText(const string& text);
258 
264  virtual void setTextChangeListener(GEventListener func);
265 
271  virtual void setTextChangeListener(GEventListenerVoid func);
272 
277  virtual void setValue(bool value);
278 
283  virtual void setValue(char value);
284 
289  virtual void setValue(double value);
290 
295  virtual void setValue(int value);
296 
302  virtual void setValue(const string& value);
303 
309  virtual bool valueIsBool() const;
310 
317  virtual bool valueIsChar() const;
318 
325  virtual bool valueIsDouble() const;
326 
333  virtual bool valueIsInt() const;
334 
341  virtual bool valueIsInteger() const;
342 
349  virtual bool valueIsReal() const;
350 
351 protected:
355  string getActionEventType() const override;
356 
357 private:
358  Q_DISABLE_COPY(GTextField)
359 
360  // pointers to the internal Qt text field;
361  // at most one of these will be non-null for a given instance
362  _Internal_QLineEdit* _iqlineedit;
363  _Internal_QSpinBox* _iqspinbox;
364  _Internal_QDoubleSpinBox* _iqdoublespinbox;
365 
366  // type of text field; helps tell us which of the above internal Qt widgets
367  // will be non-null
368  InputType _inputType;
369 
370  friend class _Internal_QLineEdit;
371  friend class _Internal_QSpinBox;
372  friend class _Internal_QDoubleSpinBox;
373 };
374 
379 class _Internal_QLineEdit : public QLineEdit, public _Internal_QWidget {
380  Q_OBJECT
381 
382 public:
383  _Internal_QLineEdit(GTextField* gtextField, QWidget* parent = nullptr);
384  void detach() override;
385  void keyPressEvent(QKeyEvent* event) override;
386  void keyReleaseEvent(QKeyEvent* event) override;
387  QSize sizeHint() const override;
388 
389 public slots:
390  void handleTextChange(const QString&);
391 
392 private:
393  GTextField* _gtextfield;
394 
395  friend class GTextField;
396 };
397 
402 class _Internal_QSpinBox : public QSpinBox, public _Internal_QWidget {
403  Q_OBJECT
404 
405 public:
406  _Internal_QSpinBox(GTextField* qgtextField, int min, int max, int step = 1, QWidget* parent = nullptr);
407  void detach() override;
408  void keyPressEvent(QKeyEvent* event) override;
409  void keyReleaseEvent(QKeyEvent* event) override;
410  virtual QLineEdit* lineEdit() const;
411  QSize sizeHint() const override;
412 
413 public slots:
414  void handleTextChange(const QString&);
415 
416 private:
417  GTextField* _gtextfield;
418 
419  friend class GTextField;
420 };
421 
426 class _Internal_QDoubleSpinBox : public QDoubleSpinBox, public _Internal_QWidget {
427  Q_OBJECT
428 
429 public:
430  _Internal_QDoubleSpinBox(GTextField* qgtextField, double min, double max, double step = 0.1, QWidget* parent = nullptr);
431  void detach() override;
432  void keyPressEvent(QKeyEvent* event) override;
433  void keyReleaseEvent(QKeyEvent* event) override;
434  virtual QLineEdit* lineEdit() const;
435  QSize sizeHint() const override;
436 
437 public slots:
438  void handleTextChange(const QString&);
439 
440 private:
441  GTextField* _gtextfield;
442 
443  friend class GTextField;
444 };
445 
446 } // namespace sgl
447 
448 #endif // _gtextfield_h
virtual bool valueIsInteger() const
Returns true if the currently typed value in the text field can be interpreted as an integer...
Definition: gtextfield.cpp:394
virtual int getCharsWide() const
Returns the number of characters that can fit in the visible area of this text field.
Definition: gtextfield.cpp:111
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:244
virtual double getValueAsDouble() const
Returns the currently typed value in the text field, interpreted as a real number value...
Definition: gtextfield.cpp:196
virtual int getValueAsInt() const
Returns the currently typed value in the text field, interpreted as an integer value.
Definition: gtextfield.cpp:201
This interactor subclass represents a text field for entering short text strings. ...
Definition: gtextfield.h:45
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:386
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gtextfield.cpp:174
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:240
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:288
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:182
Definition: gtextfield.h:53
std::function< void(GEvent)> GEventListener
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:38
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:358
Definition: console.h:45
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
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:398
Definition: gtextfield.h:52
virtual void setMaxLength(int maxLength)
Sets the maximum number of characters that can be typed into the field.
Definition: gtextfield.cpp:314
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:220
virtual bool isEditable() const
Returns true if the text field&#39;s value can be edited.
Definition: gtextfield.cpp:230
virtual int getMaxLength() const
Returns the maximum length of string allowed in the text field.
Definition: gtextfield.cpp:135
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextfield.cpp:210
virtual void setAutocompleteEnabled(bool enabled)
Sets whether the autocompletion feature is enabled for this text field.
Definition: gtextfield.cpp:273
std::function< void()> GEventListenerVoid
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:44
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:350
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:154
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:382
virtual char getValueAsChar() const
Returns the currently typed value in the text field as a char value.
Definition: gtextfield.cpp:187
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:378
virtual InputType getInputType() const
Returns the type of input accepted by this text field.
Definition: gtextfield.cpp:121
~GTextField() override
Frees memory allocated internally by the text field.
Definition: gtextfield.cpp:91
virtual int getValueAsInteger() const
Returns the currently typed value in the text field, interpreted as an integer value.
Definition: gtextfield.cpp:205
Definition: gtextfield.h:51
virtual string getText() const
Returns the text field&#39;s current text.
Definition: gtextfield.cpp:164
virtual void setText(string text)
Sets the current text value in the text field.
Definition: gtextfield.cpp:338
virtual void setEditable(bool value)
Sets whether the value in the text box can be edited.
Definition: gtextfield.cpp:302
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gtextfield.cpp:125
virtual string getValue() const
Returns the text field&#39;s current text.
Definition: gtextfield.cpp:178
InputType
Constants for the valid types of text field input.
Definition: gtextfield.h:50
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:326
virtual bool valueIsInt() const
Returns true if the currently typed value in the text field can be interpreted as an integer...
Definition: gtextfield.cpp:390
GTextField(string text="", int charsWide=0, QWidget* parent=nullptr)
Creates a text field with the given initial text.
Definition: gtextfield.cpp:38