GnuCash  4.12-558-g06612b8434
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 <string>
38 #include <iostream>
39 #include <iomanip>
40 #include <variant>
41 #include <memory>
42 #include <tuple>
43 #include "gnc-option-ui.hpp"
44 #include "gnc-option-date.hpp"
45 #include "guid.hpp"
46 
47 struct OptionClassifier;
48 class GncOptionUIItem;
49 using GncOptionUIItemPtr = std::unique_ptr<GncOptionUIItem>;
50 #ifndef SWIG //SWIG pulls in GncOwner from swig-engine.
51 struct _gncOwner;
52 using GncOwner = _gncOwner;
53 #endif
54 struct _QofQuery;
56 struct QofInstance_s;
58 template <typename ValueType> class GncOptionValue;
63 template <typename ValueType> class GncOptionRangeValue;
65 class GncOptionDateValue;
66 using GncOptionReportPlacement = std::tuple<uint32_t, uint32_t, uint32_t>;
67 using GncOptionReportPlacementVec = std::vector<GncOptionReportPlacement>;
68 template <typename T>
70 {
71  static constexpr bool value =
72  std::is_base_of_v<OptionClassifier, std::decay_t<T>>;
73 };
74 
75 template <typename T> inline constexpr bool
76 is_OptionClassifier_v = is_OptionClassifier<T>::value;
77 
78 template <typename T, typename U>
80 {
81  static constexpr bool value = std::is_same_v<std::decay_t<T>,
82  std::decay_t<U>>;
83 };
84 
85 template <typename T, typename U> inline constexpr bool
86 is_same_decayed_v = is_same_decayed<T, U>::value;
87 
88 template <typename T>
90 {
91  static constexpr bool value =
92  (is_same_decayed_v<T, GncOptionRangeValue<int>> ||
93  is_same_decayed_v<T, GncOptionRangeValue<double>>);
94 };
95 
96 template <typename T> inline constexpr bool
97 is_RangeValue_v = is_RangeValue<T>::value;
98 
99 
100 using GncOptionVariant = std::variant<GncOptionValue<std::string>,
114 
115 using GncOptionVariantPtr = std::unique_ptr<GncOptionVariant>;
116 
117 enum class GncOptionMultichoiceKeyType
118 {
119  SYMBOL,
120  STRING,
121  NUMBER,
122 };
123 
131 {
132 public:
133  template <typename OptionType,
134  typename std::enable_if_t<is_OptionClassifier_v<OptionType>,
135  int> = 0>
136 
137  GncOption(OptionType option) :
138  m_option{std::make_unique<GncOptionVariant>(option)} {}
139  template <typename ValueType,
140  typename std::enable_if_t<!is_OptionClassifier_v<ValueType>,
141  int> = 0>
142  GncOption(const char* section, const char* name,
143  const char* key, const char* doc_string,
144  ValueType value,
145  GncOptionUIType ui_type = GncOptionUIType::INTERNAL);
146  template <typename ValueType> void set_value(ValueType value);
147  template <typename ValueType> void set_default_value(ValueType value);
148  template <typename ValueType> ValueType get_default_value() const;
149  template <typename ValueType> ValueType get_value() const;
150  void reset_default_value();
151 
152  const std::string& get_section() const;
153  const std::string& get_name() const;
154  const std::string& get_key() const;
155  const std::string& get_docstring() const;
156  void set_ui_item(GncOptionUIItemPtr&& ui_elem);
157  const GncOptionUIType get_ui_type() const;
158  void set_ui_item_selectable(bool) const noexcept;
159  GncOptionUIItem* const get_ui_item() const;
160  void set_ui_item_from_option();
161  void set_option_from_ui_item();
162  void make_internal();
163  bool is_internal();
164  bool is_changed() const noexcept;
168  bool is_multiselect() const noexcept;
170  template <typename ValueType> void get_limits(ValueType&, ValueType&,
171  ValueType&) const noexcept;
173  template <typename ValueType> bool validate(ValueType value) const;
175  uint16_t num_permissible_values() const;
177  uint16_t permissible_value_index(const char* value) const;
179  const char* permissible_value(uint16_t index) const;
181  const char* permissible_value_name(uint16_t index) const;
183  GList* account_type_list() const noexcept;
184  bool is_alternate() const noexcept;
185  void set_alternate(bool) noexcept;
189  std::string serialize() const;
194  bool deserialize(const std::string& str);
199  std::istream& in_stream(std::istream& iss);
200  friend GncOptionVariant& swig_get_option(GncOption*);
201  void set_widget_changed (void* cb) { m_widget_changed = cb; }
202  void* get_widget_changed () { return m_widget_changed; }
203 private:
204  inline static const std::string c_empty_string{""};
205  GncOptionVariantPtr m_option;
206  GncOptionUIItemPtr m_ui_item{nullptr};
207 /* This is a hack to reserve space for a Guile callback pointer. */
208  void* m_widget_changed{};
209 };
210 
211 inline bool
212 operator<(const GncOption& right, const GncOption& left)
213 {
214  return right.get_key() < left.get_key();
215 }
216 
217 inline std::ostream&
218 operator<<(std::ostream& oss, const GncOption& opt)
219 {
220  oss << opt.serialize();
221  return oss;
222 }
223 
224 inline std::istream&
225 operator>>(std::istream& iss, GncOption& opt)
226 {
227  return opt.in_stream(iss);
228 }
229 
230 inline std::ostream&
231 output_color_value(std::ostream& oss, const std::string& value)
232 {
233  oss << "'(";
234  oss << std::fixed << std::showpoint << std::setprecision(1);
235  auto len{value.length() > 8 ? 8 : value.length()};
236  for (size_t i{}; i < len; i += 2)
237  {
238  oss << static_cast<float>(stoi(value.substr(i, 2), nullptr, 16));
239  if (i < 6)
240  oss << " ";
241  }
242  if (len < 8)
243  oss << 256.0;
244  oss << ")";
245  return oss;
246 }
247 
253 template<typename ValueType> GncOption*
254 gnc_make_option(const char* section, const char* name,
255  const char* key, const char* doc_string,
256  ValueType value, GncOptionUIType ui_type)
257 {
258  return new GncOption(section, name, key, doc_string, value, ui_type);
259 }
260 
261 #endif //GNC_OPTION_HPP_
262 
bool is_multiselect() const noexcept
Definition: gnc-option.cpp:301
The generic option-value class.
bool deserialize(const std::string &str)
Set the option&#39;s value from a character sequence.
Definition: gnc-option.cpp:434
uint16_t permissible_value_index(const char *value) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:350
A legal date value is a pair of either a RelativeDatePeriod, the absolute flag and a time64...
Represents the public interface for an option.
Definition: gnc-option.hpp:130
Set one or more accounts on which to report, optionally restricted to certain account types...
This class is the parent of all option implmentations.
bool validate(ValueType value) const
Not implemented for GncOptionValue.
Definition: gnc-option.cpp:314
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:335
std::string serialize() const
Get a string suitable for storage representing the option&#39;s value.
Definition: gnc-option.cpp:424
class GncOptionCommodityValue Commodities are stored with their namespace and mnemonic instead of the...
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...
const char * permissible_value(uint16_t index) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:365
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:254
void get_limits(ValueType &, ValueType &, ValueType &) const noexcept
Implemented only for GncOptionNumericRange.
Definition: gnc-option.cpp:173
const char * permissible_value_name(uint16_t index) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:379
A Query.
Definition: qofquery.cpp:77
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:442
GList * account_type_list() const noexcept
Implemented only for GncOptionAccountListValue.
Definition: gnc-option.cpp:393
Relative date enumeration and manipulation functions.