Update screenshot of main view
[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
28   WeightDataModel(QString &datafile, QObject *parent = 0);
29
30   int size() const { return weights.size(); }
31   int rowCount(const QModelIndex& = QModelIndex()) const { return weights.size(); }
32   int columnCount(const QModelIndex& = QModelIndex()) const { return 2; }
33   Qt::ItemFlags flags(const QModelIndex &index) const;
34   QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
35
36   bool dateExists(const QDate &date) const;
37   int rowOfDate(const QDate &date) const;
38   QModelIndex indexOfDate(const QDate &date) const;
39   QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
40   const WeightList &getWeights() const { return weights; }
41   double minWeight() const;
42   double maxWeight() const;
43
44   bool setData(const QModelIndex &index, const QVariant &value, int role);
45   bool setDataForRow(int row, const DateWeight &dw);
46   void setWeightForDate(const QDate &date, double weight);
47   void setWeightForDate(const DateWeight &dw);
48
49   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
50   void clear();
51 private:
52   bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
53   int rowForNewDate(const QDate &date) const;
54   void writeToDisk();
55   void readFromDisk();
56
57   WeightList weights;
58   QFile datafile;
59 };
60
61 typedef WeightDataModel::DateWeight DW;
62
63 #endif // WEIGHTDATA_H