-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasterplayer_model.cpp
More file actions
141 lines (121 loc) · 3.85 KB
/
casterplayer_model.cpp
File metadata and controls
141 lines (121 loc) · 3.85 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "casterplayer_model.h"
#include "casterplayer_controller.h"
CasterPlayerModel::CasterPlayerModel(QObject *parent)
: QAbstractListModel(parent)
, mList(nullptr)
{
}
int CasterPlayerModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid() || !mList)
return 0;
return mList->items().size();
}
QVariant CasterPlayerModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mList)
return QVariant();
const CasterPlayerItem item = mList->items().at(index.row());
switch (role) {
case IsInPlayerModeRole:
return QVariant(item.isInPlayerMode);
case IsLoopedRole:
return QVariant(item.isLooped);
case VolumeRole:
return QVariant(item.volume);
case IsPlayingRegionEnabledRole:
return QVariant(item.isPlayRegionEnabled);
case PlayRegionBeginRole:
return QVariant(item.playRegionBegin);
case PlayRegionEndRole:
return QVariant(item.playRegionEnd);
case TriggerStyleRole:
return QVariant(item.triggerStyle);
}
return QVariant();
}
bool CasterPlayerModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!mList)
return false;
CasterPlayerItem item = mList->items().at(index.row());
switch (role) {
case IsInPlayerModeRole:
item.isInPlayerMode = value.toBool();
break;
case IsLoopedRole:
item.isLooped = value.toBool();
break;
case VolumeRole:
item.volume = value.toDouble();
break;
case IsPlayingRegionEnabledRole:
item.isPlayRegionEnabled = value.toBool();
break;
case PlayRegionBeginRole:
item.playRegionBegin = value.toInt();
break;
case PlayRegionEndRole:
item.playRegionEnd = value.toInt();
break;
case TriggerStyleRole:
item.triggerStyle = value.toInt();
}
if (mList->setItemAt(index.row(), item)) {
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags CasterPlayerModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable;
}
/*
* Define the names of the model properties to be used in QML, by
* associating the names with their respective role in the model.
*/
QHash<int, QByteArray> CasterPlayerModel::roleNames() const
{
// Set QML property names
QHash<int, QByteArray> names;
names[IsInPlayerModeRole] = "isInPlayerMode";
names[IsLoopedRole] = "isLooped";
names[VolumeRole] = "volume";
names[IsPlayingRegionEnabledRole] = "isPlayRegionEnabled";
names[PlayRegionBeginRole] = "playRegionBegin";
names[PlayRegionEndRole] = "playRegionEnd";
names[TriggerStyleRole] = "triggerStyle";
return names;
}
CasterPlayerController *CasterPlayerModel::list() const
{
return mList;
}
void CasterPlayerModel::setList(CasterPlayerController *list)
{
beginResetModel();
if(mList)
mList->disconnect(this);
mList = list;
if (mList) {
connect(mList, &CasterPlayerController::preItemAppend, this, [=]() {
const int index = mList->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(mList, &CasterPlayerController::postItemAppend, this, [=]() {
endInsertRows();
});
connect(mList, &CasterPlayerController::preItemRemoved, this, [=](int index) {
beginRemoveRows(QModelIndex(), index, index);
});
connect(mList, &CasterPlayerController::postItemRemoved, this, [=]() {
endRemoveRows();
});
}
endResetModel();
}