Release 1.0.4
[weightgraph] / weightgraph / settings.cpp
1 #include "settings.h"
2 #include <QDir>
3
4 Settings *Settings::singleton = new Settings();
5 QSettings Settings::s(Settings::settingsfile(), QSettings::IniFormat);
6 const QString Settings::weightUnitKey = "WeightUnit";
7 const QString Settings::goalWeightMinKey = "GoalWeightMin";
8 const QString Settings::goalWeightMaxKey = "GoalWeightMax";
9 const QString Settings::grabZoomKeysKey = "GrabZoomKeys";
10 const QString Settings::graphSettingsGroupSuffix = "Graph";
11 const QString Settings::goalWeightEnabledKey = "GoalWeightEnabled";
12 const QString Settings::weightIntervalModeKey = "WeightIntervalMode";
13 const QString Settings::weightIntervalMinKey = "WeightIntervalMin";
14 const QString Settings::weightIntervalMaxKey = "WeightIntervalMax";
15 const QString Settings::defaultTimeIntervalKey = "DefaultTimeInterval";
16
17 QString Settings::weightUnit()
18 {
19   return s.value(weightUnitKey, "kg").toString();
20 }
21 void Settings::setWeightUnit(QString wu)
22 {
23   s.setValue(weightUnitKey, wu);
24 }
25 void Settings::setWeightUnitAndSync(QString wu)
26 {
27   setWeightUnit(wu);
28   sync();
29 }
30
31 double Settings::goalWeightMin()
32 {
33   return s.value(goalWeightMinKey, 0.0).toDouble();
34 }
35 void Settings::setGoalWeightMin(double min)
36 {
37   s.setValue(goalWeightMinKey, min);
38 }
39 void Settings::setGoalWeightMinAndSync(double min)
40 {
41   setGoalWeightMin(min);
42   sync();
43 }
44
45 double Settings::goalWeightMax()
46 {
47   return s.value(goalWeightMaxKey, 0.0).toDouble();
48 }
49 void Settings::setGoalWeightMax(double max)
50 {
51   s.setValue(goalWeightMaxKey, max);
52 }
53 void Settings::setGoalWeightMaxAndSync(double max)
54 {
55   setGoalWeightMax(max);
56   sync();
57 }
58
59 bool Settings::grabZoomKeys()
60 {
61   return s.value(grabZoomKeysKey, true).toBool();
62 }
63 void Settings::setGrabZoomKeys(bool grab)
64 {
65   s.setValue(grabZoomKeysKey, grab);
66 }
67 void Settings::setGrabZoomKeysAndSync(bool grab)
68 {
69   setGrabZoomKeys(grab);
70   sync();
71 }
72
73 GraphSettings Settings::graphSettings(const QString &graphId)
74 {
75   GraphSettings ret;
76
77   s.beginGroup(graphId+graphSettingsGroupSuffix);
78   ret.goalWeightEnabled = s.value(goalWeightEnabledKey, false).toBool();
79   ret.weightIntervalMode = (GraphSettings::WeightIntervalMode)s.value(
80       weightIntervalModeKey, (int)GraphSettings::AutomaticWithoutGoalWeight).toInt();
81   ret.weightIntervalMin = s.value(weightIntervalMinKey, 0.0).toDouble();
82   ret.weightIntervalMax = s.value(weightIntervalMaxKey, 0.0).toDouble();
83   ret.defaultTimeInterval = s.value(defaultTimeIntervalKey, 0).toInt();
84   s.endGroup();
85
86   return ret;
87 }
88 void Settings::setGraphSettings(const QString &graphId, GraphSettings &gs)
89 {
90   s.beginGroup(graphId+graphSettingsGroupSuffix);
91   s.setValue(goalWeightEnabledKey, gs.goalWeightEnabled);
92   s.setValue(weightIntervalModeKey, (int)gs.weightIntervalMode);
93   s.setValue(weightIntervalMinKey, gs.weightIntervalMin);
94   s.setValue(weightIntervalMaxKey, gs.weightIntervalMax);
95   s.setValue(defaultTimeIntervalKey, gs.defaultTimeInterval);
96   s.endGroup();
97 }
98 void Settings::setGraphSettingsAndSync(const QString &graphId,
99                                        GraphSettings &gs)
100 {
101   setGraphSettings(graphId, gs);
102   sync();
103 }
104
105 QStringList Settings::graphIds()
106 {
107   QStringList tmp = s.childGroups();
108   tmp = tmp.filter(QRegExp("^\\w+Graph$"));
109   return tmp.replaceInStrings(QRegExp("^(\\w+)Graph$"), "\\1");
110 }
111 void Settings::sync()
112 {
113   s.sync();
114   emit singleton->settingChanged();
115 }
116
117 QString Settings::datadir()
118 {
119 #ifdef Q_WS_MAEMO_5
120   return QDir::home().path()+"/MyDocs/WeightGraph";
121 #else
122   return QDir::home().path()+"/.weightgraph";
123 #endif
124 }
125 QString Settings::datafile()
126 {
127   return datadir()+"/weightdata.txt";
128 }
129 QString Settings::settingsfile()
130 {
131   return datadir()+"/config.ini";
132 }