NCBI C++ ToolKit
settings_set.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_CONFIG____SETTINGSSETREG_HPP
2 #define GUI_CONFIG____SETTINGSSETREG_HPP
3 
4 /* $Id: settings_set.hpp 39688 2017-10-26 16:55:30Z katargir $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Robert G. Smith
30  *
31  */
32 
33 #include <corelib/ncbiobj.hpp>
34 #include <corelib/ncbistr.hpp>
35 #include <gui/gui_export.h>
37 #include <gui/utils/rgba_color.hpp>
39 
40 #include <string>
41 #include <map>
42 
44 
45  /**
46  * @class CSettingsSet
47  *
48  * CSettingsSet is an adapter of the CGuiRegistry to provide persistance of
49  * and easy access to view's (or other plugins') configuration settings.
50  * You may use this as a base class for your plugin's configuration class,
51  * or you may include it as a member. The last is especially handy if you
52  * do not need to add/delete/rename new sets of settings values (styles).
53  *
54  * CSettingsSet expects a certain hierachy of keys inside its registry.
55  * Here is an example:
56  *
57  * GBPlugins
58  * SequenceViewStuff
59  * name = Prefs for My Plugin
60  * current = default_set
61  * default_values
62  * name = *
63  * Key1 = default value 1
64  * Key2.subkey1 = 32
65  * Key2.color = blue
66  * ...
67  * default_set
68  * name = Default Style
69  * other set
70  * name = Special Cool Style
71  * Key2.subkey1 = 64
72  * ....
73  * AlignViewThings
74  * name = Alignment View
75  * ...
76  *
77  * GBPlugins is a required word.
78  * Below it are the keys of the various types of values this system knows about.
79  * Each instance of CSettingsSet tracks one of these types, and is initialized with
80  * its key.
81  *
82  * In the next level down, the 'name' key for the type gives a user visible string
83  * shortly describing this type. Currently it is displayed on the title bar of the
84  * configuration dialog.
85  *
86  * The rest of the keys are groups of different value we can
87  * switch between. These are called 'styles' or sometimes 'sets'.
88  * 'current's value is the key of the currently selected style.
89  *
90  * There should always be styles with the keys 'default_values' and 'default_set'.
91  * The default_values style should have a name of '*'. Any style with '*' as its name
92  * will not be offered as a choice to the user.
93  *
94  * The default_values style should have every key that this CSettingsSet may access
95  * along with a default value for it. Any other set may omit any key in which case
96  * we will get the value from 'default_values'.
97  * The set 'default_set' will usually be empty, though it can be modified, and will be
98  * what the user is shown first.
99  *
100  * When accessing values from CSettingsSet you use the keys relative to the current style.
101  * e.g. (from the above hierarchy)
102  * GetString("Key1");
103  * GetInt("Key2.subkey1);
104  * GetColor("Key2.color");
105  *
106  */
107 
109  class CUser_field;
111 
112 
114 {
115 public:
116 
117  /// must be the same used as a default in CGuiRegistry.
118  static const string kDefaultDelim;
119 
120  /// constructor
121  /**
122  @param type The text key in the registry containing all values for this type.
123  @param registry A pointer to the registry we are going to wrap.
124  If null, use the one initialized by CSettings from the files plugin_config.asn
125  found in <sys>/etc and <home>.
126  */
127  CSettingsSet(const string& type,
128  CGuiRegistry* registry = 0);
129 
130  virtual ~CSettingsSet();
131 
132  /// uncache any cached lookup data
133  void Uncache();
134 
135  /// Return the type passed in the constructor
136  const string& GetType(void) const;
137  string GetCurrentStyleName(void) const;
138  void SetCurrentStyleName(const string& new_style);
139 
140  /// These are so that you can get data that is all default values.
141  /// make sure you always restore the current style and you do not do any
142  /// Set's to the data in between. Used by mediators.
143  void SetDefaultCurrentStyle();
144  void RestoreCurrentStyle();
145 
146  string GetTypeDescription(void) const;
147 
148  /**
149  Methods to edit our list of valid styles.
150  These do not change the current style, except DeleteStyle().
151  */
152 
153  /// use to set up menu to chose between saved sets/styles.
154  list<string> GetStyleNames(void) const;
155  /// these all return the name of the style just added.
156  /// on failure they will return empty strings.
157  string AddStyle(void);
158  string DuplicateStyle(const string& style);
159  string RenameStyle(const string& old_style, const string& new_style);
160  bool CanRenameStyle(const string& style);
161  // returns true on success, false on failure.
162  bool DeleteStyle(const string& style);
163 
164  /// @name Value Getters
165  /// All getters retrieve values from the current style set.
166  /// All value getters follow either of the patterns:
167  ///
168  /// <value> GetXxx(const string& section, const string& key,
169  /// <value> default_value = <some-value>);
170  ///
171  /// or
172  ///
173  /// void GetXxxVec(const string& section, const string& key,
174  /// <value>& output);
175  /// @{
176 
177  /// Retrieve a named string value from a section.key
178  string GetString(const string& key,
179  const string& default_val = kEmptyStr) const;
180 
181  /// Retrieve a named int value from a section.key
182  int GetInt(const string& key,
183  int default_val = 0) const;
184 
185  /// Retrieve a named double value from a section.key
186  double GetReal(const string& key,
187  double default_val = 0) const;
188 
189  /// Retrieve a named bool value from a section.key
190  bool GetBool(const string& key,
191  bool default_val = false) const;
192 
193  /// Retrieve a named color value from a section.key
194  CRgbaColor GetColor(const string& key,
195  const CRgbaColor& default_val = CRgbaColor()) const;
196 
197  /// Retrieve a named int vector value from a section.key
198  void GetIntVec(const string& key,
199  vector<int>& val) const;
200 
201  /// Retrieve a named real vector value from a section.key
202  void GetRealVec(const string& key,
203  vector<double>& val) const;
204 
205  /// Retrieve a named string vector value from a section.key
206  void GetStringVec(const string& key,
207  vector<string>& val) const;
208 
209  /// @}
210 
211  /// @name Value Setters
212  /// All getters write values to the current style set.
213  /// @{
214 
215  /// set a named integer value
216  void Set(const string& key, int val);
217 
218  /// set a named real value
219  void Set(const string& key, double val);
220 
221  /// set a named bool value
222  void Set(const string& key, bool val);
223 
224  /// ste a named string value
225  void Set(const string& key, const string& val);
226 
227  /// set a named string value as a const char* (avoids implicit
228  /// conversion to bool)
229  void Set(const string& key, const char* val);
230 
231  /// set a named value as a vector of ints
232  void Set(const string& key, const vector<int>& val);
233 
234  /// set a named value as a vector of reals
235  void Set(const string& key, const vector<double>& val);
236 
237  /// set a named value as a vector of strings
238  void Set(const string& key, const vector<string>& val);
239 
240  /// set a named value as a color
241  void Set(const string& key, const CRgbaColor& val);
242 
243  /// @}
244 
245  /// Delete a key/value in the current settings.
246  /// returns true if the key was found and deleted, false if not.
247  bool Delete(const string& key);
248 
249 
250  /// we use int instead of CSeqFeatData::ESubtype for two reasons:
251  /// - avoids unnecessary includes
252  /// - gives transparent warning-free compiles with generic serialized
253  /// code (i.e., when we serialize a subtype, it's as an int not as an
254  /// enum)
255  typedef int TFeatSubtype;
256 
257  /// retrieve the key name to read values for a given feature subtype
258  string GetFeatReadKey(TFeatSubtype feat_subtype, const string& section,
259  const string& subkey = kEmptyStr) const;
260 
261  /// retrieve the key name to write values for a given feature subtype
262  static string GetFeatWriteKey(TFeatSubtype feat_subtype, const string& section);
263 
264  /// write all keys and values we know about.
265  void DumpAll(CNcbiOstream& ostr);
266 
267  /// convert a color into a vector of ints within a user field
268  static void ColorToUserField(const CRgbaColor& c, objects::CUser_field& f);
269 
270  /// convert a user field into a color.
271  /// The user field can be a vector of ints, a vector of doubles or a string.
272  /// If you run across colors encoded in user fields some other way put them in here.
273  /// returns true if succesful. false otherwise.
274  static bool UserFieldToColor(const objects::CUser_field& f, CRgbaColor& c);
275 
276  /// style keys are only for internal use.
277  string GetCurrentStyleKey(void) const;
278  void SetCurrentStyleKey(const string& new_style_key);
279 
280 protected: //###
281 
282  static const string sm_DefaultValuesKey; ///< the key for the default set of values.
283 
284 
285  /// every style has a name and a key. Both should be unique within this Type.
286  /// translate between style names and keys.
287  /// We store keys. We display names.
288  string x_GetStyleName(const string& key) const;
289  string x_GetStyleKey(const string& name) const;
290  /// make unique names and keys.
291  /// result may be different than the argument.
292  string x_MakeUniqueStyleName(const string& name) const;
293  string x_MakeUniqueStyleKey(const string& key) const;
294 
295  /// Retrieve a read-only view. This is protected to make sure that caching can
296  /// work effectively; clients should use the GetXXX() functions instead.
298  x_GetReadView(const string& section) const;
299 
300 private:
301 
302  /// get the full key string used to get this type's part of the registry.
303  string x_GetSection() const;
304 
305  /// get the full key string used to get the current style's part of the registry.
306  string x_GetStyleSection() const;
307 
308  /// get a view containing the values given a certain style.
309  CRegistryReadView x_GetStyleView(const string& style_key) const;
310 
311  /// get a view showing the values under the current style.
312  CRegistryReadView x_GetCurrentStyleView() const;
313 
314  /// @name Cached access to fields
315  /// @{
316 
317  /// retrieve (and cache) a field
319  x_GetCurrentViewField(const string& key) const;
320 
321  /// uncache a given key
322  void x_Uncache(const string& key) const;
323 
324  /// invalidate the entire cache
325  void x_UncacheAll() const;
326 
327  /// dictionary for caching look-ups into CGuiRegistry
328  typedef std::map<string, CConstRef<objects::CUser_field> > TFieldDictionary;
330 
331  /// dictionary to cache string -> color lookups
332  typedef std::map<string, CRgbaColor> TColorDictionary;
334  /// @}
335 
336  CRef<CGuiRegistry> m_Registry; ///> the registry these views come from.
337  string m_Type; ///> key used in cache.
338  string m_SavedStyle; ///> remember the current style when getting default values.
339 };
340 
341 
342 
344 
345 #endif // GUI_CONFIG____SETTINGSSETREG_HPP
CObject –.
Definition: ncbiobj.hpp:180
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
class CRgbaColor provides a simple abstraction for managing colors.
Definition: rgba_color.hpp:58
CSettingsSet is an adapter of the CGuiRegistry to provide persistance of and easy access to view's (o...
std::map< string, CRgbaColor > TColorDictionary
dictionary to cache string -> color lookups
string m_SavedStyle
> key used in cache.
TFieldDictionary m_FieldDictionary
static const string sm_DefaultValuesKey
the key for the default set of values.
static const string kDefaultDelim
must be the same used as a default in CGuiRegistry.
std::map< string, CConstRef< objects::CUser_field > > TFieldDictionary
dictionary for caching look-ups into CGuiRegistry
int TFeatSubtype
we use int instead of CSeqFeatData::ESubtype for two reasons:
CRef< CGuiRegistry > m_Registry
TColorDictionary m_ColorDictionary
string m_Type
> the registry these views come from.
int GetInt(void) const
get value
Definition: User_field.hpp:327
const string & GetString(void) const
Definition: User_field.hpp:348
bool GetBool(void) const
Definition: User_field.hpp:341
static CMemoryRegistry registry
Definition: cn3d_tools.cpp:81
The NCBI C++ standard methods for dealing with std::string.
CRgbaColor & GetColor(CSeqFeatData::ESubtype subtype)
static FILE * f
Definition: readconf.c:23
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define kEmptyStr
Definition: ncbistr.hpp:123
#define NCBI_GUICONFIG_EXPORT
Definition: gui_export.h:507
Defines to provide correct exporting from DLLs in Windows.
const struct ncbi::grid::netcache::search::fields::KEY key
const struct ncbi::grid::netcache::search::fields::SUBKEY subkey
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Definition: type.c:6
Modified on Fri Sep 20 14:57:50 2024 by modify_doxy.py rev. 669887