-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pianobar_QT_MainWindow.cpp
480 lines (355 loc) · 13.2 KB
/
Pianobar_QT_MainWindow.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "Pianobar_QT_MainWindow.h"
/**
* Converts the time given in milliseconds to a qstring
*/
inline QString timeToString (long time_msecs)
{
long stime = time_msecs / 1000;
int hours = stime / 600;
int minutes = (stime / 60) % 60;
int seconds = stime % 60;
QString retString ("");
//TODO: If deemed necessary, append a 0 if hours is less than 10.
if (hours != 0) {
retString = QString::number (hours);
retString.append (":");
}
if (minutes < 10) {
retString.append ("0");
}
retString.append (QString::number (minutes));
retString.append (":");
if (seconds < 10) {
retString.append ("0");
}
retString.append (QString::number (seconds));
return retString;
}
inline QPushButton* createButton (QString name, QString iconName)
{
QPushButton* ret = new QPushButton (QIcon::fromTheme (iconName), 0);
ret->setToolTip (name);
return ret;
}
Pianobar_QT_MainWindow::Pianobar_QT_MainWindow (QWidget* parent, Qt::WindowFlags flags) : QMainWindow (parent, flags)
{
//DO Nothing
}
Pianobar_QT_MainWindow::Pianobar_QT_MainWindow (QString username) : QMainWindow()
{
resize (800, 800);
QString title ("");
title.append ("Pianobar QT: ");
title.append (username);
this->setWindowTitle (title);
QWidget* centralWidget = new QWidget (this);
QGridLayout* test = new QGridLayout (centralWidget);
QVBoxLayout* infoLayout = new QVBoxLayout();
this->buildLabels();
infoLayout->addWidget (songName);
infoLayout->addWidget (artist);
infoLayout->addWidget (album);
QHBoxLayout* buttonLayout = new QHBoxLayout();
prev = createButton ("Previous Song", "media-skip-backward");
playPause = createButton ("Play/Pause", "media-playback-start");
next = createButton ("Next Song", "media-skip-forward");
//Setup the default icon
loveSong = createButton ("Neutral", "face-smile");
QMenu* ratingMenu = new QMenu(this);
likeCurrSong = ratingMenu->addAction(QIcon::fromTheme("face-smile-big"), "Like");
neutralCurrSong = ratingMenu->addAction(QIcon::fromTheme("face-smile"), "Neutral");
dislikeCurrSong = ratingMenu->addAction(QIcon::fromTheme("face-uncertain"), "Dislike");
loveSong->setMenu(ratingMenu);
buttonLayout->addWidget (prev);
buttonLayout->addWidget (playPause);
buttonLayout->addWidget (next);
test->addWidget (loveSong, 0, 0, Qt::AlignCenter);
test->addWidget (albumArt, 1, 0, Qt::AlignCenter);
test->addLayout (infoLayout, 2, 0, Qt::AlignCenter);
test->addLayout (buttonLayout, 3, 0, Qt::AlignCenter);
test->addWidget (timeLabel, 4, 0, Qt::AlignCenter);
centralWidget->setLayout (test);
setCentralWidget (centralWidget);
stationsDock = new QStationsList();
addDockWidget (Qt::LeftDockWidgetArea, stationsDock);
playlistDock = new QPlaylist();
addDockWidget (Qt::RightDockWidgetArea, playlistDock);
media = new Phonon::MediaObject (this);
media->setTickInterval (1000);
Phonon::createPath (media, new Phonon::AudioOutput (Phonon::MusicCategory, this));
Phonon::SeekSlider* seeker = new Phonon::SeekSlider (media, this);
seeker->setIconVisible (false);
seeker->setMinimumWidth (300);
test->addWidget (seeker, 5, 0, Qt::AlignCenter);
connect (media, SIGNAL (tick (qint64)), SLOT (onEachTick()));
connect (media, SIGNAL (finished()), SLOT (onEndOfSong()));
connect (media, SIGNAL (stateChanged (Phonon::State, Phonon::State)), SLOT (updateOnMediaStateChange()));
connect (playPause, SIGNAL (clicked (bool)), SLOT (playPauseToggle()));
connect (next, SIGNAL (clicked (bool)), SLOT (onNextSongSelect()));
connect (prev, SIGNAL (clicked (bool)), SLOT (onPrevSongSelect()));
connect(likeCurrSong, SIGNAL(triggered(bool)), SLOT(rate_likeSong()));
connect(neutralCurrSong, SIGNAL(triggered(bool)), SLOT(rate_neutSong()));
connect(dislikeCurrSong, SIGNAL(triggered(bool)), SLOT(rate_banSong()));
//Since they're no functions you can do on start, disable the buttons
disableButtons();
}
void Pianobar_QT_MainWindow::buildLabels()
{
//Set the time label to show something
timeLabel = new QLabel ("00:00:00:/00:00:00");
//Album art label
albumArt = new QLabel ("Album Art");
//Set somthing as a default
QIcon defaultIcon = QIcon::fromTheme ("audio-ac3");
albumArt->setAlignment (Qt::AlignCenter);
albumArt->setScaledContents (true);
albumArt->setPixmap (defaultIcon.pixmap (500, 500));
albumArt->setMinimumSize (400, 400);
albumArt->setMaximumSize (400, 400);
//Song Information Labels
songName = new QLabel ("Title: ");
artist = new QLabel ("Artist: ");
album = new QLabel ("Album: ");
songName->setMaximumWidth(400);
artist->setMaximumWidth(400);
album->setMinimumWidth(400);
}
void Pianobar_QT_MainWindow::setHandlers (PianoHandle_t ph, WaitressHandle_t wh)
{
this->ph = ph;
this->wh = wh;
piano.PianoGetStations (&ph, &wh);
std::vector<PandoraStation> stations = helper.parseStations (ph.stations);
stationsDock->setStations (stations);
connect (stationsDock->stationList, SIGNAL (itemActivated (QListWidgetItem*)), SLOT (onNewStationSelect()));
}
void Pianobar_QT_MainWindow::onNewStationSelect()
{
qDebug() << "New Station";
playlistDock->clearPlaylist();
//If there is something there already
if (!playlist.empty()) {
playlist.clear();
}
playlistIndex = 0;
nextSong();
qDebug() << "Starting song";
}
void Pianobar_QT_MainWindow::getPlaylist()
{
//Get the selected station from the dock
PandoraStation* station = stationsDock->selectedStation;
//For my own sanity, make sure it's actually something
Q_ASSERT (station != NULL);
//Make it into something pianobar can understand
PianoStation_t tmpStation = station->toPianobarStation();
//Get the linked list for this playlist
PianoSong_t* song = piano.PianoGetPlaylist (&ph, &wh, &tmpStation);
//Make sure that the song is valid, otherwise errors will happen.
Q_ASSERT (song != NULL);
//If it's empty fill it
if (playlist.empty()) {
playlistIndex = 0;
qDebug() << "Filling playlist";
playlist = helper.parsePlaylist (song);
//Pushes the song to the playlist dock
for (std::vector<PandoraSong>::size_type i = playlistIndex; i != playlist.size(); i++) {
playlistDock->pushSong (playlist[i].toShortString());
}
} else {
//Since its not empty we will append to the end of the list
qDebug() << "Re-filling playlist";
std::vector<PandoraSong> tmp = helper.parsePlaylist (song);
playlist.insert (playlist.end(), tmp.begin(), tmp.end());
tmp.clear();
//Pushes the NEW songs to the playlist dock
for (std::vector<PandoraSong>::size_type i = playlistIndex + 2; i != playlist.size(); i++) {
playlistDock->pushSong (playlist[i].toShortString());
}
}
}
void Pianobar_QT_MainWindow::nextSong()
{
if (playlistIndex + 2 == playlist.size()) {
getPlaylist();//If we are about to finish, get a new one
}
qDebug() << playlistIndex << " " << playlist.size();
if (playlist.empty()) {
getPlaylist();//Get the new playlist since we have none
playlistIndex--;//Make sure we start at 0
}
playlistIndex++;
playSong();
}
void Pianobar_QT_MainWindow::prevSong()
{
if (playlistIndex == 0) {
playSong();
} else {
playlistIndex--;
playSong();
}
}
void Pianobar_QT_MainWindow::playSong()
{
playlistDock->setSongSelected (playlistIndex);
QString url = playlist[playlistIndex].getAudioURL();
QUrl link = QUrl::fromEncoded (url.toAscii());
media->clear();
media->setCurrentSource (link);
media->play();
//Set the album art
QString albumArtUrl = playlist[playlistIndex].getAlbumArtURL();
QUrl albumArtLink = QUrl::fromEncoded (albumArtUrl.toAscii());
imageBuffer = new QBuffer (&imageData);
http = new QHttp (this);
http->setHost (albumArtLink.host());
qDebug() << "Album Art URL:" << albumArtUrl;
request = http->get (albumArtLink.path(), imageBuffer);
//Connect the http request to a response handler
connect (http, SIGNAL (requestFinished (int, bool)), SLOT (albumDownloaded (int, bool)));
//Set the song info
QString songNameText ("Title: ");
songNameText.append (QString (playlist[playlistIndex].getTitle()));
QString albumText ("Album: ");
albumText.append (QString (playlist[playlistIndex].getAlbum()));
QString artistText ("Artist: ");
artistText.append (QString (playlist[playlistIndex].getArtist()));
//Song Information
songName->setText (songNameText);
album->setText (albumText);
artist->setText (artistText);
//Song rating
if (playlist[playlistIndex].isSongNeutral()) {
loveSong->setIcon (QIcon::fromTheme ("face-smile"));
loveSong->setToolTip ("Neutral");
songRating = PIANO_RATE_NONE;
} else if (playlist[playlistIndex].isSongBanned()) {
loveSong->setIcon (QIcon::fromTheme ("face-uncertain"));
loveSong->setToolTip ("Banned");
songRating = PIANO_RATE_BAN;
} else if (playlist[playlistIndex].isSongLoved()) {
loveSong->setIcon (QIcon::fromTheme ("face-smile-big"));
loveSong->setToolTip ("Loved");
songRating = PIANO_RATE_LOVE;
}
}
void Pianobar_QT_MainWindow::onEachTick()
{
qint64 mtime = media->currentTime();
QString timeFormated = timeToString (mtime);
QString timeTotal = timeToString (media->totalTime());
QString retString = timeFormated;
retString.append ("/");
retString.append (timeTotal);
timeLabel->setText (retString);
}
void Pianobar_QT_MainWindow::onEndOfSong()
{
if (media->errorType() == Phonon::FatalError) {
qDebug() << "Phonon Fatal Error happened";
} else if (media->errorType() == Phonon::NormalError) {
qDebug() << "Phonon Normal Error, trying to continue";
}
//Call the next song when we are finished
nextSong();
qDebug() << "Changing Song";
}
void Pianobar_QT_MainWindow::albumDownloaded (int id, bool err)
{
if (request == id) {
if (err) {
qDebug() << "Error downloading Album Art";
QIcon defaultIcon = QIcon::fromTheme ("audio-ac3");
albumArt->setPixmap (defaultIcon.pixmap (500, 500));
return;
} else {
qDebug() << "Downloaded Image";
}
qDebug() << "Found my request";
QImage image = QImage::fromData (imageData);
albumArt->setPixmap (QPixmap::fromImage (image));
//Sanity check
Q_ASSERT (albumArt->pixmap() != 0);
}
}
void Pianobar_QT_MainWindow::playPauseToggle()
{
if (media->state() == Phonon::PlayingState) {
media->pause();
} else if (media->state() == Phonon::PausedState) {
media->play();
} else {
qDebug() << "Invalid Phonon State";
}
}
void Pianobar_QT_MainWindow::disableButtons()
{
prev->setEnabled (false);
playPause->setEnabled (false);
next->setEnabled (false);
loveSong->setEnabled(false);
}
void Pianobar_QT_MainWindow::enableButtons()
{
prev->setEnabled (true);
playPause->setEnabled (true);
next->setEnabled (true);
loveSong->setEnabled(true);
}
void Pianobar_QT_MainWindow::updateOnMediaStateChange()
{
if (media->state() == Phonon::PlayingState) {
playPause->setIcon (QIcon::fromTheme (PauseIconName));
enableButtons();
} else if (media->state() == Phonon::PausedState) {
playPause->setIcon (QIcon::fromTheme (PlayIconName));
enableButtons();
} else if (media->state() == Phonon::BufferingState) {
//Do Nothing
} else if (media->state() == Phonon::ErrorState) {
qDebug() << "Error occured";
disableButtons();//Disable the buttons so that they stay that way if there's more problems
playSong(); //Attempt to replay the current song
} else {
enableButtons();
}
}
void Pianobar_QT_MainWindow::onNextSongSelect()
{
qDebug() << "Next pressed";
nextSong();
}
void Pianobar_QT_MainWindow::onPrevSongSelect()
{
qDebug() << "Prev pressed";
prevSong();
}
void Pianobar_QT_MainWindow::rate_likeSong()
{
loveSong->setIcon (QIcon::fromTheme ("face-smile-big"));
loveSong->setToolTip ("Liked");
songRating = PIANO_RATE_LOVE;
qDebug() << "Submitting song rating, liking song";
PianoSong_t song = playlist[playlistIndex].toPianoSong();
piano.PianoRateSong(&ph, &wh, &song, songRating);
}
void Pianobar_QT_MainWindow::rate_neutSong()
{
loveSong->setIcon (QIcon::fromTheme ("face-smile"));
loveSong->setToolTip ("Neutral");
songRating = PIANO_RATE_NONE;
qDebug() << "Submitting song rating, unrating song";
PianoSong_t song = playlist[playlistIndex].toPianoSong();
piano.PianoRateSong(&ph, &wh, &song, songRating);
}
void Pianobar_QT_MainWindow::rate_banSong()
{
loveSong->setIcon (QIcon::fromTheme ("face-uncertain"));
loveSong->setToolTip ("Disliked");
songRating = PIANO_RATE_BAN;
qDebug() << "Submitting song rating, disliking song";
PianoSong_t song = playlist[playlistIndex].toPianoSong();
piano.PianoRateSong(&ph, &wh, &song, songRating);
}
#include "Pianobar_QT_MainWindow.moc"