-
Notifications
You must be signed in to change notification settings - Fork 2
/
musicbeans.h
189 lines (166 loc) · 4.31 KB
/
musicbeans.h
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
#ifndef MUSIC_H
#define MUSIC_H
#include <QUrl>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include <QList>
#include <QDateTime>
#include <QSettings>
#define JSON_VAL_LONG(json, x) static_cast<qint64>(json.value(#x).toDouble())
#define JVAL_LONG(x) static_cast<qint64>(json.value(#x).toDouble())
#define JVAL_INT(x) json.value(#x).toInt()
#define JVAL_STR(x) json.value(#x).toString()
#define snum(x) QString::number(x)
struct Artist
{
qint64 id = 0;
QString name;
QString faceUrl;
static Artist fromJson(QJsonObject json)
{
Artist artist;
artist.id = JVAL_LONG(id);
artist.name = JVAL_STR(name);
artist.faceUrl = JVAL_STR(img1v1Url);
return artist;
}
QJsonObject toJson() const
{
QJsonObject json;
json.insert("id", id);
json.insert("name", name);
json.insert("faceUrl", faceUrl);
return json;
}
};
struct Album
{
qint64 id = 0;
QString name;
int size = 0;
int mark = 0;
static Album fromJson(QJsonObject json)
{
Album album;
album.id = JVAL_LONG(id);
album.name = JVAL_STR(name);
album.size = JVAL_INT(size);
return album;
}
QJsonObject toJson() const
{
QJsonObject json;
json.insert("id", id);
json.insert("name", name);
json.insert("size", size);
return json;
}
};
struct Music
{
qint64 id = 0;
QString name;
int duration = 0;
QList<Artist> artists;
Album album;
QString artistNames;
static Music fromJson(QJsonObject json)
{
Music music;
music.id = JVAL_LONG(id);
music.name = JVAL_STR(name);
QJsonArray array = json.value("artists").toArray(); // json中读出的artist放入一个数组
QStringList artistNameList;
foreach (QJsonValue val, array)
{
Artist artist = Artist::fromJson(val.toObject());
music.artists.append(artist);
artistNameList.append(artist.name);
}
music.artistNames = artistNameList.join("/");
music.album = Album::fromJson(json.value("album").toObject());
music.duration = JVAL_INT(duration);
return music;
}
QJsonObject toJson() const
{
QJsonObject json;
json.insert("id", id);
json.insert("name", name);
json.insert("duration", duration);
QJsonArray array;
foreach (Artist artist, artists)
array.append(artist.toJson());
json.insert("artists", array);
json.insert("album", album.toJson());
return json;
}
bool isValid() const
{
return id;
}
bool operator==(const Music& music)
{
return this->id == music.id;
}
QString simpleString() const
{
return name + "-" + artistNames;
}
};
struct PlayListCreator
{
QString nickname;
QString signature;
QString avatarUrl;
};
struct PlayList
{
QString name;
qint64 id;
QString description;
QStringList tags;
int playCount;
PlayListCreator creator;
QList<Music> contiansMusic;
static PlayList fromJson(QJsonObject json)
{
PlayList playlist;
// music.id = JVAL_LONG(id);
playlist.name = JVAL_STR(name);
QJsonArray array = json.value("contiansMusic").toArray();
QStringList contiansMusicList;
foreach (QJsonValue val, array)
{
Music music = Music::fromJson(val.toObject());
playlist.contiansMusic.append(music);
contiansMusicList.append(music.name);
}
// music.artistNames = artistNameList.join("/");
// music.album = Album::fromJson(json.value("album").toObject());
// music.duration = JVAL_INT(duration);
return playlist;
}
QJsonObject toJson() const
{
QJsonObject json;
// json.insert("id", id);
json.insert("name", name);
QJsonArray array;
foreach (Music music, contiansMusic)
array.append(music.toJson());
json.insert("contiansMusic", array);
// json.insert("duration", duration);
// json.insert("album", album.toJson());
return json;
}
bool operator==(const PlayList& pl)
{
return this->id == pl.id;
}
};
typedef QList<Music> SongList;
typedef QList<PlayList> PlayListList;
#endif // MUSIC_H