-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanimationmodel.cpp
More file actions
89 lines (71 loc) · 2.11 KB
/
animationmodel.cpp
File metadata and controls
89 lines (71 loc) · 2.11 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
#include "animationmodel.h"
#include "animation.h"
#include <QDebug>
AnimationModel::AnimationModel(QObject* parent) : QAbstractListModel(parent) {}
void AnimationModel::clear() {
beginResetModel();
qDeleteAll(_animations);
_animations.clear();
endResetModel();
}
void AnimationModel::addAnimation(Animation* animation) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
animation->setParent(this);
connect(animation, SIGNAL(propertyUpdated()), this, SLOT(onAnimationPropertyChanged()));
_animations.append(animation);
endInsertRows();
}
void AnimationModel::setAnimations(const QList<Animation*> &animations) {
clear();
foreach (Animation* animation, animations) {
addAnimation(animation);
}
}
QHash<int, QByteArray> AnimationModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[IdRole] = "id";
roles[NameRole] = "name";
roles[DescriptionRole] = "description";
roles[ActiveRole] = "active";
return roles;
}
int AnimationModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return _animations.count();
}
void AnimationModel::onAnimationPropertyChanged() {
QVector<int> roles;
roles.append(ActiveRole);
// TODO: Use the proper index for the changed animation, rather
// than updating the entire list
QModelIndex topLeft = index(0, 0);
QModelIndex bottomRight = index(_animations.size() - 1, 0);
emit dataChanged(topLeft, bottomRight, roles);
}
QVariant AnimationModel::data(const QModelIndex& index, int role) const {
if (index.row() < 0 || index.row() >= _animations.count()) {
return QVariant();
}
Animation* animation = _animations[index.row()];
if (role == IdRole) {
return animation->id();
} else if (role == NameRole) {
return animation->name();
} else if (role == DescriptionRole) {
return animation->description();
} else if (role == ActiveRole) {
return animation->active();
} else {
return QVariant();
}
}
Animation* AnimationModel::getAnimation(int id) {
QList<Animation*>::iterator i;
for (i = _animations.begin(); i != _animations.end(); i++) {
Animation* animation = *i;
if (animation->id() == id) {
return animation;
}
}
return 0;
}