-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmastermodel.cpp
277 lines (236 loc) · 6.44 KB
/
mastermodel.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "mastermodel.h"
MasterModel::MasterModel(QObject *parent) :
QAbstractTableModel(parent)
{
qRegisterMetaType<QMap<QString,QMap<int,QString> >>("MyQMapType");
qRegisterMetaType<QMap<QString, QStringList >>("MyQMapType2");
qRegisterMetaType<QList<QPair<int, QString>> >("MyListType");
mode = Mode::ALBUM;
count = 0;
}
void MasterModel::initializeModel()
{
beginResetModel();
LibraryCreator *albumCreator = new LibraryCreator(this);
albumCreator->setIndex(1);
albumCreator->start();
connect(albumCreator, SIGNAL(createdAlbumList(QMap<QString,QStringList >)),
this, SLOT(setAlbumList(QMap<QString,QStringList >)));
LibraryCreator *artistCreator = new LibraryCreator(this);
artistCreator->setIndex(2);
artistCreator->start();
connect(artistCreator, SIGNAL(createdArtistList(QList<QPair<int, QString>>)),
this, SLOT(setArtistList(QList<QPair<int, QString>>)));
LibraryCreator *songCreator = new LibraryCreator(this);
songCreator->setIndex(3);
songCreator->start();
connect(songCreator, SIGNAL(createdSongList(QMap<QString,QMap<int,QString> >)),
this, SLOT(setSongList(QMap<QString,QMap<int,QString> >)));
albumCreator->wait();
artistCreator->wait();
songCreator->wait();
count = Manager::parser.GetAllAlbumsCount();
endResetModel();
delete albumCreator;
delete artistCreator;
delete songCreator;
}
void MasterModel::setMode(const MasterModel::Mode &_mode)
{
beginResetModel();
mode = _mode;
switch (mode)
{
case Mode::ALBUM:
count = Manager::parser.GetAllAlbumsCount();
break;
case Mode::ARTIST:
count = Manager::parser.GetAllArtistsCount();
break;
case Mode::SONG:
count = Manager::parser.GetAllSongsCount();
break;
}
resetInternalData();
endResetModel();
}
void MasterModel::playPlaylist(int row)
{
int num;
switch(mode)
{
case Mode::ALBUM:
num = albumList["ID"].at(row).toInt();
emit playlistPlayed("Album", num);
break;
case Mode::ARTIST:
num = artistList.at(row).first;
emit playlistPlayed("Artist", num);
break;
case Mode::SONG:
num = row;
emit playlistPlayed("Song", num);
break;
}
}
void MasterModel::queuePlaylist(int row)
{
int num;
switch(mode)
{
case Mode::ALBUM:
num = albumList["ID"].at(row).toInt();
emit playlistQueued("Album", num);
break;
case Mode::ARTIST:
num = artistList.at(row).first;
emit playlistQueued("Artist", num);
break;
case Mode::SONG:
num = row;
emit playlistQueued("Song", num);
break;
}
}
void MasterModel::setAlbumList(QMap<QString, QStringList > _albumList)
{
albumList = _albumList;
}
void MasterModel::setArtistList(QList<QPair<int, QString>> _artistList)
{
artistList = _artistList;
}
void MasterModel::setSongList(QMap<QString, QMap<int, QString> > _songList)
{
songList = _songList;
}
int MasterModel::rowCount(const QModelIndex &parent) const
{
return count;
}
int MasterModel::columnCount(const QModelIndex &parent) const
{
if (Manager::master.GetCount() <= 0)
return 0;
switch(mode)
{
case Mode::ALBUM:
return AlbumCol;
break;
case Mode::ARTIST:
return ArtistCol;
break;
case Mode::SONG:
return SongCol;
break;
}
}
QVariant MasterModel::data(const QModelIndex &index, int role) const
{
if (Manager::master.GetCount() <= 0)
return QVariant();
if (!index.isValid())
return QVariant();
int row = index.row();
int col = index.column();
if (role == Qt::DisplayRole)
{
switch(mode)
{
case Mode::ALBUM:
{
if (col < 0 || col >= AlbumCol || albumList.count() <= 0)
return QVariant();
switch(col)
{
case 0:
return albumList["Album_name"].at(row);
break;
case 1:
return albumList["Artist_name"].at(row);
break;
case 2:
QString year = albumList["Year"].at(row);
if (year != "0")
return year;
else
return QVariant();
break;
}
}
break;
case Mode::ARTIST:
return artistList.at(row).second;
break;
case Mode::SONG:
{
if (col < 0 || col >= SongCol)
return QVariant();
switch(col)
{
case 0:
return songList["Title"][row];
break;
case 1:
return songList["Artist"][row];
break;
case 2:
return songList["Album"][row];
break;
case 3:
return songList["Genre"][row];
break;
case 4:
return songList["Year"][row];
break;
case 5:
return songList["Duration"][row];
break;
}
}
break;
}
}
return QVariant();
}
Qt::ItemFlags MasterModel::flags(const QModelIndex &index) const
{
if (index.isValid())
return (QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled);
}
QStringList MasterModel::mimeTypes() const
{
QStringList types;
types << "application/x-playlist";
return types;
}
QMimeData *MasterModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
QString type;
int num;
switch(mode)
{
case Mode::ALBUM:
type = "Album";
num = albumList["ID"].at(index.row()).toInt();
break;
case Mode::ARTIST:
type = "Artist";
num = artistList.at(index.row()).first;
break;
case Mode::SONG:
type = "Song";
num = index.row();
break;
}
stream << type << num;
}
}
mimeData->setData("application/x-playlist", encodedData);
return mimeData;
}