SGL
gchooser.h
1 /*
2  * File: gchooser.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 event support
12  * @version 2018/09/07
13  * - added doc comments for new documentation generation
14  * @version 2018/09/04
15  * - added GComboBox alias
16  * @version 2018/08/23
17  * - renamed to gchooser.h to replace Java version
18  * @version 2018/06/28
19  * - initial version
20  */
21 
22 
23 #ifndef _gchooser_h
24 #define _gchooser_h
25 
26 #include <initializer_list>
27 #include <string>
28 #include <vector>
29 #include <QComboBox>
30 
31 #include "ginteractor.h"
32 
33 namespace sgl {
34 
35 class _Internal_QComboBox;
36 
42 class GChooser : public GInteractor {
43 public:
47  GChooser(QWidget* parent = nullptr);
48 
52  GChooser(const std::initializer_list<string>& items, QWidget* parent = nullptr);
53 
57  GChooser(const std::vector<string>& items, QWidget* parent = nullptr);
58 
62  ~GChooser() override;
63 
67  virtual void addItem(const string& item);
68 
72  virtual void addItems(const std::initializer_list<string>& items);
73 
77  virtual void addItems(const std::vector<string>& items);
78 
82  virtual void clearItems();
83 
84  /* @inherit */
85  string getActionCommand() const override;
86 
87  /* @inherit */
88  _Internal_QWidget* getInternalWidget() const override;
89 
94  virtual string getItem(int index) const;
95 
99  virtual int getItemCount() const;
100 
106  virtual int getSelectedIndex() const;
107 
112  virtual string getSelectedItem() const;
113 
114  /* @inherit */
115  string getType() const override;
116 
117  /* @inherit */
118  QWidget* getWidget() const override;
119 
124  virtual bool isEditable() const;
125 
129  virtual bool isEmpty() const;
130 
135  virtual void setEditable(bool editable);
136 
141  virtual void setItem(int index, const string& item);
142 
147  virtual void setSelectedIndex(int index);
148 
153  virtual void setSelectedItem(const string& item);
154 
158  virtual int size() const;
159 
160 protected:
164  string getActionEventType() const override;
165 
166 private:
167  Q_DISABLE_COPY(GChooser)
168 
169  _Internal_QComboBox* _iqcomboBox;
170 
171  void checkIndex(const string& member, int index, int min = 0, int max = -1) const;
172 
173  friend class _Internal_QComboBox;
174 };
175 
180 class _Internal_QComboBox : public QComboBox, public _Internal_QWidget {
181  Q_OBJECT
182 
183 public:
184  _Internal_QComboBox(GChooser* gchooser, QWidget* parent = nullptr);
185  void detach() override;
186  void keyPressEvent(QKeyEvent* event) override;
187  void keyReleaseEvent(QKeyEvent* event) override;
188  QSize sizeHint() const override;
189 
190 public slots:
191  void handleChange();
192 
193 private:
194  GChooser* _gchooser;
195 
196  friend class GChooser;
197 };
198 
199 // alias GComboBox for GChooser for name compatibility with Java and Qt
201 
202 } // namespace sgl
203 
204 #endif // _gchooser_h
virtual int getItemCount() const
Returns the number of items in the chooser.
Definition: gchooser.cpp:117
GChooser(QWidget* parent=nullptr)
Creates a chooser that initially contains no items.
Definition: gchooser.cpp:27
string getActionCommand() const override
Returns an action command for this interactor, which is a semi-unique string you can use to identify ...
Definition: gchooser.cpp:96
virtual void setSelectedItem(string item)
Sets the given item in the chooser to be selected.
Definition: gchooser.cpp:165
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gchooser.cpp:133
virtual void setSelectedIndex(int index)
Sets the item at the given index in the chooser to be selected.
Definition: gchooser.cpp:152
virtual void addItems(const std::initializer_list< string > &items)
Adds each item from the given list to the end of the chooser&#39;s list.
Definition: gchooser.cpp:63
virtual void clearItems()
Removes all items from the chooser.
Definition: gchooser.cpp:90
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gchooser.cpp:129
Definition: console.h:45
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
virtual string getSelectedItem() const
Returns the currently selected item in the chooser, or an empty string if no item is currently select...
Definition: gchooser.cpp:125
virtual bool isEditable() const
Returns true if the chooser has an editable area for typing new items.
Definition: gchooser.cpp:137
This interactor subclass represents a selectable drop-down list.
Definition: gchooser.h:42
virtual string getItem(int index) const
Returns the item in the chooser at the given 0-based index.
Definition: gchooser.cpp:112
virtual int size() const
Returns the number of items in the chooser.
Definition: gchooser.cpp:175
virtual void setEditable(bool editable)
Sets whether the chooser has an editable area for typing new items.
Definition: gchooser.cpp:159
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gchooser.cpp:108
virtual void addItem(string item)
Adds a new item consisting of the specified string to the end of the list.
Definition: gchooser.cpp:56
virtual void setItem(int index, string item)
Sets the item at the given index in the chooser to the given value.
Definition: gchooser.cpp:145
virtual bool isEmpty() const
Returns true if the chooser has no items.
Definition: gchooser.cpp:141
virtual int getSelectedIndex() const
Returns which index is selected in the chooser.
Definition: gchooser.cpp:121
~GChooser() override
Frees memory allocated internally by the chooser.
Definition: gchooser.cpp:50
GChooser GComboBox
Definition: gchooser.h:200