Fixed a locale bug in stats view, improved edit window.
[weightgraph] / weightgraph / weightdata.h
1 #ifndef WEIGHTDATA_H
2 #define WEIGHTDATA_H
3
4 #include <QAbstractTableModel>
5 #include <QDate>
6 #include <QList>
7 #include <QFile>
8 #include <QDoubleSpinBox>
9 #include <QItemEditorCreatorBase>
10
11 //For debugging:
12 #include <iostream>
13
14 // A table model with 2 columns: Date | Weight
15 class WeightDataModel : public QAbstractTableModel
16 {
17   Q_OBJECT
18
19 public:
20   struct DateWeight
21   {
22     QDate date;
23     double weight;
24     bool operator<(const DateWeight &o) const { return date < o.date; }
25   };
26   typedef QList<DateWeight> WeightList;
27   WeightDataModel(QString &datafile, QObject *parent = 0);
28   int rowCount(const QModelIndex &/**/) const { return weights.size(); }
29   int columnCount(const QModelIndex &/**/) const { return 2; }
30   Qt::ItemFlags flags(const QModelIndex &index) const;
31   QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
32   bool setData(const QModelIndex &index, const QVariant &value, int role);
33   bool setDataForRow(int row, const DateWeight &dw);
34   void setWeightForDate(const QDate &date, double weight);
35   void setWeightForDate(const DateWeight &dw);
36   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
37   bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
38   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
39   int rowOfDate(const QDate &date) const;
40   QModelIndex indexOfDate(const QDate &date) const;
41   bool dateExists(const QDate &date) const;
42   int rowForNewDate(const QDate &date) const;
43   void clear();
44
45   int size() const { return weights.size(); }
46   const WeightList &getWeights() const { return weights; }
47 private:
48   void writeToDisk();
49   void readFromDisk();
50
51 private:
52   WeightList weights;
53   QFile datafile;
54 };
55
56 typedef WeightDataModel::DateWeight DW;
57
58 #endif // WEIGHTDATA_H