GnuCash  4.11-354-g0815ab64c1
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 <libguile.h>
38 #include <string>
39 #include <iostream>
40 #include <iomanip>
41 #include <variant>
42 #include <memory>
43 #include <tuple>
44 #include "gnc-option-ui.hpp"
45 #include "gnc-option-date.hpp"
46 #include <guid.hpp>
47 
48 struct OptionClassifier;
49 class GncOptionUIItem;
50 using GncOptionUIItemPtr = std::unique_ptr<GncOptionUIItem>;
51 #ifndef SWIG //SWIG pulls in GncOwner from swig-engine.
52 struct _gncOwner;
53 using GncOwner = _gncOwner;
54 #endif
55 struct _QofQuery;
57 struct QofInstance_s;
59 template <typename ValueType> class GncOptionValue;
64 template <typename ValueType> class GncOptionRangeValue;
66 class GncOptionDateValue;
67 using GncOptionReportPlacement = std::tuple<uint32_t, uint32_t, uint32_t>;
68 using GncOptionReportPlacementVec = std::vector<GncOptionReportPlacement>;
69 template <typename T>
71 {
72  static constexpr bool value =
73  std::is_base_of_v<OptionClassifier, std::decay_t<T>>;
74 };
75 
76 template <typename T> inline constexpr bool
77 is_OptionClassifier_v = is_OptionClassifier<T>::value;
78 
79 template <typename T, typename U>
81 {
82  static constexpr bool value = std::is_same_v<std::decay_t<T>,
83  std::decay_t<U>>;
84 };
85 
86 template <typename T, typename U> inline constexpr bool
87 is_same_decayed_v = is_same_decayed<T, U>::value;
88 
89 template <typename T>
91 {
92  static constexpr bool value =
93  (is_same_decayed_v<T, GncOptionRangeValue<int>> ||
94  is_same_decayed_v<T, GncOptionRangeValue<double>>);
95 };
96 
97 template <typename T> inline constexpr bool
98 is_RangeValue_v = is_RangeValue<T>::value;
99 
100 
101 using GncOptionVariant = std::variant<GncOptionValue<std::string>,
115 
116 using GncOptionVariantPtr = std::unique_ptr<GncOptionVariant>;
117 
118 enum class GncOptionMultichoiceKeyType
119 {
120  SYMBOL,
121  STRING,
122  NUMBER,
123 };
124 
132 {
133 public:
134  template <typename OptionType,
135  typename std::enable_if_t<is_OptionClassifier_v<OptionType>,
136  int> = 0>
137 
138  GncOption(OptionType option) :
139  m_option{std::make_unique<GncOptionVariant>(option)} {}
140  template <typename ValueType,
141  typename std::enable_if_t<!is_OptionClassifier_v<ValueType>,
142  int> = 0>
143  GncOption(const char* section, const char* name,
144  const char* key, const char* doc_string,
145  ValueType value,
146  GncOptionUIType ui_type = GncOptionUIType::INTERNAL);
147  template <typename ValueType> void set_value(ValueType value);
148  template <typename ValueType> void set_default_value(ValueType value);
149  template <typename ValueType> ValueType get_default_value() const;
150  template <typename ValueType> ValueType get_value() const;
151  void reset_default_value();
152 
153  const std::string& get_section() const;
154  const std::string& get_name() const;
155  const std::string& get_key() const;
156  const std::string& get_docstring() const;
157  void set_ui_item(GncOptionUIItemPtr&& ui_elem);
158  const GncOptionUIType get_ui_type() const;
159  void set_ui_item_selectable(bool) const noexcept;
160  GncOptionUIItem* const get_ui_item() const;
161  void set_ui_item_from_option();
162  void set_option_from_ui_item();
163  void make_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  std::size_t num_permissible_values() const;
177  std::size_t permissible_value_index(const char* value) const;
179  const char* permissible_value(std::size_t index) const;
181  const char* permissible_value_name(std::size_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 
202 private:
203  inline static const std::string c_empty_string{""};
204  GncOptionVariantPtr m_option;
205  GncOptionUIItemPtr m_ui_item{nullptr};
206 };
207 
208 inline bool
209 operator<(const GncOption& right, const GncOption& left)
210 {
211  return right.get_key() < left.get_key();
212 }
213 
214 inline std::ostream&
215 operator<<(std::ostream& oss, const GncOption& opt)
216 {
217  oss << opt.serialize();
218  return oss;
219 }
220 
221 inline std::istream&
222 operator>>(std::istream& iss, GncOption& opt)
223 {
224  return opt.in_stream(iss);
225 }
226 
227 inline std::ostream&
228 output_color_value(std::ostream& oss, const std::string& value)
229 {
230  oss << "'(";
231  oss << std::fixed << std::showpoint << std::setprecision(1);
232  auto len{value.length() > 8 ? 8 : value.length()};
233  for (size_t i{}; i < len; i += 2)
234  {
235  oss << static_cast<float>(stoi(value.substr(i, 2), nullptr, 16));
236  if (i < 6)
237  oss << " ";
238  }
239  if (len < 8)
240  oss << 256.0;
241  oss << ")";
242  return oss;
243 }
244 
250 template<typename ValueType> GncOption*
251 gnc_make_option(const char* section, const char* name,
252  const char* key, const char* doc_string,
253  ValueType value, GncOptionUIType ui_type)
254 {
255  return new GncOption(section, name, key, doc_string, value, ui_type);
256 }
257 
258 #endif //GNC_OPTION_HPP_
259 
bool is_multiselect() const noexcept
Definition: gnc-option.cpp:287
std::ostream & operator<<(std::ostream &ostr, RelativeDatePeriod per)
Add the display string to the provided std::ostream.
std::size_t num_permissible_values() const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:321
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:420
A legal date value is a pair of either a RelativeDatePeriod, the absolute flag and a time64...
const char * permissible_value(std::size_t index) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:351
std::size_t permissible_value_index(const char *value) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:336
const char * permissible_value_name(std::size_t index) const
Implemented only for GncOptionMultiselectValue.
Definition: gnc-option.cpp:365
Represents the public interface for an option.
Definition: gnc-option.hpp:131
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:300
Multichoice options have a vector of valid options (GncMultichoiceOptionChoices) and validate the sel...
std::string serialize() const
Get a string suitable for storage representing the option&#39;s value.
Definition: gnc-option.cpp:410
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...
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:251
void get_limits(ValueType &, ValueType &, ValueType &) const noexcept
Implemented only for GncOptionNumericRange.
Definition: gnc-option.cpp:167
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:428
GList * account_type_list() const noexcept
Implemented only for GncOptionAccountListValue.
Definition: gnc-option.cpp:379
Relative date enumeration and manipulation functions.