forked from zel1b08a/QmlTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbankmodel.cpp
More file actions
95 lines (76 loc) · 2.24 KB
/
bankmodel.cpp
File metadata and controls
95 lines (76 loc) · 2.24 KB
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
#include "bankmodel.h"
#include "bank.h"
BankModel::BankModel(QObject *parent)
: QAbstractListModel(parent)
, _bank(nullptr)
{
}
int BankModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !_bank)
return 0;
return _bank->coefficients().size();
}
QVariant BankModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !_bank)
return QVariant();
const Coefficient coefficient = _bank->coefficients().at(index.row());
switch (role) {
case CoefficientRole: return QVariant::fromValue(coefficient);
}
return QVariant();
}
bool BankModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!_bank)
return false;
Coefficient coefficient = _bank->coefficients().at(index.row());
switch (role) {
case CoefficientRole: coefficient = value.value<Coefficient>(); break;
}
if (_bank->setCoefficientAt(index.row(), coefficient)) {
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags BankModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable;
}
QHash<int, QByteArray> BankModel::roleNames() const
{
QHash<int, QByteArray> names;
names[CoefficientRole] = "coefficient";
return names;
}
Bank* BankModel::bank() const
{
return _bank;
}
void BankModel::setBank(Bank* bank)
{
beginResetModel();
if (_bank)
_bank->disconnect(this);
_bank = bank;
if (_bank) {
connect(_bank, &Bank::preCoefficientAppended, this, [=](){
const int index = _bank->coefficients().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(_bank, &Bank::postCoefficientAppended, this, [=](){
endResetModel();
});
connect(_bank, &Bank::preCoefficientRemoved, this, [=](int index){
beginRemoveRows(QModelIndex(), index, index);
});
connect(_bank, &Bank::postCoefficientRemoved, this, [=](){
endRemoveRows();
});
}
endResetModel();
}