From 4a7ac7d52b66c917a4ebf04421cae00d30328727 Mon Sep 17 00:00:00 2001 From: Visa Putkinen Date: Mon, 17 Dec 2012 01:37:12 +0200 Subject: [PATCH] Refactoring / code cleanup --- weightgraph/editwindow.cpp | 4 +- weightgraph/editwindow.h | 1 - weightgraph/main.cpp | 1 - weightgraph/settings.cpp | 59 ++++------------- weightgraph/settingswindow.cpp | 119 ++++++++++++++++++----------------- weightgraph/settingswindow.h | 3 + weightgraph/weightdata.cpp | 136 +++++++++++++++++++++++----------------- weightgraph/weightdata.h | 29 +++++---- weightgraph/weightgraph.pro | 2 +- weightgraph/weightview.h | 13 ++-- 10 files changed, 181 insertions(+), 186 deletions(-) diff --git a/weightgraph/editwindow.cpp b/weightgraph/editwindow.cpp index 374982e..a19f90b 100644 --- a/weightgraph/editwindow.cpp +++ b/weightgraph/editwindow.cpp @@ -50,8 +50,8 @@ EditWindow::EditWindow(QWidget *parent) : SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection &)), this, SLOT(updateButtons())); updateButtons(); - } + AddWeightDialog::AddWeightDialog(QWidget *parent) : QDialog(parent) { @@ -59,7 +59,6 @@ AddWeightDialog::AddWeightDialog(QWidget *parent) QGridLayout *layout = new QGridLayout(this); QLabel *dateLabel = new QLabel("Date:", this); - //dateLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum); layout->addWidget(dateLabel, 0, 0); #ifdef Q_WS_MAEMO_5 @@ -75,7 +74,6 @@ AddWeightDialog::AddWeightDialog(QWidget *parent) layout->addWidget(date, 1, 0); QLabel *weightLabel = new QLabel("Weight:", this); - //weightLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum); layout->addWidget(weightLabel, 0, 1); weight = new WeightSpinBox(this); diff --git a/weightgraph/editwindow.h b/weightgraph/editwindow.h index 68b16b1..359fffc 100644 --- a/weightgraph/editwindow.h +++ b/weightgraph/editwindow.h @@ -35,7 +35,6 @@ class AddWeightDialog : public QDialog public: AddWeightDialog(QWidget *parent=0); WeightDataModel::DateWeight getDateWeight(); - private: #ifdef Q_WS_MAEMO_5 QMaemo5ValueButton *date; diff --git a/weightgraph/main.cpp b/weightgraph/main.cpp index a3e58d7..2378c50 100644 --- a/weightgraph/main.cpp +++ b/weightgraph/main.cpp @@ -12,7 +12,6 @@ //Global pointer to the weight data WeightDataModel *wdm; -Settings *settings; int main(int argc, char *argv[]) { diff --git a/weightgraph/settings.cpp b/weightgraph/settings.cpp index b88ff88..5868ef7 100644 --- a/weightgraph/settings.cpp +++ b/weightgraph/settings.cpp @@ -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) diff --git a/weightgraph/settingswindow.cpp b/weightgraph/settingswindow.cpp index 781a3b6..559bf20 100644 --- a/weightgraph/settingswindow.cpp +++ b/weightgraph/settingswindow.cpp @@ -10,39 +10,41 @@ #include #include -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(); @@ -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); diff --git a/weightgraph/settingswindow.h b/weightgraph/settingswindow.h index 45c207a..29f089e 100644 --- a/weightgraph/settingswindow.h +++ b/weightgraph/settingswindow.h @@ -34,6 +34,9 @@ private: WeightSpinBox *goalMax; QPushButton *grabZoomKeys; QList *graphSettingsList; + + QWidget *makeGeneralSettingsWidget(QWidget *parentContainer); + QWidget *makeGraphSettingsWidget(QWidget *parentContainer); }; class GraphSettingsWidget : public QWidget { diff --git a/weightgraph/weightdata.cpp b/weightgraph/weightdata.cpp index d91fe54..248744e 100644 --- a/weightgraph/weightdata.cpp +++ b/weightgraph/weightdata.cpp @@ -15,11 +15,6 @@ WeightDataModel::WeightDataModel(QString &datafilename, QObject *parent) : readFromDisk(); } -void WeightDataModel::clear() -{ - weights.clear(); -} - Qt::ItemFlags WeightDataModel::flags(const QModelIndex &index) const { if(!index.isValid()) @@ -31,10 +26,41 @@ Qt::ItemFlags WeightDataModel::flags(const QModelIndex &index) const } } +QVariant WeightDataModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + if (orientation == Qt::Horizontal) { + return section == 0 ? tr("Date") : tr("Weight"); + } + else { + return QString("%1").arg(section); + } +} + +bool WeightDataModel::dateExists(const QDate &date) const +{ + return rowOfDate(date) != -1; +} + +int WeightDataModel::rowOfDate(const QDate &date) const +{ + // TODO: binary search + for(int i=0; i= rowCount(QModelIndex()) - || index.column() >= columnCount(QModelIndex())) + if (!index.isValid() || index.row() >= rowCount() + || index.column() >= columnCount()) return QVariant(); if (role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::SizeHintRole) @@ -61,16 +87,42 @@ QVariant WeightDataModel::data(const QModelIndex &index, int role) const return QVariant(); } +double WeightDataModel::minWeight() const { + // TODO: cache minimum and maximum weight on initial read and modifications? + double min = std::numeric_limits::max(); + foreach(const DW& dw, weights) { + if (dw.weight < min) + min = dw.weight; + } + return min; +} + +double WeightDataModel::maxWeight() const { + double max = std::numeric_limits::min(); + foreach(const DW& dw, weights) { + if (dw.weight > max) + max = dw.weight; + } + return max; +} + bool WeightDataModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid() || role != Qt::EditRole) return false; switch (index.column()) { - case 0: - weights[index.row()].date = value.toDate(); + case 0: { + QDate date = value.toDate(); + if (!date.isValid()) + return false; + weights[index.row()].date = date; break; + } case 1: { - double weight = value.toDouble(); + bool ok; + double weight = value.toDouble(&ok); + if (!ok) + return false; weights[index.row()].weight = weight; break; } @@ -107,22 +159,26 @@ void WeightDataModel::setWeightForDate(const QDate &date, double weight) DateWeight dw = {date, weight}; setDataForRow(row, dw); } - -void WeightDataModel::setWeightForDate(const WeightDataModel::DateWeight &dw) +int WeightDataModel::rowForNewDate(const QDate &date) const { - setWeightForDate(dw.date, dw.weight); + if (weights.size() == 0) + return 0; + if (date < weights.first().date) + return 0; + // TODO: binary search + for(int i=1; i date) + return i; + } + if (date > weights.last().date) + return weights.size(); + assert(0 && "UNREACHABLE"); + return -1; } -QVariant WeightDataModel::headerData(int section, Qt::Orientation orientation, int role) const +void WeightDataModel::setWeightForDate(const WeightDataModel::DateWeight &dw) { - if (role != Qt::DisplayRole) - return QVariant(); - if (orientation == Qt::Horizontal) { - return section == 0 ? tr("Date") : tr("Weight"); - } - else { - return QString("%1").arg(section); - } + setWeightForDate(dw.date, dw.weight); } // Insert count "empty" rows starting from row. @@ -132,8 +188,6 @@ bool WeightDataModel::insertRows(int row, int count, const QModelIndex &/*parent { beginInsertRows(QModelIndex(), row, row+count-1); DateWeight empty; - //empty.date.setDate(2000,1,1); - empty.weight = 0.0; while(count--) weights.insert(row, empty); endInsertRows(); @@ -150,39 +204,9 @@ bool WeightDataModel::removeRows(int row, int count, const QModelIndex &/*parent return true; } -//Returns the row of the date or -1 if it doesn't exist. -int WeightDataModel::rowOfDate(const QDate &date) const -{ - for(int i=0; i date) - return i; - } - if (date > weights.last().date) - return weights.size(); - assert(0 && "UNREACHABLE"); - return -1; + weights.clear(); } void WeightDataModel::writeToDisk() diff --git a/weightgraph/weightdata.h b/weightgraph/weightdata.h index 170ec75..1e6a471 100644 --- a/weightgraph/weightdata.h +++ b/weightgraph/weightdata.h @@ -24,31 +24,36 @@ public: bool operator<(const DateWeight &o) const { return date < o.date; } }; typedef QList WeightList; + WeightDataModel(QString &datafile, QObject *parent = 0); - int rowCount(const QModelIndex &/**/) const { return weights.size(); } - int columnCount(const QModelIndex &/**/) const { return 2; } + + int size() const { return weights.size(); } + int rowCount(const QModelIndex& = QModelIndex()) const { return weights.size(); } + int columnCount(const QModelIndex& = QModelIndex()) const { return 2; } Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + bool dateExists(const QDate &date) const; + int rowOfDate(const QDate &date) const; + QModelIndex indexOfDate(const QDate &date) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + const WeightList &getWeights() const { return weights; } + double minWeight() const; + double maxWeight() const; + bool setData(const QModelIndex &index, const QVariant &value, int role); bool setDataForRow(int row, const DateWeight &dw); void setWeightForDate(const QDate &date, double weight); void setWeightForDate(const DateWeight &dw); - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); + bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); - int rowOfDate(const QDate &date) const; - QModelIndex indexOfDate(const QDate &date) const; - bool dateExists(const QDate &date) const; - int rowForNewDate(const QDate &date) const; void clear(); - - int size() const { return weights.size(); } - const WeightList &getWeights() const { return weights; } private: + bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()); + int rowForNewDate(const QDate &date) const; void writeToDisk(); void readFromDisk(); -private: WeightList weights; QFile datafile; }; diff --git a/weightgraph/weightgraph.pro b/weightgraph/weightgraph.pro index 46de8cd..20437b0 100644 --- a/weightgraph/weightgraph.pro +++ b/weightgraph/weightgraph.pro @@ -29,7 +29,7 @@ HEADERS += mainwindow.h \ settings.h \ settingswindow.h -FORMS += mainwindow.ui +FORMS += CONFIG += mobility console MOBILITY = diff --git a/weightgraph/weightview.h b/weightgraph/weightview.h index 7234b36..a95345e 100644 --- a/weightgraph/weightview.h +++ b/weightgraph/weightview.h @@ -30,17 +30,18 @@ public: setFont(f); } - class WeightEditCreator : public QItemEditorCreatorBase + struct WeightEditCreator : public QItemEditorCreatorBase { - public: - WeightEditCreator() : QItemEditorCreatorBase() { } - virtual QWidget *createWidget(QWidget *parent) const + QWidget *createWidget(QWidget *parent) const { return new WeightSpinBox(parent); } - virtual QByteArray valuePropertyName() const { return "value"; } + virtual QByteArray valuePropertyName() const { + return "value"; + } }; - private: + +private: QStyledItemDelegate delegate; }; -- 1.7.9.5