-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicpagealbum.cpp
218 lines (200 loc) · 8.24 KB
/
musicpagealbum.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
#include "musicpagealbum.h"
#include "musicplayer.h"
#include "musicpage.h"
#include "currentdevicemanager.h"
#include "tools/taglibtool.h"
#include <QVBoxLayout>
#include <QDebug>
#include <QListWidget>
#include <ddialog.h>
#include <QMenu>
DWIDGET_USE_NAMESPACE
MusicPageAlbum::MusicPageAlbum(QList<MusicPageMusicItem> *musicItems, QWidget *parent) : QWidget(parent)
{
QVBoxLayout *mainVBLayout = new QVBoxLayout(this);
ToolsBar *toolsBar = MusicPage::getToolsBar();
m_backButton = new QPushButton(this);
m_backButton->setIcon(QIcon(":/images/back.png"));
m_backButton->hide();
toolsBar->addLeftWidget(m_backButton);
m_stack = new QStackedWidget(this);
m_stack->setContentsMargins(0, 0, 0, 0);
initMap(musicItems);
initUI();
Device *currentDevice = CurrentDeviceManager::getInstance()->getCurrentDevice();
connect(currentDevice, &Device::musicFilesDeleted, this, &MusicPageAlbum::removeMusicItems);
connect(m_backButton, &QPushButton::clicked, this, &MusicPageAlbum::backToAlbumList);
mainVBLayout->setMargin(0);
mainVBLayout->setContentsMargins(0, 0, 0, 0);
mainVBLayout->addWidget(m_stack);
this->setLayout(mainVBLayout);
}
void MusicPageAlbum::initMap(QList<MusicPageMusicItem> *musicItems)
{
foreach (MusicPageMusicItem musicItem, *musicItems) {
QString album = musicItem.album();
QString title = musicItem.title();
QString filePath = musicItem.filePath();
QListWidgetItem *listItem = new QListWidgetItem(title);
// 设置行高
listItem->setSizeHint(QSize(0, 30));
listItem->setWhatsThis(filePath);
QListWidget *list;
QMediaPlaylist *playList;
// 如果歌曲中没有专辑信息(长度为零)将其设置为"unknown"
if (album.count() == 0) {
album = "unknown";
}
if (m_albumMap.contains(album)) {
list = m_albumMap.value(album);
playList = m_playlistMap.value(album);
} else {
list = new QListWidget(m_stack);
playList = new QMediaPlaylist(this);
m_albumMap.insert(album, list);
m_playlistMap.insert(album, playList);
}
list->addItem(listItem);
playList->addMedia(QUrl::fromLocalFile(filePath));
}
}
void MusicPageAlbum::initUI()
{
QListWidget *albumList = new QListWidget(m_stack);
albumList->setViewMode(QListWidget::IconMode);
albumList->setIconSize(DefaultIconSize);
albumList->setAcceptDrops(false);
connect(albumList, &QListWidget::itemDoubleClicked, this, &MusicPageAlbum::changeToList);
// 让albumList作为第一个stack的控件
m_stack->addWidget(albumList);
QPixmap defaultAlbumPix(":/images/album.svg");
QIcon defaultAlbumIcon(defaultAlbumPix.scaled(DefaultIconSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
QList<QString> albums = m_albumMap.keys();
foreach (QString album, albums) {
// 添加map中和album这个key对应的listwidget到mstack
QListWidget * list = m_albumMap.value(album);
list->setSelectionMode(QListWidget::ExtendedSelection);
list->setContextMenuPolicy(Qt::CustomContextMenu);
connect(list, &QListWidget::customContextMenuRequested, this, &MusicPageAlbum::showMusicContextMenu);
connect(list, &QListWidget::itemDoubleClicked, this, &MusicPageAlbum::playSelectedMusic);
setListItemBackground(list);
m_stack->addWidget(list);
// 添加album到首页albumList
QListWidgetItem *albumItem = new QListWidgetItem(album, albumList);
albumItem->setSizeHint(QSize(150, 150));
// 通过这个专辑的歌曲列表的第一个歌曲获取专辑封面
QPixmap albumPix = TaglibTool::getAlbumPicture(list->item(0)->whatsThis());
if (!albumPix.isNull()) {
// 缩放下图片, 否则占用内存太大
QIcon albumIcon(albumPix.scaled(DefaultIconSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
albumItem->setIcon(albumIcon);
} else {
albumItem->setIcon(defaultAlbumIcon);
}
albumList->addItem(albumItem);
}
}
void MusicPageAlbum::setListItemBackground(QListWidget *list)
{
for (int row = 0; row < list->count(); ++row) {
QListWidgetItem *item = list->item(row);
if (row % 2 != 0) {
item->setBackgroundColor(QColor("#f5f6f7"));
} else {
item->setBackgroundColor(QColor("#ffffff"));
}
}
}
// 获取当前选择的项目并通过信号传递给MusicPage类去删除文件
void MusicPageAlbum::removeSelectedMusics()
{
if (m_stack->currentIndex() != 0) {
QListWidget * currentList = static_cast<QListWidget *>(m_stack->currentWidget());
QList<QListWidgetItem *> selectedMusicItems = currentList->selectedItems();
QStringList selectedMusicsPath;
QString selectedMusicsName;
foreach (QListWidgetItem *item, selectedMusicItems) {
QString path = item->whatsThis();
if (!selectedMusicsPath.contains(path)) {
selectedMusicsPath << path;
selectedMusicsName += item->text() + "\n";
}
}
if (selectedMusicsPath.count() > 0) {
DDialog delDialog("Delete:", selectedMusicsName, this);
delDialog.addButton("Cancel");
delDialog.addButton("Delete", true, DDialog::ButtonWarning);
if (delDialog.exec() == DDialog::Accepted) {
CurrentDeviceManager::getInstance()->getCurrentDevice()->deleteMusicFiles(selectedMusicsPath);
} else {
qDebug() << "(MusicPageAlbum::removeSelectedMusics) cancel delete selection.";
}
} else {
qDebug() << "(MusicPageAlbum::removeSelectedMusics) no selected music!";
}
}}
// 收到文件删除的信号后更新ui
void MusicPageAlbum::removeMusicItems(QStringList deletedMusicsPath)
{
// 遍历每个列表中的每个项
// index从1开始,是因为mstack第一个widget是albumList其不包含音乐
for (int i = 1; i < m_stack->count(); ++i) {
QListWidget * list = static_cast<QListWidget *>(m_stack->widget(i));
QMediaPlaylist * playList = m_playlistMap.value(m_albumMap.key(list));
// 获取要移除的行
QList<int> rows;
for (int row = 0; row < list->count(); ++row) {
if (deletedMusicsPath.contains(list->item(row)->whatsThis())) {
rows << row;
}
}
// 移除
for (int times = 0; times < rows.count(); ++times) {
// 每移除一行后list的内容就会发生变动,移除行之后的行会向前填补,
// 那么rows中记录的行数就不能定位到要删除的行, 因此行数需要减去之前移除过的次数
delete list->takeItem(rows.at(times) - times);
// 播放列表同理
playList->removeMedia(rows.at(times) - times);
}
setListItemBackground(list);
}
}
void MusicPageAlbum::playSelectedMusic()
{
QListWidget * currentList = static_cast<QListWidget *>(m_stack->currentWidget());
QMediaPlaylist * playList = m_playlistMap.value(m_albumMap.key(currentList));
QMediaPlayer *player = MusicPlayer::getPlayer();
player->stop();
player->setPlaylist(playList);
playList->setCurrentIndex(currentList->currentRow());
player->play();
}
void MusicPageAlbum::changeToList(QListWidgetItem *item)
{
QListWidget * albumList = static_cast<QListWidget *>(m_stack->widget(0));
int row = albumList->row(item);
// m_stack中的页面的index和albumList中的项对应
// 但m_stack中的第一个页面是albumList, 因此row需要+1
m_stack->setCurrentIndex(row + 1);
m_backButton->show();
}
void MusicPageAlbum::backToAlbumList()
{
m_stack->setCurrentIndex(0);
m_backButton->hide();
}
void MusicPageAlbum::showMusicContextMenu()
{
QMenu contextMenu(this);
contextMenu.addAction("Play");
contextMenu.addSeparator();
contextMenu.addAction("Delete");
QAction *selectedAction = contextMenu.exec(QCursor::pos());
if (selectedAction != 0) {
if (selectedAction->text() == "Play") {
playSelectedMusic();
} else if (selectedAction->text() == "Delete") {
removeSelectedMusics();
}
}
}