-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicpagedirs.cpp
240 lines (220 loc) · 8.67 KB
/
musicpagedirs.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
#include "musicpagedirs.h"
#include "musicpage.h"
#include "musicplayer.h"
#include "currentdevicemanager.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QDebug>
#include <QMenu>
#include <ddialog.h>
DWIDGET_USE_NAMESPACE
MusicPageDirs::MusicPageDirs(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);
initStack();
Device *currentDevice = CurrentDeviceManager::getInstance()->getCurrentDevice();
connect(currentDevice, &Device::musicFilesDeleted, this, &MusicPageDirs::removeMusicItems);
connect(m_backButton, &QPushButton::clicked, this, &MusicPageDirs::backToDirs);
mainVBLayout->setMargin(0);
mainVBLayout->setContentsMargins(0, 0, 0, 0);
mainVBLayout->addWidget(m_stack);
this->setLayout(mainVBLayout);
}
void MusicPageDirs::initMap(QList<MusicPageMusicItem> *musicItems)
{
foreach (MusicPageMusicItem musicItem, *musicItems) {
QString dir = musicItem.parentDir();
QString title = musicItem.title();
QString filePath = musicItem.filePath();
QListWidgetItem *listItem = new QListWidgetItem(title);
listItem->setWhatsThis(filePath);
QListWidget *list;
QMediaPlaylist *playList;
if (m_dirMap.contains(dir)) {
list = m_dirMap.value(dir);
playList = m_playlistMap.value(dir);
} else {
list = new QListWidget(m_stack);
playList = new QMediaPlaylist(this);
m_dirMap.insert(dir, list);
m_playlistMap.insert(dir, playList);
}
list->addItem(listItem);
playList->addMedia(QUrl::fromLocalFile(filePath));
}
}
void MusicPageDirs::initStack()
{
QTableWidget *dirTable = new QTableWidget(m_stack);
initDirTable(dirTable);
connect(dirTable, &QTableWidget::cellDoubleClicked, this, &MusicPageDirs::changeToList);
// 让dirTable作为第一个stack的控件
m_stack->addWidget(dirTable);
QList<QString> dirs = m_dirMap.keys();
int row = 0;
foreach (QString dir, dirs) {
// 添加map中和dir这个key对应的listwidget到mstack
QListWidget * list = m_dirMap.value(dir);
list->setSelectionMode(QListWidget::ExtendedSelection);
list->setContextMenuPolicy(Qt::CustomContextMenu);
connect(list, &QListWidget::customContextMenuRequested, this, &MusicPageDirs::showMusicContextMenu);
connect(list, &QListWidget::itemDoubleClicked, this, &MusicPageDirs::playSelectedMusic);
setListItemStyle(list, true);
m_stack->addWidget(list);
// 设置dirTable每一行的内容(文件夹名称和包含的音乐的数量)
QTableWidgetItem *nameItem = new QTableWidgetItem;
QTableWidgetItem *countItem = new QTableWidgetItem;
nameItem->setText(dir);
nameItem->setIcon(QIcon(":/images/dir.png"));
countItem->setText(QString::number(m_dirMap.value(dir)->count()));
dirTable->setItem(row, 0, nameItem);
dirTable->setItem(row, 1, countItem);
row++;
}
setTableRowBackgroud(dirTable);
}
void MusicPageDirs::initDirTable(QTableWidget *dirTable)
{
dirTable->setIconSize(QSize(30, 30));
dirTable->setRowCount(m_dirMap.count());
dirTable->setColumnCount(2);
dirTable->setSelectionBehavior(QTableWidget::SelectRows);
dirTable->setShowGrid(false);
dirTable->setWordWrap(false);
dirTable->setEditTriggers(QTableWidget::NoEditTriggers);
dirTable->setColumnWidth(0, 600);
dirTable->setColumnWidth(1, 180);
dirTable->setHorizontalHeaderLabels(QStringList() << "Name" << "Count");
dirTable->verticalHeader()->setHidden(true);
dirTable->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
dirTable->setContextMenuPolicy(Qt::CustomContextMenu);
}
void MusicPageDirs::setTableRowBackgroud(QTableWidget *dirTable)
{
for (int row = 0; row < dirTable->rowCount(); ++row) {
for (int col = 0; col < dirTable->columnCount(); ++col) {
QTableWidgetItem *tableItem = dirTable->item(row, col);
if (row % 2 != 0) {
tableItem->setBackgroundColor(QColor("#f5f6f7"));
} else {
tableItem->setBackgroundColor(QColor("#ffffff"));
}
}
}
}
void MusicPageDirs::setListItemStyle(QListWidget *list, bool setItemHigh)
{
for (int row = 0; row < list->count(); ++row) {
QListWidgetItem *item = list->item(row);
// 设置行高
if (setItemHigh) {
item->setSizeHint(QSize(0, 30));
}
//设置背景
if (row % 2 != 0) {
item->setBackgroundColor(QColor("#f5f6f7"));
} else {
item->setBackgroundColor(QColor("#ffffff"));
}
}
}
// 获取当前选择的项目并通过信号传递给MusicPage类去删除文件
void MusicPageDirs::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() << "(MusicPageDirs::removeSelectedMusics) cancel delete selection.";
}
} else {
qDebug() << "(MusicPageDirs::removeSelectedMusics) no selected music!";
}
}
}
// 收到文件删除的信号后更新ui
void MusicPageDirs::removeMusicItems(QStringList deletedMusicsPath)
{
// 遍历每个列表中的每个项
// index从1开始,是因为mstack第一个widget是dirTabel不是listwidget
for (int i = 1; i < m_stack->count(); ++i) {
QListWidget * list = static_cast<QListWidget *>(m_stack->widget(i));
QMediaPlaylist * playList = m_playlistMap.value(m_dirMap.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);
}
setListItemStyle(list);
}
}
void MusicPageDirs::playSelectedMusic()
{
QListWidget * currentList = static_cast<QListWidget *>(m_stack->currentWidget());
QMediaPlaylist * playList = m_playlistMap.value(m_dirMap.key(currentList));
QMediaPlayer *player = MusicPlayer::getPlayer();
player->stop();
player->setPlaylist(playList);
playList->setCurrentIndex(currentList->currentRow());
player->play();
}
void MusicPageDirs::changeToList(int row)
{
// m_stack中的页面的index和dirTable中的行对应
// 但m_stack中的第一个页面是dirTab, 因此row需要+1
m_stack->setCurrentIndex(row + 1);
m_backButton->show();
}
void MusicPageDirs::backToDirs()
{
m_stack->setCurrentIndex(0);
m_backButton->hide();
}
void MusicPageDirs::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();
}
}
}