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 class NDEVR_WIDGETS_API QCustomComboBox : public QComboBox
47 {
48 Q_OBJECT
49 public:
50 QCustomComboBox(QWidget* parent = nullptr);
51 const QCustomValidator& customValidator() const;
52 QCustomValidator& customValidator();
53 void setCustomValidator(const QCustomValidator& validator);
54 void setup(const TranslatedString& title, const Buffer<TranslatedString>& values);
55 void setup(const TranslatedString& title, const Buffer<String>& values, const Buffer<TranslatedString>& display_values);
56 bool isUsingCustom() const { return m_using_custom; }
57
58 template<class t_object>
59 void setup(const TranslatedString& title, const Buffer<std::pair<TranslatedString, t_object>>& items)
60 {
61 Buffer<String> values;
62 Buffer<TranslatedString> display_values;
63 for (uint04 i = 0; i < items.size(); i++)
64 {
65 values.add(String(items[i].second));
66 display_values.add(items[i].first);
67 }
68 setup(title, values, display_values);
69 }
70 template<class t_object>
71 typename std::enable_if<ObjectInfo<t_object>::Enum>::type setup(const TranslatedString& title, const Buffer<t_object>& items)
72 {
73 Buffer<String> values;
74 Buffer<TranslatedString> display_values;
75 for (uint04 i = 0; i < items.size(); i++)
76 {
77 values.add(String(items[i]));
78 display_values.add(String::DisplayString(items[i]));
79 }
80 setup(title, values, display_values);
81 }
82 template<class t_object>
83 typename std::enable_if<ObjectInfo<t_object>::Number>::type setup(const TranslatedString& title, const Buffer<t_object>& items)
84 {
85 Buffer<String> values;
86 Buffer<TranslatedString> display_values;
87 for (uint04 i = 0; i < items.size(); i++)
88 {
89 values.add(String(items[i]));
90 String s(items[i]);
91 display_values.add(TranslatedString(customValidator().fixup(s, true, false)));
92 }
93 setup(title, values, display_values);
94 }
95 template<class t_enum>
96 typename std::enable_if<ObjectInfo<t_enum>::Enum>::type setup(const TranslatedString& title, uint04 size)
97 {
98 setTitle(title);
99 m_values.clear();
100 index_to_enum.clear();
101 for (uint04 i = 0; i < size; i++)
102 {
103 m_values.add(String(cast<t_enum>(i)));
104 m_display_values.add(String::DisplayString(cast<t_enum>(i)));
105 index_to_enum[i] = i;
106 enum_to_index[i] = i;
107 }
108 refreshWidget();
109 }
110 template<class t_object>
111 typename std::enable_if<!ObjectInfo<t_object>::Enum && !ObjectInfo<t_object>::Number>::type setup(const TranslatedString& title, const Buffer<t_object>& items)
112 {
113 Buffer<String> values;
114 Buffer<TranslatedString> display_values;
115 for (uint04 i = 0; i < items.size(); i++)
116 {
117 values.add(String(items[i]));
118 display_values.add(String::DisplayString(items[i]));
119 }
120 setup(title, values, display_values);
121 }
122 template<class t_enum>
123 typename std::enable_if<ObjectInfo<t_enum>::Enum>::type setup(const TranslatedString& title, t_enum first, t_enum last)
124 {
125 setTitle(title);
126 m_values.clear();
127 for (uint04 i = 0; i <= cast<uint04>(last) - cast<uint04>(first); i++)
128 {
129 m_values.add(String(cast<t_enum>(i + cast<uint04>(first))));
130 m_display_values.add(String::DisplayString(cast<t_enum>(i + cast<uint04>(first))));
131 index_to_enum[i] = i + cast<uint04>(first);
132 enum_to_index[i + cast<uint04>(first)] = i;
133 }
134 refreshWidget();
135 }
136 template<class t_type>
137 void add(const t_type& value, const TranslatedString& display_value, bool refresh_widget = false)
138 {
139 m_values.add(String(value));
140 m_display_values.add(display_value);
141 if (refresh_widget)
142 refresh();
143 }
144 template<class t_type>
145 void remove(const t_type& value, bool refresh_widget = false)
146 {
147 uint04 index = m_values.indexOf(String(value));
148 if (!isNaN(index))
149 {
150 m_values.removeIndex(index);
151 m_display_values.removeIndex(index);
152 if (refresh_widget)
153 refresh();
154 }
155 }
156 void refresh()
157 {
158 refreshWidget();
159 updateGeometry();
160 }
161 template<class t_type>
162 void setupResource(const TranslatedString& title, Resource<t_type>& resource)
163 {
164 setTitle(title);
165 connect(this, &QCustomComboBox::edited, this, [this, &resource]
166 {
167 if (!customValidator().unit().isNull())
168 resource.set(ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueFromFloat(getUnitValue()));
169 else
170 resource.set(getAs<t_type>());
171 });
172 if (m_resource_changed_listener)
173 delete m_resource_changed_listener;
174 m_resource_changed_listener = new QTResourceListener([this, &resource]
175 {
176 if (!customValidator().unit().isNull())
177 setUnitValue(ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueToFloat(resource.get()));
178 else
179 setValue(resource.get());
180 }, this);
181 resource.addListener(m_resource_changed_listener);
182 }
183
184 void setAutoFormat(bool auto_format) { m_auto_format = auto_format; }
185 template<class t_enum>
186 typename std::enable_if<ObjectInfo<t_enum>::Enum>::type set(t_enum value)
187 {
188 setCurrentIndex(enum_to_index[cast<uint04>(value)]);
189 }
190 template<class t_type>
191 t_type getAs() const
192 {
193 if(m_using_custom || currentIndex() == -1 || m_values.size() <= cast<uint04>(currentIndex()))
194 return m_current_custom.template getAs<t_type>();
195 return m_values[currentIndex()].template getAs<t_type>();
196 }
197 template<class t_type>
198 t_type getAs(uint04 index) const
199 {
200 return m_values[index].template getAs<t_type>();
201 }
202 template<class t_type>
203 void setValue(const t_type& t_value)
204 {
205 setValue(String(t_value));
206 }
207 void setValue(const TranslatedString& t_value)
208 {
209 setValue(String(t_value));
210 lib_assert(!isUsingCustom(), "unexpect custom translated string");
211 }
212 void setTitle(const TranslatedString& title);
213 void clearValue();
214 QSize minimumSizeHint() const override;
215 QSize sizeHint() const override;
216 fltp08 getUnitValue() const;
217 Angle<fltp08> getUnitAngle() const;
218 void setValue(const String& value);
219 void setToolTip(const TranslatedString& tooltip);
220 void setUnitType(UnitCategory unit, uint01 dimension = Constant<uint01>::NaN);
221 void setConstantUnit(const ConstPointer<Unit>& unit, uint01 dimension = Constant<uint01>::NaN);
222 void setCustomUnit(const String& unit, UnitCategory fallback_unit, uint01 dimension = Constant<uint01>::NaN);
223 void setUnitValue(fltp08 value, uint01 unit_dimension = 0);
224 void setAllowCustom(bool allow_custom) { m_allow_custom = allow_custom; refreshWidget(); }
225 void setSuffix(const TranslatedString& suffix) { m_suffix = suffix; refreshWidget(); }
226 const TranslatedString& suffix() const { return m_suffix; }
227 void focusInEvent(QFocusEvent* e) override;
228 void updateFontSize();
229 bool event(QEvent* event) override;
230 void sortAlphabetically(uint04 start = 0, uint04 size = Constant<uint04>::NaN);
231 const Buffer<String>& values() const { return m_values; }
232 const Buffer<TranslatedString>& displayValues() const { return m_display_values; }
233 void showPopup() override;
234 void hidePopup() override;
235 void resizeEvent(QResizeEvent* event) override;
236 void paintEvent(QPaintEvent*) override;
237 void wheelEvent(QWheelEvent* e) override;
238 void lockInCustomValue();
239 protected:
240 void refreshWidget();
241 void adjustLineEditSize();
242 virtual void onSorted(const Buffer<uint04>& swap_indices);
243 protected slots:
244 void onEdit();
245 void onEditTextChangedSlot();
246 void onChange();
247 signals:
248 void edited();
249 protected:
250 bool m_is_refreshing = false;
251 bool m_is_changing = false;
255 bool m_sort_alphabetically = false;
256 bool m_is_showing_popup = false;
258 uint04 m_sort_offset = 0;
268 };
269}
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
#define NDEVR_WIDGETS_API
Definition DLLInfo.h:81
Stores an angle in an optimized format.
Definition StringStream.h:408
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:59
void add(t_type &&object)
Definition Buffer.hpp:222
constexpr t_index_type size() const
Definition Buffer.hpp:1507
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.
Definition Dictionary.h:59
Definition QCustomCombobox.h:47
Buffer< String > m_values
Definition QCustomCombobox.h:260
void setup(const TranslatedString &title, const Buffer< std::pair< TranslatedString, t_object > > &items)
Definition QCustomCombobox.h:59
bool m_using_custom
Definition QCustomCombobox.h:253
const TranslatedString & suffix() const
Definition QCustomCombobox.h:226
t_type getAs(uint04 index) const
Definition QCustomCombobox.h:198
String m_current_custom
Definition QCustomCombobox.h:262
TranslatedString m_suffix
Definition QCustomCombobox.h:263
void setValue(const TranslatedString &t_value)
Definition QCustomCombobox.h:207
std::enable_if< ObjectInfo< t_enum >::Enum >::type set(t_enum value)
Definition QCustomCombobox.h:186
void setAutoFormat(bool auto_format)
Definition QCustomCombobox.h:184
uint04 m_last_index
Definition QCustomCombobox.h:259
void setValue(const t_type &t_value)
Definition QCustomCombobox.h:203
Dictionary< uint04, uint04 > enum_to_index
Definition QCustomCombobox.h:265
bool m_auto_format
Definition QCustomCombobox.h:254
bool isUsingCustom() const
Definition QCustomCombobox.h:56
void remove(const t_type &value, bool refresh_widget=false)
Definition QCustomCombobox.h:145
const Buffer< String > & values() const
Definition QCustomCombobox.h:231
void refresh()
Definition QCustomCombobox.h:156
std::enable_if< ObjectInfo< t_object >::Number >::type setup(const TranslatedString &title, const Buffer< t_object > &items)
Definition QCustomCombobox.h:83
std::enable_if<!ObjectInfo< t_object >::Enum &&!ObjectInfo< t_object >::Number >::type setup(const TranslatedString &title, const Buffer< t_object > &items)
Definition QCustomCombobox.h:111
void setSuffix(const TranslatedString &suffix)
Definition QCustomCombobox.h:225
void setAllowCustom(bool allow_custom)
Definition QCustomCombobox.h:224
std::enable_if< ObjectInfo< t_enum >::Enum >::type setup(const TranslatedString &title, uint04 size)
Definition QCustomCombobox.h:96
Buffer< TranslatedString > m_display_values
Definition QCustomCombobox.h:261
void add(const t_type &value, const TranslatedString &display_value, bool refresh_widget=false)
Definition QCustomCombobox.h:137
bool m_allow_custom
Definition QCustomCombobox.h:252
QTResourceListener * m_resource_changed_listener
Definition QCustomCombobox.h:267
t_type getAs() const
Definition QCustomCombobox.h:191
const Buffer< TranslatedString > & displayValues() const
Definition QCustomCombobox.h:232
std::enable_if< ObjectInfo< t_enum >::Enum >::type setup(const TranslatedString &title, t_enum first, t_enum last)
Definition QCustomCombobox.h:123
Dictionary< uint04, uint04 > index_to_enum
Definition QCustomCombobox.h:266
QCustomLineEdit * m_line_edit
Definition QCustomCombobox.h:264
std::enable_if< ObjectInfo< t_object >::Enum >::type setup(const TranslatedString &title, const Buffer< t_object > &items)
Definition QCustomCombobox.h:71
uint04 m_custom_index
Definition QCustomCombobox.h:257
void setupResource(const TranslatedString &title, Resource< t_type > &resource)
Definition QCustomCombobox.h:162
Definition QCustomLineEdit.h:118
Definition QCustomValidator.h:43
Definition QTTools.h:183
ResourceListener * addListener(ResourceListener *listener) const
Definition ResourceListener.cpp:37
A core part of the engine, stores variables that can be listened to with ResourceListener.
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
Definition QCustomLineEdit.h:49
The core String class for the software.
Definition String.h:47
Any text displayed to the user should be defined as a TranslatedString which allows the.
Definition TranslatedString.h:13
Definition ACIColor.h:37
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:93
UnitCategory
A category of unit describing what it relates to. Any category can have some number of units.
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:115
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:195
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:176
Definition BaseValues.hpp:267
Information about the object.
Definition ObjectInfo.h:51