CMiniLexicon::FindMajorSignatures(): use log file routines
[linguistica.git] / LPreferences.h
blobe0a0ce95afa870076e94792f09f758a29ebc738e
1 // key/value user settings service
2 // Copyright © 2009 The University of Chicago
3 #ifndef LPREFERENCES_H
4 #define LPREFERENCES_H
6 class CLPreferences;
8 #include <map>
9 #include <QString>
10 template<class K, class V> class QMap;
12 /// A CLPreferences object maintains a list of key/value pairs
13 /// representing parameters for Linguistica’s various algorithms.
14 ///
15 /// Settings can be read from and written to a QSettings object
16 /// (for transparently remembering their values in the Windows
17 /// registry or a dotfile in the user’s home directory), a
18 /// named file (for importing or exporting preferences to share
19 /// them), or both.
20 ///
21 /// Settings are read on construction and written on destruction
22 /// of the preferences object. One can also explicitly request
23 /// a read or write of preferences using the LoadPrefs or
24 /// StorePrefs method.
25 ///
26 /// The values of all parameters can be saved into memory and
27 /// restored using the data() method. Example:
28 ///
29 /// QSettings settings
30 /// CLPreferences prefs(&settings);
31 /// // Unless do_something_to() calls StorePrefs(), the
32 /// // following block has no effect on prefs.
33 /// {
34 /// CLPreferences::data_type saved = prefs.data();
35 /// do_something_to(prefs);
36 /// // oops! back that out:
37 /// prefs.data() = saved;
38 /// }
39 class CLPreferences {
40 public:
41 typedef std::map<QString, QString> data_type;
42 private:
43 // where to read/write settings for long-term storage
44 /// pointer to a handle for a Windows Registry-style
45 /// setting storage service. Unused if null.
46 class QSettings* registry;
47 /// filename for an “exported settings” file to use.
48 /// Unused if empty.
49 QString prefs_filename;
51 // Linguistica parameters
52 data_type m;
53 public:
54 // construction/destruction.
56 /// preferences stored in *settings
57 explicit CLPreferences(QSettings* settings);
58 /// preferences stored in named file
59 explicit CLPreferences(QString filename);
60 /// save preferences to long-term storage
61 ~CLPreferences();
62 private:
63 // disable default-construction and copy.
64 CLPreferences();
65 CLPreferences(const CLPreferences& x);
66 CLPreferences& operator=(const CLPreferences& x);
67 public:
69 // string preference values.
71 QString GetPreference(QString key) const;
72 void SetPreference(QString key, QString value);
73 void RemovePreference(QString key);
75 // integer preference values.
77 /// result is -1 on error
78 int GetIntPreference(QString key) const;
79 void SetIntPreference(QString key, int value);
80 void RemoveIntPreference(QString key) { RemovePreference(key); }
82 // font preference values.
84 class QFont GetFontPreference(QString key) const;
85 void SetFontPreference(QString key, class QFont value);
86 void RemoveFontPreference(QString key);
88 // StringList preference values.
90 void GetStringListPreference(QString key,
91 class QStringList* out) const;
92 void SetStringListPreference(QString key,
93 const class QStringList& value);
95 // dictionary preference values.
97 void GetDictionaryPreference(QString key,
98 QMap<QString, QString>* out) const;
99 /// requires: no key in val.keys() contains a colon
100 void SetDictionaryPreference(QString key,
101 const QMap<QString, QString>& val);
103 // mass storage and retrieval.
105 void LoadPrefs();
106 void StorePrefs() const;
108 // export to file.
110 void load_prefs_from_file(const QString& filename);
111 /// requires: no key in m contains an equal sign
112 void store_prefs_to_file(const QString& filename) const;
114 // save/restore preference data.
116 data_type& data() { return m; }
117 const data_type& data() const { return m; }
120 #endif // LPREFERENCES_H