-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodeModel.h
82 lines (75 loc) · 2.2 KB
/
nodeModel.h
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
#include <QObject>
#include <QAbstractListModel>
#include "node.h"
#ifndef NODEMODEL_H
#define NODEMODEL_H
class nodeModel : public QAbstractListModel{
Q_OBJECT
public:
enum NodeRoles {
idRole,
nodeRowRole,
nodeColRole,
occupiedRole
};
nodeModel(QObject *parent = 0):QAbstractListModel(parent){
;
}
Q_INVOKABLE void clear(){
beginResetModel();
this->m_devices.clear();
endResetModel();
}
void addModel(const node &animal){
//qDebug() << animal.getCol()<< " " << animal.getRow();
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_devices << animal;
endInsertRows();
}
Q_INVOKABLE void test(){
qDebug()<<"OK";
}
Q_INVOKABLE void add(QString id,int row,int col, bool oc){
addModel(node(id,row,col,oc));
}
int rowCount(const QModelIndex & parent = QModelIndex()) const{
return m_devices.count();
};
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const{
if (index.row() < 0 || index.row() > m_devices.count())
return QVariant();
const node &animal = m_devices[index.row()];
if (role == idRole)
return animal.carID();
else if (role == nodeRowRole)
return animal.getRow();
else if (role == nodeColRole)
return animal.getCol();
else if (role == occupiedRole)
return animal.getOccupied();
return QVariant();
}
QHash<int,QByteArray> roleNames()const{
QHash<int, QByteArray> roles;
roles[idRole] = "id";
roles[nodeRowRole] = "row";
roles[nodeColRole] = "col";
roles[occupiedRole] = "occupied";
return roles;
}
Q_INVOKABLE QVariantMap get(int row) {
QHash<int,QByteArray> names = roleNames();
QHashIterator<int, QByteArray> i(names);
QVariantMap res;
while (i.hasNext()) {
i.next();
QModelIndex idx = index(row, 0);
QVariant data = idx.data(i.key());
res[i.value()] = data;
}
return res;
}
private:
QList<node> m_devices;
};
#endif // NODEMODEL_H