This repository was archived by the owner on Jul 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaylistmodel.h
More file actions
107 lines (89 loc) · 3.42 KB
/
playlistmodel.h
File metadata and controls
107 lines (89 loc) · 3.42 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
#ifndef PLAYLISTMODEL_H
#define PLAYLISTMODEL_H
#include <vector>
#include <QAbstractTableModel>
#include <QStringList>
#include <QFileInfo>
#include <QDir>
#include <QUrl>
#include <QMediaPlayer>
#include <QEventLoop>
#include <QMediaMetaData>
#include <QImage>
#include <QStyledItemDelegate>
struct MusicTrack {
QString filePath, title, artist, album;
qint64 duration;
QImage cover;
MusicTrack(const QString& path) noexcept : filePath(path) {
QMediaPlayer probe;
probe.setSource(QUrl::fromLocalFile(path));
QEventLoop loop;
QObject::connect(&probe, &QMediaPlayer::metaDataChanged,&loop, &QEventLoop::quit);
loop.exec();
duration = probe.duration();
cover = probe.metaData().value(QMediaMetaData::CoverArtImage).value<QImage>();
if (cover.isNull()) cover = probe.metaData().value(QMediaMetaData::ThumbnailImage).value<QImage>();
title = probe.metaData().stringValue(QMediaMetaData::Title);
if (title.isEmpty()) title = QFileInfo(path).baseName();
artist = probe.metaData().stringValue(QMediaMetaData::Author);
if (artist.isEmpty()){
artist = probe.metaData().stringValue(QMediaMetaData::AlbumArtist);
if(artist.isEmpty()){
artist = probe.metaData().stringValue(QMediaMetaData::ContributingArtist);
if(artist.isEmpty()) artist = "未知艺术家";
}
}
album = probe.metaData().stringValue(QMediaMetaData::AlbumTitle);
if (album.isEmpty()) album = "未知专辑";
}
};
class PlaylistModel : public QAbstractTableModel {
Q_OBJECT
public:
enum Column {
Delete = 0, Title, Artist, Album, Duration,
ColumnCount
};
enum PlayMode : uint8_t {
Ordered, Looped, Shuffled
} playMode{PlayMode::Ordered};
explicit PlaylistModel(QWidget* parent = nullptr) noexcept;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
void addMusicFile(const QString& filePath) noexcept;
void addMusicFolder(const QString& folderPath) noexcept;
void clearPlaylist() noexcept;
const MusicTrack* getTrack(int index) const noexcept;
int getTrackCount() const noexcept;
void removeTrack(int index) noexcept;
void shuffle() noexcept;
std::vector<int> order;
public slots:
bool savePlayList() noexcept;
bool loadPlayList() noexcept;
signals:
void playlistChanged();
private:
QString defaultPath() noexcept;
QString formatDuration(qint64 milliseconds) const noexcept;
QWidget* parent;
QList<MusicTrack> m_tracks;
QStringList m_supportedFormats;
};
class CenterIconDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
void initStyleOption(QStyleOptionViewItem* opt, const QModelIndex& idx) const override {
QStyledItemDelegate::initStyleOption(opt, idx);
if (idx.column() == PlaylistModel::Delete) {
opt->decorationAlignment = Qt::AlignCenter;
opt->decorationPosition = QStyleOptionViewItem::Left;
opt->decorationSize = opt->rect.size() / 1.25f;
}
}
};
#endif // PLAYLISTMODEL_H