SGL
gchooser.h
1 /*
2  * File: gchooser.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 event support
10  * @version 2018/09/07
11  * - added doc comments for new documentation generation
12  * @version 2018/09/04
13  * - added GComboBox alias
14  * @version 2018/08/23
15  * - renamed to gchooser.h to replace Java version
16  * @version 2018/06/28
17  * - initial version
18  */
19 
20 
21 #ifndef _gchooser_h
22 #define _gchooser_h
23 
24 #include <initializer_list>
25 #include <string>
26 #include <vector>
27 #include <QComboBox>
28 
29 #include "ginteractor.h"
30 
31 class _Internal_QComboBox;
32 
38 class GChooser : public GInteractor {
39 public:
43  GChooser(QWidget* parent = nullptr);
44 
48  GChooser(const std::initializer_list<string>& items, QWidget* parent = nullptr);
49 
53  GChooser(const std::vector<string>& items, QWidget* parent = nullptr);
54 
58  ~GChooser() override;
59 
63  virtual void addItem(const string& item);
64 
68  virtual void addItems(const std::initializer_list<string>& items);
69 
73  virtual void addItems(const std::vector<string>& items);
74 
78  virtual void clearItems();
79 
80  /* @inherit */
81  string getActionCommand() const override;
82 
83  /* @inherit */
84  _Internal_QWidget* getInternalWidget() const override;
85 
90  virtual string getItem(int index) const;
91 
95  virtual int getItemCount() const;
96 
102  virtual int getSelectedIndex() const;
103 
108  virtual string getSelectedItem() const;
109 
110  /* @inherit */
111  string getType() const override;
112 
113  /* @inherit */
114  QWidget* getWidget() const override;
115 
120  virtual bool isEditable() const;
121 
125  virtual bool isEmpty() const;
126 
131  virtual void setEditable(bool editable);
132 
137  virtual void setItem(int index, const string& item);
138 
143  virtual void setSelectedIndex(int index);
144 
149  virtual void setSelectedItem(const string& item);
150 
154  virtual int size() const;
155 
156 protected:
160  string getActionEventType() const override;
161 
162 private:
163  Q_DISABLE_COPY(GChooser)
164 
165  _Internal_QComboBox* _iqcomboBox;
166 
167  void checkIndex(const string& member, int index, int min = 0, int max = -1) const;
168 
169  friend class _Internal_QComboBox;
170 };
171 
176 class _Internal_QComboBox : public QComboBox, public _Internal_QWidget {
177  Q_OBJECT
178 
179 public:
180  _Internal_QComboBox(GChooser* gchooser, QWidget* parent = nullptr);
181  void detach() override;
182  void keyPressEvent(QKeyEvent* event) override;
183  void keyReleaseEvent(QKeyEvent* event) override;
184  QSize sizeHint() const override;
185 
186 public slots:
187  void handleChange();
188 
189 private:
190  GChooser* _gchooser;
191 
192  friend class GChooser;
193 };
194 
195 // alias GComboBox for GChooser for name compatibility with Java and Qt
196 typedef GChooser GComboBox;
197 
198 #endif // _gchooser_h
virtual bool isEmpty() const
Returns true if the chooser has no items.
Definition: gchooser.cpp:137
virtual void setItem(int index, string item)
Sets the item at the given index in the chooser to the given value.
Definition: gchooser.cpp:141
~GChooser() override
Frees memory allocated internally by the chooser.
Definition: gchooser.cpp:46
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:92
virtual string getItem(int index) const
Returns the item in the chooser at the given 0-based index.
Definition: gchooser.cpp:108
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gchooser.cpp:129
This interactor subclass represents a selectable drop-down list.
Definition: gchooser.h:38
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
virtual void clearItems()
Removes all items from the chooser.
Definition: gchooser.cpp:86
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gchooser.cpp:104
virtual bool isEditable() const
Returns true if the chooser has an editable area for typing new items.
Definition: gchooser.cpp:133
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:121
GChooser(QWidget* parent=nullptr)
Creates a chooser that initially contains no items.
Definition: gchooser.cpp:23
virtual void setSelectedItem(string item)
Sets the given item in the chooser to be selected.
Definition: gchooser.cpp:161
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:59
virtual int size() const
Returns the number of items in the chooser.
Definition: gchooser.cpp:171
virtual int getItemCount() const
Returns the number of items in the chooser.
Definition: gchooser.cpp:113
virtual void addItem(string item)
Adds a new item consisting of the specified string to the end of the list.
Definition: gchooser.cpp:52
virtual void setEditable(bool editable)
Sets whether the chooser has an editable area for typing new items.
Definition: gchooser.cpp:155
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gchooser.cpp:125
virtual void setSelectedIndex(int index)
Sets the item at the given index in the chooser to be selected.
Definition: gchooser.cpp:148
virtual int getSelectedIndex() const
Returns which index is selected in the chooser.
Definition: gchooser.cpp:117