-
Notifications
You must be signed in to change notification settings - Fork 1
/
playerwidget.cpp
124 lines (94 loc) · 4.01 KB
/
playerwidget.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
# include "playerwidget.h"
# include "ui_playerwidget.h"
# include "listmodelbase.h"
# include "sound.h"
# include "soundcloudapi.h"
PlayerWidget::PlayerWidget(QWidget *parent) : QWidget(parent), ui(new Ui::PlayerWidget)
{
ui->setupUi(this);
// component setup
setupPlaybackManager();
// connections
connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(playPreviousSong()));
connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(playNextSong()));
connect(&SoundCloudApi::getInstance(), SIGNAL(artworkReceived(int,QPixmap&)), this, SLOT(handleArtworkUpdate(int,QPixmap&)));
}
PlayerWidget::~PlayerWidget()
{
soundManager->pause();
delete soundManager;
delete ui;
}
// -- public slots
void PlayerWidget::handlePlayRequest(QModelIndex index) {
const ListModelBase* currentModel = (ListModelBase*) index.model();
const Sound song = currentModel->getItem(index);
// should prev/next buttons be enabled?
ui->prevButton->setEnabled(index.row() > 0);
ui->nextButton->setEnabled(index.row() < (currentModel->rowCount() - 1));
soundManager->playSound(song.getId());
ui->soundNameLabel->setText(song.getTitle());
ui->creatorNameLabel->setText(song.getUser());
if (song.hasArtwork()) {
ui->artworkLabel->setPixmap(song.getArtwork());
} else {
SoundCloudApi::getInstance().requestArtwork(song.getId(), song.getArtworkUrl());
ui->artworkLabel->setPixmap(QPixmap(":/icons/placeholder.png"));
}
currentSongIndex = index;
currentSongId = song.getId();
emit songChanged(song.getTitle(), song.getUser());
}
void PlayerWidget::togglePlayPause() {
soundManager->pause();
}
void PlayerWidget::playNextSong() {
QModelIndex nextSongIndex = currentSongIndex.model()->index(currentSongIndex.row()+1,currentSongIndex.column());
if (nextSongIndex.isValid()) {
handlePlayRequest(nextSongIndex);
} else {
ui->nextButton->setEnabled(false);
setPlayButtonIcon();
}
}
void PlayerWidget::playPreviousSong() {
QModelIndex prevSongIndex = currentSongIndex.model()->index(currentSongIndex.row()-1,currentSongIndex.column());
if (prevSongIndex.isValid()) {
handlePlayRequest(prevSongIndex);
} else {
ui->prevButton->setEnabled(false);
setPlayButtonIcon();
}
}
// -- private slots
void PlayerWidget::handleSliderUserMove() {
soundManager->requestNewPosition(ui->progressBar->sliderPosition());
}
void PlayerWidget::setPlayButtonIcon() {
ui->playPauseButton->setText(" \u25B6");
disconnect(ui->playPauseButton, SIGNAL(clicked()), soundManager, SLOT(pause()));
connect(ui->playPauseButton, SIGNAL(clicked()), soundManager, SLOT(play()));
}
void PlayerWidget::setPauseButtonIcon() {
ui->playPauseButton->setText("\u007C\u007C");
disconnect(ui->playPauseButton, SIGNAL(clicked()), soundManager, SLOT(play()));
connect(ui->playPauseButton, SIGNAL(clicked()), soundManager, SLOT(pause()));
}
void PlayerWidget::handleArtworkUpdate(int id, QPixmap &p) {
if (currentSongId == id) {
ui->artworkLabel->setPixmap(p);
}
}
// -- private methods
void PlayerWidget::setupPlaybackManager() {
soundManager = new PlaybackManager(this);
connect(soundManager, SIGNAL(started()), this, SLOT(setPauseButtonIcon()));
connect(soundManager, SIGNAL(paused()), this, SLOT(setPlayButtonIcon()));
connect(soundManager, SIGNAL(finished()), this, SLOT(playNextSong()));
connect(soundManager, SIGNAL(newSongDuration(int, int)), ui->progressBar, SLOT(setRange(int,int)));
connect(soundManager, SIGNAL(playTimeElapsed(int)), ui->progressBar, SLOT(setValue(int)));
connect(ui->progressBar, SIGNAL(sliderReleased()), this, SLOT(handleSliderUserMove()));
// this is being connected to the pause slot, as the button is only enabled upon play beging
connect(ui->playPauseButton, SIGNAL(clicked()), soundManager, SLOT(pause()));
connect(soundManager, SIGNAL(started()), this, SIGNAL(playbackStarted()));
}