3 #include <QInputDialog>
5 #include "mainwindow.h"
8 //#define AVOID_INPUT_DIALOG 0
10 MainWindow::MainWindow()
11 : plman (this), settings (tr ("TomAmp"), "TomAmp"), isPlaying (false)
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 int curind = settings.value("currentIndex", -1).toInt ();
40 setItem (curind, false);
41 audioOutput->setVolume(settings.value("volume", .5).toReal());
42 QApplication::setWindowIcon(QIcon (QPixmap (":images/tomamp")));
45 MainWindow::~MainWindow()
47 settings.setValue("shuffle", shuffle);
48 settings.setValue("repeat", repeat);
49 settings.setValue("lastPlaylist", plman.playlistStrings());
50 settings.setValue("volume", audioOutput->volume());
51 settings.setValue("currentIndex", plman.indexOf(mediaObject->currentSource()));
52 for (int i = 0; i < musicTable->columnCount(); ++i)
54 QString lab = QString ("colWidth_%1").arg (i);
55 settings.setValue(lab, musicTable->columnWidth(i));
59 void MainWindow::addFiles()
61 QString folder = settings.value("LastFolder").toString();
63 folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
64 QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Files To Add"),
65 folder, "Music files (*.mp3 *.ogg *.wav *.flac);;Playlists (*.m3u *.pls)");
70 QString dir = QFileInfo (files[0]).absoluteDir().absolutePath();
71 settings.setValue("LastFolder", dir);
73 foreach (QString string, files)
75 if (string.toLower().endsWith(".pls") || string.toLower().endsWith(".m3u"))
76 plman.addPlaylist(string);
78 toadd.append (string);
80 plman.addStringList(toadd);
83 void MainWindow::addFolder()
85 QString folder = settings.value("LastFolder").toString();
87 folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
88 QString dir = QFileDialog::getExistingDirectory(this,
89 tr("Select Directory To Add"),
95 settings.setValue("LastFolder", dir);
98 QStringList files = QDir (dir).entryList(filters, QDir::AllDirs);
100 files.removeAll("..");
101 bool recursive = false;
103 recursive = QMessageBox::question(this, "Add all folders", "Subfolders have been detected, add everything?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
104 plman.parseAndAddFolder(dir, recursive);
108 void MainWindow::addUrl()
110 #ifdef AVOID_INPUT_DIALOG
111 QString url = "http://war.str3am.com:7970";
113 QString url = QInputDialog::getText(this, "Get URL", "Please type in the stream URL");
115 if (url.isEmpty() || !url.toLower().startsWith("http"))
119 plman.addStringList(toadd);
123 void MainWindow::about()
125 QMessageBox::information(this, tr("About TomAmp v0.1"),
126 tr("TomAmp is a simple playlist-based music player.\n\n"
127 "(c) 2010 Tamas Marki <tmarki@gmail.com>\n\n"
128 "Please send comments and bug reports to the above e-mail address.\n\n"
129 "Icons by http://itweek.deviantart.com/"));
132 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
136 case Phonon::ErrorState:
137 if (mediaObject->errorType() == Phonon::FatalError)
139 // QMessageBox::warning(this, tr("Fatal Error"),
140 // mediaObject->errorString() + mediaObject->currentSource().fileName() + ", " + mediaObject->currentSource().url().toString());
144 // QMessageBox::warning(this, tr("Error"),
145 // mediaObject->errorString());
149 case Phonon::PlayingState:
150 setWindowTitle(mediaObject->metaData().value("TITLE") + " - TomAmp");
151 pauseAction->setVisible(true);
152 playAction->setVisible (false);
153 playAction->setEnabled(false);
154 pauseAction->setEnabled(true);
155 stopAction->setEnabled(true);
156 //lastPlayed = plman.indexOf(mediaObject->currentSource());
158 case Phonon::StoppedState:
159 setWindowTitle("TomAmp");
160 stopAction->setEnabled(false);
161 playAction->setEnabled(true);
162 pauseAction->setVisible(false);
163 playAction->setVisible(true);
164 pauseAction->setEnabled(false);
165 timeLcd->display("00:00");
166 unhighlightRow(plman.indexOf(mediaObject->currentSource()));
168 case Phonon::PausedState:
169 pauseAction->setEnabled(false);
170 stopAction->setEnabled(true);
171 pauseAction->setVisible(false);
172 playAction->setVisible(true);
173 playAction->setEnabled(true);
175 case Phonon::BufferingState:
182 void MainWindow::next()
184 bool wasPlaying = isPlaying;
185 if (mediaObject->state () == Phonon::ErrorState)
187 int index = plman.indexOf(mediaObject->currentSource());
190 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
191 while (index < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
195 if (index < shuffleList.size ())
197 setItem (index, wasPlaying);
202 while ((index) < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
206 setItem (index, wasPlaying);
212 while ((index) < plman.size () && !plman.getItem(index).playable)
216 if (index < plman.size())
218 setItem (index, wasPlaying);
223 while ((index) < plman.size () && !plman.getItem(index).playable)
227 setItem (index, wasPlaying);
232 void MainWindow::setItem(int i, bool doplay)
234 if (i < plman.size() && i >= 0)
237 unhighlightRow(lastPlayed);
240 mediaObject->setCurrentSource(plman.at (shuffleList[i]));
244 mediaObject->setCurrentSource(plman.at(i));
247 if (doplay && mediaObject->currentSource().type() != Phonon::MediaSource::Invalid)
255 void MainWindow::previous()
257 bool wasPlaying = isPlaying;//(mediaObject->state () == Phonon::PlayingState);
258 if (mediaObject->state () == Phonon::ErrorState)
260 int index = plman.indexOf(mediaObject->currentSource());
263 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) - 1;
264 while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
270 setItem (index, wasPlaying);
274 index = plman.size () - 1;
275 while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
279 setItem (index, wasPlaying);
282 wasPlaying = false;*/
288 while ((index) >= 0 && !plman.getItem(index).playable)
294 setItem (index, wasPlaying);
298 index = plman.size() - 1;
299 while ((index) >= 0 && !plman.getItem(index).playable)
303 setItem (index, wasPlaying);
308 void MainWindow::highlightRow (int i)
310 for (int j = 0; j < 3; ++j)
312 QTableWidgetItem* item = musicTable->item(i, j);
315 QFont font = item->font();
317 font.setItalic(true);
323 void MainWindow::unhighlightRow (int i)
325 for (int j = 0; j < 3; ++j)
327 QTableWidgetItem* item = musicTable->item(i, j);
330 QFont font = item->font();
332 font.setItalic(false);
339 void MainWindow::tick(qint64 time)
341 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
343 timeLcd->display(displayTime.toString("mm:ss"));
346 void MainWindow::tableClicked(int row, int /* column */)
348 // bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
350 /* mediaObject->stop();
351 mediaObject->clearQueue();*/
353 if (row >= plman.size())
357 while (index < shuffleList.size () && !plman.getItem(index).playable)
361 if (plman.size() > index)
364 index = shuffleList.indexOf(index);
365 setItem (index, true);
366 // mediaObject->play();
371 while (index < plman.size () && !plman.getItem(index).playable)
375 if (plman.size() > index)
378 index = shuffleList.indexOf(index);
379 setItem (index, true);
380 // mediaObject->play();
386 void MainWindow::sourceChanged(const Phonon::MediaSource &source)
388 int ind = plman.indexOf(source);
390 unhighlightRow(lastPlayed);
392 musicTable->selectRow(ind);
393 timeLcd->display("00:00");
397 void MainWindow::aboutToFinish()
399 int index = plman.indexOf(mediaObject->currentSource()) + 1;
402 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
403 if (index < shuffleList.size ())
405 mediaObject->enqueue(plman.at (shuffleList[index]));
409 mediaObject->enqueue(plman.at (shuffleList[0]));
415 if (plman.size() > index)
417 mediaObject->enqueue(plman.at(index));
421 mediaObject->enqueue(plman.at(0));
426 void MainWindow::finished()
430 void MainWindow::setupActions()
432 playAction = new QAction(QIcon (QPixmap (":images/play")), tr("Play"), this);
433 playAction->setShortcut(tr("Crl+P"));
434 playAction->setDisabled(true);
435 pauseAction = new QAction(QIcon (QPixmap (":images/pause")), tr("Pause"), this);
436 pauseAction->setShortcut(tr("Ctrl+A"));
437 pauseAction->setDisabled(true);
438 pauseAction->setVisible(false);
439 stopAction = new QAction(QIcon (QPixmap (":images/stop")), tr("Stop"), this);
440 stopAction->setShortcut(tr("Ctrl+S"));
441 stopAction->setDisabled(true);
442 nextAction = new QAction(QIcon (QPixmap (":images/next")), tr("Next"), this);
443 nextAction->setShortcut(tr("Ctrl+N"));
444 previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
445 previousAction->setShortcut(tr("Ctrl+R"));
447 repeatAction = new QAction(QIcon (QPixmap (":images/repeatActive")), tr("Repeat"), this);
449 repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
450 repeatAction->setCheckable(true);
451 repeatAction->setChecked(repeat);
452 repeatAction->setShortcut(tr("Ctrl+I"));
454 shuffleAction = new QAction(QIcon (QPixmap (":images/shuffleActive")), tr("Shuffle"), this);
456 shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
457 shuffleAction->setCheckable(true);
458 shuffleAction->setChecked(shuffle);
459 shuffleAction->setShortcut(tr("Ctrl+H"));
460 volumeAction = new QAction(QIcon (QPixmap (":images/volume")), tr("Volume"), this);
461 volumeAction->setCheckable(true);
462 volumeAction->setShortcut(tr("Ctrl+V"));
463 addFilesAction = new QAction(tr("Add &File"), this);
464 addFilesAction->setShortcut(tr("Ctrl+F"));
465 addFoldersAction = new QAction(tr("Add F&older"), this);
466 addFoldersAction->setShortcut(tr("Ctrl+O"));
467 addUrlAction = new QAction(tr("Add &Url"), this);
468 addUrlAction->setShortcut(tr("Ctrl+U"));
469 savePlaylistAction = new QAction (tr("Sa&ve Playlist"), this);
470 savePlaylistAction->setShortcut(tr ("Ctrl+V"));
471 loadPlaylistAction = new QAction (tr("&Load Playlist"), this);
472 loadPlaylistAction->setShortcut(tr("Ctrl+L"));
473 clearPlaylistAction = new QAction (tr("&Clear Playlist"), this);
474 clearPlaylistAction->setShortcut(tr("Ctrl+C"));
475 exitAction = new QAction(tr("E&xit"), this);
476 exitAction->setShortcut(tr("Ctrl+X"));
477 aboutAction = new QAction(tr("A&bout"), this);
478 aboutAction->setShortcut(tr("Ctrl+B"));
479 aboutQtAction = new QAction(tr("About &Qt"), this);
480 aboutQtAction->setShortcut(tr("Ctrl+Q"));
481 /* removeSelected = new QAction (tr("&Delete from playlist"));
482 removeSelected->setShortcut(tr ("Ctrl+D"));*/
484 connect(playAction, SIGNAL(triggered()), this, SLOT(play()));
485 connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
486 connect(stopAction, SIGNAL(triggered()), this, SLOT(stop()));
487 connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
488 connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
489 connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
491 connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
492 connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolder()));
493 connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
494 connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
495 connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
496 connect (clearPlaylistAction, SIGNAL (triggered()), &plman, SLOT (clearPlaylist()));
497 connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
498 connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
499 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
500 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
501 connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
502 // connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
505 void MainWindow::removeSelectedItem()
507 int row = musicTable->currentRow();
509 plman.removeItem(row);
512 void MainWindow::removeAllButSelectedItem()
514 int row = musicTable->currentRow();
517 QString uri = plman.getItem(row).uri;
520 plman.clearPlaylist();
521 plman.addStringList(lst);
525 void MainWindow::repeatToggle ()
528 settings.setValue("repeat", QVariant (repeat));
530 repeatAction->setIcon(QIcon (QPixmap (":images/repeatActive")));
532 repeatAction->setIcon(QIcon (QPixmap (":images/repeat")));
535 void MainWindow::shuffleToggle ()
538 settings.setValue("shuffle", QVariant (shuffle));
540 shuffleAction->setIcon(QIcon (QPixmap (":images/shuffleActive")));
542 shuffleAction->setIcon(QIcon (QPixmap (":images/shuffle")));
545 void MainWindow::volumeToggle ()
547 volumeSlider->setVisible(volumeAction->isChecked());
550 void MainWindow::play()
553 lastPlayed = plman.indexOf(mediaObject->currentSource());
554 highlightRow(lastPlayed);
558 void MainWindow::stop()
565 void MainWindow::setupMenus()
567 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
568 fileMenu->addAction(addFilesAction);
569 fileMenu->addAction(addFoldersAction);
570 fileMenu->addAction(addUrlAction);
571 fileMenu->addSeparator();
572 fileMenu->addAction(savePlaylistAction);
573 fileMenu->addAction(loadPlaylistAction);
574 fileMenu->addAction(clearPlaylistAction);
575 // fileMenu->addAction(exitAction);
577 QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
578 aboutMenu->addAction(aboutAction);
579 aboutMenu->addAction(aboutQtAction);
582 void MainWindow::setupUi()
584 QToolBar *bar = new QToolBar;
586 bar->setOrientation(Qt::Vertical);
587 bar->setStyleSheet("padding:7px");
588 //bar->addAction(volumeAction);
590 seekSlider = new Phonon::SeekSlider(this);
591 seekSlider->setMediaObject(mediaObject);
593 volumeSlider = new Phonon::VolumeSlider(this);
594 volumeSlider->setAudioOutput(audioOutput);
595 volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
596 volumeSlider->setOrientation(Qt::Horizontal);
597 volumeSlider->setMuteVisible(false);
598 // volumeAddedAction = bar->addWidget(volumeSlider);
599 // volumeAddedAction->setVisible(false);
600 bar->addAction(playAction);
601 bar->addAction(pauseAction);
602 bar->addAction(stopAction);
603 bar->addAction(repeatAction);
604 bar->addAction(shuffleAction);
605 bar->addAction(nextAction);
606 bar->addAction(previousAction);
608 contextMenu = new QMenu (this);
609 removeSelected = contextMenu->addAction(tr ("Remove selected"));
610 removeAllButSelected = contextMenu->addAction(tr("Remove all but selected"));
611 connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
612 connect (removeAllButSelected, SIGNAL (triggered()), this, SLOT (removeAllButSelectedItem()));
615 timeLcd = new QLCDNumber;
618 headers << tr("Artist") << tr("Title") << tr("Album");
620 musicTable = new QTableWidget(0, 3);
621 musicTable->setHorizontalHeaderLabels(headers);
622 musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
623 musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
624 connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
625 this, SLOT(tableClicked(int,int)));
626 connect(musicTable, SIGNAL(cellClicked(int,int)),
627 this, SLOT(cellClicked(int,int)));
628 /* for (int i = 0; i < 3; ++i)
630 if (!musicTable->horizontalHeaderItem(i))
632 musicTable->horizontalHeaderItem(i)->setBackgroundColor(QColor (128, 128, 255));;
633 musicTable->horizontalHeaderItem(i)->setForeground(QColor (255, 255, 255));
635 for (int i = 0; i < musicTable->columnCount(); ++i)
637 QString lab = QString ("colWidth_%1").arg (i);
638 int val = settings.value(lab, 0).toInt();
640 musicTable->setColumnWidth(i, val);
641 // settings.setValue(lab, musicTable->columnWidth(i));
645 QHBoxLayout *seekerLayout = new QHBoxLayout;
646 QToolBar* bar2 = new QToolBar;
647 bar2->addAction(volumeAction);
648 seekerLayout->addWidget(bar2);
649 seekerLayout->addWidget(volumeSlider);
650 seekerLayout->addWidget(seekSlider);
651 seekerLayout->addWidget(timeLcd);
653 QVBoxLayout *playbackLayout = new QVBoxLayout;
654 volumeSlider->hide ();
655 playbackLayout->addWidget(bar);
657 QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
659 seekAndTableLayout->addWidget(musicTable);
660 seekAndTableLayout->addLayout(seekerLayout);
662 QHBoxLayout *mainLayout = new QHBoxLayout;
663 mainLayout->addLayout(seekAndTableLayout);
664 mainLayout->addLayout(playbackLayout);
666 QWidget *widget = new QWidget;
667 widget->setLayout(mainLayout);
669 setCentralWidget(widget);
670 setWindowTitle("TomAmp");
673 void MainWindow::cellClicked(int /*row*/, int)
677 void MainWindow::contextMenuEvent (QContextMenuEvent*e)
679 if (!childAt (e->pos()))
681 if (childAt (e->pos())->parentWidget() != musicTable)
683 contextMenu->popup(e->globalPos());
687 void MainWindow::setupShuffleList()
690 int index = plman.indexOf(mediaObject->currentSource());
693 for (int i = 0; i < plman.size(); ++i)
699 shuffleList.append (index);
702 int ind = qrand () % tmp.size();
703 shuffleList.append(tmp[ind]);
708 void MainWindow::savePlaylist ()
710 QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u *.pls)");
711 if (filename.isEmpty())
713 plman.savePlaylist(filename);
716 void MainWindow::loadPlaylist ()
718 QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u *.pls");
719 if (filename.isEmpty())
721 plman.loadPlaylist (filename);
724 void MainWindow::playlistChanged(int from)
726 while (musicTable->rowCount() > from)
728 musicTable->removeRow(musicTable->rowCount () - 1);
731 for (int i = from; i < plman.size (); ++i)
733 if (firstGood < 0 && plman.getItem (i).playable)
735 int currentRow = musicTable->rowCount();
736 musicTable->insertRow(currentRow);
737 setRowFromItem (currentRow, plman.getItem(i));
739 if (plman.indexOf(mediaObject->currentSource()) < 0)
741 setItem (firstGood, false);
746 void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
748 if (row >= musicTable->rowCount())
750 if (item.artist.isEmpty() && item.title.isEmpty())
752 QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
753 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
754 musicTable->setItem(row, 1, item1);
758 QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
759 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
760 musicTable->setItem(row, 0, item1);
761 QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
762 item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
763 musicTable->setItem(row, 1, item2);
764 QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
765 item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
766 musicTable->setItem(row, 2, item3);
770 void MainWindow::itemUpdated(int index)
772 if (plman.indexOf(mediaObject->currentSource()) < 0 && plman.getItem (index).playable)
774 setItem (index, false);
776 setRowFromItem (index, plman.getItem(index));
777 if (plman.indexOf(mediaObject->currentSource()) == index)
779 if (shuffle) index = shuffleList.indexOf(index);
780 setItem (index, false);