3 #include <QInputDialog>
5 #include "mainwindow.h"
8 #define AVOID_INPUT_DIALOG 1
10 MainWindow::MainWindow()
11 : settings (tr ("TomAmp"), "TomAmp")
13 audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
14 mediaObject = new Phonon::MediaObject(this);
15 metaInformationResolver = new Phonon::MediaObject(this);
17 mediaObject->setTickInterval(500);
18 connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
19 connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
20 this, SLOT(stateChanged(Phonon::State,Phonon::State)));
21 connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
22 this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
23 connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
24 this, SLOT(sourceChanged(Phonon::MediaSource)));
25 connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
27 Phonon::createPath(mediaObject, audioOutput);
29 repeat = settings.value("repeat", false).toBool();
30 shuffle = settings.value("shuffle", false).toBool();
36 timeLcd->display("00:00");
37 addStringList(settings.value("lastPlaylist").toStringList());
38 audioOutput->setVolume(settings.value("volume", .5).toReal());
41 MainWindow::~MainWindow()
43 settings.setValue("shuffle", shuffle);
45 foreach (Phonon::MediaSource src, sources)
47 if (src.type () == Phonon::MediaSource::LocalFile)
48 curList.append(src.fileName());
50 curList.append(src.url().toString());
52 settings.setValue("lastPlaylist", curList);
53 settings.setValue("volume", audioOutput->volume());
54 qDebug () << "cucc: " << musicTable->columnWidth(1);
57 void MainWindow::addFiles()
59 QString folder = settings.value("LastFolder").toString();
61 folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
62 QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Files To Add"),
68 QString dir = QFileInfo (files[0]).absoluteDir().absolutePath();
69 settings.setValue("LastFolder", dir);
70 int index = sources.size();
71 foreach (QString string, files)
73 Phonon::MediaSource source (string);
74 sources.append(source);
76 if (!sources.isEmpty())
77 metaInformationResolver->setCurrentSource(sources.at(index));
82 void MainWindow::addFolders()
84 QString folder = settings.value("LastFolder").toString();
86 folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
87 QString dir = QFileDialog::getExistingDirectory(this,
88 tr("Select Directory To Add"),
94 QStringList files = QDir (dir).entryList(filters);
99 settings.setValue("LastFolder", dir);
100 int index = sources.size();
101 foreach (QString string, files)
103 QString fname = dir + "/" + string;
105 Phonon::MediaSource source(fname);
106 sources.append(source);
108 if (!sources.isEmpty())
109 metaInformationResolver->setCurrentSource(sources.at(index));
114 void MainWindow::addUrl()
116 #ifdef AVOID_INPUT_DIALOG
117 QString url = "http://war.str3am.com:7970";
119 QString url = QInputDialog::getText(this, "Get URL", "Please type in the stream URL");
121 int index = sources.size();
124 Phonon::MediaSource source(url);
125 sources.append(source);
127 if (!sources.isEmpty())
128 metaInformationResolver->setCurrentSource(sources.at(index));
132 void MainWindow::addStringList(const QStringList& list)
134 int index = sources.size();
135 foreach (QString string, list)
137 Phonon::MediaSource source(string);
138 sources.append(source);
140 if (!sources.isEmpty())
141 metaInformationResolver->setCurrentSource(sources.at(index));
145 void MainWindow::about()
147 QMessageBox::information(this, tr("About TomAmp v0.1"),
148 tr("TomAmp is a simple playlist-based music player.\n\n"
149 "(c) 2010 Tamas Marki <tmarki@gmail.com>\n\n"
150 "Please send comments and bug reports to the above e-mail address.\n\n"
151 "Icons by deleket (http://www.deleket.com)"));
154 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
156 qDebug () << "State: " << newState;
159 case Phonon::ErrorState:
160 if (mediaObject->errorType() == Phonon::FatalError)
162 QMessageBox::warning(this, tr("Fatal Error"),
163 mediaObject->errorString());
167 QMessageBox::warning(this, tr("Error"),
168 mediaObject->errorString());
171 case Phonon::PlayingState:
172 setWindowTitle(mediaObject->metaData().value("TITLE") + " - TomAmp");
173 pauseAction->setVisible(true);
174 playAction->setVisible (false);
175 playAction->setEnabled(false);
176 pauseAction->setEnabled(true);
177 stopAction->setEnabled(true);
179 case Phonon::StoppedState:
180 stopAction->setEnabled(false);
181 playAction->setEnabled(true);
182 pauseAction->setVisible(false);
183 playAction->setVisible(true);
184 pauseAction->setEnabled(false);
185 timeLcd->display("00:00");
187 case Phonon::PausedState:
188 pauseAction->setEnabled(false);
189 stopAction->setEnabled(true);
190 pauseAction->setVisible(false);
191 playAction->setVisible(true);
192 playAction->setEnabled(true);
193 qDebug () << "Queue size: " << mediaObject->queue().size ();
194 if (mediaObject->queue().size ())
196 mediaObject->setCurrentSource(mediaObject->queue()[0]);
197 musicTable->selectRow(sources.indexOf(mediaObject->currentSource()));
200 mediaObject->clearQueue();
202 case Phonon::BufferingState:
203 qDebug () << "Buffering";
210 void MainWindow::next()
215 void MainWindow::previous()
220 void MainWindow::tick(qint64 time)
222 QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
224 timeLcd->display(displayTime.toString("mm:ss"));
227 void MainWindow::tableClicked(int row, int /* column */)
229 // bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
232 mediaObject->clearQueue();
234 if (row >= sources.size())
237 mediaObject->setCurrentSource(sources[row]);
240 int ind = shuffleList.indexOf(row);
241 shuffleList.removeAt(ind);
242 shuffleList.insert(0, row);
243 qDebug () << "Modified shuffle list: " << shuffleList;
246 void MainWindow::sourceChanged(const Phonon::MediaSource &source)
248 musicTable->selectRow(sources.indexOf(source));
249 timeLcd->display("00:00");
252 void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
254 if (newState == Phonon::ErrorState)
256 QMessageBox::warning(this, tr("Error opening files"),
257 metaInformationResolver->errorString());
258 while (!sources.isEmpty() &&
259 !(sources.takeLast() == metaInformationResolver->currentSource())) {} /* loop */;
263 if (newState != Phonon::StoppedState && newState != Phonon::PausedState)
268 if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
271 QMap<QString, QString> metaData = metaInformationResolver->metaData();
273 QString title = metaData.value("TITLE");
275 title = metaInformationResolver->currentSource().fileName();
278 title = metaInformationResolver->currentSource().url().toString();
280 QTableWidgetItem *titleItem = new QTableWidgetItem(title);
281 titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
282 QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST"));
283 artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
284 QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
285 albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
287 int currentRow = musicTable->rowCount();
288 musicTable->insertRow(currentRow);
289 musicTable->setItem(currentRow, 0, artistItem);
290 musicTable->setItem(currentRow, 1, titleItem);
291 musicTable->setItem(currentRow, 2, albumItem);
294 if (musicTable->selectedItems().isEmpty())
296 musicTable->selectRow(0);
297 mediaObject->setCurrentSource(metaInformationResolver->currentSource());
300 Phonon::MediaSource source = metaInformationResolver->currentSource();
301 int index = sources.indexOf(metaInformationResolver->currentSource()) + 1;
302 if (sources.size() > index)
304 metaInformationResolver->setCurrentSource(sources.at(index));
308 musicTable->resizeColumnsToContents();
309 /* if (musicTable->columnWidth(0) > 300)
310 musicTable->setColumnWidth(0, 300);*/
314 void MainWindow::aboutToFinish()
316 qDebug () << "Abouttotfinish";
317 int index = sources.indexOf(mediaObject->currentSource()) + 1;
320 index = shuffleList.indexOf(sources.indexOf(mediaObject->currentSource())) + 1;
321 if (index < shuffleList.size ())
323 mediaObject->enqueue(sources.at (shuffleList[index]));
327 mediaObject->enqueue(sources.at (shuffleList[0]));
333 if (sources.size() > index)
335 mediaObject->enqueue(sources.at(index));
336 qDebug () << "Enqueue " << index << " pfm " << mediaObject->prefinishMark();
340 mediaObject->enqueue(sources.at(0));
341 qDebug () << "Enqueue " << 0 << " pfm " << mediaObject->prefinishMark();
346 void MainWindow::finished()
348 qDebug () << "Finished";
351 void MainWindow::setupActions()
353 playAction = new QAction(QIcon (QPixmap (":images/play")), tr("Play"), this);
354 playAction->setShortcut(tr("Crl+P"));
355 playAction->setDisabled(true);
356 pauseAction = new QAction(QIcon (QPixmap (":images/pause")), tr("Pause"), this);
357 pauseAction->setShortcut(tr("Ctrl+A"));
358 pauseAction->setDisabled(true);
359 pauseAction->setVisible(false);
360 stopAction = new QAction(QIcon (QPixmap (":images/stop")), tr("Stop"), this);
361 stopAction->setShortcut(tr("Ctrl+S"));
362 stopAction->setDisabled(true);
363 nextAction = new QAction(QIcon (QPixmap (":images/next")), tr("Next"), this);
364 nextAction->setShortcut(tr("Ctrl+N"));
365 previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
366 previousAction->setShortcut(tr("Ctrl+R"));
367 repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
368 repeatAction->setCheckable(true);
369 repeatAction->setChecked(repeat);
370 repeatAction->setShortcut(tr("Ctrl+I"));
371 shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
372 shuffleAction->setCheckable(true);
373 shuffleAction->setChecked(shuffle);
374 shuffleAction->setShortcut(tr("Ctrl+H"));
375 volumeAction = new QAction(QIcon (QPixmap (":images/volume")), tr("Volume"), this);
376 volumeAction->setCheckable(true);
377 volumeAction->setShortcut(tr("Ctrl+V"));
378 addFilesAction = new QAction(tr("Add &File"), this);
379 addFilesAction->setShortcut(tr("Ctrl+F"));
380 addFoldersAction = new QAction(tr("Add F&older"), this);
381 addFoldersAction->setShortcut(tr("Ctrl+O"));
382 addUrlAction = new QAction(tr("Add &Url"), this);
383 addUrlAction->setShortcut(tr("Ctrl+U"));
384 savePlaylistAction = new QAction (tr("Sa&ve Playlist"), this);
385 savePlaylistAction->setShortcut(tr ("Ctrl+V"));
386 loadPlaylistAction = new QAction (tr("&Load Playlist"), this);
387 loadPlaylistAction->setShortcut(tr("Ctrl+L"));
388 clearPlaylistAction = new QAction (tr("&Clear Playlist"), this);
389 clearPlaylistAction->setShortcut(tr("Ctrl+C"));
390 exitAction = new QAction(tr("E&xit"), this);
391 exitAction->setShortcut(tr("Ctrl+X"));
392 aboutAction = new QAction(tr("A&bout"), this);
393 aboutAction->setShortcut(tr("Ctrl+B"));
394 aboutQtAction = new QAction(tr("About &Qt"), this);
395 aboutQtAction->setShortcut(tr("Ctrl+Q"));
397 connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
398 connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
399 connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
400 connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
401 connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
402 connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
403 connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
404 connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolders()));
405 connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
406 connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
407 connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
408 connect (clearPlaylistAction, SIGNAL (triggered()), this, SLOT (clearPlaylist()));
409 connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
410 connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
411 connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
412 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
413 connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
416 void MainWindow::savePlaylist()
418 QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u)");
419 if (filename.isEmpty())
421 if (filename.length() < 4 || filename.right(4).toLower() != ".m3u")
426 f.open(QFile::WriteOnly);
427 for (int i = 0; i < sources.size(); ++i)
429 if (sources[i].type() == Phonon::MediaSource::LocalFile)
430 f.write (sources[i].fileName().toAscii());
432 f.write(sources[i].url().toString().toAscii());
439 QMessageBox::critical(this, "Write error", "Could not write playlist file", QMessageBox::Ok);
443 void MainWindow::loadPlaylist()
445 QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u");
447 f.open (QFile::ReadOnly);
448 QString tmp = f.readAll();
450 QStringList lines = tmp.split("\n");
452 foreach (QString l, lines)
454 if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
456 qDebug () << "not loadable: " << l;\
459 qDebug () << "Load " << l;
460 Phonon::MediaSource source(l);
461 sources.append(source);
463 if (!sources.isEmpty())
464 metaInformationResolver->setCurrentSource(sources.at(0));
468 void MainWindow::clearPlaylist()
471 while (musicTable->rowCount())
472 musicTable->removeRow(0);
473 mediaObject->clear();
476 void MainWindow::repeatToggle ()
479 qDebug() << "Repeat toggled to " << repeat;
480 settings.setValue("repeat", QVariant (repeat));
483 void MainWindow::shuffleToggle ()
486 settings.setValue("shuffle", QVariant (shuffle));
489 void MainWindow::volumeToggle ()
491 qDebug () << "Volumetoggle: " << volumeAction->isChecked();
492 volumeSlider->setVisible(volumeAction->isChecked());
496 void MainWindow::setupMenus()
498 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
499 fileMenu->addAction(addFilesAction);
500 fileMenu->addAction(addFoldersAction);
501 fileMenu->addAction(addUrlAction);
502 fileMenu->addSeparator();
503 fileMenu->addAction(savePlaylistAction);
504 fileMenu->addAction(loadPlaylistAction);
505 fileMenu->addAction(clearPlaylistAction);
506 // fileMenu->addAction(exitAction);
508 QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
509 aboutMenu->addAction(aboutAction);
510 aboutMenu->addAction(aboutQtAction);
513 void MainWindow::setupUi()
515 QToolBar *bar = new QToolBar;
517 bar->setOrientation(Qt::Vertical);
518 bar->setStyleSheet("padding:7px");
519 //bar->addAction(volumeAction);
521 seekSlider = new Phonon::SeekSlider(this);
522 seekSlider->setMediaObject(mediaObject);
524 volumeSlider = new Phonon::VolumeSlider(this);
525 volumeSlider->setAudioOutput(audioOutput);
526 volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
527 volumeSlider->setOrientation(Qt::Horizontal);
528 volumeSlider->setMuteVisible(false);
529 // volumeAddedAction = bar->addWidget(volumeSlider);
530 // volumeAddedAction->setVisible(false);
531 bar->addAction(playAction);
532 bar->addAction(pauseAction);
533 bar->addAction(stopAction);
534 bar->addAction(repeatAction);
535 bar->addAction(shuffleAction);
536 bar->addAction(nextAction);
537 bar->addAction(previousAction);
539 /* QLabel *volumeLabel = new QLabel;
540 volumeLabel->setPixmap(QPixmap("images/volume.png"));*/
543 palette.setBrush(QPalette::Light, Qt::darkGray);*/
545 timeLcd = new QLCDNumber;
546 // timeLcd->setPalette(palette);
549 headers << tr("Artist") << tr("Title") << tr("Album");
551 musicTable = new QTableWidget(0, 3);
552 musicTable->setHorizontalHeaderLabels(headers);
553 musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
554 musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
555 connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
556 this, SLOT(tableClicked(int,int)));
558 QHBoxLayout *seekerLayout = new QHBoxLayout;
559 QToolBar* bar2 = new QToolBar;
560 bar2->addAction(volumeAction);
561 seekerLayout->addWidget(bar2);
562 seekerLayout->addWidget(volumeSlider);
563 seekerLayout->addWidget(seekSlider);
564 seekerLayout->addWidget(timeLcd);
566 QVBoxLayout *playbackLayout = new QVBoxLayout;
567 volumeSlider->hide ();
568 playbackLayout->addWidget(bar);
569 // playbackLayout->addStretch();
570 // playbackLayout->addWidget(volumeSlider);
571 // playbackLayout->addWidget(volumeLabel);
573 QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
575 seekAndTableLayout->addWidget(musicTable);
576 seekAndTableLayout->addLayout(seekerLayout);
578 QHBoxLayout *mainLayout = new QHBoxLayout;
579 mainLayout->addLayout(seekAndTableLayout);
580 mainLayout->addLayout(playbackLayout);
582 QWidget *widget = new QWidget;
583 widget->setLayout(mainLayout);
585 setCentralWidget(widget);
586 setWindowTitle("TomAmp");
587 qDebug () << "cucc: " << musicTable->columnWidth(1);
591 void MainWindow::setupShuffleList()
594 for (int i = 0; i < sources.size(); ++i)
599 int ind = qrand () % tmp.size();
600 shuffleList.append(tmp[ind]);
603 qDebug () << shuffleList;