Spinboxes now correct when rounding. (#6748)

* Spinboxes now correct when rounding.

Signed-off-by: John Jones-Steele <82226755+jjjoness@users.noreply.github.com>

* Changes from PR

Signed-off-by: John Jones-Steele <82226755+jjjoness@users.noreply.github.com>

* Fixed tests after change to rounding in spinbox

Signed-off-by: John Jones-Steele <82226755+jjjoness@users.noreply.github.com>
This commit is contained in:
John Jones-Steele
2022-01-10 11:49:22 +00:00
committed by GitHub
parent afc531d4c3
commit 7c88f20e1e
5 changed files with 34 additions and 10 deletions
@@ -1460,7 +1460,7 @@ QString DoubleSpinBox::stringValue(double value, bool truncated) const
numDecimals = 0;
}
return toString(value, numDecimals, locale(), isGroupSeparatorShown());
return toString(value, numDecimals, locale(), isGroupSeparatorShown(), true);
}
void DoubleSpinBox::updateToolTip(double value)
@@ -9,6 +9,8 @@
#include <AzTest/AzTest.h>
#include <AzCore/Math/Color.h>
#include <AzQtComponents/Components/Widgets/ColorPicker/Palette.h>
#include <AzQtComponents/Utilities/Conversions.h>
#include <QLocale>
// Environments subclass from AZ::Test::ITestEnvironment
class AzQtComponentsTestEnvironment : public AZ::Test::ITestEnvironment
@@ -34,3 +36,17 @@ protected:
};
AZ_UNIT_TEST_HOOK(new AzQtComponentsTestEnvironment);
TEST(AzQtComponents, ToStringReturnsTruncatedString)
{
double testVal = 1.2399999;
QString result = AzQtComponents::toString(testVal, 3, QLocale(), false, false);
EXPECT_TRUE(result == "1.239");
}
TEST(AzQtComponents, ToStringReturnsRoundedString)
{
double testVal = 1.2399999;
QString result = AzQtComponents::toString(testVal, 3, QLocale(), false, true);
EXPECT_TRUE(result == "1.24");
}
@@ -39,15 +39,23 @@ namespace AzQtComponents
return AZ::Color(static_cast<float>(rgb.redF()), static_cast<float>(rgb.greenF()), static_cast<float>(rgb.blueF()), static_cast<float>(rgb.alphaF()));
}
QString toString(double value, int numDecimals, const QLocale& locale, bool showGroupSeparator)
QString toString(double value, int numDecimals, const QLocale& locale, bool showGroupSeparator, bool round)
{
const QChar decimalPoint = locale.decimalPoint();
const QChar zeroDigit = locale.zeroDigit();
const int numToStringDecimals = AZStd::max(numDecimals, 20);
QString retValue;
// We want to truncate, not round. toString will round, so we add extra decimal places to the formatting
// so we can remove the last values
QString retValue = locale.toString(value, 'f', (numDecimals > 0) ? numToStringDecimals : 0);
// If we want to truncate, not round, we add extra decimal places to the formatting
// so we can remove the last values otherwise we allow rounding
if (round)
{
retValue = locale.toString(value, 'f', (numDecimals > 0) ? numDecimals : 0);
}
else
{
retValue = locale.toString(value, 'f', (numDecimals > 0) ? numToStringDecimals : 0);
}
// Handle special cases when we have decimals in our value
if (numDecimals > 0)
@@ -22,7 +22,7 @@ namespace AzQtComponents
AZ_QT_COMPONENTS_API AZ::Color fromQColor(const QColor& color);
AZ_QT_COMPONENTS_API QString toString(double value, int numDecimals, const QLocale& locale, bool showGroupSeparator = false);
AZ_QT_COMPONENTS_API QString toString(double value, int numDecimals, const QLocale& locale, bool showGroupSeparator = false, bool round = false);
// Maintained for backwards compile compatibility
inline QColor ToQColor(const AZ::Color& color)
@@ -275,7 +275,7 @@ namespace UnitTest
QString testString = "0" + QString(testLocale.decimalPoint()) + "9999999";
QString value = setupTruncationTest(testString);
testString = "0" + QString(testLocale.decimalPoint()) + "999";
testString = "1" + QString(testLocale.decimalPoint()) + "0";
EXPECT_TRUE(value == testString);
}
@@ -295,19 +295,19 @@ namespace UnitTest
QString testString = "0" + QString(testLocale.decimalPoint()) + "12395";
QString value = setupTruncationTest(testString);
testString = "0" + QString(testLocale.decimalPoint()) + "123";
testString = "0" + QString(testLocale.decimalPoint()) + "124";
EXPECT_TRUE(value == testString);
testString = "0" + QString(testLocale.decimalPoint()) + "94496";
value = setupTruncationTest(testString);
testString = "0" + QString(testLocale.decimalPoint()) + "944";
testString = "0" + QString(testLocale.decimalPoint()) + "945";
EXPECT_TRUE(value == testString);
testString = "0" + QString(testLocale.decimalPoint()) + "0009999";
value = setupTruncationTest(testString);
testString = "0" + QString(testLocale.decimalPoint()) + "0";
testString = "0" + QString(testLocale.decimalPoint()) + "001";
EXPECT_TRUE(value == testString);
}