tilde~
Puredata Qt-based GUI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Preferences.h
Go to the documentation of this file.
1 // (c) 2017 Alex Nadzharov
2 // License: GPL3
3 
4 #ifndef CM_PREFERENCES_H
5 #define CM_PREFERENCES_H
6 
7 #include <QString>
8 #include <map>
9 #include <string.h>
10 
11 #include "PropertyList.h"
12 
13 #include <QDebug>
14 #include <QFile>
15 #include <QStandardPaths>
16 
17 #define PREF_QSTRING(x) tilde::Preferences::inst().getQString(x)
18 
19 //move
20 #define TILDE_APP_VERSION "0.1"
21 #define TILDE_PREF_INIT tilde::Preferences::inst().init()
22 
23 namespace tilde {
24 
26 class Preferences : public PropertyList {
27 public:
28  static Preferences& inst()
29  {
30  static Preferences instance;
31  return instance;
32  }
33 
34 private:
35  std::map<std::string, QString> _data;
36 
37  Preferences()
38  {
39  }
40 
41 public:
42  Preferences(Preferences const&) = delete;
43  void operator=(Preferences const&) = delete;
44 
45  // temporary
46  QString getQString(QString key)
47  {
48  if (get(key))
49  return get(key)->asQString();
50 
52 
53  return "";
54  }
55 
56  void init()
57  {
58  create("appVersion", "System", TILDE_APP_VERSION, (std::string)(TILDE_APP_VERSION));
59  create("Font", "UI", TILDE_APP_VERSION, (std::string)("Source Code Pro")); //
60 
61  // qDebug("pref init");
62  // qDebug() << getQString("appVersion");
63  // qDebug() << getQString("Font");
64  }
65 
66  //------------------------------------------
67  // path handling - probably move to separate class
68 
69  void addPath(QString newPath)
70  {
71  if (!get("Paths"))
72  set("Paths", (QString) "");
73 
74  QStringList paths = get("Paths")->asQStringList();
75  // TODO
76  //cmp_add_searchpath(gensym(newPath.toStdString().c_str()));
77 
78  paths.append(newPath);
79  set("Paths", paths);
80  }
81 
82  QStringList paths()
83  {
84  QStringList ret;
85  if (!get("Paths"))
86  return ret;
87 
88  ret = get("Paths")->asQStringList();
89  return ret;
90  }
91 
93  {
94  QFile textFile(QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).at(0) + "/tilde~/Settings/Preferences.txt");
95 
96  if (!textFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
97  qDebug() << textFile.errorString();
98  }
99 
100  QStringList prefList;
101 
102  QTextStream textStream(&textFile);
103  while (true) {
104  QString line = textStream.readLine();
105  if (line.isNull())
106  break;
107  else
108  prefList.append(line);
109  }
110 
111  if (prefList.size() > 0)
112  extractFromPdFileString(prefList.at(0));
113 
114  textFile.close();
115  }
116 
118  {
119  QFile textFile(QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).at(0) + "/tilde~/Settings/Preferences.txt");
120 
121  if (!textFile.open(QIODevice::WriteOnly)) {
122  //check and mkpath here
123  qDebug() << textFile.errorString();
124  }
125 
126  QStringList prefList;
127 
128  prefList.append(asPdFileString().c_str());
129 
130  textFile.open(QIODevice::WriteOnly | QIODevice::Text);
131  QTextStream logStream(&textFile);
132  for (int i = 0; i < prefList.size(); i++) {
133  logStream << prefList.at(i) << "\n";
134  }
135 
136  textFile.close();
137  }
138 };
139 }
140 
141 #endif // CM_PREFERENCES_H
app Preferences singleton
Definition: Preferences.h:26
void create(string pName, string pGroup, QString pVersion, T defaultData)
Definition: PropertyList.h:48
void readFromTextFile()
Definition: Preferences.h:92
#define TILDE_APP_VERSION
Definition: Preferences.h:20
QString extractFromPdFileString(QString input)
extract properties from string in pd file
Definition: PropertyList.cpp:99
string asPdFileString()
returns string for saving in file
Definition: PropertyList.cpp:43
void init()
Definition: Preferences.h:56
QString getQString(QString key)
Definition: Preferences.h:46
static Preferences & inst()
Definition: Preferences.h:28
Property handling class for ui object - property list.
Definition: PropertyList.h:34
OOPD * instance
Definition: OOPD.cpp:20
void operator=(Preferences const &)=delete
QStringList paths()
Definition: Preferences.h:82
void saveToTextFile()
Definition: Preferences.h:117
void addPath(QString newPath)
Definition: Preferences.h:69
void set(string pName, U value)
Definition: PropertyList.h:70