API Documentation
Loading...
Searching...
No Matches
QCustomCombobox.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Widgets
28File: QCustomCombobox
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/QCustomValidator.h>
35#include <NDEVR/QCustomLineEdit.h>
36#include <NDEVR/QTTools.h>
37#include <NDEVR/ObjectInfo.h>
38#include <NDEVR/Dictionary.h>
39#include <NDEVR/String.h>
40#include <NDEVR/Resource.h>
41#include <QComboBox>
42namespace NDEVR
43{
44 class QCustomValidator;
45 class QCustomLineEdit;
46 /**--------------------------------------------------------------------------------------------------
47 \brief A compact way to present a list of options to the user.
48
49 A combobox is a selection widget that shows the current item, and pops up a list of selectable
50 items when clicked. Comboboxes can contain lists of translated data alongside of their program-values.
51 **/
52 class NDEVR_WIDGETS_API QCustomComboBox : public QComboBox
53 {
54 Q_OBJECT
55 public:
56 QCustomComboBox(QWidget* parent = nullptr);
59 void setCustomValidator(const QCustomValidator& validator);
60 void setup(const TranslatedString& title, const Buffer<TranslatedString>& values);
61 void setup(const TranslatedString& title, const Buffer<String>& values, const Buffer<TranslatedString>& display_values);
62 bool isUsingCustom() const { return m_using_custom; }
63
64 template<class t_object>
65 void setup(const TranslatedString& title, const Buffer<std::pair<TranslatedString, t_object>>& items)
66 {
67 Buffer<String> values;
68 Buffer<TranslatedString> display_values;
69 for (uint04 i = 0; i < items.size(); i++)
70 {
71 values.add(String(items[i].second));
72 display_values.add(items[i].first);
73 }
74 setup(title, values, display_values);
75 }
76 template<class t_object>
77 typename std::enable_if<ObjectInfo<t_object>::Enum>::type setup(const TranslatedString& title, const Buffer<t_object>& items)
78 {
79 Buffer<String> values;
80 Buffer<TranslatedString> display_values;
81 for (uint04 i = 0; i < items.size(); i++)
82 {
83 values.add(String(items[i]));
84 display_values.add(String::DisplayString(items[i]));
85 }
86 setup(title, values, display_values);
87 }
88 template<class t_object>
89 typename std::enable_if<ObjectInfo<t_object>::Number>::type setup(const TranslatedString& title, const Buffer<t_object>& items)
90 {
91 Buffer<String> values;
92 Buffer<TranslatedString> display_values;
93 for (uint04 i = 0; i < items.size(); i++)
94 {
95 values.add(String(items[i]));
96 String s(items[i]);
97 display_values.add(TranslatedString(customValidator().fixup(s, true, false)));
98 }
99 setup(title, values, display_values);
100 }
101 template<class t_enum>
102 typename std::enable_if<ObjectInfo<t_enum>::Enum>::type setup(const TranslatedString& title, uint04 size)
103 {
104 setTitle(title);
105 m_values.clear();
106 index_to_enum.clear();
107 for (uint04 i = 0; i < size; i++)
108 {
109 m_values.add(String(cast<t_enum>(i)));
110 m_display_values.add(String::DisplayString(cast<t_enum>(i)));
111 index_to_enum[i] = i;
112 enum_to_index[i] = i;
113 }
114 refreshWidget();
115 }
116 template<class t_object>
117 typename std::enable_if<!ObjectInfo<t_object>::Enum && !ObjectInfo<t_object>::Number>::type setup(const TranslatedString& title, const Buffer<t_object>& items)
118 {
119 Buffer<String> values;
120 Buffer<TranslatedString> display_values;
121 for (uint04 i = 0; i < items.size(); i++)
122 {
123 values.add(String(items[i]));
124 display_values.add(String::DisplayString(items[i]));
125 }
126 setup(title, values, display_values);
127 }
128 template<class t_enum>
129 typename std::enable_if<ObjectInfo<t_enum>::Enum>::type setup(const TranslatedString& title, t_enum first, t_enum last)
130 {
131 setTitle(title);
132 m_values.clear();
133 for (uint04 i = 0; i <= cast<uint04>(last) - cast<uint04>(first); i++)
134 {
135 m_values.add(String(cast<t_enum>(i + cast<uint04>(first))));
136 m_display_values.add(String::DisplayString(cast<t_enum>(i + cast<uint04>(first))));
137 index_to_enum[i] = i + cast<uint04>(first);
138 enum_to_index[i + cast<uint04>(first)] = i;
139 }
140 refreshWidget();
141 }
142 template<class t_type>
143 void add(const t_type& value, const TranslatedString& display_value, bool refresh_widget = false)
144 {
145 m_values.add(String(value));
146 m_display_values.add(display_value);
147 if (refresh_widget)
148 refresh();
149 }
150 template<class t_type>
151 void remove(const t_type& value, bool refresh_widget = false)
152 {
153 uint04 index = m_values.indexOf(String(value));
154 if (!IsInvalid(index))
155 {
156 m_values.removeIndex(index);
157 m_display_values.removeIndex(index);
158 if (refresh_widget)
159 refresh();
160 }
161 }
162 void refresh()
163 {
164 refreshWidget();
165 updateGeometry();
166 }
167 template<class t_type>
168 void setupResource(const TranslatedString& title, Resource<t_type>& resource)
169 {
170 setTitle(title);
171 connect(this, &QCustomComboBox::edited, this, [this, &resource]
172 {
173 if (!customValidator().unit().isNull())
174 resource.set(QCustomLineEdit::ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueFromFloat(getUnitValue()));
175 else
176 resource.set(getAs<t_type>());
177 });
178 if (m_resource_changed_listener)
179 delete m_resource_changed_listener;
180 m_resource_changed_listener = new QTResourceListener([this, &resource]
181 {
182 if (!customValidator().unit().isNull())
183 setUnitValue(QCustomLineEdit::ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueToFloat(resource.get()));
184 else
185 setValue(resource.get());
186 }, this);
187 resource.addListener(m_resource_changed_listener);
188 }
189
190 void setAutoFormat(bool auto_format) { m_auto_format = auto_format; }
191 template<class t_enum>
192 typename std::enable_if<ObjectInfo<t_enum>::Enum>::type set(t_enum value)
193 {
194 setCurrentIndex(enum_to_index[cast<uint04>(value)]);
195 }
196 template<class t_type>
197 t_type getAs() const
198 {
199 if(m_using_custom || currentIndex() == -1 || m_values.size() <= cast<uint04>(currentIndex()))
200 return m_current_custom.template getAs<t_type>();
201 return m_values[currentIndex()].template getAs<t_type>();
202 }
203 template<class t_type>
204 t_type getAs(uint04 index) const
205 {
206 return m_values[index].template getAs<t_type>();
207 }
208 template<class t_type>
209 void setValue(const t_type& t_value)
210 {
211 setValue(String(t_value));
212 }
213 void setValue(const TranslatedString& t_value)
214 {
215 setValue(String(t_value));
216 lib_assert(!isUsingCustom(), "unexpect custom translated string");
217 }
218 void setTitle(const TranslatedString& title);
220 QSize minimumSizeHint() const override;
221 QSize sizeHint() const override;
224 void setValue(const String& value);
225 void setToolTip(const TranslatedString& tooltip);
228 void setCustomUnit(const String& unit, UnitCategory fallback_unit, uint01 dimension = Constant<uint01>::Invalid);
229 void setUnitValue(fltp08 value, uint01 unit_dimension = 0);
230 void setAllowCustom(bool allow_custom) { m_allow_custom = allow_custom; refreshWidget(); }
231 void setSuffix(const TranslatedString& suffix) { m_suffix = suffix; refreshWidget(); }
232 const TranslatedString& suffix() const { return m_suffix; }
233 void focusInEvent(QFocusEvent* e) override;
235 bool event(QEvent* event) override;
237 const Buffer<String>& values() const { return m_values; }
238 const Buffer<TranslatedString>& displayValues() const { return m_display_values; }
239 void showPopup() override;
240 void hidePopup() override;
241 void resizeEvent(QResizeEvent* event) override;
242 void paintEvent(QPaintEvent*) override;
243 void wheelEvent(QWheelEvent* e) override;
245 protected:
248 virtual void onSorted(const Buffer<uint04>& swap_indices);
249 protected slots:
250 void onEdit();
252 void onChange();
253 signals:
254 void edited();
255 protected:
256 bool m_is_refreshing = false;
257 bool m_is_changing = false;
261 bool m_sort_alphabetically = false;
262 bool m_is_showing_popup = false;
264 uint04 m_sort_offset = 0;
274 };
275}
#define lib_assert(expression, message)
Definition LibAssert.h:61
#define NDEVR_WIDGETS_API
Definition DLLInfo.h:59
The primary angle storage class for this API. Stores an angle in an optimized format.
Definition StringStream.h:540
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
void add(t_type &&object)
Adds object to the end of the buffer.
Definition Buffer.hpp:186
constexpr t_index_type size() const
Definition Buffer.hpp:823
Provides a constant, unmodifiable pointer that has shared ownership of a dynamically allocated object...
Definition GraphicsPipeline.h:42
A hash-based key-value store, useful for quick associative lookups. Key features include:
Definition Dictionary.h:61
A compact way to present a list of options to the user.
Definition QCustomCombobox.h:53
QCustomComboBox(QWidget *parent=nullptr)
void setup(const TranslatedString &title, const Buffer< String > &values, const Buffer< TranslatedString > &display_values)
QSize minimumSizeHint() const override
void paintEvent(QPaintEvent *) override
Buffer< String > m_values
Definition QCustomCombobox.h:266
void setUnitType(UnitCategory unit, uint01 dimension=Constant< uint01 >::Invalid)
void setup(const TranslatedString &title, const Buffer< std::pair< TranslatedString, t_object > > &items)
Definition QCustomCombobox.h:65
bool m_using_custom
Definition QCustomCombobox.h:259
const TranslatedString & suffix() const
Definition QCustomCombobox.h:232
void focusInEvent(QFocusEvent *e) override
void hidePopup() override
void showPopup() override
t_type getAs(uint04 index) const
Definition QCustomCombobox.h:204
String m_current_custom
Definition QCustomCombobox.h:268
TranslatedString m_suffix
Definition QCustomCombobox.h:269
void setValue(const TranslatedString &t_value)
Definition QCustomCombobox.h:213
QSize sizeHint() const override
std::enable_if< ObjectInfo< t_enum >::Enum >::type set(t_enum value)
Definition QCustomCombobox.h:192
void setAutoFormat(bool auto_format)
Definition QCustomCombobox.h:190
void setCustomUnit(const String &unit, UnitCategory fallback_unit, uint01 dimension=Constant< uint01 >::Invalid)
void setTitle(const TranslatedString &title)
uint04 m_last_index
Definition QCustomCombobox.h:265
const QCustomValidator & customValidator() const
void setValue(const t_type &t_value)
Definition QCustomCombobox.h:209
bool event(QEvent *event) override
Dictionary< uint04, uint04 > enum_to_index
Definition QCustomCombobox.h:271
bool m_auto_format
Definition QCustomCombobox.h:260
bool isUsingCustom() const
Definition QCustomCombobox.h:62
void remove(const t_type &value, bool refresh_widget=false)
Definition QCustomCombobox.h:151
const Buffer< String > & values() const
Definition QCustomCombobox.h:237
void sortAlphabetically(uint04 start=0, uint04 size=Constant< uint04 >::Invalid)
void refresh()
Definition QCustomCombobox.h:162
virtual void onSorted(const Buffer< uint04 > &swap_indices)
std::enable_if< ObjectInfo< t_object >::Number >::type setup(const TranslatedString &title, const Buffer< t_object > &items)
Definition QCustomCombobox.h:89
void setup(const TranslatedString &title, const Buffer< TranslatedString > &values)
std::enable_if<!ObjectInfo< t_object >::Enum &&!ObjectInfo< t_object >::Number >::type setup(const TranslatedString &title, const Buffer< t_object > &items)
Definition QCustomCombobox.h:117
void setToolTip(const TranslatedString &tooltip)
void wheelEvent(QWheelEvent *e) override
void setSuffix(const TranslatedString &suffix)
Definition QCustomCombobox.h:231
void setAllowCustom(bool allow_custom)
Definition QCustomCombobox.h:230
fltp08 getUnitValue() const
std::enable_if< ObjectInfo< t_enum >::Enum >::type setup(const TranslatedString &title, uint04 size)
Definition QCustomCombobox.h:102
Buffer< TranslatedString > m_display_values
Definition QCustomCombobox.h:267
void setCustomValidator(const QCustomValidator &validator)
void resizeEvent(QResizeEvent *event) override
void add(const t_type &value, const TranslatedString &display_value, bool refresh_widget=false)
Definition QCustomCombobox.h:143
Angle< fltp08 > getUnitAngle() const
bool m_allow_custom
Definition QCustomCombobox.h:258
QTResourceListener * m_resource_changed_listener
Definition QCustomCombobox.h:273
t_type getAs() const
Definition QCustomCombobox.h:197
const Buffer< TranslatedString > & displayValues() const
Definition QCustomCombobox.h:238
std::enable_if< ObjectInfo< t_enum >::Enum >::type setup(const TranslatedString &title, t_enum first, t_enum last)
Definition QCustomCombobox.h:129
Dictionary< uint04, uint04 > index_to_enum
Definition QCustomCombobox.h:272
void setUnitValue(fltp08 value, uint01 unit_dimension=0)
QCustomLineEdit * m_line_edit
Definition QCustomCombobox.h:270
QCustomValidator & customValidator()
std::enable_if< ObjectInfo< t_object >::Enum >::type setup(const TranslatedString &title, const Buffer< t_object > &items)
Definition QCustomCombobox.h:77
void setConstantUnit(const ConstPointer< Unit > &unit, uint01 dimension=Constant< uint01 >::Invalid)
uint04 m_custom_index
Definition QCustomCombobox.h:263
void setValue(const String &value)
void setupResource(const TranslatedString &title, Resource< t_type > &resource)
Definition QCustomCombobox.h:168
A line edit allows users to enter and edit a single line of plain text with useful editing functions,...
Definition QCustomLineEdit.h:56
Responsible on most user input fields for correcting the input when possible or allerting the user th...
Definition QCustomValidator.h:47
Definition QTTools.h:168
ResourceListener * addListener(ResourceListener *listener) const
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Toggle.h:41
const T & get() const
Definition Resource.h:71
void set(const T &info, bool check_equal=true)
Definition Resource.h:53
The core String class for the NDEVR API.
Definition String.h:69
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
Definition TranslatedString.h:13
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
UnitCategory
A category of unit describing what it relates to. Any category can have some number of units defined ...
Definition Unit.h:14
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
Information about the object.
Definition ObjectInfo.h:54