27#include "gnc-option-ui.hpp"
29#include "kvp-value.hpp"
31static const char* log_module{
"gnc.engine.gnc-option"};
35template <
typename ValueType,
36 typename std::enable_if_t<!is_OptionClassifier_v<ValueType>,
38GncOption::GncOption(
const char* section,
const char* name,
39 const char* key,
const char* doc_string,
41 m_option{std::make_unique<GncOptionVariant>(
43 section, name, key, doc_string, value, ui_type)}
47template <
typename ValueType> ValueType
48GncOption::get_value()
const
51 [](
const auto& option)->ValueType {
52 if constexpr (is_same_decayed_v<
decltype(option.get_value()),
54 return option.get_value();
55 if constexpr (is_same_decayed_v<
decltype(option),
58 if constexpr (is_same_decayed_v<ValueType,
60 return option.get_period();
61 if constexpr (std::is_same_v<ValueType, time64>)
62 return option.get_value();
63 if constexpr (std::is_same_v<ValueType, uint16_t>)
64 return option.get_period_index();
67 if constexpr (is_same_decayed_v<
decltype(option),
70 if constexpr (std::is_same_v<ValueType, uint16_t>)
71 return option.get_index();
72 if constexpr (is_same_decayed_v<ValueType,
73 GncMultichoiceOptionIndexVec>)
74 return option.get_multiple();
80template <
typename ValueType> ValueType
81GncOption::get_default_value()
const
84 [](
const auto& option)->ValueType {
85 if constexpr (is_same_decayed_v<
decltype(option.get_value()),
87 return option.get_default_value();
88 if constexpr (is_same_decayed_v<
decltype(option),
91 if constexpr (is_same_decayed_v<ValueType,
93 return option.get_default_period();
94 if constexpr (std::is_same_v<ValueType, time64>)
95 return option.get_value();
96 if constexpr (std::is_same_v<ValueType, uint16_t>)
97 return option.get_default_period_index();
101 (is_same_decayed_v<
decltype(option),
103 is_same_decayed_v<ValueType,
104 GncMultichoiceOptionIndexVec>)
105 return option.get_default_multiple();
111template <
typename ValueType>
void
112GncOption::set_value(ValueType value)
115 [value](
auto& option) {
117 (is_same_decayed_v<
decltype(option.get_value()), ValueType> ||
118 is_same_decayed_v<
decltype(option), GncOptionDateFormat> ||
119 (is_same_decayed_v<
decltype(option),
121 (is_same_decayed_v<ValueType, RelativeDatePeriod> ||
122 std::is_same_v<ValueType, time64> ||
123 std::is_same_v<ValueType, uint16_t>)))
124 option.set_value(value);
125 else if constexpr (is_same_decayed_v<
decltype(option),
128 if constexpr (is_same_decayed_v<ValueType,
129 GncMultichoiceOptionIndexVec>)
130 option.set_multiple(value);
132 (std::is_same_v<ValueType, uint16_t> ||
133 is_same_decayed_v<ValueType, std::string> ||
134 std::is_same_v<std::remove_cv<ValueType>,
char*>)
135 option.set_value(value);
138 PWARN(
"No set_value handler: get_value returns %s, value_type is %s",
139 typeid(option.get_value()).name(),
typeid(value).name());
143template <
typename ValueType>
void
144GncOption::set_default_value(ValueType value)
147 [value](
auto& option) {
149 (is_same_decayed_v<
decltype(option.get_value()), ValueType>||
150 is_same_decayed_v<
decltype(option), GncOptionDateFormat> ||
152 (is_same_decayed_v<ValueType, RelativeDatePeriod> ||
153 std::is_same_v<ValueType, time64> ||
154 std::is_same_v<ValueType, uint16_t>)))
155 option.set_default_value(value);
156 if constexpr (is_same_decayed_v<
decltype(option),
159 if constexpr (is_same_decayed_v<ValueType,
160 GncMultichoiceOptionIndexVec>)
161 option.set_default_multiple(value);
163 (std::is_same_v<ValueType, uint16_t> ||
164 is_same_decayed_v<ValueType, std::string> ||
165 std::is_same_v<std::remove_cv<ValueType>,
char*>)
166 option.set_default_value(value);
171GncOption::reset_default_value()
173 std::visit([](
auto& option) { option.reset_default_value(); }, *m_option);
176template <
typename ValueType>
void
179 std::visit([&max, &min, &step](
const auto& option) {
181 (is_same_decayed_v<
decltype(option),
183 option.get_limits(max, min, step);
188GncOption::get_section()
const
190 return std::visit([](
const auto& option)->
const std::string& {
191 return option.m_section;
196GncOption::get_name()
const
198 return std::visit([](
const auto& option)->
const std::string& {
199 return option.m_name;
204GncOption::get_key()
const
206 return std::visit([](
const auto& option)->
const std::string& {
207 return option.m_sort_tag;
212GncOption::get_docstring()
const
214 return std::visit([](
const auto& option)->
const std::string& {
215 return option.m_doc_string;
220GncOption::set_ui_item(GncOptionUIItemPtr&& ui_item)
223 auto opt_ui_type = std::visit([](
const auto& option)->
GncOptionUIType {
224 return option.get_ui_type();
228 if (ui_item && ui_item->get_ui_type() != opt_ui_type)
230 PERR(
"Setting option %s:%s UI element failed, mismatched UI types.",
231 get_section().c_str(), get_name().c_str());
235 m_ui_item = std::move(ui_item);
239GncOption::set_ui_item_selectable(
bool selectable)
const noexcept
242 m_ui_item->set_selectable(selectable);
246GncOption::get_ui_type()
const
249 return option.get_ui_type();
254GncOption::get_ui_item()
const
256 return m_ui_item.get();
260GncOption::set_ui_item_from_option()
264 m_ui_item->set_ui_item_from_option(*
this);
268GncOption::set_option_from_ui_item()
272 m_ui_item->set_option_from_ui_item(*
this);
276GncOption::make_internal()
280 PERR(
"Option %s:%s has a UI Element, can't be INTERNAL.",
281 get_section().c_str(), get_name().c_str());
284 std::visit([](
auto& option) {
285 option.make_internal();
290GncOption::is_internal()
292 return std::visit([](
auto& option)->
bool {
293 return option.is_internal();
300 std::visit([](
auto& option)->
void {
308 return std::visit([](
const auto& option)->
bool {
309 return option.is_dirty();
316 return std::visit([](
const auto& option)->
bool {
317 return option.is_changed();
325 [](
const auto& option)->
bool {
326 if constexpr (is_same_decayed_v<
decltype(option),
328 return option.is_multiselect();
334template<
typename ValueType>
bool
338 [value] (
const auto& option) ->
bool {
339 if constexpr ((is_same_decayed_v<
decltype(option),
341 is_same_decayed_v<ValueType, std::string>) ||
342 (is_same_decayed_v<
decltype(option),
344 is_same_decayed_v<ValueType,
345 GncMultichoiceOptionIndexVec>) ||
346 (is_same_decayed_v<
decltype(option),
348 is_same_decayed_v<ValueType, gnc_commodity*>))
349 return option.validate(value);
359 [] (
const auto& option) -> uint16_t {
360 if constexpr (is_same_decayed_v<
decltype(option),
362 is_same_decayed_v<
decltype(option),
364 return option.num_permissible_values();
374 [&value] (
const auto& option) -> uint16_t {
375 if constexpr (is_same_decayed_v<
decltype(option),
377 is_same_decayed_v<
decltype(option),
379 return option.permissible_value_index(value);
388 return std::visit([index] (
const auto& option) ->
const char* {
389 if constexpr (std::is_same_v<std::decay_t<
decltype(option)>,
391 std::is_same_v<std::decay_t<
decltype(option)>,
393 return option.permissible_value(index);
402 return std::visit([index] (
const auto& option) ->
const char* {
403 if constexpr (std::is_same_v<std::decay_t<
decltype(option)>,
405 std::is_same_v<std::decay_t<
decltype(option)>,
407 return option.permissible_value_name(index);
416 return std::visit([] (
const auto& option) -> GList* {
417 if constexpr (std::is_same_v<std::decay_t<
decltype(option)>,
419 return option.account_type_list();
426GncOption::is_alternate() const noexcept
428 return std::visit([](
auto& option) ->
bool {
429 if constexpr(is_RangeValue_v<
decltype(option)>)
430 return option.is_alternate();
436GncOption::set_alternate(
bool alt)
noexcept
438 std::visit([alt](
auto& option) {
439 if constexpr(is_RangeValue_v<
decltype(option)>)
440 option.set_alternate(alt);
447 if (m_option->valueless_by_exception())
448 return "Valueless Option";
449 return std::visit([&](
auto& option) -> std::string {
450 return option.serialize();
457 return std::visit([&str](
auto& option) ->
bool {
458 return option.deserialize(str);
465 return std::visit([&iss](
auto& option) -> std::istream& {
476template GncOption::GncOption(
const char*,
const char*,
const char*,
480template GncOption::GncOption(
const char*,
const char*,
const char*,
486template GncOption::GncOption(
const char*,
const char*,
const char*,
488template GncOption::GncOption(
const char*,
const char*,
const char*,
491template GncOption::GncOption(
const char *,
const char*,
const char*,
492 const char *, GncOptionDateFormat,
495template bool GncOption::get_value<bool>()
const;
496template int GncOption::get_value<int>()
const;
497template int64_t GncOption::get_value<int64_t>()
const;
498template double GncOption::get_value<double>()
const;
499template uint16_t GncOption::get_value<uint16_t>()
const;
500template const char* GncOption::get_value<const char*>()
const;
501template std::string GncOption::get_value<std::string>()
const;
502template const QofInstance* GncOption::get_value<const QofInstance*>()
const;
503template const GncOwner* GncOption::get_value<const GncOwner*>()
const;
504template gnc_commodity* GncOption::get_value<gnc_commodity*>()
const;
505template const Account* GncOption::get_value<const Account*>()
const;
507template GncOptionAccountList GncOption::get_value<GncOptionAccountList>()
const;
508template GncMultichoiceOptionIndexVec GncOption::get_value<GncMultichoiceOptionIndexVec>()
const;
509template GncOptionReportPlacementVec GncOption::get_value<GncOptionReportPlacementVec>()
const;
510template GncOptionDateFormat GncOption::get_value<GncOptionDateFormat>()
const;
512template bool GncOption::get_default_value<bool>()
const;
513template int GncOption::get_default_value<int>()
const;
514template int64_t GncOption::get_default_value<int64_t>()
const;
515template double GncOption::get_default_value<double>()
const;
516template const char* GncOption::get_default_value<const char*>()
const;
517template std::string GncOption::get_default_value<std::string>()
const;
518template const QofInstance* GncOption::get_default_value<const QofInstance*>()
const;
519template gnc_commodity* GncOption::get_default_value<gnc_commodity*>()
const;
520template const Account* GncOption::get_default_value<const Account*>()
const;
522template GncOptionAccountList GncOption::get_default_value<GncOptionAccountList>()
const;
523template GncMultichoiceOptionIndexVec GncOption::get_default_value<GncMultichoiceOptionIndexVec>()
const;
524template GncOptionReportPlacementVec GncOption::get_default_value<GncOptionReportPlacementVec>()
const;
525template GncOptionDateFormat GncOption::get_default_value<GncOptionDateFormat>()
const;
527template void GncOption::set_value(
bool);
528template void GncOption::set_value(
int);
529template void GncOption::set_value(int64_t);
530template void GncOption::set_value(
double);
531template void GncOption::set_value(
char*);
532template void GncOption::set_value(
const char*);
533template void GncOption::set_value(std::string);
534template void GncOption::set_value(
const QofInstance*);
535template void GncOption::set_value(
const GncOwner*);
536template void GncOption::set_value(gnc_commodity*);
537template void GncOption::set_value(
const Account*);
539template void GncOption::set_value(uint16_t);
540template void GncOption::set_value(GncOptionAccountList);
541template void GncOption::set_value(GncMultichoiceOptionIndexVec);
542template void GncOption::set_value(GncOptionReportPlacementVec);
543template void GncOption::set_value(GncOptionDateFormat);
545template void GncOption::set_default_value(
bool);
546template void GncOption::set_default_value(
int);
547template void GncOption::set_default_value(int64_t);
548template void GncOption::set_default_value(
double);
549template void GncOption::set_default_value(
char*);
550template void GncOption::set_default_value(
const char*);
551template void GncOption::set_default_value(std::string);
552template void GncOption::set_default_value(
const QofInstance*);
553template void GncOption::set_default_value(
const Account*);
555template void GncOption::set_default_value(uint16_t);
556template void GncOption::set_default_value(GncOptionAccountList);
557template void GncOption::set_default_value(GncMultichoiceOptionIndexVec);
558template void GncOption::set_default_value(GncOptionReportPlacementVec);
559template void GncOption::set_default_value(GncOptionDateFormat);
578template GncOption* gnc_make_option<const std::string&>(
const char*,
584template GncOption* gnc_make_option<bool>(
const char*,
const char*,
const char*,
586template GncOption* gnc_make_option<int64_t>(
const char*,
const char*,
587 const char*,
const char*, int64_t,
Set one or more accounts on which to report, optionally restricted to certain account types.
class GncOptionCommodityValue Commodities are stored with their namespace and mnemonic instead of the...
A legal date value is a pair of either a RelativeDatePeriod, the absolute flag and a time64,...
Multichoice options have a vector of valid options (GncMultichoiceOptionChoices) and validate the sel...
Used for numeric ranges and plot sizes.
Holds a pointer to the UI item which will control the option and an enum representing the type of the...
The generic option-value class.
Represents the public interface for an option.
Implementation templates and specializtions for GncOption values.
C++ Public interface for individual options.
Business Interface: Object OWNERs.
bool validate(ValueType value) const
Not implemented for GncOptionValue.
void get_limits(ValueType &, ValueType &, ValueType &) const noexcept
Implemented only for GncOptionNumericRange.
GList * account_type_list() const noexcept
Implemented only for GncOptionAccountListValue.
void mark_saved() noexcept
Mark the option as needing to be saved.
bool is_dirty() const noexcept
const char * permissible_value(uint16_t index) const
Implemented only for GncOptionMultiselectValue.
const char * permissible_value_name(uint16_t index) const
Implemented only for GncOptionMultiselectValue.
std::istream & in_stream(std::istream &iss)
Set the option's value from an input stream.
bool deserialize(const std::string &str)
Set the option's value from a character sequence.
bool is_multiselect() const noexcept
uint16_t permissible_value_index(const char *value) const
Implemented only for GncOptionMultiselectValue.
bool is_changed() const noexcept
uint16_t num_permissible_values() const
Implemented only for GncOptionMultiselectValue.
std::string serialize() const
Get a string suitable for storage representing the option's value.
#define PERR(format, args...)
Log a serious error.
#define PWARN(format, args...)
Log a warning.
RelativeDatePeriod
Reporting periods relative to the current date.
GncOptionUIType
Used by GncOptionClassifier to indicate to dialog-options what control should be displayed for the op...