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 qDebug () << "Getting index of current playing";
176 int index = plman.indexOf(mediaObject->currentSource());
177 qDebug () << "Next index is " << index;
180 qDebug () << "Shuffle next";
181 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource()));
185 qDebug () << "Index increase a " << index;
187 while (index < shuffleList.size () && !plman.getItem(index).playable);
188 qDebug () << "Shuffle next 2 " << index;
189 if (index < shuffleList.size ())
191 mediaObject->setCurrentSource(plman.at (shuffleList[index]));
198 qDebug () << "Index increase 2a " << index;
201 while (index < shuffleList.size () && !plman.getItem(index).playable);
202 if (index < shuffleList.size ())
203 mediaObject->setCurrentSource(plman.at (shuffleList[index]));
209 qDebug () << "Normal next";
210 while ((index + 1) < plman.size ())
213 qDebug () << "Index increase " << index;
214 if (plman.getItem(index).playable)
217 qDebug () << "Normal next 2 " << index;
218 if (plman.size() > index)
220 mediaObject->setCurrentSource(plman.at(index));
227 qDebug () << "Index increase 2 " << index;
229 if (plman.getItem(index).playable)
232 while ((index + 1) < shuffleList.size ());
233 mediaObject->setCurrentSource(plman.at(index));
236 musicTable->selectRow (plman.indexOf(mediaObject->currentSource()));
241 void MainWindow::previous()
243 bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
244 int index = plman.indexOf(mediaObject->currentSource()) - 1;
247 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) - 1;
250 mediaObject->setCurrentSource(plman.at (shuffleList[index]));
254 mediaObject->setCurrentSource(plman.at (shuffleList[shuffleList.size() - 1]));
262 mediaObject->setCurrentSource(plman.at(index));
266 mediaObject->setCurrentSource(plman.at(plman.size() - 1));
274 void MainWindow::tick(qint64 time)
276 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
278 timeLcd->display(displayTime.toString("mm:ss"));
281 void MainWindow::tableClicked(int row, int /* column */)
283 // bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
286 mediaObject->clearQueue();
288 if (row >= plman.size())
292 while (index < plman.size () && !plman.getItem(index).playable)
296 if (plman.size() > index)
298 mediaObject->setCurrentSource(plman.at(index));
299 int ind = shuffleList.indexOf(index);
300 shuffleList.removeAt(ind);
301 shuffleList.insert(0, index);
302 qDebug () << "Modified shuffle list: " << shuffleList;
312 void MainWindow::sourceChanged(const Phonon::MediaSource &source)
314 musicTable->selectRow(plman.indexOf(source));
315 timeLcd->display("00:00");
319 void MainWindow::aboutToFinish()
321 qDebug () << "Abouttotfinish";
322 int index = plman.indexOf(mediaObject->currentSource()) + 1;
325 index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
326 if (index < shuffleList.size ())
328 mediaObject->enqueue(plman.at (shuffleList[index]));
332 mediaObject->enqueue(plman.at (shuffleList[0]));
338 if (plman.size() > index)
340 mediaObject->enqueue(plman.at(index));
341 qDebug () << "Enqueue " << index << " pfm " << mediaObject->prefinishMark();
345 mediaObject->enqueue(plman.at(0));
346 qDebug () << "Enqueue " << 0 << " pfm " << mediaObject->prefinishMark();
351 void MainWindow::finished()
353 qDebug () << "Finished";
356 void MainWindow::setupActions()
358 playAction = new QAction(QIcon (QPixmap (":images/play")), tr("Play"), this);
359 playAction->setShortcut(tr("Crl+P"));
360 playAction->setDisabled(true);
361 pauseAction = new QAction(QIcon (QPixmap (":images/pause")), tr("Pause"), this);
362 pauseAction->setShortcut(tr("Ctrl+A"));
363 pauseAction->setDisabled(true);
364 pauseAction->setVisible(false);
365 stopAction = new QAction(QIcon (QPixmap (":images/stop")), tr("Stop"), this);
366 stopAction->setShortcut(tr("Ctrl+S"));
367 stopAction->setDisabled(true);
368 nextAction = new QAction(QIcon (QPixmap (":images/next")), tr("Next"), this);
369 nextAction->setShortcut(tr("Ctrl+N"));
370 previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
371 previousAction->setShortcut(tr("Ctrl+R"));
372 repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
373 repeatAction->setCheckable(true);
374 repeatAction->setChecked(repeat);
375 repeatAction->setShortcut(tr("Ctrl+I"));
376 shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
377 shuffleAction->setCheckable(true);
378 shuffleAction->setChecked(shuffle);
379 shuffleAction->setShortcut(tr("Ctrl+H"));
380 volumeAction = new QAction(QIcon (QPixmap (":images/volume")), tr("Volume"), this);
381 volumeAction->setCheckable(true);
382 volumeAction->setShortcut(tr("Ctrl+V"));
383 addFilesAction = new QAction(tr("Add &File"), this);
384 addFilesAction->setShortcut(tr("Ctrl+F"));
385 addFoldersAction = new QAction(tr("Add F&older"), this);
386 addFoldersAction->setShortcut(tr("Ctrl+O"));
387 addUrlAction = new QAction(tr("Add &Url"), this);
388 addUrlAction->setShortcut(tr("Ctrl+U"));
389 savePlaylistAction = new QAction (tr("Sa&ve Playlist"), this);
390 savePlaylistAction->setShortcut(tr ("Ctrl+V"));
391 loadPlaylistAction = new QAction (tr("&Load Playlist"), this);
392 loadPlaylistAction->setShortcut(tr("Ctrl+L"));
393 clearPlaylistAction = new QAction (tr("&Clear Playlist"), this);
394 clearPlaylistAction->setShortcut(tr("Ctrl+C"));
395 exitAction = new QAction(tr("E&xit"), this);
396 exitAction->setShortcut(tr("Ctrl+X"));
397 aboutAction = new QAction(tr("A&bout"), this);
398 aboutAction->setShortcut(tr("Ctrl+B"));
399 aboutQtAction = new QAction(tr("About &Qt"), this);
400 aboutQtAction->setShortcut(tr("Ctrl+Q"));
402 connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
403 connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
404 connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
405 connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
406 connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
407 connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
408 connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
409 connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolder()));
410 connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
411 connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
412 connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
413 connect (clearPlaylistAction, SIGNAL (triggered()), &plman, SLOT (clearPlaylist()));
414 connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
415 connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
416 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
417 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
418 connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
422 void MainWindow::repeatToggle ()
425 qDebug() << "Repeat toggled to " << repeat;
426 settings.setValue("repeat", QVariant (repeat));
429 void MainWindow::shuffleToggle ()
432 settings.setValue("shuffle", QVariant (shuffle));
435 void MainWindow::volumeToggle ()
437 qDebug () << "Volumetoggle: " << volumeAction->isChecked();
438 volumeSlider->setVisible(volumeAction->isChecked());
441 void MainWindow::play()
447 void MainWindow::setupMenus()
449 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
450 fileMenu->addAction(addFilesAction);
451 fileMenu->addAction(addFoldersAction);
452 fileMenu->addAction(addUrlAction);
453 fileMenu->addSeparator();
454 fileMenu->addAction(savePlaylistAction);
455 fileMenu->addAction(loadPlaylistAction);
456 fileMenu->addAction(clearPlaylistAction);
457 // fileMenu->addAction(exitAction);
459 QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
460 aboutMenu->addAction(aboutAction);
461 aboutMenu->addAction(aboutQtAction);
464 void MainWindow::setupUi()
466 QToolBar *bar = new QToolBar;
468 bar->setOrientation(Qt::Vertical);
469 bar->setStyleSheet("padding:7px");
470 //bar->addAction(volumeAction);
472 seekSlider = new Phonon::SeekSlider(this);
473 seekSlider->setMediaObject(mediaObject);
475 volumeSlider = new Phonon::VolumeSlider(this);
476 volumeSlider->setAudioOutput(audioOutput);
477 volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
478 volumeSlider->setOrientation(Qt::Horizontal);
479 volumeSlider->setMuteVisible(false);
480 // volumeAddedAction = bar->addWidget(volumeSlider);
481 // volumeAddedAction->setVisible(false);
482 bar->addAction(playAction);
483 bar->addAction(pauseAction);
484 bar->addAction(stopAction);
485 bar->addAction(repeatAction);
486 bar->addAction(shuffleAction);
487 bar->addAction(nextAction);
488 bar->addAction(previousAction);
490 /* QLabel *volumeLabel = new QLabel;
491 volumeLabel->setPixmap(QPixmap("images/volume.png"));*/
494 palette.setBrush(QPalette::Light, Qt::darkGray);*/
496 timeLcd = new QLCDNumber;
497 // timeLcd->setPalette(palette);
500 headers << tr("Artist") << tr("Title") << tr("Album");
502 musicTable = new QTableWidget(0, 3);
503 musicTable->setHorizontalHeaderLabels(headers);
504 musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
505 musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
506 connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
507 this, SLOT(tableClicked(int,int)));
508 connect(musicTable, SIGNAL(cellClicked(int,int)),
509 this, SLOT(cellClicked(int,int)));
510 musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
512 QHBoxLayout *seekerLayout = new QHBoxLayout;
513 QToolBar* bar2 = new QToolBar;
514 bar2->addAction(volumeAction);
515 seekerLayout->addWidget(bar2);
516 seekerLayout->addWidget(volumeSlider);
517 seekerLayout->addWidget(seekSlider);
518 seekerLayout->addWidget(timeLcd);
520 QVBoxLayout *playbackLayout = new QVBoxLayout;
521 volumeSlider->hide ();
522 playbackLayout->addWidget(bar);
523 // playbackLayout->addStretch();
524 // playbackLayout->addWidget(volumeSlider);
525 // playbackLayout->addWidget(volumeLabel);
527 QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
529 seekAndTableLayout->addWidget(musicTable);
530 seekAndTableLayout->addLayout(seekerLayout);
532 QHBoxLayout *mainLayout = new QHBoxLayout;
533 mainLayout->addLayout(seekAndTableLayout);
534 mainLayout->addLayout(playbackLayout);
536 QWidget *widget = new QWidget;
537 widget->setLayout(mainLayout);
539 setCentralWidget(widget);
540 setWindowTitle("TomAmp");
541 qDebug () << "cucc: " << musicTable->columnWidth(1);
544 void MainWindow::cellClicked(int /*row*/, int)
546 /*if (mediaObject->state() == Phonon::PlayingState)
548 int index = plman.indexOf(mediaObject->currentSource());
551 musicTable->selectRow(index);
554 else if (row < plman.size())
556 mediaObject->setCurrentSource(plman.at(row));
557 shuffleList.removeAll(row);
558 shuffleList.insert(0, row);
559 qDebug () << shuffleList;
563 void MainWindow::setupShuffleList()
566 int index = plman.indexOf(mediaObject->currentSource());
569 for (int i = 0; i < plman.size(); ++i)
575 shuffleList.append (index);
578 int ind = qrand () % tmp.size();
579 shuffleList.append(tmp[ind]);
582 qDebug () << shuffleList;
583 qDebug () << shuffleList;
586 void MainWindow::savePlaylist ()
588 QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u)");
589 plman.loadPlaylist(filename);
592 void MainWindow::loadPlaylist ()
594 QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u");
595 plman.loadPlaylist (filename);
598 void MainWindow::playlistChanged(int from)
600 while (musicTable->rowCount() > from)
602 musicTable->removeRow(musicTable->rowCount () - 1);
604 for (int i = from; i < plman.size (); ++i)
606 int currentRow = musicTable->rowCount();
607 musicTable->insertRow(currentRow);
608 setRowFromItem (currentRow, plman.getItem(i));
613 void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
615 if (row >= musicTable->rowCount())
617 if (item.artist.isEmpty() && item.title.isEmpty())
619 QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
620 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
621 musicTable->setItem(row, 1, item1);
625 QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
626 item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
627 musicTable->setItem(row, 0, item1);
628 QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
629 item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
630 musicTable->setItem(row, 1, item2);
631 QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
632 item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
633 musicTable->setItem(row, 2, item3);
637 void MainWindow::itemUpdated(int index)
639 setRowFromItem (index, plman.getItem(index));