-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableSequenceModel.cpp
More file actions
94 lines (82 loc) · 2.5 KB
/
VariableSequenceModel.cpp
File metadata and controls
94 lines (82 loc) · 2.5 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
#include "VariableSequenceModel.h"
#include "Variable.h"
#include <QDebug>
VariableSequenceModel::VariableSequenceModel(VariableSequence* sequence, QObject *parent):
QAbstractListModel(parent),
_sequence(sequence)
{}
int VariableSequenceModel::rowCount(const QModelIndex& parent) const {
Q_UNUSED(parent);
if(_sequence == nullptr) {
return 0;
} else {
return (int)_sequence->length();
}
}
QVariant VariableSequenceModel::data(const QModelIndex& index, int role) const {
if(!index.isValid()) {
if(role == Qt::DisplayRole) {
return _sequence->getName();
} else if(role == Qt::UserRole){
return QVariant::fromValue<void*>((void*)_sequence);
} else {
return QVariant();
}
}
switch(role) {
case Qt::DisplayRole:
return _sequence->at(index.row())->getName();
case Qt::EditRole:
return QVariant::fromValue<void*>((void*)_sequence->at(index.row()));
case Qt::UserRole:
return QVariant::fromValue<void*>((void*)_sequence);
default:
return QVariant();
}
}
bool VariableSequenceModel::setData(const QModelIndex& index, const QVariant& value, int role) {
Variable* var;
switch(role) {
case Qt::EditRole:
case Qt::UserRole:
if(index.row() < 0 || index.row() >= _sequence->length()) return false;
var = (Variable*)value.value<void*>();
_sequence->set(index.row(), var);
break;
case Qt::DisplayRole:
_sequence->setName(value.value<QString>());
break;
}
QVector<int> roles;
roles << role;
emit dataChanged(index, index, roles);
return true;
}
bool VariableSequenceModel::insertRows(int row, int count, const QModelIndex& parent) {
if(count <= 0) return true;
beginInsertRows(parent, row, row + count - 1);
while(count--) _sequence->insert(row, nullptr);
endInsertRows();
return true;
}
bool VariableSequenceModel::removeRows(int row, int count, const QModelIndex& parent) {
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), row, row + count - 1);
while(count--) _sequence->remove(row);
endRemoveRows();
return true;
}
bool VariableSequenceModel::moveRows(const QModelIndex& sourceParent, int sourceRow, int count, const QModelIndex& destinationParent, int destinationChild) {
beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
VariableSequence removed;
for(int i = 0; i < count; i++) {
removed.pushBack(_sequence->at(sourceRow));
_sequence->remove(sourceRow);
}
for(int i = 0; i < removed.length(); i++) {
_sequence->insert(destinationChild - count + i, removed.at(0));
removed.popFront();
}
endMoveRows();
return true;
}