GnuCash  5.6-139-g03622b03d0+
gnc-option.hpp
Go to the documentation of this file.
1 /********************************************************************\
2  * gnc-option.hpp -- Application options system *
3  * Copyright (C) 2020 John Ralls <jralls@ceridwen.us> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21  * *
22 \********************************************************************/
33 #ifndef GNC_OPTION_HPP_
34 #define GNC_OPTION_HPP_
35 
36 #include <glib.h>
37 #include <any>
38 #include <string>
39 #include <iostream>
40 #include <iomanip>
41 #include <type_traits>
42 #include <variant>
43 #include <memory>
44 #include <tuple>
45 #include "gnc-option-ui.hpp"
46 #include "gnc-option-date.hpp"
47 #include "guid.hpp"
48 
49 struct OptionClassifier;
50 class GncOptionUIItem;
51 using GncOptionUIItemPtr = std::unique_ptr<GncOptionUIItem>;
52 #ifndef SWIG //SWIG pulls in GncOwner from swig-engine.
53 struct _gncOwner;
54 using GncOwner = _gncOwner;
55 struct _QofQuery;
57 #endif
58 struct QofInstance_s;
60 template <typename ValueType> class GncOptionValue;
66 template <typename ValueType> class GncOptionRangeValue;
68 class GncOptionDateValue;
69 using GncOptionDateFormat = std::tuple<QofDateFormat, GNCDateMonthFormat, bool, std::string>;
70 using GncOptionReportPlacement = std::tuple<uint32_t, uint32_t, uint32_t>;
71 using GncOptionReportPlacementVec = std::vector<GncOptionReportPlacement>;
72 template <typename T>
74 {
75  static constexpr bool value =
76  std::is_base_of_v<OptionClassifier, std::decay_t<T>>;
77 };
78 
79 template <typename T> inline constexpr bool
80 is_OptionClassifier_v = is_OptionClassifier<T>::value;
81 
82 template <typename T, typename U>
84 {
85  static constexpr bool value = std::is_same_v<std::decay_t<T>,
86  std::decay_t<U>>;
87 };
88 
89 template <typename T, typename U> inline constexpr bool
90 is_same_decayed_v = is_same_decayed<T, U>::value;
91 
92 template <typename T>
94 {
95  static constexpr bool value =
96  (is_same_decayed_v<T, GncOptionRangeValue<int>> ||
97  is_same_decayed_v<T, GncOptionRangeValue<double>>);
98 };
99 
100 template <typename T> inline constexpr bool
101 is_RangeValue_v = is_RangeValue<T>::value;
102 
103 
104 using GncOptionVariant = std::variant<GncOptionValue<std::string>,
119 
120 using GncOptionVariantPtr = std::unique_ptr<GncOptionVariant>;
121 
122 enum class GncOptionMultichoiceKeyType
123 {
124  SYMBOL,
125  STRING,
126  NUMBER,
127 };
128 
136 {
137 public:
138  template <typename OptionType,
139  typename std::enable_if_t<is_OptionClassifier_v<OptionType>,
140  int> = 0>
141 
142  GncOption(OptionType option) :
143  m_option{std::make_unique<GncOptionVariant>(option)} {}
144  template <typename ValueType,
145  typename std::enable_if_t<!is_OptionClassifier_v<ValueType>,
146  int> = 0>
147  GncOption(const char* section, const char* name,
148  const char* key, const char* doc_string,
149  ValueType value,
150  GncOptionUIType ui_type = GncOptionUIType::INTERNAL);
151  template <typename ValueType> void set_value(ValueType value);
152  template <typename ValueType> void set_default_value(ValueType value);
153  template <typename ValueType> ValueType get_default_value() const;
154  template <typename ValueType> ValueType get_value() const;
155  void reset_default_value();
156 
157  const std::string& get_section() const;
158  const std::string& get_name() const;
159  const std::string& get_key() const;
160  const std::string& get_docstring() const;
161  void set_ui_item(GncOptionUIItemPtr&& ui_elem);
162  const GncOptionUIType get_ui_type() const;
163  void set_ui_item_selectable(bool) const noexcept;
164  GncOptionUIItem* const get_ui_item() const;
165  void set_ui_item_from_option();
166  void set_option_from_ui_item();
167  void make_internal();
168  bool is_internal();
170  void mark_saved() noexcept;
172  bool is_dirty() const noexcept;
174  bool is_changed() const noexcept;
178  bool is_multiselect() const noexcept;
180  template <typename ValueType> void get_limits(ValueType&, ValueType&,
181  ValueType&) const noexcept;
183  template <typename ValueType> bool validate(ValueType value) const;
185  uint16_t num_permissible_values() const;
187  uint16_t permissible_value_index(const char* value) const;
189  const char* permissible_value(uint16_t index) const;
191  const char* permissible_value_name(uint16_t index) const;
193  GList* account_type_list() const noexcept;
194  bool is_alternate() const noexcept;
195  void set_alternate(bool) noexcept;
199  std::string serialize() const;
204  bool deserialize(const std::string& str);
209  std::istream& in_stream(std::istream& iss);
210  friend GncOptionVariant& swig_get_option(GncOption*);
211  void set_widget_changed (std::any cb) { m_widget_changed = cb; }
212  std::any& get_widget_changed () { return m_widget_changed; }
213 private:
214  inline static const std::string c_empty_string{""};
215  GncOptionVariantPtr m_option;
216  GncOptionUIItemPtr m_ui_item{nullptr};
217  std::any m_widget_changed{};
218 };
219 
220 inline bool
221 operator<(const GncOption& right, const GncOption& left)
222 {
223  return right.get_key() < left.get_key();
224 }
225 
226 inline std::ostream&
227 operator<<(std::ostream& oss, const GncOption& opt)
228 {
229  oss << opt.serialize();
230  return oss;
231 }
232 
233 inline std::istream&
234 operator>>(std::istream& iss, GncOption& opt)
235 {
236  return opt.in_stream(iss);
237 }
238 
239 inline std::ostream&
240 output_color_value(std::ostream& oss, const std::string& value)
241 {
242  oss << "'(";
243  oss << std::fixed << std::showpoint << std::setprecision(1);
244  auto len{value.length() > 8 ? 8 : value.length()};
245  for (size_t i{}; i < len; i += 2)
246  {
247  oss << static_cast<float>(stoi(value.substr(i, 2), nullptr, 16));
248  if (i < 6)
249  oss << " ";
250  }
251  if (len < 8)
252  oss << 256.0;
253  oss << ")";
254  return oss;
255 }
256 
262 template<typename ValueType> GncOption*
263 gnc_make_option(const char* section, const char* name,
264  const char* key, const char* doc_string,
265  ValueType value, GncOptionUIType ui_type)
266 {
267  return new GncOption(section, name, key, doc_string, value, ui_type);
268 }
269 
270 #endif //GNC_OPTION_HPP_
271 
bool is_multiselect() const noexcept
Definition: gnc-option.cpp:322
The generic option-value class.
void mark_saved() noexcept
Mark the option as needing to be saved.
Definition: gnc-option.cpp:298
bool deserialize(const std::string &str)
Set the option&#39;s value from a character sequence.
Definition: gnc-option.cpp:455
uint16_t permissible_value_index(const char *value) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:371
A legal date value is a pair of either a RelativeDatePeriod, the absolute flag and a time64...
bool is_changed() const noexcept
Definition: gnc-option.cpp:314
Represents the public interface for an option.
Definition: gnc-option.hpp:135
Set one or more accounts on which to report, optionally restricted to certain account types...
This class is the parent of all option implementations.
bool validate(ValueType value) const
Not implemented for GncOptionValue.
Definition: gnc-option.cpp:335
Multichoice options have a vector of valid options (GncMultichoiceOptionChoices) and validate the sel...
uint16_t num_permissible_values() const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:356
std::string serialize() const
Get a string suitable for storage representing the option&#39;s value.
Definition: gnc-option.cpp:445
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
Definition: gnc-option.cpp:306
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.
Definition: gnc-option.cpp:386
GncOption * gnc_make_option(const char *section, const char *name, const char *key, const char *doc_string, ValueType value, GncOptionUIType ui_type)
Free function wrapping GncOption&#39;s constructor.
Definition: gnc-option.hpp:263
void get_limits(ValueType &, ValueType &, ValueType &) const noexcept
Implemented only for GncOptionNumericRange.
Definition: gnc-option.cpp:177
const char * permissible_value_name(uint16_t index) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:400
A Query.
Definition: qofquery.cpp:74
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&#39;s value from an input stream.
Definition: gnc-option.cpp:463
GList * account_type_list() const noexcept
Implemented only for GncOptionAccountListValue.
Definition: gnc-option.cpp:414
Relative date enumeration and manipulation functions.