27 #include "gnc-option-ui.hpp" 29 #include "kvp-value.hpp" 31 static const char* log_module{
"gnc.engine.gnc-option"};
35 template <
typename ValueType,
36 typename std::enable_if_t<!is_OptionClassifier_v<ValueType>,
38 GncOption::GncOption(
const char* section,
const char* name,
39 const char* key,
const char* doc_string,
41 m_option{std::make_unique<GncOptionVariant>(
42 std::in_place_type<GncOptionValue<ValueType>>,
43 section, name, key, doc_string, value, ui_type)}
47 template <
typename ValueType> ValueType
48 GncOption::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();
80 template <
typename ValueType> ValueType
81 GncOption::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();
111 template <
typename ValueType>
void 112 GncOption::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());
143 template <typename ValueType>
void 144 GncOption::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> ||
151 (is_same_decayed_v<decltype(option), GncOptionDateValue> &&
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);
173 std::visit([](
auto& option) { option.reset_default_value(); }, *m_option);
176 template <
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);
188 GncOption::get_section()
const 190 return std::visit([](
const auto& option)->
const std::string& {
191 return option.m_section;
196 GncOption::get_name()
const 198 return std::visit([](
const auto& option)->
const std::string& {
199 return option.m_name;
204 GncOption::get_key()
const 206 return std::visit([](
const auto& option)->
const std::string& {
207 return option.m_sort_tag;
212 GncOption::get_docstring()
const 214 return std::visit([](
const auto& option)->
const std::string& {
215 return option.m_doc_string;
220 GncOption::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);
239 GncOption::set_ui_item_selectable(
bool selectable)
const noexcept
242 m_ui_item->set_selectable(selectable);
246 GncOption::get_ui_type()
const 249 return option.get_ui_type();
254 GncOption::get_ui_item()
const 256 return m_ui_item.get();
260 GncOption::set_ui_item_from_option()
264 m_ui_item->set_ui_item_from_option(*
this);
268 GncOption::set_option_from_ui_item()
272 m_ui_item->set_option_from_ui_item(*
this);
276 GncOption::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();
290 GncOption::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();
334 template<
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();
426 GncOption::is_alternate() const noexcept
428 return std::visit([](
auto& option) ->
bool {
429 if constexpr(is_RangeValue_v<decltype(option)>)
430 return option.is_alternate();
436 GncOption::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& {
476 template GncOption::GncOption(
const char*,
const char*,
const char*,
480 template GncOption::GncOption(
const char*,
const char*,
const char*,
486 template GncOption::GncOption(
const char*,
const char*,
const char*,
488 template GncOption::GncOption(
const char*,
const char*,
const char*,
491 template GncOption::GncOption(
const char *,
const char*,
const char*,
492 const char *, GncOptionDateFormat,
495 template bool GncOption::get_value<bool>()
const;
496 template int GncOption::get_value<int>()
const;
497 template int64_t GncOption::get_value<int64_t>()
const;
498 template double GncOption::get_value<double>()
const;
499 template uint16_t GncOption::get_value<uint16_t>()
const;
500 template const char* GncOption::get_value<const char*>()
const;
501 template std::string GncOption::get_value<std::string>()
const;
502 template const QofInstance* GncOption::get_value<const QofInstance*>()
const;
503 template const GncOwner* GncOption::get_value<const GncOwner*>()
const;
504 template gnc_commodity* GncOption::get_value<gnc_commodity*>()
const;
505 template const Account* GncOption::get_value<const Account*>()
const;
507 template GncOptionAccountList GncOption::get_value<GncOptionAccountList>()
const;
508 template GncMultichoiceOptionIndexVec GncOption::get_value<GncMultichoiceOptionIndexVec>()
const;
509 template GncOptionReportPlacementVec GncOption::get_value<GncOptionReportPlacementVec>()
const;
510 template GncOptionDateFormat GncOption::get_value<GncOptionDateFormat>()
const;
512 template bool GncOption::get_default_value<bool>()
const;
513 template int GncOption::get_default_value<int>()
const;
514 template int64_t GncOption::get_default_value<int64_t>()
const;
515 template double GncOption::get_default_value<double>()
const;
516 template const char* GncOption::get_default_value<const char*>()
const;
517 template std::string GncOption::get_default_value<std::string>()
const;
518 template const QofInstance* GncOption::get_default_value<const QofInstance*>()
const;
519 template gnc_commodity* GncOption::get_default_value<gnc_commodity*>()
const;
520 template const Account* GncOption::get_default_value<const Account*>()
const;
522 template GncOptionAccountList GncOption::get_default_value<GncOptionAccountList>()
const;
523 template GncMultichoiceOptionIndexVec GncOption::get_default_value<GncMultichoiceOptionIndexVec>()
const;
524 template GncOptionReportPlacementVec GncOption::get_default_value<GncOptionReportPlacementVec>()
const;
525 template GncOptionDateFormat GncOption::get_default_value<GncOptionDateFormat>()
const;
527 template void GncOption::set_value(
bool);
528 template void GncOption::set_value(
int);
529 template void GncOption::set_value(int64_t);
530 template void GncOption::set_value(
double);
531 template void GncOption::set_value(
char*);
532 template void GncOption::set_value(
const char*);
533 template void GncOption::set_value(std::string);
534 template void GncOption::set_value(
const QofInstance*);
535 template void GncOption::set_value(
const GncOwner*);
536 template void GncOption::set_value(gnc_commodity*);
537 template void GncOption::set_value(
const Account*);
539 template void GncOption::set_value(uint16_t);
540 template void GncOption::set_value(GncOptionAccountList);
541 template void GncOption::set_value(GncMultichoiceOptionIndexVec);
542 template void GncOption::set_value(GncOptionReportPlacementVec);
543 template void GncOption::set_value(GncOptionDateFormat);
545 template void GncOption::set_default_value(
bool);
546 template void GncOption::set_default_value(
int);
547 template void GncOption::set_default_value(int64_t);
548 template void GncOption::set_default_value(
double);
549 template void GncOption::set_default_value(
char*);
550 template void GncOption::set_default_value(
const char*);
551 template void GncOption::set_default_value(std::string);
552 template void GncOption::set_default_value(
const QofInstance*);
553 template void GncOption::set_default_value(
const Account*);
555 template void GncOption::set_default_value(uint16_t);
556 template void GncOption::set_default_value(GncOptionAccountList);
557 template void GncOption::set_default_value(GncMultichoiceOptionIndexVec);
558 template void GncOption::set_default_value(GncOptionReportPlacementVec);
559 template void GncOption::set_default_value(GncOptionDateFormat);
578 template GncOption* gnc_make_option<const std::string&>(
const char*,
584 template GncOption* gnc_make_option<bool>(
const char*,
const char*,
const char*,
586 template GncOption* gnc_make_option<int64_t>(
const char*,
const char*,
587 const char*,
const char*, int64_t,
bool is_multiselect() const noexcept
Business Interface: Object OWNERs.
void mark_saved() noexcept
Mark the option as needing to be saved.
bool deserialize(const std::string &str)
Set the option's value from a character sequence.
uint16_t permissible_value_index(const char *value) const
Implemented only for GncOptionMultiselectValue.
A legal date value is a pair of either a RelativeDatePeriod, the absolute flag and a time64...
bool is_changed() const noexcept
#define PERR(format, args...)
Log a serious error.
Represents the public interface for an option.
Set one or more accounts on which to report, optionally restricted to certain account types...
#define PWARN(format, args...)
Log a warning.
C++ Public interface for individual options.
bool validate(ValueType value) const
Not implemented for GncOptionValue.
Multichoice options have a vector of valid options (GncMultichoiceOptionChoices) and validate the sel...
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.
RelativeDatePeriod
Reporting periods relative to the current date.
Implementation templates and specializtions for GncOption values.
class GncOptionCommodityValue Commodities are stored with their namespace and mnemonic instead of the...
Used for numeric ranges and plot sizes.
bool is_dirty() const noexcept
Holds a pointer to the UI item which will control the option and an enum representing the type of the...
const char * permissible_value(uint16_t index) const
Implemented only for GncOptionMultiselectValue.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
void get_limits(ValueType &, ValueType &, ValueType &) const noexcept
Implemented only for GncOptionNumericRange.
const char * permissible_value_name(uint16_t index) const
Implemented only for GncOptionMultiselectValue.
GncOptionUIType
Used by GncOptionClassifier to indicate to dialog-options what control should be displayed for the op...
std::istream & in_stream(std::istream &iss)
Set the option's value from an input stream.
GList * account_type_list() const noexcept
Implemented only for GncOptionAccountListValue.