Refactoring / code cleanup
[weightgraph] / weightgraph / settingswindow.cpp
index 781a3b6..559bf20 100644 (file)
 #include <iostream>
 #include <QDebug>
 
-static QStringList timeIntervalStrings() {
-  QStringList timeIntervals;
-  timeIntervals.append("1 week");
-  timeIntervals.append("1 month");
-  timeIntervals.append("3 months");
-  timeIntervals.append("6 months");
-  timeIntervals.append("1 year");
-  timeIntervals.append("all");
-  return timeIntervals;
-}
-static int timeIntervalToIndex(int days) {
-  switch(days) {
-  case 0: return 5;
-  case 7: return 0;
-  case 30: return 1;
-  case 90: return 2;
-  case 180: return 3;
-  case 365: return 4;
-  default: Q_ASSERT(0 && "unknown time interval");
+namespace {
+  QStringList timeIntervalStrings() {
+    QStringList timeIntervals;
+    timeIntervals.append("1 week");
+    timeIntervals.append("1 month");
+    timeIntervals.append("3 months");
+    timeIntervals.append("6 months");
+    timeIntervals.append("1 year");
+    timeIntervals.append("all");
+    return timeIntervals;
   }
-  return 0; //unreachable
-}
-static int indexToTimeInterval(int index) {
-  switch(index) {
-  case 5: return 0;
-  case 0: return 7;
-  case 1: return 30;
-  case 2: return 90;
-  case 3: return 180;
-  case 4: return 365;
-  default: Q_ASSERT(0 && "unknown time interval index");
+  int timeIntervalToIndex(int days) {
+    switch(days) {
+    case 0: return 5;
+    case 7: return 0;
+    case 30: return 1;
+    case 90: return 2;
+    case 180: return 3;
+    case 365: return 4;
+    default: Q_ASSERT(0 && "unknown time interval");
+    }
+    return 0; //unreachable
+  }
+  int indexToTimeInterval(int index) {
+    switch(index) {
+    case 5: return 0;
+    case 0: return 7;
+    case 1: return 30;
+    case 2: return 90;
+    case 3: return 180;
+    case 4: return 365;
+    default: Q_ASSERT(0 && "unknown time interval index");
+    }
+    return 0; //unreachable
   }
-  return 0; //unreachable
 }
 
 SettingsWindow::SettingsWindow(QWidget *parent) :
@@ -57,59 +59,64 @@ SettingsWindow::SettingsWindow(QWidget *parent) :
   QWidget *rootContainer = new QWidget(this);
   QVBoxLayout *rootLayout = new QVBoxLayout(rootContainer);
 
-  QWidget *topContainer = new QWidget(rootContainer);
-  QGridLayout *topLayout = new QGridLayout(topContainer);
+  rootLayout->addWidget(makeGeneralSettingsWidget(rootContainer));
+  rootLayout->addWidget(makeGraphSettingsWidget(rootContainer));
+
+  setCentralWidget(rootContainer);
+}
+
+QWidget *SettingsWindow::makeGeneralSettingsWidget(QWidget *parentContainer) {
+  QWidget *container = new QWidget(parentContainer);
+  QGridLayout *lo = new QGridLayout(container);
 
   QStringList units; units.append("kg"); units.append("lb");
-  QStringListModel *weightUnitModel = new QStringListModel(units, topContainer);
+  QStringListModel *weightUnitModel = new QStringListModel(units, container);
 #ifdef Q_WS_MAEMO_5
-  weightUnit = new QMaemo5ValueButton("Unit", topContainer);
+  weightUnit = new QMaemo5ValueButton("Unit", container);
   weightUnit->setValueLayout(QMaemo5ValueButton::ValueUnderTextCentered);
-  QMaemo5ListPickSelector *weightUnitSelector = new QMaemo5ListPickSelector(topContainer);
+  QMaemo5ListPickSelector *weightUnitSelector = new QMaemo5ListPickSelector(container);
   weightUnitSelector->setModel(weightUnitModel);
   weightUnitSelector->setCurrentIndex(Settings::weightUnit() == "kg" ? 0 : 1);
   weightUnit->setPickSelector(weightUnitSelector);
   connect(weightUnit->pickSelector(), SIGNAL(selected(QString)),
           Settings::self(), SLOT(setWeightUnitAndSync(QString)));
 #else
-  weightUnit = new QComboBox(topContainer);
+  weightUnit = new QComboBox(container);
   weightUnit->setModel(weightUnitModel);
 #endif
 
-  topLayout->addWidget(weightUnit, 0, 0);
-
-//  QWidget *spacer = new QWidget(topContainer);
-//  spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
-//  topLayout->addWidget(spacer);
+  lo->addWidget(weightUnit, 0, 0);
 
-  QFrame *goalFrame = new QFrame(topContainer);
+  QFrame *goalFrame = new QFrame(container);
   goalFrame->setFrameShadow(QFrame::Sunken);
   goalFrame->setFrameStyle(QFrame::StyledPanel);
   goalFrame->setLineWidth(2);
   goalFrame->setMidLineWidth(2);
   QHBoxLayout *goalLayout = new QHBoxLayout(goalFrame);
 
-  goalLayout->addWidget(new QLabel("Goal weight:", topContainer));
+  goalLayout->addWidget(new QLabel("Goal weight:", container));
 
-  goalMin = new WeightSpinBox(topContainer);
+  goalMin = new WeightSpinBox(container);
   goalMin->setValue(Settings::goalWeightMin());
   goalLayout->addWidget(goalMin);
 
-  goalLayout->addWidget(new QLabel("-", topContainer));
+  goalLayout->addWidget(new QLabel("-", container));
 
-  goalMax = new WeightSpinBox(topContainer);
+  goalMax = new WeightSpinBox(container);
   goalMax->setValue(Settings::goalWeightMax());
   goalLayout->addWidget(goalMax);
-  topLayout->addWidget(goalFrame, 0, 1);
+  lo->addWidget(goalFrame, 0, 1);
 
-  grabZoomKeys = new QPushButton("Use zoom/volume keys to zoom", rootContainer);
+  grabZoomKeys = new QPushButton("Use zoom/volume keys to zoom", container);
   grabZoomKeys->setCheckable(true);
   grabZoomKeys->setChecked(Settings::grabZoomKeys());
-  topLayout->addWidget(grabZoomKeys, 1, 0, 1, 2);
+  lo->addWidget(grabZoomKeys, 1, 0, 1, 2);
 
-  rootLayout->addWidget(topContainer);
+  return container;
+}
 
-  QTabWidget *tabWidget = new QTabWidget(rootContainer);
+QWidget *SettingsWindow::makeGraphSettingsWidget(QWidget *parentContainer) {
+  QTabWidget *tabWidget = new QTabWidget(parentContainer);
 
   graphSettingsList = new QList<GraphSettingsWidget*>();
 
@@ -120,9 +127,7 @@ SettingsWindow::SettingsWindow(QWidget *parent) :
     tabWidget->addTab(gsw, id+" graph");
   }
 
-  rootLayout->addWidget(tabWidget);
-
-  setCentralWidget(rootContainer);
+  return tabWidget;
 }
 
 void SettingsWindow::closeEvent(QCloseEvent *event)
@@ -190,10 +195,6 @@ GraphSettingsWidget::GraphSettingsWidget(QString graphId, QWidget *parent) :
 #endif
   rootLayout->addWidget(defaultTimeInterval, 2, 0);
 
-//  QWidget *spacer = new QWidget(rootContainer);
-//  spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
-//  rootLayout->addWidget(spacer, 0, 1, 2, 1);
-
   QFrame *weightFrame = new QFrame(rootContainer);
   weightFrame->setFrameShadow(QFrame::Sunken);
   weightFrame->setFrameStyle(QFrame::StyledPanel);