b6a2ef8199b3a1566df13922f6087ff71ddab9d5
[harbour-pedalo.git] / src / statsmodel.cpp
1 #include "statsmodel.h"
2
3 StatsModel::StatsModel(QObject *parent) : QAbstractListModel(parent) {
4 roles[TitleRole] = "title";
5 roles[ValuesRole] = "values";
6 roles[LabelsRole] = "labels";
7 roles[UnitsRole] = "units";
8 roles[MinValRole] = "minval";
9 roles[MaxValRole] = "maxval";
10 roles[StepRole] = "step";
11 }
12
13 void StatsModel::addStats(Stats &stats) {
14 this->stats.append(&stats);
15 }
16
17 QHash<int, QByteArray> StatsModel::roleNames() const {
18 return roles;
19 }
20
21 int StatsModel::rowCount(const QModelIndex & parent) const {
22 Q_UNUSED(parent)
23 return stats.count();
24 }
25
26 QVariant StatsModel::data(const QModelIndex & index, int role) const {
27 if (index.row() < 0 || index.row() > stats.count())
28 return QVariant();
29
30 const Stats *stat = stats[index.row()];
31 if (role == TitleRole)
32 return stat->getTitle();
33 else if (role == ValuesRole)
34 return QVariant::fromValue<QList<float>>(stat->getValues());
35 else if (role == LabelsRole)
36 return stat->getLabels();
37 else if (role == UnitsRole)
38 return stat->getUnits();
39 else if (role == MinValRole)
40 return stat->getMinVal();
41 else if (role == MaxValRole)
42 return stat->getMaxVal();
43 else if (role == StepRole)
44 return stat->getStep();
45 return QVariant();
46 }
47
48 void StatsModel::clear() {
49 stats.clear();
50 }
51
52 void StatsModel::updateAll() {
53 foreach (Stats * stat, stats) {
54 stat->update();
55 }
56 }