Commit 451accf2 authored by Thomas Baumgart's avatar Thomas Baumgart
Browse files

Allow parens around an amount to be a valid when locale uses it

A locale can request numbers to be presented with parens around the
value. This change allows them to be entered in this format in the
amount fields.

CCBUG: 439819

(cherry picked from commit b3ffeec2)
parent 42fcbcab
Pipeline #71660 canceled with stage
......@@ -192,6 +192,7 @@ target_link_libraries(kmm_widgets PUBLIC
kmm_base_dialogs
kmm_base_widgets
kmm_selections
kmm_utils_platformtools
)
if (KMYMONEY_STABLE_BRANCH)
......
......@@ -15,6 +15,8 @@
// ----------------------------------------------------------------------------
// Project Includes
#include "platformtools.h"
AmountValidator::AmountValidator(QObject * parent) :
AmountValidator(-HUGE_VAL, HUGE_VAL, 1000, parent)
{
......@@ -26,3 +28,47 @@ AmountValidator::AmountValidator(double bottom, double top, int decimals,
{
setNotation(StandardNotation);
}
QValidator::State AmountValidator::validate(QString& input, int& pos) const
{
const auto openParen = QStringLiteral("(");
const auto closeParen = QStringLiteral(")");
if ((platformTools::currencySignPosition(true) == platformTools::ParensAround)
|| (platformTools::currencySignPosition(false) == platformTools::ParensAround)) {
const auto openCount = input.count(openParen);
const auto closeCount = input.count(closeParen);
// if we don't have any, use the normal validator
if ((openCount == 0) && (closeCount == 0)) {
return QDoubleValidator::validate(input, pos);
}
// more than one open and close paren is invalid
if ((openCount > 1) || (closeCount > 1)) {
return Invalid;
}
// close paren w/o open paren is invalid
if (openCount < closeCount) {
return Invalid;
}
// make sure open paren is first char
if (openCount == 1 && !input.startsWith(openParen)) {
return Invalid;
}
// make sure close paren is last char
if (closeCount == 1 && !input.endsWith(closeParen)) {
return Invalid;
}
// open and close paren count differs
if (openCount != closeCount) {
return Intermediate;
}
// remove the open and close paren and check the remainder
auto modifiedInput(input);
modifiedInput.remove(openParen);
modifiedInput.remove(closeParen);
return QDoubleValidator::validate(modifiedInput, pos);
}
return QDoubleValidator::validate(input, pos);
}
......@@ -34,6 +34,8 @@ public:
explicit AmountValidator(QObject * parent);
explicit AmountValidator(double bottom, double top, int decimals,
QObject * parent);
virtual QValidator::State validate(QString& input, int& pos) const override;
};
#endif
......@@ -8,6 +8,7 @@ add_library(testwidgets STATIC ${testwidgets_SOURCES})
target_link_libraries(testwidgets PUBLIC
Qt5::Gui
Qt5::Core
kmm_utils_platformtools
)
file(GLOB tests_sources "*-test.cpp")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment