3 #include <QInputDialog>
5 #include "mainwindow.h"
8 //#define AVOID_INPUT_DIALOG 0
10 MainWindow::MainWindow()
11 : plman (this), settings (tr ("TomAmp"), "TomAmp")
13 audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
14 mediaObject = new Phonon::MediaObject(this);
16 mediaObject->setTickInterval(1000);
17 connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
18 connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
19 this, SLOT(stateChanged(Phonon::State,Phonon::State)));
20 connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
21 this, SLOT(sourceChanged(Phonon::MediaSource)));
22 connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
23 connect (&plman, SIGNAL (playlistChanged (int)), this, SLOT (playlistChanged(int)));
24 connect (&plman, SIGNAL (itemUpdated(int)), this, SLOT (itemUpdated (int)));
26 Phonon::createPath(mediaObject, audioOutput);
29 repeat = settings.value("repeat", false).toBool();
30 shuffle = settings.value("shuffle", false).toBool();
35 timeLcd->display("00:00");
36 plman.addStringList(settings.value("lastPlaylist").toStringList());
38 audioOutput->setVolume(settings.value("volume", .5).toReal());
41 MainWindow::~MainWindow()
43 settings.setValue("shuffle", shuffle);
44 settings.setValue("repeat", repeat);
45 settings.setValue("lastPlaylist", plman.playlistStrings());
46 settings.setValue("volume", audioOutput->volume());
49 void MainWindow::addFiles()
51 QString folder = settings.value("LastFolder").toString();
53 folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
54 QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Files To Add"),
60 QString dir = QFileInfo (files[0]).absoluteDir().absolutePath();
61 settings.setValue("LastFolder", dir);
63 foreach (QString string, files)
65 toadd.append (string);
67 plman.addStringList(toadd);
70 void MainWindow::addFolder()
72 QString folder = settings.value("LastFolder").toString();
74 folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
75 QString dir = QFileDialog::getExistingDirectory(this,
76 tr("Select Directory To Add"),
80 QStringList files = QDir (dir).entryList(filters, QDir::AllDirs);
82 files.removeAll("..");
84 bool recursive = false;
86 recursive = QMessageBox::question(this, "Add all folders", "Subfolders have been detected, add everything?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
87 plman.parseAndAddFolder(dir, recursive);
91 void MainWindow::addUrl()
93 #ifdef AVOID_INPUT_DIALOG
94 QString url = "http://war.str3am.com:7970";
96 QString url = QInputDialog::getText(this, "Get URL", "Please type in the stream URL");
100 plman.addStringList(toadd);
104 void MainWindow::about()
106 QMessageBox::information(this, tr("About TomAmp v0.1"),
107 tr("TomAmp is a simple playlist-based music player.\n\n"
108 "(c) 2010 Tamas Marki <tmarki@gmail.com>\n\n"
109 "Please send comments and bug reports to the above e-mail address.\n\n"
110 "Icons by deleket (http://www.deleket.com)"));
113 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
115 qDebug () << "State: " << newState;
118 case Phonon::ErrorState:
119 if (mediaObject->errorType() == Phonon::FatalError)
121 // QMessageBox::warning(this, tr("Fatal Error"),
122 // mediaObject->errorString() + mediaObject->currentSource().fileName() + ", " + mediaObject->currentSource().url().toString());
126 // QMessageBox::warning(this, tr("Error"),
127 // mediaObject->errorString());
131 case Phonon::PlayingState:
132 setWindowTitle(mediaObject->metaData().value("TITLE") + " - TomAmp");
133 pauseAction->setVisible(true);
134 playAction->setVisible (false);
135 playAction->setEnabled(false);
136 pauseAction->setEnabled(true);
137 stopAction->setEnabled(true);
139 case Phonon::StoppedState:
140 stopAction->setEnabled(false);
141 playAction->setEnabled(true);
142 pauseAction->setVisible(false);
143 playAction->setVisible(true);
144 pauseAction->setEnabled(false);
145 timeLcd->display("00:00");
147 case Phonon::PausedState:
148 pauseAction->setEnabled(false);
149 stopAction->setEnabled(true);
150 pauseAction->setVisible(false);
151 playAction->setVisible(true);
152 playAction->setEnabled(true);
153 qDebug () << "Queue size: " << mediaObject->queue().size ();
154 if (mediaObject->queue().size ())
156 mediaObject->setCurrentSource(mediaObject->queue()[0]);
157 musicTable->selectRow(plman.indexOf(mediaObject->currentSource()));
160 mediaObject->clearQueue();
162 case Phonon::BufferingState:
163 qDebug () << "Buffering";
170 void MainWindow::next()
172 bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
173 if (mediaObject->state () == Phonon::ErrorState)
175 int index = plman.indexOf(mediaObject->currentSource()) + 1;
178 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource()));
183 while (index < shuffleList.size () && !plman.getItem(index).playable);
184 if (index < shuffleList.size ())
186 mediaObject->setCurrentSource(plman.at (shuffleList[index]));
195 while (index < shuffleList.size () && !plman.getItem(index).playable);
196 if (index < shuffleList.size ())
197 mediaObject->setCurrentSource(plman.at (shuffleList[index]));
203 while (index < plman.size () && !plman.getItem(index).playable);
207 if (plman.size() > index)
209 mediaObject->setCurrentSource(plman.at(index));
218 while (index < shuffleList.size () && !plman.getItem(index).playable);
219 mediaObject->setCurrentSource(plman.at(index));
226 void MainWindow::previous()
228 bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
229 int index = plman.indexOf(mediaObject->currentSource()) - 1;
232 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) - 1;
235 mediaObject->setCurrentSource(plman.at (shuffleList[index]));
239 mediaObject->setCurrentSource(plman.at (shuffleList[shuffleList.size() - 1]));
247 mediaObject->setCurrentSource(plman.at(index));
251 mediaObject->setCurrentSource(plman.at(plman.size() - 1));
259 void MainWindow::tick(qint64 time)
261 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
263 timeLcd->display(displayTime.toString("mm:ss"));
266 void MainWindow::tableClicked(int row, int /* column */)
268 // bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
271 mediaObject->clearQueue();
273 if (row >= plman.size())
277 while (index < plman.size () && !plman.getItem(index).playable);
281 if (plman.size() > index)
283 mediaObject->setCurrentSource(plman.at(index));
284 int ind = shuffleList.indexOf(index);
285 shuffleList.removeAt(ind);
286 shuffleList.insert(0, index);
287 qDebug () << "Modified shuffle list: " << shuffleList;
297 void MainWindow::sourceChanged(const Phonon::MediaSource &source)
299 musicTable->selectRow(plman.indexOf(source));
300 timeLcd->display("00:00");
304 void MainWindow::aboutToFinish()
306 qDebug () << "Abouttotfinish";
307 int index = plman.indexOf(mediaObject->currentSource()) + 1;
310 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
311 if (index < shuffleList.size ())
313 mediaObject->enqueue(plman.at (shuffleList[index]));
317 mediaObject->enqueue(plman.at (shuffleList[0]));
323 if (plman.size() > index)
325 mediaObject->enqueue(plman.at(index));
326 qDebug () << "Enqueue " << index << " pfm " << mediaObject->prefinishMark();
330 mediaObject->enqueue(plman.at(0));
331 qDebug () << "Enqueue " << 0 << " pfm " << mediaObject->prefinishMark();
336 void MainWindow::finished()
338 qDebug () << "Finished";
341 void MainWindow::setupActions()
343 playAction = new QAction(QIcon (QPixmap (":images/play")), tr("Play"), this);
344 playAction->setShortcut(tr("Crl+P"));
345 playAction->setDisabled(true);
346 pauseAction = new QAction(QIcon (QPixmap (":images/pause")), tr("Pause"), this);
347 pauseAction->setShortcut(tr("Ctrl+A"));
348 pauseAction->setDisabled(true);
349 pauseAction->setVisible(false);
350 stopAction = new QAction(QIcon (QPixmap (":images/stop")), tr("Stop"), this);
351 stopAction->setShortcut(tr("Ctrl+S"));
352 stopAction->setDisabled(true);
353 nextAction = new QAction(QIcon (QPixmap (":images/next")), tr("Next"), this);
354 nextAction->setShortcut(tr("Ctrl+N"));
355 previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
356 previousAction->setShortcut(tr("Ctrl+R"));
357 repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
358 repeatAction->setCheckable(true);
359 repeatAction->setChecked(repeat);
360 repeatAction->setShortcut(tr("Ctrl+I"));
361 shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
362 shuffleAction->setCheckable(true);
363 shuffleAction->setChecked(shuffle);
364 shuffleAction->setShortcut(tr("Ctrl+H"));
365 volumeAction = new QAction(QIcon (QPixmap (":images/volume")), tr("Volume"), this);
366 volumeAction->setCheckable(true);
367 volumeAction->setShortcut(tr("Ctrl+V"));
368 addFilesAction = new QAction(tr("Add &File"), this);
369 addFilesAction->setShortcut(tr("Ctrl+F"));
370 addFoldersAction = new QAction(tr("Add F&older"), this);
371 addFoldersAction->setShortcut(tr("Ctrl+O"));
372 addUrlAction = new QAction(tr("Add &Url"), this);
373 addUrlAction->setShortcut(tr("Ctrl+U"));
374 savePlaylistAction = new QAction (tr("Sa&ve Playlist"), this);
375 savePlaylistAction->setShortcut(tr ("Ctrl+V"));
376 loadPlaylistAction = new QAction (tr("&Load Playlist"), this);
377 loadPlaylistAction->setShortcut(tr("Ctrl+L"));
378 clearPlaylistAction = new QAction (tr("&Clear Playlist"), this);
379 clearPlaylistAction->setShortcut(tr("Ctrl+C"));
380 exitAction = new QAction(tr("E&xit"), this);
381 exitAction->setShortcut(tr("Ctrl+X"));
382 aboutAction = new QAction(tr("A&bout"), this);
383 aboutAction->setShortcut(tr("Ctrl+B"));
384 aboutQtAction = new QAction(tr("About &Qt"), this);
385 aboutQtAction->setShortcut(tr("Ctrl+Q"));
387 connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
388 connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
389 connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
390 connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
391 connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
392 connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
393 connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
394 connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolder()));
395 connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
396 connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
397 connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
398 connect (clearPlaylistAction, SIGNAL (triggered()), &plman, SLOT (clearPlaylist()));
399 connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
400 connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
401 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
402 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
403 connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
407 void MainWindow::repeatToggle ()
410 qDebug() << "Repeat toggled to " << repeat;
411 settings.setValue("repeat", QVariant (repeat));
414 void MainWindow::shuffleToggle ()
417 settings.setValue("shuffle", QVariant (shuffle));
420 void MainWindow::volumeToggle ()
422 qDebug () << "Volumetoggle: " << volumeAction->isChecked();
423 volumeSlider->setVisible(volumeAction->isChecked());
426 void MainWindow::play()
432 void MainWindow::setupMenus()
434 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
435 fileMenu->addAction(addFilesAction);
436 fileMenu->addAction(addFoldersAction);
437 fileMenu->addAction(addUrlAction);
438 fileMenu->addSeparator();
439 fileMenu->addAction(savePlaylistAction);
440 fileMenu->addAction(loadPlaylistAction);
441 fileMenu->addAction(clearPlaylistAction);
442 // fileMenu->addAction(exitAction);
444 QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
445 aboutMenu->addAction(aboutAction);
446 aboutMenu->addAction(aboutQtAction);
449 void MainWindow::setupUi()
451 QToolBar *bar = new QToolBar;
453 bar->setOrientation(Qt::Vertical);
454 bar->setStyleSheet("padding:7px");
455 //bar->addAction(volumeAction);
457 seekSlider = new Phonon::SeekSlider(this);
458 seekSlider->setMediaObject(mediaObject);
460 volumeSlider = new Phonon::VolumeSlider(this);
461 volumeSlider->setAudioOutput(audioOutput);
462 volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
463 volumeSlider->setOrientation(Qt::Horizontal);
464 volumeSlider->setMuteVisible(false);
465 // volumeAddedAction = bar->addWidget(volumeSlider);
466 // volumeAddedAction->setVisible(false);
467 bar->addAction(playAction);
468 bar->addAction(pauseAction);
469 bar->addAction(stopAction);
470 bar->addAction(repeatAction);
471 bar->addAction(shuffleAction);
472 bar->addAction(nextAction);
473 bar->addAction(previousAction);
475 /* QLabel *volumeLabel = new QLabel;
476 volumeLabel->setPixmap(QPixmap("images/volume.png"));*/
479 palette.setBrush(QPalette::Light, Qt::darkGray);*/
481 timeLcd = new QLCDNumber;
482 // timeLcd->setPalette(palette);
485 headers << tr("Artist") << tr("Title") << tr("Album");
487 musicTable = new QTableWidget(0, 3);
488 musicTable->setHorizontalHeaderLabels(headers);
489 musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
490 musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
491 connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
492 this, SLOT(tableClicked(int,int)));
493 connect(musicTable, SIGNAL(cellClicked(int,int)),
494 this, SLOT(cellClicked(int,int)));
495 musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
497 QHBoxLayout *seekerLayout = new QHBoxLayout;
498 QToolBar* bar2 = new QToolBar;
499 bar2->addAction(volumeAction);
500 seekerLayout->addWidget(bar2);
501 seekerLayout->addWidget(volumeSlider);
502 seekerLayout->addWidget(seekSlider);
503 seekerLayout->addWidget(timeLcd);
505 QVBoxLayout *playbackLayout = new QVBoxLayout;
506 volumeSlider->hide ();
507 playbackLayout->addWidget(bar);
508 // playbackLayout->addStretch();
509 // playbackLayout->addWidget(volumeSlider);
510 // playbackLayout->addWidget(volumeLabel);
512 QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
514 seekAndTableLayout->addWidget(musicTable);
515 seekAndTableLayout->addLayout(seekerLayout);
517 QHBoxLayout *mainLayout = new QHBoxLayout;
518 mainLayout->addLayout(seekAndTableLayout);
519 mainLayout->addLayout(playbackLayout);
521 QWidget *widget = new QWidget;
522 widget->setLayout(mainLayout);
524 setCentralWidget(widget);
525 setWindowTitle("TomAmp");
526 qDebug () << "cucc: " << musicTable->columnWidth(1);
529 void MainWindow::cellClicked(int row, int)
531 if (mediaObject->state() == Phonon::PlayingState)
533 int index = plman.indexOf(mediaObject->currentSource());
536 musicTable->selectRow(index);
539 else if (row < plman.size())
541 mediaObject->setCurrentSource(plman.at(row));
542 shuffleList.removeAll(row);
543 shuffleList.insert(0, row);
544 qDebug () << shuffleList;
548 void MainWindow::setupShuffleList()
551 int index = plman.indexOf(mediaObject->currentSource());
554 for (int i = 0; i < plman.size(); ++i)
560 shuffleList.append (index);
563 int ind = qrand () % tmp.size();
564 shuffleList.append(tmp[ind]);
567 qDebug () << shuffleList;
568 qDebug () << shuffleList;
571 void MainWindow::savePlaylist ()
573 QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u)");
574 plman.loadPlaylist(filename);
577 void MainWindow::loadPlaylist ()
579 QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u");
580 plman.loadPlaylist (filename);
583 void MainWindow::playlistChanged(int from)
585 while (musicTable->rowCount() > from)
587 musicTable->removeRow(musicTable->rowCount () - 1);
589 for (int i = from; i < plman.size (); ++i)
591 int currentRow = musicTable->rowCount();
592 musicTable->insertRow(currentRow);
593 setRowFromItem (currentRow, plman.getItem(i));
598 void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
600 if (row >= musicTable->rowCount())
602 if (item.artist.isEmpty() && item.title.isEmpty())
604 QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
605 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
606 musicTable->setItem(row, 1, item1);
610 QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
611 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
612 musicTable->setItem(row, 0, item1);
613 QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
614 item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
615 musicTable->setItem(row, 1, item2);
616 QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
617 item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
618 musicTable->setItem(row, 2, item3);
622 void MainWindow::itemUpdated(int index)
624 setRowFromItem (index, plman.getItem(index));