NDEVR
API Documentation
QCustomValidator.h
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: QCustomValidator
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/Unit.h>
35#include <NDEVR/TypeInfo.h>
36#include <NDEVR/Pointer.h>
37#include <NDEVR/String.h>
38#include <QValidator>
39namespace NDEVR
40{
41 struct Unit;
42
48 class NDEVR_WIDGETS_API QCustomValidator : public QValidator
49 {
50 Q_OBJECT
51 public:
68
85 public:
90 explicit QCustomValidator(QObject *parent = nullptr);
96 explicit QCustomValidator(const TypeInfo& info, QObject *parent = nullptr);
102 virtual ~QCustomValidator();
107 void fixup(QString& input) const override;
112 void fixup(StringAllocatingView& input) const;
119 void fixup(StringAllocatingView& input, bool to_unit, bool from_unit = true) const;
126 QValidator::State validate(QString& input, int& pos) const override;
133
145 {
146 m_number_of_decimals = decimals;
147 }
148
152 {
153 m_number_of_decimals = Constant<uint04>::Invalid;
154 }
155
159 void setCustomCallback(const std::function<ValidityMessage(const StringView&)>& callback) { m_custom_validation_step = callback; }
164 void setAllowWrapping(bool allow_wrapping) { m_allow_wrapping = allow_wrapping; }
169 void setAllowEmpty(bool allow_empty) { m_allow_empty = allow_empty; }
174 void setAllowNewline(bool allow_newline) { m_allow_newline = allow_newline; }
179 void setMax(fltp08 max) { m_max = max; }
184 void setMin(fltp08 min) { m_min = min; }
189 fltp08 getMax() const { return m_max; }
194 fltp08 getMin() const { return m_min; }
199 template<class t_angle_type>
200 void setMax(Angle<t_angle_type> max) { m_max = max.template as<INTERNAL_ANGLE>(); }
205 template<class t_angle_type>
206 void setMin(Angle<t_angle_type> min) { m_min = min.template as<INTERNAL_ANGLE>(); }
211 [[nodiscard]] fltp08 max() const { return m_max; }
216 [[nodiscard]] fltp08 min() const { return m_min; }
221 void setStep(fltp08 step) { m_step = step; }
226 [[nodiscard]] fltp08 step() const;
236 [[nodiscard]] const String& regex() const { return m_regex; }
242 void setUnitValidator(const ConstPointer<Unit>& unit, uint01 dimension = Constant<uint01>::Invalid);
252 [[nodiscard]] const ConstPointer<Unit>& unit() const { return m_unit; }
257 [[nodiscard]] const UnitCategory& unitType() const { return m_unit_type; }
262 void setTypeInfo(const TypeInfo& info) { m_type_info = info; }
267 [[nodiscard]] const TypeInfo& typeInfo() const { return m_type_info; }
272 [[nodiscard]] uint01 unitDimension() const { return m_unit_dimension; }
279 private:
280 ConstPointer<Unit> m_unit;
281 UnitCategory m_unit_type;
282 uint01 m_unit_dimension;
283 TypeInfo m_type_info;
284 uint04 m_number_of_decimals;
285 String m_regex;
286 fltp08 m_max;
287 fltp08 m_min;
288 fltp08 m_step;
289 bool m_allow_wrapping;
290 bool m_allow_empty;
291 bool m_allow_newline = true;
292 std::function<ValidityMessage(const StringView&)> m_custom_validation_step = nullptr;
293 };
294}
Stores an angle in an optimized internal format with support for efficient trigonometric operations.
Definition Angle.h:83
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:54
Provides a constant, unmodifiable pointer that has shared ownership of a dynamically allocated object...
Definition Pointer.hpp:276
void fixup(StringAllocatingView &input, bool to_unit, bool from_unit=true) const
Fixes up an NDEVR string input with optional unit conversion.
void setMin(fltp08 min)
Sets the minimum allowed numeric value.
const TypeInfo & typeInfo() const
Returns the type information used by this validator.
fltp08 getMax() const
Returns the maximum allowed numeric value.
QCustomValidator(const TypeInfo &info, QObject *parent=nullptr)
Constructs a validator configured for the specified type info.
InvalidProperties getValidity(const StringView &input) const
Returns detailed validity information for the given input string.
fltp08 min() const
Returns the minimum allowed numeric value.
void fixup(StringAllocatingView &input) const
Fixes up an NDEVR string input by correcting or clamping invalid values.
const String & regex() const
Returns the regular expression pattern for validation.
uint01 unitDimension() const
Returns the unit dimension index used for multi-dimensional unit conversions.
void setMax(Angle< t_angle_type > max)
Sets the maximum allowed value from an angle type, converting to internal units.
TranslatedString getMessageString(ValidityMessage message)
Returns a user-facing translated message for the given validity state.
void setMin(Angle< t_angle_type > min)
Sets the minimum allowed value from an angle type, converting to internal units.
void fixup(QString &input) const override
Fixes up a Qt string input by correcting or clamping invalid values.
void setNumberOfDecimals(uint04 decimals)
Sets the maximum number of allowed decimal places.
void setAllowWrapping(bool allow_wrapping)
Sets whether values are allowed to wrap from max to min and vice versa.
fltp08 max() const
Returns the maximum allowed numeric value.
void setUnitValidator(const ConstPointer< Unit > &unit, uint01 dimension=Constant< uint01 >::Invalid)
Sets the unit used for converting between display and internal values.
void setAllowNewline(bool allow_newline)
Sets whether newline characters are allowed in the input.
fltp08 getMin() const
Returns the minimum allowed numeric value.
void setMax(fltp08 max)
Sets the maximum allowed numeric value.
void setStep(fltp08 step)
Sets the step increment used for value adjustment (e.g., spin box arrows).
void setCustomCallback(const std::function< ValidityMessage(const StringView &)> &callback)
Sets a custom validation callback that returns a ValidityMessage for the input.
const UnitCategory & unitType() const
Returns the unit category type.
QCustomValidator(QObject *parent=nullptr)
Constructs a validator with default settings.
ValidityMessage
Describes the type of validation failure encountered during input checking.
@ e_value_is_not_a_real_number
The input is not a valid real number.
@ e_value_too_low
The input value is below the minimum.
@ e_too_many_decimals
The input has more decimal places than allowed.
@ e_unexpected_format
The input has an unexpected format.
@ e_is_valid
The input is valid.
@ e_file_exists
The input refers to a file that already exists.
@ e_string_is_too_short
The input string is shorter than the minimum length.
@ e_string_is_too_long
The input string exceeds the maximum length.
@ e_value_too_high
The input value exceeds the maximum.
@ e_value_is_empty
The input is empty when not allowed.
QCustomValidator(const QCustomValidator &validator)
Copy constructor.
const ConstPointer< Unit > & unit() const
Returns the unit used for validation and conversion.
void setUnitType(UnitCategory type)
Sets the unit category type used for unit validation.
void setRegex(const StringView &regex)
Sets a regular expression pattern that the input must match.
void clearNumberOfDecimals()
Clears the decimal place limit, allowing any number of decimals.
QValidator::State validate(QString &input, int &pos) const override
Validates a Qt string input and returns the validation state.
const QCustomValidator & operator=(const QCustomValidator &other)
Assignment operator.
fltp08 step() const
Returns the step increment value.
void setTypeInfo(const TypeInfo &info)
Sets the type information used to configure validation rules.
void setAllowEmpty(bool allow_empty)
Sets whether an empty input is considered valid.
This class is like a string view, but may optionally store the data internally Useful if the return t...
Definition String.h:1278
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
Stores information about a type, relevant for certain templated functions.
Definition TypeInfo.h:43
The primary namespace for the NDEVR SDK.
UnitCategory
A category of unit describing what it relates to.
Definition Unit.h:15
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Contains the result of a validation check, including the validity state, a suggested fix-up message,...
Bounds< 1, uint04 > range
The character range of the invalid portion.
ValidityMessage validity
The validity state of the input.
StringAllocatingView fixed_up_message
A suggested corrected version of the input.
InvalidProperties(ValidityMessage validity)
Constructs invalid properties with the given validity state.
Defines a unit of measurement for converting and displaying data values.
Definition Unit.h:39