-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicmodel.cpp
126 lines (95 loc) · 2.59 KB
/
micmodel.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "micmodel.h"
MicModel::MicModel(Config * c, QObject *parent)
: QAbstractListModel(parent), c(c)
{
m_mics = c->property("mics").value<QList<Mic>>();
}
void MicModel::addMic(const Mic &mic)
{
m_mics << mic;
}
int MicModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return m_mics.count();
}
bool MicModel::setOwnData(const int index, const QString &value, QString role) {
if(role == "x") {
m_mics[index].x = value.toDouble();
} else if(role == "y") {
m_mics[index].y = value.toDouble();
} else if(role == "z") {
m_mics[index].z = value.toDouble();
} else {
return false;
}
for(int i = 0; i < m_mics.size(); i++) {
}
QVariant q;
q.setValue(m_mics);
c->setProperty("mics", q);
// emit dataChanged(QModelIndex, index);
return true;
}
bool MicModel::append(const QModelIndex &parent) {
Mic m;
m.x = 0;
m.y = 0;
m.z = 0;
beginInsertRows(parent, m_mics.size(), m_mics.size());
m_mics.append(m);
endInsertRows();
QVariant q;
q.setValue(m_mics);
c->setProperty("mics", q);
return true;
}
bool MicModel::remove(const QModelIndex &parent) {
if(m_mics.size() <= 0)
return false;
beginRemoveRows(parent, m_mics.size() - 1, m_mics.size());
m_mics.removeLast();
endRemoveRows();
QVariant q;
q.setValue(m_mics);
c->setProperty("mics", q);
return true;
}
Qt::ItemFlags MicModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
QVariant MicModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= m_mics.count())
return QVariant();
const Mic &mic = m_mics[index.row()];
if (role == xRole) {
return QVariant(mic.x);
} else if (role == yRole) {
return QVariant(mic.y);
} else if (role == zRole) {
return QVariant(mic.z);
}
return QVariant();
}
QString MicModel::ownData(const int index, QString role) const {
if (index < 0 || index >= m_mics.count())
return QString();
const Mic &mic = m_mics[index];
if (role == "x") {
return QString::number(mic.x);
} else if (role == "y") {
return QString::number(mic.y);
} else if (role == "z") {
return QString::number(mic.z);
}
return QString();
}
QHash<int, QByteArray> MicModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[xRole] = "x";
roles[yRole] = "y";
roles[zRole] = "z";
return roles;
}