From 7c88f20e1e6bf4b24ea6c0631cccc20867c024bb Mon Sep 17 00:00:00 2001 From: John Jones-Steele <82226755+jjjoness@users.noreply.github.com> Date: Mon, 10 Jan 2022 11:49:22 +0000 Subject: [PATCH] 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> --- .../Components/Widgets/SpinBox.cpp | 2 +- .../AzQtComponents/Tests/AzQtComponentTests.cpp | 16 ++++++++++++++++ .../AzQtComponents/Utilities/Conversions.cpp | 16 ++++++++++++---- .../AzQtComponents/Utilities/Conversions.h | 2 +- .../AzToolsFramework/Tests/SpinBoxTests.cpp | 8 ++++---- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/SpinBox.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/SpinBox.cpp index 8ace8d0f40..e9f484ee11 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/SpinBox.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/SpinBox.cpp @@ -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) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Tests/AzQtComponentTests.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Tests/AzQtComponentTests.cpp index 3c911c8acc..d87b238250 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Tests/AzQtComponentTests.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Tests/AzQtComponentTests.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include // 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"); +} diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp index bef41bb487..542bfaf7c8 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp @@ -39,15 +39,23 @@ namespace AzQtComponents return AZ::Color(static_cast(rgb.redF()), static_cast(rgb.greenF()), static_cast(rgb.blueF()), static_cast(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) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.h b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.h index 84e874fc5a..29988545f7 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.h +++ b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.h @@ -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) diff --git a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp index 2d524cbcf4..f885240fde 100644 --- a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp +++ b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp @@ -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); }