-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicpagemusics.cpp
165 lines (153 loc) · 5.91 KB
/
musicpagemusics.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
#include "musicpagemusics.h"
#include <QHeaderView>
#include <QVBoxLayout>
#include <QDebug>
#include <ddialog.h>
#include <QMenu>
#include "musicplayer.h"
#include "currentdevicemanager.h"
DWIDGET_USE_NAMESPACE
MusicPageMusics::MusicPageMusics(QList<MusicPageMusicItem> *musicItems, QWidget *parent) : QWidget(parent)
{
QVBoxLayout *mainVBLayout = new QVBoxLayout(this);
m_musicsTable = new QTableWidget(this);
m_columnCount = 5;
m_musicsTable->setRowCount(musicItems->count());
m_musicsTable->setColumnCount(m_columnCount);
m_musicsTable->setSelectionBehavior(QTableWidget::SelectRows);
m_musicsTable->setShowGrid(false);
m_musicsTable->setWordWrap(false);
m_musicsTable->setEditTriggers(QTableWidget::NoEditTriggers);
m_musicsTable->setColumnWidth(0, 200);
m_musicsTable->setColumnWidth(1, 180);
m_musicsTable->setColumnWidth(2, 180);
m_musicsTable->setHorizontalHeaderLabels(QStringList() << "Name" << "Artist" << "Album" << "Year" << "Time");
m_musicsTable->verticalHeader()->setHidden(true);
m_musicsTable->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
m_musicsTable->setContextMenuPolicy(Qt::CustomContextMenu);
initMusicTable(musicItems, m_columnCount);
Device *currentDevice = CurrentDeviceManager::getInstance()->getCurrentDevice();
connect(currentDevice, &Device::musicFilesDeleted, this, &MusicPageMusics::removeMusicItems);
connect(m_musicsTable, &QTableWidget::cellDoubleClicked, this, &MusicPageMusics::playSelected);
connect(m_musicsTable, &QTableWidget::customContextMenuRequested, this, &MusicPageMusics::showContextMenu);
mainVBLayout->setContentsMargins(0, 0, 0, 0);
mainVBLayout->addWidget(m_musicsTable);
this->setLayout(mainVBLayout);
}
void MusicPageMusics::initMusicTable(QList<MusicPageMusicItem> *musicItems, int columnCount)
{
int row = 0;
foreach (MusicPageMusicItem musicItem, *musicItems) {
QString path = musicItem.filePath();
for (int column = 0; column < columnCount; ++column) {
QTableWidgetItem *tableItem = new QTableWidgetItem;
switch (column) {
case 0:
tableItem->setText(musicItem.title());
break;
case 1:
tableItem->setText(musicItem.artist());
break;
case 2:
tableItem->setText(musicItem.album());
break;
case 3:
tableItem->setText(musicItem.year());
break;
case 4:
tableItem->setText(musicItem.time());
break;
default:
break;
}
tableItem->setWhatsThis(path);
m_musicsTable->setItem(row, column, tableItem);
}
m_playList.addMedia(QUrl::fromLocalFile(path));
row++;
}
setRowBackgroud();
}
void MusicPageMusics::setRowBackgroud()
{
for (int row = 0; row < m_musicsTable->rowCount(); ++row) {
for (int col = 0; col < m_columnCount; ++col) {
QTableWidgetItem *tableItem = m_musicsTable->item(row, col);
if (row % 2 != 0) {
tableItem->setBackgroundColor(QColor("#f5f6f7"));
} else {
tableItem->setBackgroundColor(QColor("#ffffff"));
}
}
}
}
void MusicPageMusics::showContextMenu()
{
QMenu contextMenu(this);
contextMenu.addAction("Play");
contextMenu.addSeparator();
contextMenu.addAction("Delete");
QAction *selectedAction = contextMenu.exec(QCursor::pos());
if (selectedAction != 0) {
if (selectedAction->text() == "Play") {
playSelected();
} else if (selectedAction->text() == "Delete") {
removeSelectedMusics();
}
}
}
// 获取当前选择的项目并通过信号传递给MusicPage类去删除文件
void MusicPageMusics::removeSelectedMusics()
{
QList<QTableWidgetItem *> selectedMusicItems = m_musicsTable->selectedItems();
QStringList selectedMusicsPath;
QString selectedMusicsName;
foreach (QTableWidgetItem *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() << "(MusicPageMusics::removeSelectedMusics) cancel delete selection.";
}
} else {
qDebug() << "(MusicPageMusics::removeSelectedMusics) no selected music!";
}
}
// 收到文件删除的信号后更新ui
void MusicPageMusics::removeMusicItems(QStringList deletedMusicsPath)
{
// 获取要移除的行
QList<int> rows;
for (int row = 0; row < m_musicsTable->rowCount(); ++row) {
QTableWidgetItem *item = m_musicsTable->item(row, 0);
if (deletedMusicsPath.contains(item->whatsThis())) {
rows << row;
}
}
// 移除
for (int times = 0; times < rows.count(); ++times) {
// 每移除一行后musicTable的内容就会发生变动,移除行之后的行会向前填补,
// 那么rows中记录的行数就不能定位到要删除的行, 因此行数需要减去之前移除过的次数
m_musicsTable->removeRow(rows[times] - times);
// 播放列表同理
m_playList.removeMedia(rows[times] - times);
}
setRowBackgroud();
}
void MusicPageMusics::playSelected()
{
QMediaPlayer *player = MusicPlayer::getPlayer();
player->stop();
player->setPlaylist(&m_playList);
m_playList.setCurrentIndex(m_musicsTable->currentRow());
player->play();
}