Skip to content

Commit

Permalink
Provide GncOption::GetLimits for setting a NUMBER_RANGE spin button.
Browse files Browse the repository at this point in the history
  • Loading branch information
jralls committed Aug 1, 2021
1 parent a995343 commit 5fd53c9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libgnucash/app-utils/gnc-option-impl.hpp
Expand Up @@ -400,6 +400,12 @@ class GncOptionRangeValue : public OptionClassifier
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; }
Expand Down
13 changes: 13 additions & 0 deletions libgnucash/app-utils/gnc-option.cpp
Expand Up @@ -123,6 +123,17 @@ GncOption::set_value(ValueType value)
}, *m_option);
}

template <typename ValueType> 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<std::decay_t<decltype(option)>,
GncOptionRangeValue<ValueType>>)
option.get_limits(max, min, step);
}, *m_option);
}

const std::string&
GncOption::get_section() const
{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions libgnucash/app-utils/gnc-option.hpp
Expand Up @@ -87,6 +87,8 @@ class GncOption
void make_internal();
bool is_changed() const noexcept;
bool is_multiselect() const noexcept;
template <typename ValueType> void get_limits(ValueType&, ValueType&,
ValueType&) const noexcept;
template <typename ValueType> bool validate(ValueType value) const;
std::size_t num_permissible_values() const;
std::size_t permissible_value_index(const char* value) const;
Expand Down
14 changes: 14 additions & 0 deletions libgnucash/app-utils/test/gtest-gnc-option.cpp
Expand Up @@ -622,6 +622,20 @@ TEST_F(GncRangeOption, test_setter)
EXPECT_EQ(1.5, m_doubleoption.get_default_value<double>());
}

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;
Expand Down

0 comments on commit 5fd53c9

Please sign in to comment.