-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylistswidget.cpp
More file actions
132 lines (119 loc) · 4.31 KB
/
Copy pathplaylistswidget.cpp
File metadata and controls
132 lines (119 loc) · 4.31 KB
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
#include "playlistswidget.h"
#include "createdialog.h"
#include "deletedialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QListWidget>
#include <QListWidgetItem>
#include <QIcon>
#include <QPainter>
#include <QMessageBox>
#include <QDebug>
#include <QStyledItemDelegate>
class PlaylistItemDelegate : public QStyledItemDelegate
{
public:
explicit PlaylistItemDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QVariant data = index.data(Qt::UserRole);
if (data.canConvert<PlaylistInfo>()) {
PlaylistInfo info = data.value<PlaylistInfo>();
QRect rect = option.rect;
QRect coverRect(rect.x(), rect.y(), 150, 150);
if (!info.cover.isNull()) {
painter->drawImage(coverRect, info.cover.scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation));
} else {
painter->fillRect(coverRect, QBrush(Qt::lightGray));
painter->drawText(coverRect, Qt::AlignCenter, "No image");
}
QRect nameRect(rect.x(), rect.y() + 150, rect.width(), 30);
painter->drawText(nameRect, Qt::AlignCenter, info.name);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
Q_UNUSED(option);
Q_UNUSED(index);
return QSize(160, 190);
}
};
PlaylistsWidget::PlaylistsWidget(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
QHBoxLayout *topLayout = new QHBoxLayout;
QPushButton *addBtn = new QPushButton("+");
addBtn->setFixedSize(40, 40);
QPushButton *deleteBtn = new QPushButton("Delete");
deleteBtn->setFixedSize(70, 40);
topLayout->addWidget(addBtn);
topLayout->addWidget(deleteBtn);
topLayout->addStretch();
layout->addLayout(topLayout);
m_listWidget = new QListWidget;
m_listWidget->setViewMode(QListView::IconMode);
m_listWidget->setIconSize(QSize(150, 150));
m_listWidget->setGridSize(QSize(160, 190));
m_listWidget->setResizeMode(QListView::Adjust);
m_listWidget->setItemDelegate(new PlaylistItemDelegate(this));
layout->addWidget(m_listWidget);
connect(addBtn, &QPushButton::clicked, this, &PlaylistsWidget::onAddClicked);
connect(deleteBtn, &QPushButton::clicked, this, &PlaylistsWidget::onDeleteClicked);
connect(m_listWidget, &QListWidget::itemDoubleClicked, this, &PlaylistsWidget::onItemDoubleClicked);
loadPlaylists();
}
void PlaylistsWidget::loadPlaylists()
{
m_listWidget->clear();
QList<PlaylistInfo> playlists = m_manager.getAllPlaylists();
for (const PlaylistInfo &info : playlists) {
QListWidgetItem *item = new QListWidgetItem;
item->setData(Qt::UserRole, QVariant::fromValue(info));
item->setSizeHint(QSize(160, 190));
m_listWidget->addItem(item);
}
qDebug() << "Loaded" << playlists.size() << "playlists";
}
void PlaylistsWidget::onAddClicked()
{
CreateDialog dlg(this);
if (dlg.exec() == QDialog::Accepted) {
QString name = dlg.playlistName();
QStringList files = dlg.selectedFiles();
if (!name.isEmpty() && !files.isEmpty()) {
if (m_manager.createPlaylist(name, files)) {
loadPlaylists();
} else {
QMessageBox::warning(this, "Ошибка", "Не удалось создать плейлист.");
}
}
}
}
void PlaylistsWidget::onDeleteClicked()
{
DeleteDialog dlg(this);
if (dlg.exec() == QDialog::Accepted) {
QString name = dlg.playlistName();
if (!name.isEmpty()) {
if (m_manager.deletePlaylist(name)) {
loadPlaylists();
} else {
QMessageBox::warning(this, "Ошибка", "Плейлист не найден или");
}
}
}
}
void PlaylistsWidget::onItemDoubleClicked(QListWidgetItem *item)
{
PlaylistInfo info = item->data(Qt::UserRole).value<PlaylistInfo>();
if (!info.tracks.isEmpty()) {
emit playlistSelected(info.tracks);
}
}
void PlaylistsWidget::refreshPlaylists()
{
loadPlaylists();
}