SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection &)),
this, SLOT(updateButtons()));
updateButtons();
-
}
+
AddWeightDialog::AddWeightDialog(QWidget *parent)
: QDialog(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
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);
public:
AddWeightDialog(QWidget *parent=0);
WeightDataModel::DateWeight getDateWeight();
-
private:
#ifdef Q_WS_MAEMO_5
QMaemo5ValueButton *date;
//Global pointer to the weight data
WeightDataModel *wdm;
-Settings *settings;
int main(int argc, char *argv[])
{
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)
{
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)
{
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)
{
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)
{
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)
#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) :
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*>();
tabWidget->addTab(gsw, id+" graph");
}
- rootLayout->addWidget(tabWidget);
-
- setCentralWidget(rootContainer);
+ return tabWidget;
}
void SettingsWindow::closeEvent(QCloseEvent *event)
#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);
WeightSpinBox *goalMax;
QPushButton *grabZoomKeys;
QList<GraphSettingsWidget*> *graphSettingsList;
+
+ QWidget *makeGeneralSettingsWidget(QWidget *parentContainer);
+ QWidget *makeGraphSettingsWidget(QWidget *parentContainer);
};
class GraphSettingsWidget : public QWidget {
readFromDisk();
}
-void WeightDataModel::clear()
-{
- weights.clear();
-}
-
Qt::ItemFlags WeightDataModel::flags(const QModelIndex &index) const
{
if(!index.isValid())
}
}
+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<weights.size(); i++)
+ if (weights[i].date == date)
+ return i;
+ return -1;
+}
+
+QModelIndex WeightDataModel::indexOfDate(const QDate &date) const
+{
+ return index(rowOfDate(date), 0);
+}
+
QVariant WeightDataModel::data(const QModelIndex &index, int role) const
{
- if (!index.isValid() || index.row() >= 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)
return QVariant();
}
+double WeightDataModel::minWeight() const {
+ // TODO: cache minimum and maximum weight on initial read and modifications?
+ double min = std::numeric_limits<double>::max();
+ foreach(const DW& dw, weights) {
+ if (dw.weight < min)
+ min = dw.weight;
+ }
+ return min;
+}
+
+double WeightDataModel::maxWeight() const {
+ double max = std::numeric_limits<double>::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;
}
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<weights.size(); i++) {
+ if (weights.at(i-1).date < date && weights.at(i).date > 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.
{
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();
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<weights.size(); i++)
- if (weights[i].date == date)
- return i;
- return -1;
-}
-QModelIndex WeightDataModel::indexOfDate(const QDate &date) const
-{
- return index(rowOfDate(date), 0);
-}
-
-bool WeightDataModel::dateExists(const QDate &date) const
-{
- return rowOfDate(date) != -1;
-}
-
-int WeightDataModel::rowForNewDate(const QDate &date) const
+void WeightDataModel::clear()
{
- if (weights.size() == 0)
- return 0;
- if (date < weights.first().date)
- return 0;
- for(int i=1; i<weights.size(); i++) {
- if (weights.at(i-1).date < date
- && weights.at(i).date > date)
- return i;
- }
- if (date > weights.last().date)
- return weights.size();
- assert(0 && "UNREACHABLE");
- return -1;
+ weights.clear();
}
void WeightDataModel::writeToDisk()
bool operator<(const DateWeight &o) const { return date < o.date; }
};
typedef QList<DateWeight> 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;
};
settings.h \
settingswindow.h
-FORMS += mainwindow.ui
+FORMS +=
CONFIG += mobility console
MOBILITY =
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;
};