API Documentation
Loading...
Searching...
No Matches
QCustomLineEdit.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: QCustomLineEdit
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/QCustomValidator.h>
34#include "DLLInfo.h"
35#include <NDEVR/QTTools.h>
36#include <NDEVR/String.h>
37#include <NDEVR/ApplicationOptions.h>
38#include <NDEVR/ObjectInfo.h>
39#include <NDEVR/TimeSpan.h>
40#include <NDEVR/Unit.h>
41#include <NDEVR/ObjectInfo.h>
42#include <NDEVR/RGBColor.h>
43#include <QLineEdit>
44
45namespace NDEVR
46{
47
48 class Keypad;
49 /**--------------------------------------------------------------------------------------------------
50 \brief A line edit allows users to enter and edit a single line of plain text with useful editing
51 functions, including undo and redo, cut and paste, and drag and drop.
52
53 In touch mode, a popup Keypad will appear based on the type of data being input.
54 **/
55 class NDEVR_WIDGETS_API QCustomLineEdit : public QLineEdit
56 {
57 Q_OBJECT
58 public:
59 friend class QCustomComboBox;
60 explicit QCustomLineEdit(QWidget *parent = Q_NULLPTR);
61 explicit QCustomLineEdit(const QString& value, QWidget *parent = Q_NULLPTR);
63 virtual void setTitle(const TranslatedString& title);
67 void setCustomUnit(const String& unit, UnitCategory fallback_unit, uint01 dimension = Constant<uint01>::Invalid);
70 bool isValid() const;
71 void setEditable( bool editable );
72 bool isEditable() const { return m_is_editable; }
73 bool isEditing() const;
74 bool hasUserFocus() const;
76 void setInputHint(const TranslatedString& hint);
78 void setToolTip(const TranslatedString& tooltip);
79 String getValue(bool allow_placeholder) const;
81 template<class t_angle_type>
83 {
84 setUnitValue(value.template as<INTERNAL_ANGLE>());
85 }
86 void setUnitValue(fltp08 value);
88 template<class t_angle_type = fltp08>
89 Angle<t_angle_type> getUnitAngle(bool allow_placeholder = true) const
90 {
91 return Angle<t_angle_type>(INTERNAL_ANGLE, getUnitValue(allow_placeholder));
92 }
93 fltp08 getUnitValue(bool allow_placeholder = true) const;
94 template<class t_type>
95 void setup(const TranslatedString& title)
96 {
97 setTitle(title);
98 m_validator.setRegex(StringStream<t_type>::getValidRegex());
99 m_validator.setTypeInfo(GetTypeInfo<t_type>());
100 updateHints();
101 }
103 void setup(const TranslatedString& title, const TypeInfo& type);
104 void setup(GenericOption& option);
105
106 template<class t_type>
108 {
109 setup(option.label(), option.getResource());
110 }
111 template<class t_type>
112 void setup(const TranslatedString& title, Resource<t_type>& resource)
113 {
114 setup<t_type>(title);
115 connect(this, &QCustomLineEdit::editingFinishedSignal, this, [this, &resource]
116 {
117 if constexpr(ObjectInfo<t_type>::Number)
118 {
119 if (!customValidator().unit().isNull())
120 resource.set(ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueFromFloat(getUnitValue()));
121 else
122 resource.set(getAs<t_type>());
123 }
124 else
125 {
126 resource.set(getAs<t_type>());
127 }
128 });
129 if (m_resource_changed_listener)
130 delete m_resource_changed_listener;
131 m_resource_changed_listener = new QTResourceListener([this, &resource]
132 {
133 if constexpr(ObjectInfo<t_type>::Number)
134 {
135 if (!customValidator().unit().isNull())
136 setUnitValue(ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueToFloat(resource.get()));
137 else
138 setValue(resource.get());
139 }
140 else
141 {
142 setValue(resource.get());
143 }
144 }, this);
145 resource.addListener(m_resource_changed_listener);
146 }
147 void setup(const TranslatedString& title, Resource<String>& resource);
149 template<class t_type>
150 t_type getAs(bool allow_placeholder) const
151 {
152 return getValue(allow_placeholder).getAs<t_type>();
153 }
154 template<class t_type>
155 t_type getAs() const
156 {
157 return getValue(m_allow_placeholder_as_value).getAs<t_type>();
158 }
159 template<class t_type>
160 typename std::enable_if<ObjectInfo<t_type>::Number, void>::type setValue(const t_type& input_value)
161 {
162 if (m_ignore_focus_lock || !hasUserFocus())
163 {
164 if(m_validator.unit().isNull())
165 m_current_value = String(input_value);
166 else if(!IsInvalid(m_validator.unitDimension()))
167 m_current_value = m_validator.unit()->toString(input_value, false, 0, Constant<uint04>::Max, 0, false, m_validator.unitDimension());
168 else
169 m_current_value = m_validator.unit()->toString(input_value, false, 0, Constant<uint04>::Max, 0, false);
170 QString input = m_current_value.getAs<QString>();
171 m_validator.fixup(input);
172 m_fixed_up_current_value = String(input);
173 int valid_pos = -1;
174 if (m_validator.validate(input, valid_pos))
175 m_last_valid = m_fixed_up_current_value;
176 setText(input);
177 }
178 }
179 template<class t_type>
180 typename std::enable_if<!ObjectInfo<t_type>::Number, void>::type setValue(const t_type& input_value)
181 {
182 if (m_ignore_focus_lock || !hasUserFocus())
183 {
184 String val(input_value);
185 if (m_current_value != val)
186 {
187 m_current_value = val;
188 QString input = TranslatedString(val).translation().getAs<QString>();
189 m_validator.fixup(input);
190 m_fixed_up_current_value = String(input);
191 int valid_pos = -1;
192 if (m_validator.validate(input, valid_pos))
193 m_last_valid = m_fixed_up_current_value;
194 setText(input);
195 }
196 }
197 }
198 void setValue(const TimeSpan& input_value);
199 void setValue(const Angle<sint04>& input_value)
200 {
201 setValue(String(input_value));
202 }
203 void setValue(const Angle<fltp08>& input_value)
204 {
205 setValue(String(input_value));
206 }
207 void setBackgroundColor(const RGBColor& color);
209 template<class t_type>
210 void setPlaceholderValue(const t_type& input_value, bool allow_placeholder_as_value = true)
211 {
212 setAllowPlaceholderAsValue(allow_placeholder_as_value);
213 QString input = String(input_value).getAs<QString>();
214 m_validator.fixup(input);
215 setPlaceholderText(input);
216 }
217 void setPlaceholderValue(const TranslatedString& input_value, bool allow_placeholder_as_value = true);
218 QSize sizeHint() const override;
219 QSize minimumSizeHint() const override;
220 void wheelEvent(QWheelEvent *event) override;
221 void forceValue(const String& units);
222 bool event(QEvent* event) override;
225 void setStep(fltp08 step);
226 void setAutoShowTooltip(bool auto_show_tooltip) { m_auto_show_tooltip = auto_show_tooltip; }
227 static bool UseKeypad();
228 void setPlaceholderText(const QString& value);
229 void startUserEdit(bool select_all = true);
230 void setFontMultiplier(fltp04 font_multiplier);
231 void setBold(bool is_bold);
232 private:
233 void _setInputHint(const TranslatedString& hint);
234 void paintEvent(QPaintEvent *event) override;
235 void setupForUnit(const ConstPointer<Unit>& unit, uint01 dimension = Constant<uint01>::Invalid);
236 virtual void mousePressEvent(QMouseEvent *e) override;
237 virtual void focusInEvent(QFocusEvent *e) override;
238 virtual void focusOutEvent(QFocusEvent *e) override;
239 virtual void creatUnitLister();
240 void updateFont();
241 void _setBackgroundColor(const RGBColor& color);
242
243 signals:
247 public slots:
251 void onEdited();
252 protected:
255 private:
256 String m_current_value;
257 String m_fixed_up_current_value;
258 String m_last_valid;
259 String m_custom_unit;
260 TranslatedString m_input_hint;
261 TranslatedString m_custom_input_hint;
262 RGBColor m_background_color = Constant<RGBColor>::Invalid;
263 QTResourceListener* m_resource_changed_listener = nullptr;
264 QTResourceListener* m_unit_changed_listener = nullptr;
265 QCustomValidator m_validator;
266 fltp04 m_font_multiplier = 1.0f;
267 bool m_is_editable = true;
268 bool m_auto_show_tooltip = true;
269 bool m_suppress_focus_out = false;
270 bool m_allow_placeholder_as_value = true;
271 bool m_is_bold = false;
272 bool m_ignore_focus_lock = false;
273 bool m_has_edited = false;
274 private:
275 /**--------------------------------------------------------------------------------------------------
276 \brief A basic class for helping convert a Value into a resource.
277 **/
278 template<class t_type, bool is_number>
279 class ResourceSetter
280 {
281 public:
282 static fltp08 ValueToFloat(const t_type& value)
283 {
284 UNUSED(value);
285 lib_assert(false, "Trying to convert non-number");
287 }
288 static t_type ValueFromFloat(const fltp08& value)
289 {
290 UNUSED(value);
291 lib_assert(false, "Trying to convert non-number");
293 }
294 };
295 template<class t_type>
296 class ResourceSetter<t_type, true>
297 {
298 public:
299 static fltp08 ValueToFloat(const t_type& value)
300 {
301 return cast<fltp08>(value);
302 }
303 static t_type ValueFromFloat(const fltp08& value)
304 {
305 return cast<t_type>(value);
306 }
307 };
308 template<>
309 class ResourceSetter<TimeSpan, ObjectInfo<TimeSpan>::Number>
310 {
311 public:
312 static fltp08 ValueToFloat(const TimeSpan& value)
313 {
314 return value.elapsedSeconds();
315 }
316 static TimeSpan ValueFromFloat(const fltp08& value)
317 {
318 return TimeSpan(value);
319 }
320 };
321 template<>
322 class ResourceSetter<Angle<fltp08>, ObjectInfo<Angle<fltp08>>::Number>
323 {
324 public:
325 static fltp08 ValueToFloat(const Angle<fltp08>& value)
326 {
327 return value.internal<false>();
328 }
329 static Angle<fltp08> ValueFromFloat(const fltp08& value)
330 {
331 return Angle<fltp08>(INTERNAL_ANGLE, value);
332 }
333 };
334 template<>
335 class ResourceSetter<Angle<sint04>, ObjectInfo<Angle<sint04>>::Number>
336 {
337 public:
338 static fltp08 ValueToFloat(const Angle<sint04>& value)
339 {
340 return value.internal<false>();
341 }
342 static Angle<sint04> ValueFromFloat(const fltp08& value)
343 {
344 return Angle<sint04>(INTERNAL_ANGLE, value);
345 }
346 };
347 };
348}
#define UNUSED(expr)
Definition BaseValues.hpp:409
#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 default object to store data of any type that should persist through sessions of the application.
Definition Button.h:57
Resource< t_type > & getResource()
Definition ApplicationOptions.h:106
const TranslatedString & label() const
Definition ApplicationOptions.h:144
Provides a constant, unmodifiable pointer that has shared ownership of a dynamically allocated object...
Definition GraphicsPipeline.h:42
A compact way to present a list of options to the user.
Definition QCustomCombobox.h:53
A line edit allows users to enter and edit a single line of plain text with useful editing functions,...
Definition QCustomLineEdit.h:56
bool isEditable() const
Definition QCustomLineEdit.h:72
bool isEditing() const
void setup(const TranslatedString &title)
Definition QCustomLineEdit.h:95
QCustomLineEdit(const QString &value, QWidget *parent=Q_NULLPTR)
RGBColor backgroundColor() const
QSize minimumSizeHint() const override
void setUnitType(UnitCategory unit, uint01 dimension=Constant< uint01 >::Invalid)
TranslatedString m_tooltip
Definition QCustomLineEdit.h:254
const TranslatedString & inputHint() const
void setStep(fltp08 step)
String getValue() const
void forceValue(const String &units)
QSize sizeHint() const override
t_type getAs(bool allow_placeholder) const
Definition QCustomLineEdit.h:150
void setInputHint(const TranslatedString &hint)
QCustomLineEdit(QWidget *parent=Q_NULLPTR)
void setCustomUnit(const String &unit, UnitCategory fallback_unit, uint01 dimension=Constant< uint01 >::Invalid)
const QCustomValidator & customValidator() const
fltp08 getUnitValue(bool allow_placeholder=true) const
bool event(QEvent *event) override
void setEditable(bool editable)
void setPlaceholderText(const QString &value)
void setup(const TranslatedString &title, Resource< t_type > &resource)
Definition QCustomLineEdit.h:112
TranslatedString m_title
Definition QCustomLineEdit.h:253
bool hasUserFocus() const
String getValue(bool allow_placeholder) const
void setPlaceHolderUnitValue(fltp08 value)
void setToolTip(const TranslatedString &tooltip)
void setPlaceholderValue(const TranslatedString &input_value, bool allow_placeholder_as_value=true)
std::enable_if< ObjectInfo< t_type >::Number, void >::type setValue(const t_type &input_value)
Definition QCustomLineEdit.h:160
void startUserEdit(bool select_all=true)
void setUnitValue(Angle< t_angle_type > value)
Definition QCustomLineEdit.h:82
void setBackgroundColor(const RGBColor &color)
void setValue(const Angle< sint04 > &input_value)
Definition QCustomLineEdit.h:199
void setup(GenericOption &option)
void setAllowPlaceholderAsValue(bool allow)
void setAutoShowTooltip(bool auto_show_tooltip)
Definition QCustomLineEdit.h:226
void setPlaceholderValue(const t_type &input_value, bool allow_placeholder_as_value=true)
Definition QCustomLineEdit.h:210
void setup(const TranslatedString &title, Resource< String > &resource)
t_type getAs() const
Definition QCustomLineEdit.h:155
void wheelEvent(QWheelEvent *event) override
void setValue(const TimeSpan &input_value)
virtual void setTitle(const TranslatedString &title)
void setup(const TranslatedString &title, UnitCategory unit_type, uint01 dim=Constant< uint01 >::Invalid)
void setUnitValue(fltp08 value)
QCustomValidator & customValidator()
void setConstantUnit(const ConstPointer< Unit > &unit, uint01 dimension=Constant< uint01 >::Invalid)
Angle< t_angle_type > getUnitAngle(bool allow_placeholder=true) const
Definition QCustomLineEdit.h:89
void setup(ApplicationOption< t_type > &option)
Definition QCustomLineEdit.h:107
std::enable_if<!ObjectInfo< t_type >::Number, void >::type setValue(const t_type &input_value)
Definition QCustomLineEdit.h:180
static bool UseKeypad()
void setValue(const Angle< fltp08 > &input_value)
Definition QCustomLineEdit.h:203
void setup(const TranslatedString &title, const TypeInfo &type)
void setBold(bool is_bold)
void setFontMultiplier(fltp04 font_multiplier)
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
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:54
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
t_type getAs() const
Converts a string into an object. To use this function an object must have overwritten StringStream<t...
Definition String.h:143
Logic for reading or writing to a string or a user friendly, TranslatedString.
Definition StringStream.h:230
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
Definition TranslatedString.h:13
const String & translation() const
Stores information about a type, relevant for certain templated functions. To get information about a...
Definition TypeInfo.h:43
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:64
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
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
Definition BaseValues.hpp:127
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
@ INTERNAL_ANGLE
Definition Angle.h:59
UnitCategory
A category of unit describing what it relates to. Any category can have some number of units defined ...
Definition Unit.h:14
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
Stores a generic option of any type as well as some information about how the user might interact wit...
Definition GenericOptions.h:57
Information about the object.
Definition ObjectInfo.h:54