From 5fd53c94defb6d08bdf5069a2e561006d7e313a5 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sun, 15 Mar 2020 13:39:34 -0700 Subject: [PATCH] Provide GncOption::GetLimits for setting a NUMBER_RANGE spin button. --- libgnucash/app-utils/gnc-option-impl.hpp | 6 ++++++ libgnucash/app-utils/gnc-option.cpp | 13 +++++++++++++ libgnucash/app-utils/gnc-option.hpp | 2 ++ libgnucash/app-utils/test/gtest-gnc-option.cpp | 14 ++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/libgnucash/app-utils/gnc-option-impl.hpp b/libgnucash/app-utils/gnc-option-impl.hpp index 69951dbb13..e26e29dcf6 100644 --- a/libgnucash/app-utils/gnc-option-impl.hpp +++ b/libgnucash/app-utils/gnc-option-impl.hpp @@ -400,6 +400,12 @@ public: else throw std::invalid_argument("Validation failed, value not set."); } + void get_limits(ValueType& upper, ValueType& lower, ValueType& step) const noexcept + { + upper = m_max; + lower = m_min; + step = m_step; + } bool is_changed() const noexcept { return m_value != m_default_value; } GncOptionUIType get_ui_type() const noexcept { return m_ui_type; } void make_internal() { m_ui_type = GncOptionUIType::INTERNAL; } diff --git a/libgnucash/app-utils/gnc-option.cpp b/libgnucash/app-utils/gnc-option.cpp index b4b8e33f7b..9f9d9c28ce 100644 --- a/libgnucash/app-utils/gnc-option.cpp +++ b/libgnucash/app-utils/gnc-option.cpp @@ -123,6 +123,17 @@ GncOption::set_value(ValueType value) }, *m_option); } +template void +GncOption::get_limits(ValueType& max, ValueType& min, ValueType& step) const noexcept +{ + std::visit([&max, &min, &step](const auto& option) { + if constexpr + (std::is_same_v, + GncOptionRangeValue>) + option.get_limits(max, min, step); + }, *m_option); +} + const std::string& GncOption::get_section() const { @@ -478,6 +489,8 @@ template void GncOption::set_value(RelativeDatePeriod); template void GncOption::set_value(size_t); template void GncOption::set_value(GncMultichoiceOptionIndexVec); +template void GncOption::get_limits(double&, double&, double&) const noexcept; +template void GncOption::get_limits(int&, int&, int&) const noexcept; template bool GncOption::validate(bool) const; template bool GncOption::validate(int) const; template bool GncOption::validate(int64_t) const; diff --git a/libgnucash/app-utils/gnc-option.hpp b/libgnucash/app-utils/gnc-option.hpp index 8a1b126ba2..befa221ea6 100644 --- a/libgnucash/app-utils/gnc-option.hpp +++ b/libgnucash/app-utils/gnc-option.hpp @@ -87,6 +87,8 @@ public: void make_internal(); bool is_changed() const noexcept; bool is_multiselect() const noexcept; + template void get_limits(ValueType&, ValueType&, + ValueType&) const noexcept; template bool validate(ValueType value) const; std::size_t num_permissible_values() const; std::size_t permissible_value_index(const char* value) const; diff --git a/libgnucash/app-utils/test/gtest-gnc-option.cpp b/libgnucash/app-utils/test/gtest-gnc-option.cpp index d36b19b752..790303b7ff 100644 --- a/libgnucash/app-utils/test/gtest-gnc-option.cpp +++ b/libgnucash/app-utils/test/gtest-gnc-option.cpp @@ -622,6 +622,20 @@ TEST_F(GncRangeOption, test_setter) EXPECT_EQ(1.5, m_doubleoption.get_default_value()); } +TEST_F(GncRangeOption, test_get_info) +{ + int imax{}, imin{}, istep{}; + double dmax{}, dmin{}, dstep{}; + m_intoption.get_limits(imax, imin, istep); + m_doubleoption.get_limits(dmax, dmin, dstep); + EXPECT_EQ(30, imax); + EXPECT_EQ(1, imin); + EXPECT_EQ(1, istep); + EXPECT_EQ(1.0, dmin); + EXPECT_EQ(3.0, dmax); + EXPECT_EQ(0.1, dstep); +} + TEST_F(GncRangeOption, test_range_out) { std::ostringstream oss;