-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcellmodel.cpp
92 lines (75 loc) · 2.14 KB
/
bcellmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <QBrush>
#include "bcellmodel.h"
BCellModel::BCellModel(QObject *parent) : QAbstractTableModel(parent)
{
}
BCellModel::BCellModel(QList<BCell> cells0, QObject *parent) : QAbstractTableModel(parent)
{
cells = cells0;
}
int BCellModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 144;
}
int BCellModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant BCellModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= cells.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole) {
BCell cell = cells.at(index.row());
if (index.column() == 0)
return QVariant(cell.id);
else if (index.column() == 1)
return QVariant(cell.voltage);
else if (index.column() == 2)
return QVariant(cell.temp);
} else if (role == Qt::BackgroundRole) {
BCell cell = cells.at(index.row());
if (index.column() == 1 && cell.voltage > BCell::warningVoltage)
return QBrush(Qt::yellow);
else if (index.column() == 2 && cell.temp > BCell::warningTemperature)
return QBrush(Qt::yellow);
return QBrush();
}
return QVariant();
}
QVariant BCellModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("ID");
case 1:
return tr("Voltage");
case 2:
return tr("Temperature");
default:
return QVariant();
}
}
return QVariant();
}
bool BCellModel::onVoltageChanged(int16_t id, int16_t voltage)
{
cells[id].voltage = voltage;
QModelIndex cellIndex = index(id, 1);
emit dataChanged(cellIndex, cellIndex);
return true;
}
bool BCellModel::onTempChanged(int16_t id, int16_t temp)
{
cells[id].temp = temp;
QModelIndex cellIndex = index(id, 2);
emit dataChanged(cellIndex, cellIndex);
return true;
}