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: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: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((time / 3600000), (time / 60000) % 60, (time / 1000) % 60);
343 timeLcd->display(displayTime.toString("HH: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: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")), "", this);
433 playAction->setShortcut(tr("Crl+P"));
434 playAction->setDisabled(true);
435 pauseAction = new QAction(QIcon (QPixmap (":images/pause")), "", this);
436 pauseAction->setShortcut(tr("Ctrl+A"));
437 pauseAction->setDisabled(true);
438 pauseAction->setVisible(false);
439 stopAction = new QAction(QIcon (QPixmap (":images/stop")), "", this);
440 stopAction->setShortcut(tr("Ctrl+S"));
441 stopAction->setDisabled(true);
442 nextAction = new QAction(QIcon (QPixmap (":images/next")), "", this);
443 nextAction->setShortcut(tr("Ctrl+N"));
444 previousAction = new QAction(QIcon (QPixmap (":images/previous")), "", this);
445 previousAction->setShortcut(tr("Ctrl+R"));
447 repeatAction = new QAction(QIcon (QPixmap (":images/repeatActive")), "", this);
449 repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), "", this);
450 repeatAction->setCheckable(true);
451 repeatAction->setChecked(repeat);
452 repeatAction->setShortcut(tr("Ctrl+I"));
454 shuffleAction = new QAction(QIcon (QPixmap (":images/shuffleActive")), "", this);
456 shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), "", this);
457 shuffleAction->setCheckable(true);
458 shuffleAction->setChecked(shuffle);
459 shuffleAction->setShortcut(tr("Ctrl+H"));
460 volumeAction = new QAction(QIcon (QPixmap (":images/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 if (QMessageBox::question(this, "Confirm remove", "Are you sure you want to remove this item?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
509 int row = musicTable->currentRow();
511 plman.removeItem(row);
514 void MainWindow::removeAllButSelectedItem()
516 if (QMessageBox::question(this, "Confirm remove", "Are you sure you want to remove all other items?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
518 int row = musicTable->currentRow();
521 QString uri = plman.getItem(row).uri;
524 plman.clearPlaylist();
525 plman.addStringList(lst);
529 void MainWindow::repeatToggle ()
532 settings.setValue("repeat", QVariant (repeat));
534 repeatAction->setIcon(QIcon (QPixmap (":images/repeatActive")));
536 repeatAction->setIcon(QIcon (QPixmap (":images/repeat")));
539 void MainWindow::shuffleToggle ()
542 settings.setValue("shuffle", QVariant (shuffle));
544 shuffleAction->setIcon(QIcon (QPixmap (":images/shuffleActive")));
546 shuffleAction->setIcon(QIcon (QPixmap (":images/shuffle")));
549 void MainWindow::volumeToggle ()
551 volumeSlider->setVisible(volumeAction->isChecked());
554 void MainWindow::play()
557 lastPlayed = plman.indexOf(mediaObject->currentSource());
558 highlightRow(lastPlayed);
562 void MainWindow::stop()
569 void MainWindow::setupMenus()
571 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
572 fileMenu->addAction(addFilesAction);
573 fileMenu->addAction(addFoldersAction);
574 fileMenu->addAction(addUrlAction);
575 fileMenu->addSeparator();
576 fileMenu->addAction(savePlaylistAction);
577 fileMenu->addAction(loadPlaylistAction);
578 fileMenu->addAction(clearPlaylistAction);
579 // fileMenu->addAction(exitAction);
581 QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
582 aboutMenu->addAction(aboutAction);
583 aboutMenu->addAction(aboutQtAction);
586 void MainWindow::setupUi()
588 QToolBar *bar = new QToolBar;
590 bar->setOrientation(Qt::Vertical);
591 bar->setStyleSheet("padding:7px");
592 //bar->addAction(volumeAction);
594 seekSlider = new Phonon::SeekSlider(this);
595 seekSlider->setMediaObject(mediaObject);
597 volumeSlider = new Phonon::VolumeSlider(this);
598 volumeSlider->setAudioOutput(audioOutput);
599 volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
600 volumeSlider->setOrientation(Qt::Horizontal);
601 volumeSlider->setMuteVisible(false);
602 // volumeAddedAction = bar->addWidget(volumeSlider);
603 // volumeAddedAction->setVisible(false);
604 bar->addAction(playAction);
605 bar->addAction(pauseAction);
606 bar->addAction(stopAction);
607 bar->addAction(repeatAction);
608 bar->addAction(shuffleAction);
609 bar->addAction(nextAction);
610 bar->addAction(previousAction);
612 contextMenu = new QMenu (this);
613 removeSelected = contextMenu->addAction(tr ("Remove selected"));
614 removeAllButSelected = contextMenu->addAction(tr("Remove all but selected"));
615 connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
616 connect (removeAllButSelected, SIGNAL (triggered()), this, SLOT (removeAllButSelectedItem()));
619 timeLcd = new QLCDNumber;
622 headers << tr("Artist") << tr("Title") << tr("Album") << "Controls";
624 musicTable = new QTableWidget(0, 4);
625 musicTable->setHorizontalHeaderLabels(headers);
626 musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
627 musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
628 connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
629 this, SLOT(tableClicked(int,int)));
630 connect(musicTable, SIGNAL(cellClicked(int,int)),
631 this, SLOT(cellClicked(int,int)));
632 /* for (int i = 0; i < 3; ++i)
634 if (!musicTable->horizontalHeaderItem(i))
636 musicTable->horizontalHeaderItem(i)->setBackgroundColor(QColor (128, 128, 255));;
637 musicTable->horizontalHeaderItem(i)->setForeground(QColor (255, 255, 255));
639 for (int i = 0; i < musicTable->columnCount(); ++i)
641 QString lab = QString ("colWidth_%1").arg (i);
642 int val = settings.value(lab, 0).toInt();
644 musicTable->setColumnWidth(i, val);
645 // settings.setValue(lab, musicTable->columnWidth(i));
649 QHBoxLayout *seekerLayout = new QHBoxLayout;
650 QToolBar* bar2 = new QToolBar;
651 bar2->addAction(volumeAction);
652 seekerLayout->addWidget(bar2);
653 seekerLayout->addWidget(volumeSlider);
654 seekerLayout->addWidget(seekSlider);
655 seekerLayout->addWidget(timeLcd);
657 QVBoxLayout *playbackLayout = new QVBoxLayout;
658 volumeSlider->hide ();
659 playbackLayout->addWidget(bar);
661 QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
663 seekAndTableLayout->addWidget(musicTable);
664 seekAndTableLayout->addLayout(seekerLayout);
666 QHBoxLayout *mainLayout = new QHBoxLayout;
667 mainLayout->addLayout(seekAndTableLayout);
668 mainLayout->addLayout(playbackLayout);
670 QWidget *widget = new QWidget;
671 widget->setLayout(mainLayout);
673 setCentralWidget(widget);
674 setWindowTitle("TomAmp");
677 void MainWindow::cellClicked(int /*row*/, int)
681 void MainWindow::contextMenuEvent (QContextMenuEvent*e)
683 if (!childAt (e->pos()))
685 if (childAt (e->pos())->parentWidget() != musicTable)
687 contextMenu->popup(e->globalPos());
691 void MainWindow::setupShuffleList()
694 int index = plman.indexOf(mediaObject->currentSource());
697 for (int i = 0; i < plman.size(); ++i)
703 shuffleList.append (index);
706 int ind = qrand () % tmp.size();
707 shuffleList.append(tmp[ind]);
712 void MainWindow::savePlaylist ()
714 QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u *.pls)");
715 if (filename.isEmpty())
717 plman.savePlaylist(filename);
720 void MainWindow::loadPlaylist ()
722 QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u *.pls");
723 if (filename.isEmpty())
725 plman.loadPlaylist (filename);
728 void MainWindow::playlistChanged(int from)
730 while (musicTable->rowCount() > from)
732 musicTable->removeRow(musicTable->rowCount () - 1);
735 for (int i = from; i < plman.size (); ++i)
737 if (firstGood < 0 && plman.getItem (i).playable)
739 int currentRow = musicTable->rowCount();
740 musicTable->insertRow(currentRow);
741 setRowFromItem (currentRow, plman.getItem(i));
743 /* if (plman.indexOf(mediaObject->currentSource()) < 0)
745 setItem (firstGood, false);
750 void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
752 if (row >= musicTable->rowCount())
754 if (item.artist.isEmpty() && item.title.isEmpty())
756 QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
757 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
758 musicTable->setItem(row, 1, item1);
762 QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
763 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
764 musicTable->setItem(row, 0, item1);
765 QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
766 item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
767 musicTable->setItem(row, 1, item2);
768 QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
769 item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
770 musicTable->setItem(row, 2, item3);
772 qDebug () << "Widget: " << musicTable->cellWidget(row, 3);
774 if (!musicTable->cellWidget(row, 3))
776 QToolBar* bar = new QToolBar;
777 QPushButton* up = new QPushButton;
779 up->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
780 bar->setProperty("row", row);
782 musicTable->setCellWidget(row, 3, bar);
783 connect (up, SIGNAL (clicked ()), this, SLOT (buttonUp ()));
787 void MainWindow::buttonUp()
789 qDebug () << "Presses up on " << sender()->parent()->property("row");
790 plman.moveItemUp(sender()->parent()->property("row").toInt());
793 void MainWindow::itemUpdated(int index)
795 if (plman.indexOf(mediaObject->currentSource()) < 0 && plman.getItem (index).playable)
797 setItem (index, false);
799 setRowFromItem (index, plman.getItem(index));
800 if (plman.indexOf(mediaObject->currentSource()) == index)
802 if (shuffle) index = shuffleList.indexOf(index);
803 setItem (index, false);