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 template<class t_type, bool is_number>
49 {
50 public:
51 static fltp08 ValueToFloat(const t_type& value)
52 {
53 UNUSED(value);
54 lib_assert(false, "Trying to convert non-number");
56 }
57 static t_type ValueFromFloat(const fltp08& value)
58 {
59 UNUSED(value);
60 lib_assert(false, "Trying to convert non-number");
62 }
63 };
64 template<class t_type>
65 class ResourceSetter<t_type, true>
66 {
67 public:
68 static fltp08 ValueToFloat(const t_type& value)
69 {
70 return cast<fltp08>(value);
71 }
72 static t_type ValueFromFloat(const fltp08& value)
73 {
74 return cast<t_type>(value);
75 }
76 };
77 template<>
79 {
80 public:
81 static fltp08 ValueToFloat(const TimeSpan& value)
82 {
83 return value.elapsedSeconds();
84 }
85 static TimeSpan ValueFromFloat(const fltp08& value)
86 {
87 return TimeSpan(value);
88 }
89 };
90 template<>
92 {
93 public:
94 static fltp08 ValueToFloat(const Angle<fltp08>& value)
95 {
96 return value.internal<false>();
97 }
98 static Angle<fltp08> ValueFromFloat(const fltp08& value)
99 {
100 return Angle<fltp08>(INTERNAL_ANGLE, value);
101 }
102 };
103 template<>
105 {
106 public:
107 static fltp08 ValueToFloat(const Angle<sint04>& value)
108 {
109 return value.internal<false>();
110 }
112 {
113 return Angle<sint04>(INTERNAL_ANGLE, value);
114 }
115 };
116 class Keypad;
117 class NDEVR_WIDGETS_API QCustomLineEdit : public QLineEdit
118 {
119 Q_OBJECT
120 public:
121 explicit QCustomLineEdit(QWidget *parent = Q_NULLPTR);
122 explicit QCustomLineEdit(const QString& value, QWidget *parent = Q_NULLPTR);
124 virtual void setTitle(const TranslatedString& title);
125 QCustomValidator& customValidator();
126 const QCustomValidator& customValidator() const;
127 void setConstantUnit(const ConstPointer<Unit>& unit, uint01 dimension = Constant<uint01>::NaN);
128 void setCustomUnit(const String& unit, UnitType fallback_unit, uint01 dimension = Constant<uint01>::NaN);
129 void setUnitType(UnitType unit, uint01 dimension = Constant<uint01>::NaN);
130 void setAsKeyboardFocus();
131 bool isValid() const;
132 void setEditable( bool editable );
133 bool isEditable() const { return m_is_editable; }
134 bool isEditing() const;
135 bool hasUserFocus() const;
136 void clearBackgroundColor();
137 void setInputHint(const TranslatedString& hint);
138 void clearInputHint();
139 void setToolTip(const TranslatedString& tooltip);
140 String getValue(bool allow_placeholder) const;
141 String getValue() const;
142 template<class t_angle_type>
144 {
145 setUnitValue(value.template as<INTERNAL_ANGLE>());
146 }
147 void setUnitValue(fltp08 value);
148 void setPlaceHolderUnitValue(fltp08 value);
149 template<class t_angle_type = fltp08>
150 Angle<t_angle_type> getUnitAngle(bool allow_placeholder = true) const
151 {
152 return Angle<t_angle_type>(INTERNAL_ANGLE, getUnitValue(allow_placeholder));
153 }
154 fltp08 getUnitValue(bool allow_placeholder = true) const;
155 template<class t_type>
156 void setup(const TranslatedString& title)
157 {
158 setTitle(title);
159 m_validator.setRegex(StringStream<t_type>::getValidRegex());
160 m_validator.setTypeInfo(GetTypeInfo<t_type>());
161 updateHints();
162 }
163 void setup(const TranslatedString& title, UnitType unit_type, uint01 dim = Constant<uint01>::NaN);
164 void setup(const TranslatedString& title, const TypeInfo& type);
165 void setup(GenericOption& option);
166
167 template<class t_type>
169 {
170 setup(option.label(), option.getResource());
171 }
172 template<class t_type>
173 void setup(const TranslatedString& title, Resource<t_type>& resource)
174 {
175 setup<t_type>(title);
176 connect(this, &QCustomLineEdit::editingFinishedSignal, this, [this, &resource]
177 {
178 if constexpr(ObjectInfo<t_type>::Number)
179 {
180 if (!customValidator().unit().isNull())
181 resource.set(ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueFromFloat(getUnitValue()));
182 else
183 resource.set(getAs<t_type>());
184 }
185 else
186 {
187 resource.set(getAs<t_type>());
188 }
189 });
190 if (m_resource_changed_listener)
191 delete m_resource_changed_listener;
192 m_resource_changed_listener = new QTResourceListener([this, &resource]
193 {
194 if constexpr(ObjectInfo<t_type>::Number)
195 {
196 if (!customValidator().unit().isNull())
197 setUnitValue(ResourceSetter<t_type, ObjectInfo<t_type>::Number>::ValueToFloat(resource.get()));
198 else
199 setValue(resource.get());
200 }
201 else
202 {
203 setValue(resource.get());
204 }
205 }, this);
206 resource.addListener(m_resource_changed_listener);
207 }
208 void setup(const TranslatedString& title, Resource<String>& resource);
209 void setAllowPlaceholderAsValue(bool allow);
210 template<class t_type>
211 t_type getAs(bool allow_placeholder) const
212 {
213 return getValue(allow_placeholder).getAs<t_type>();
214 }
215 template<class t_type>
216 t_type getAs() const
217 {
218 return getValue(m_allow_placeholder_as_value).getAs<t_type>();
219 }
220 template<class t_type>
221 typename std::enable_if<ObjectInfo<t_type>::Number, void>::type setValue(const t_type& input_value)
222 {
223 if (m_ignore_focus_lock || !hasUserFocus())
224 {
225 if(m_validator.unit().isNull())
226 m_current_value = String(input_value);
227 else if(!isNaN(m_validator.unitDimension()))
228 m_current_value = m_validator.unit()->toString(input_value, false, 0, Constant<uint04>::Max, 0, false, m_validator.unitDimension());
229 else
230 m_current_value = m_validator.unit()->toString(input_value, false, 0, Constant<uint04>::Max, 0, false);
231 QString input = m_current_value.getAs<QString>();
232 m_validator.fixup(input);
233 m_fixed_up_current_value = String(input);
234 int valid_pos = -1;
235 if (m_validator.validate(input, valid_pos))
236 m_last_valid = m_fixed_up_current_value;
237 setText(input);
238 }
239 }
240 template<class t_type>
241 typename std::enable_if<!ObjectInfo<t_type>::Number, void>::type setValue(const t_type& input_value)
242 {
243 if (m_ignore_focus_lock || !hasUserFocus())
244 {
245 String val(input_value);
246 if (m_current_value != val)
247 {
248 m_current_value = val;
249 QString input = TranslatedString(val).translation().getAs<QString>();
250 m_validator.fixup(input);
251 m_fixed_up_current_value = String(input);
252 int valid_pos = -1;
253 if (m_validator.validate(input, valid_pos))
254 m_last_valid = m_fixed_up_current_value;
255 setText(input);
256 }
257 }
258 }
259 void setValue(const TimeSpan& input_value);
260 void setValue(const Angle<sint04>& input_value)
261 {
262 setValue(String(input_value));
263 }
264 void setValue(const Angle<fltp08>& input_value)
265 {
266 setValue(String(input_value));
267 }
268 void setBackgroundColor(const RGBColor& color);
269 RGBColor backgroundColor() const;
270 template<class t_type>
271 void setPlaceholderValue(const t_type& input_value, bool allow_placeholder_as_value = true)
272 {
273 setAllowPlaceholderAsValue(allow_placeholder_as_value);
274 QString input = String(input_value).getAs<QString>();
275 m_validator.fixup(input);
276 setPlaceholderText(input);
277 }
278 void setPlaceholderValue(const TranslatedString& input_value, bool allow_placeholder_as_value = true);
279 QSize sizeHint() const override;
280 QSize minimumSizeHint() const override;
281 void wheelEvent(QWheelEvent *event) override;
282 void forceValue(const String& units);
283 bool event(QEvent* event) override;
284 const TranslatedString& inputHint() const;
285 void updateHints();
286 void setStep(fltp08 step);
287 void setAutoShowTooltip(bool auto_show_tooltip) { m_auto_show_tooltip = auto_show_tooltip; }
288 static bool UseKeypad();
289 void setPlaceholderText(const QString& value);
290 void startUserEdit(bool select_all = true);
291 void setFontMultiplier(fltp04 font_multiplier);
292 void setBold(bool is_bold);
293 protected:
294 void _setInputHint(const TranslatedString& hint);
295 void paintEvent(QPaintEvent *event) override;
296 void setupForUnit(const ConstPointer<Unit>& unit, uint01 dimension = Constant<uint01>::NaN);
297 virtual void mousePressEvent(QMouseEvent *e) override;
298 virtual void focusInEvent(QFocusEvent *e) override;
299 virtual void focusOutEvent(QFocusEvent *e) override;
300 virtual void creatUnitLister();
301 void updateFont();
302 void _setBackgroundColor(const RGBColor& color);
303
304 signals:
308 public slots:
309 void onFinishedSlot();
310 void updateColor();
311 void updateCharColors();
312 void onEdited();
313 protected:
322 RGBColor m_background_color = Constant<RGBColor>::NaN;
323 QTResourceListener* m_resource_changed_listener = nullptr;
324 QTResourceListener* m_unit_changed_listener = nullptr;
326 fltp04 m_font_multiplier = 1.0f;
327 bool m_is_editable = true;
328 bool m_auto_show_tooltip = true;
329 bool m_suppress_focus_out = false;
330 bool m_allow_placeholder_as_value = true;
331 bool m_is_bold = false;
332 bool m_ignore_focus_lock = false;
333 bool m_has_edited = false;
334 };
335}
#define UNUSED(expr)
Definition BaseValues.hpp:433
#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:352
constexpr t_type internal() const
Definition Angle.h:313
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
Definition GraphicsPipeline.h:42
Definition QCustomLineEdit.h:118
bool isEditable() const
Definition QCustomLineEdit.h:133
void setup(const TranslatedString &title)
Definition QCustomLineEdit.h:156
TranslatedString m_tooltip
Definition QCustomLineEdit.h:321
t_type getAs(bool allow_placeholder) const
Definition QCustomLineEdit.h:211
String m_current_value
Definition QCustomLineEdit.h:314
String m_fixed_up_current_value
Definition QCustomLineEdit.h:315
void setup(const TranslatedString &title, Resource< t_type > &resource)
Definition QCustomLineEdit.h:173
QCustomValidator m_validator
Definition QCustomLineEdit.h:325
TranslatedString m_title
Definition QCustomLineEdit.h:320
String m_custom_unit
Definition QCustomLineEdit.h:317
std::enable_if< ObjectInfo< t_type >::Number, void >::type setValue(const t_type &input_value)
Definition QCustomLineEdit.h:221
void setUnitValue(Angle< t_angle_type > value)
Definition QCustomLineEdit.h:143
void setValue(const Angle< sint04 > &input_value)
Definition QCustomLineEdit.h:260
void setAutoShowTooltip(bool auto_show_tooltip)
Definition QCustomLineEdit.h:287
void setPlaceholderValue(const t_type &input_value, bool allow_placeholder_as_value=true)
Definition QCustomLineEdit.h:271
t_type getAs() const
Definition QCustomLineEdit.h:216
Angle< t_angle_type > getUnitAngle(bool allow_placeholder=true) const
Definition QCustomLineEdit.h:150
TranslatedString m_input_hint
Definition QCustomLineEdit.h:318
void setup(ApplicationOption< t_type > &option)
Definition QCustomLineEdit.h:168
std::enable_if<!ObjectInfo< t_type >::Number, void >::type setValue(const t_type &input_value)
Definition QCustomLineEdit.h:241
void setValue(const Angle< fltp08 > &input_value)
Definition QCustomLineEdit.h:264
TranslatedString m_custom_input_hint
Definition QCustomLineEdit.h:319
String m_last_valid
Definition QCustomLineEdit.h:316
Definition QCustomValidator.h:43
Definition QTTools.h:183
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:53
ResourceListener * addListener(ResourceListener *listener) const
Definition ResourceListener.cpp:37
Definition Toggle.h:41
const T & get() const
Definition Resource.h:67
void set(const T &info, bool check_equal=true)
Definition Resource.h:49
static fltp08 ValueToFloat(const Angle< fltp08 > &value)
Definition QCustomLineEdit.h:94
static Angle< fltp08 > ValueFromFloat(const fltp08 &value)
Definition QCustomLineEdit.h:98
static Angle< sint04 > ValueFromFloat(const fltp08 &value)
Definition QCustomLineEdit.h:111
static fltp08 ValueToFloat(const Angle< sint04 > &value)
Definition QCustomLineEdit.h:107
static fltp08 ValueToFloat(const TimeSpan &value)
Definition QCustomLineEdit.h:81
static TimeSpan ValueFromFloat(const fltp08 &value)
Definition QCustomLineEdit.h:85
static fltp08 ValueToFloat(const t_type &value)
Definition QCustomLineEdit.h:68
static t_type ValueFromFloat(const fltp08 &value)
Definition QCustomLineEdit.h:72
Definition QCustomLineEdit.h:49
static fltp08 ValueToFloat(const t_type &value)
Definition QCustomLineEdit.h:51
static t_type ValueFromFloat(const fltp08 &value)
Definition QCustomLineEdit.h:57
Definition String.h:40
t_type getAs() const
Definition String.h:334
Definition StringStream.h:62
Definition TimeSpan.h:40
constexpr fltp08 elapsedSeconds() const
Definition TimeSpan.h:105
Definition TranslatedString.h:9
NDEVR_BASE_API const String & translation() const
Definition TranslatedString.cpp:104
Definition TypeInfo.h:39
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:76
UnitType
Definition Unit.h:10
float fltp04
Defines an alias representing a 4 byte floating-point number.
Definition BaseValues.hpp:157
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
@ INTERNAL_ANGLE
Definition Angle.h:67
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
Definition BaseValues.hpp:272
Definition GenericOptions.h:53
Information about the object.
Definition ObjectInfo.h:56