Refactoring / code cleanup
[weightgraph] / weightgraph / settings.cpp
index b88ff88..5868ef7 100644 (file)
@@ -16,9 +16,7 @@ const QString Settings::defaultTimeIntervalKey = "DefaultTimeInterval";
 
 QString Settings::weightUnit()
 {
-  if(!s.contains(weightUnitKey))
-    setWeightUnit("kg");
-  return s.value(weightUnitKey).toString();
+  return s.value(weightUnitKey, "kg").toString();
 }
 void Settings::setWeightUnit(QString wu)
 {
@@ -32,9 +30,7 @@ void Settings::setWeightUnitAndSync(QString wu)
 
 double Settings::goalWeightMin()
 {
-  if(!s.contains(goalWeightMinKey))
-    setGoalWeightMin(0.0);
-  return s.value(goalWeightMinKey).toDouble();
+  return s.value(goalWeightMinKey, 0.0).toDouble();
 }
 void Settings::setGoalWeightMin(double min)
 {
@@ -48,9 +44,7 @@ void Settings::setGoalWeightMinAndSync(double min)
 
 double Settings::goalWeightMax()
 {
-  if(!s.contains(goalWeightMaxKey))
-    setGoalWeightMax(0.0);
-  return s.value(goalWeightMaxKey).toDouble();
+  return s.value(goalWeightMaxKey, 0.0).toDouble();
 }
 void Settings::setGoalWeightMax(double max)
 {
@@ -64,9 +58,7 @@ void Settings::setGoalWeightMaxAndSync(double max)
 
 bool Settings::grabZoomKeys()
 {
-  if(!s.contains(grabZoomKeysKey))
-    setGrabZoomKeys(true);
-  return s.value(grabZoomKeysKey).toBool();
+  return s.value(grabZoomKeysKey, true).toBool();
 }
 void Settings::setGrabZoomKeys(bool grab)
 {
@@ -81,43 +73,16 @@ void Settings::setGrabZoomKeysAndSync(bool grab)
 GraphSettings Settings::graphSettings(const QString &graphId)
 {
   GraphSettings ret;
-  bool changed = false;
-  s.beginGroup(graphId+graphSettingsGroupSuffix);
-
-  if (!s.contains(goalWeightEnabledKey)) {
-    s.setValue(goalWeightEnabledKey, false);
-    changed = true;
-  }
-  ret.goalWeightEnabled = s.value(goalWeightEnabledKey).toBool();
-
-  if (!s.contains(weightIntervalModeKey)) {
-    s.setValue(weightIntervalModeKey, (int)GraphSettings::AutomaticWithoutGoalWeight);
-    changed = true;
-  }
-  ret.weightIntervalMode =
-      (GraphSettings::WeightIntervalMode)s.value(weightIntervalModeKey).toInt();
-
-  if (!s.contains(weightIntervalMinKey)) {
-    s.setValue(weightIntervalMinKey, 0.0);
-    changed = true;
-  }
-  ret.weightIntervalMin = s.value(weightIntervalMinKey).toDouble();
-
-  if (!s.contains(weightIntervalMaxKey)) {
-    s.setValue(weightIntervalMaxKey, 0.0);
-    changed = true;
-  }
-  ret.weightIntervalMax = s.value(weightIntervalMaxKey).toDouble();
-
-  if (!s.contains(defaultTimeIntervalKey)) {
-    s.setValue(defaultTimeIntervalKey, 0);
-    changed = true;
-  }
-  ret.defaultTimeInterval = s.value(defaultTimeIntervalKey).toInt();
 
+  s.beginGroup(graphId+graphSettingsGroupSuffix);
+  ret.goalWeightEnabled = s.value(goalWeightEnabledKey, false).toBool();
+  ret.weightIntervalMode = (GraphSettings::WeightIntervalMode)s.value(
+      weightIntervalModeKey, (int)GraphSettings::AutomaticWithoutGoalWeight).toInt();
+  ret.weightIntervalMin = s.value(weightIntervalMinKey, 0.0).toDouble();
+  ret.weightIntervalMax = s.value(weightIntervalMaxKey, 0.0).toDouble();
+  ret.defaultTimeInterval = s.value(defaultTimeIntervalKey, 0).toInt();
   s.endGroup();
-  if (changed)
-    sync();
+
   return ret;
 }
 void Settings::setGraphSettings(const QString &graphId, GraphSettings &gs)