Completed home page.
[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   bool dateExists(const QDate &date) const;
41   int rowForNewDate(const QDate &date) const;
42   void clear();
43
44   int size() const { return weights.size(); }
45   const WeightList &getWeights() const { return weights; }
46 private:
47   void writeToDisk();
48   void readFromDisk();
49
50 private:
51   WeightList weights;
52   QFile datafile;
53 };
54
55 typedef WeightDataModel::DateWeight DW;
56
57 #endif // WEIGHTDATA_H