1 #ifndef AUBO_SCOPE_KEYBOARD_INPUT_VALIDATOR_H 2 #define AUBO_SCOPE_KEYBOARD_INPUT_VALIDATOR_H 13 namespace aubo_scope {
36 virtual bool isValid(std::string value) = 0;
45 virtual std::string
getMessage(std::string value) = 0;
51 : min_value_(min_value), max_value_(max_value)
56 : range_func_(range_func)
65 if (value[0] ==
'-') {
66 if (value.size() > std::to_string(INT_MIN).size()) {
69 }
else if (value.size() > std::to_string(INT_MAX).size()) {
73 int64_t v = atoll(value.c_str());
74 return v >= min_value_ && v <= max_value_;
82 if (value[0] ==
'-') {
83 if (value.size() > std::to_string(INT_MIN).size()) {
84 return QObject::tr(
"Error: The value is between %1 and %2.")
89 }
else if (value.size() > std::to_string(INT_MAX).size()) {
90 return QObject::tr(
"Error: The value is between %1 and %2.")
96 int64_t v = atoll(value.c_str());
97 if (v < min_value_ || v > max_value_) {
98 return QObject::tr(
"Error: The value is between %1 and %2.")
110 auto [min_value, max_value] = range_func_();
111 min_value_ = min_value;
112 max_value_ = max_value;
115 if (min_value_ > max_value_) {
116 min_value_ ^= max_value_ ^= min_value_ ^= max_value_;
121 int min_value_{ INT_MIN };
122 int max_value_{ INT_MAX };
123 std::function<std::pair<int, int>()> range_func_{
nullptr };
129 : min_value_(min_value), max_value_(max_value)
134 std::function<std::pair<uint32_t, uint32_t>()> range_func)
135 : range_func_(range_func)
144 if (value.size() > std::to_string(UINT_MAX).size()) {
148 uint64_t v = QString::fromStdString(value).toULongLong();
149 return v >= min_value_ && v <= max_value_;
157 if (value.size() > std::to_string(UINT_MAX).size()) {
158 return QObject::tr(
"Error: The value is between %1 and %2.")
164 uint64_t v = QString::fromStdString(value).toULongLong();
165 if (v < min_value_ || v > max_value_) {
166 return QObject::tr(
"Error: The value is between %1 and %2.")
178 auto [min_value, max_value] = range_func_();
179 min_value_ = min_value;
180 max_value_ = max_value;
183 if (min_value_ > max_value_) {
184 min_value_ ^= max_value_ ^= min_value_ ^= max_value_;
189 uint32_t min_value_{ 0 };
190 uint32_t max_value_{ UINT_MAX };
191 std::function<std::pair<uint32_t, uint32_t>()> range_func_{
nullptr };
197 bool is_min_inclusive =
true,
198 bool is_max_inclusive =
true)
199 : min_value_(minValue), max_value_(maxValue),
200 is_min_inclusive_(is_min_inclusive),
201 is_max_inclusive_(is_max_inclusive)
206 : range_func_(range_func)
211 std::function<std::tuple<double, double, bool, bool>()> range_func)
212 : range_func_with_flags_(range_func)
219 double val = QString::fromStdString(value).toDouble();
222 if (is_min_inclusive_) {
223 valid &= val >= min_value_;
225 valid &= val > min_value_;
228 if (is_max_inclusive_) {
229 valid &= val <= max_value_;
231 valid &= val < max_value_;
240 double val = QString::fromStdString(value).toDouble();
242 if ((is_min_inclusive_ ? val < min_value_ : val <= min_value_) ||
243 (is_max_inclusive_ ? val > max_value_ : val >= max_value_)) {
244 if (is_min_inclusive_ && is_max_inclusive_) {
245 return QObject::tr(
"Error: The value is between %1 and %2.")
246 .arg(min_value_, 0,
'g', 15)
247 .arg(max_value_, 0,
'g', 15)
249 }
else if (!is_min_inclusive_ && !is_max_inclusive_) {
250 return QObject::tr(
"Error: The value should be between %1 and " 252 .arg(min_value_, 0,
'g', 15)
253 .arg(max_value_, 0,
'g', 15)
255 }
else if (is_min_inclusive_ && !is_max_inclusive_) {
256 return QObject::tr(
"Error: The value should be greater than or " 257 "equal to %1 and less than %2.")
258 .arg(min_value_, 0,
'g', 15)
259 .arg(max_value_, 0,
'g', 15)
262 return QObject::tr(
"Error: The value should be greater than %1 " 263 "and less than or equal to %2.")
264 .arg(min_value_, 0,
'g', 15)
265 .arg(max_value_, 0,
'g', 15)
277 auto [min_value, max_value] = range_func_();
278 min_value_ = min_value;
279 max_value_ = max_value;
280 is_min_inclusive_ =
true;
281 is_max_inclusive_ =
true;
282 }
else if (range_func_with_flags_) {
283 auto [min_value, max_value, min_inclusive, max_inclusive] =
284 range_func_with_flags_();
285 min_value_ = min_value;
286 max_value_ = max_value;
287 is_min_inclusive_ = min_inclusive;
288 is_max_inclusive_ = max_inclusive;
291 if (min_value_ > max_value_) {
292 double t = min_value_;
293 min_value_ = max_value_;
299 double min_value_{ DBL_MIN };
300 double max_value_{ DBL_MAX };
301 std::function<std::pair<double, double>()> range_func_;
302 std::function<std::tuple<double, double, bool, bool>()>
303 range_func_with_flags_;
304 bool is_min_inclusive_{
true };
305 bool is_max_inclusive_{
true };
311 : min_length_(min_length), max_length_(max_length)
316 : range_func_(range_func)
324 auto value_size = QString::fromStdString(value).length();
325 return value_size >= min_length_ && value_size <= max_length_;
332 auto value_size = QString::fromStdString(value).length();
333 if (value_size < min_length_ || value_size > max_length_) {
335 "Error: The value is between %1 and %2 in length.")
347 auto [min_length, max_length] = range_func_();
348 min_length_ = min_length;
349 max_length_ = max_length;
352 if (min_length_ > max_length_) {
353 min_length_ ^= max_length_ ^= min_length_ ^= max_length_;
358 size_t min_length_{ 0 };
359 size_t max_length_{ ULONG_MAX };
360 std::function<std::pair<size_t, size_t>()> range_func_{
nullptr };
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
bool isValid(std::string value) override
StringLengthValidator(size_t min_length, size_t max_length)
DoubleRangeValidator(std::function< std::tuple< double, double, bool, bool >()> range_func)
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
bool isValid(std::string value) override
bool isValid(std::string value) override
UIntegerRangeValidator(uint32_t min_value, uint32_t max_value)
DoubleRangeValidator(double minValue, double maxValue, bool is_min_inclusive=true, bool is_max_inclusive=true)
IntegerRangeValidator(std::function< std::pair< int, int >()> range_func)
UIntegerRangeValidator(std::function< std::pair< uint32_t, uint32_t >()> range_func)
bool isValid(std::string value) override
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
IntegerRangeValidator(int min_value, int max_value)
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
DoubleRangeValidator(std::function< std::pair< double, double >()> range_func)
StringLengthValidator(std::function< std::pair< size_t, size_t >()> range_func)