previous/next button, some fixes
[tomamp] / mainwindow.cpp
index da48bf3..651d323 100644 (file)
@@ -3,6 +3,7 @@
 #include <QInputDialog>
 
 #include "mainwindow.h"
+#include "time.h"
 
 #define AVOID_INPUT_DIALOG 1
 
@@ -13,7 +14,7 @@ MainWindow::MainWindow()
     mediaObject = new Phonon::MediaObject(this);
     metaInformationResolver = new Phonon::MediaObject(this);
 
-    mediaObject->setTickInterval(500);
+    mediaObject->setTickInterval(1000);
     connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
     connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
         this, SLOT(stateChanged(Phonon::State,Phonon::State)));
@@ -50,6 +51,7 @@ MainWindow::~MainWindow()
     }
     settings.setValue("lastPlaylist", curList);
     settings.setValue("volume", audioOutput->volume());
+    qDebug () << "cucc: " << musicTable->columnWidth(1);
 }
 
 void MainWindow::addFiles()
@@ -77,7 +79,7 @@ void MainWindow::addFiles()
 
 }
 
-void MainWindow::addFolders()
+void MainWindow::addFolder()
 {
     QString folder = settings.value("LastFolder").toString();
     if (folder.isEmpty())
@@ -86,16 +88,33 @@ void MainWindow::addFolders()
             tr("Select Directory To Add"),
             folder);
 
+    int index = sources.size();
+    QStringList filters;
+    QStringList files = QDir (dir).entryList(filters, QDir::AllDirs);
+    qDebug () << files;
+    bool recursive = false;
+    if (files.size())
+        recursive = QMessageBox::question(this, "Add all folders", "Subfolders have been detected, add everything?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
+    parseAndAddFolder(dir, recursive);
+    if (!sources.isEmpty() && index < sources.size())
+        metaInformationResolver->setCurrentSource(sources.at(index));
+    setupShuffleList();
+
+}
+
+void MainWindow::parseAndAddFolder(const QString &dir, bool recursive)
+{
     QStringList filters;
-    filters << "*.mp3";
+//    filters << "*.mp3";
 
     QStringList files = QDir (dir).entryList(filters);
 
     if (files.isEmpty())
         return;
 
+    qDebug () << files;
+
     settings.setValue("LastFolder", dir);
-    int index = sources.size();
     foreach (QString string, files)
     {
         QString fname = dir + "/" + string;
@@ -103,9 +122,6 @@ void MainWindow::addFolders()
         Phonon::MediaSource source(fname);
         sources.append(source);
     }
-    if (!sources.isEmpty())
-        metaInformationResolver->setCurrentSource(sources.at(index));
-    setupShuffleList();
 
 }
 
@@ -142,9 +158,11 @@ void MainWindow::addStringList(const QStringList& list)
 
 void MainWindow::about()
 {
-    QMessageBox::information(this, tr("About Music Player"),
-        tr("The Music Player example shows how to use Phonon - the multimedia"
-        " framework that comes with Qt - to create a simple music player."));
+    QMessageBox::information(this, tr("About TomAmp v0.1"),
+        tr("TomAmp is a simple playlist-based music player.\n\n"
+        "(c) 2010 Tamas Marki <tmarki@gmail.com>\n\n"
+        "Please send comments and bug reports to the above e-mail address.\n\n"
+        "Icons by deleket (http://www.deleket.com)"));
 }
 
 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
@@ -166,6 +184,8 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
             break;
         case Phonon::PlayingState:
             setWindowTitle(mediaObject->metaData().value("TITLE") + " - TomAmp");
+            pauseAction->setVisible(true);
+            playAction->setVisible (false);
             playAction->setEnabled(false);
             pauseAction->setEnabled(true);
             stopAction->setEnabled(true);
@@ -173,12 +193,16 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
         case Phonon::StoppedState:
             stopAction->setEnabled(false);
             playAction->setEnabled(true);
+            pauseAction->setVisible(false);
+            playAction->setVisible(true);
             pauseAction->setEnabled(false);
             timeLcd->display("00:00");
             break;
         case Phonon::PausedState:
             pauseAction->setEnabled(false);
             stopAction->setEnabled(true);
+            pauseAction->setVisible(false);
+            playAction->setVisible(true);
             playAction->setEnabled(true);
             qDebug () << "Queue size: " << mediaObject->queue().size ();
             if (mediaObject->queue().size ())
@@ -197,6 +221,71 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
     }
 }
 
+void MainWindow::next()
+{
+    bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
+    int index = sources.indexOf(mediaObject->currentSource()) + 1;
+    if (shuffle)
+    {
+        index = shuffleList.indexOf(sources.indexOf(mediaObject->currentSource())) + 1;
+        if (index < shuffleList.size ())
+        {
+            mediaObject->setCurrentSource(sources.at (shuffleList[index]));
+        }
+        else if (repeat)
+        {
+            mediaObject->setCurrentSource(sources.at (shuffleList[0]));
+        }
+
+    }
+    else
+    {
+        if (sources.size() > index)
+        {
+            mediaObject->setCurrentSource(sources.at(index));
+        }
+        else if (repeat)
+        {
+            mediaObject->setCurrentSource(sources.at(0));
+        }
+    }
+    if (wasPlaying)
+        mediaObject->play();
+}
+
+void MainWindow::previous()
+{
+    bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
+    int index = sources.indexOf(mediaObject->currentSource()) - 1;
+    if (shuffle)
+    {
+        index = shuffleList.indexOf(sources.indexOf(mediaObject->currentSource())) - 1;
+        if (index >= 0)
+        {
+            mediaObject->setCurrentSource(sources.at (shuffleList[index]));
+        }
+        else if (repeat)
+        {
+            mediaObject->setCurrentSource(sources.at (shuffleList[shuffleList.size() - 1]));
+        }
+
+    }
+    else
+    {
+        if (index >= 0)
+        {
+            mediaObject->setCurrentSource(sources.at(index));
+        }
+        else if (repeat)
+        {
+            mediaObject->setCurrentSource(sources.at(sources.size() - 1));
+        }
+    }
+    if (wasPlaying)
+        mediaObject->play();
+
+}
+
 void MainWindow::tick(qint64 time)
 {
     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
@@ -263,15 +352,12 @@ void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldSt
     artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
     QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
     albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
-    QTableWidgetItem *yearItem = new QTableWidgetItem(metaData.value("DATE"));
-    yearItem->setFlags(yearItem->flags() ^ Qt::ItemIsEditable);
 
     int currentRow = musicTable->rowCount();
     musicTable->insertRow(currentRow);
     musicTable->setItem(currentRow, 0, artistItem);
     musicTable->setItem(currentRow, 1, titleItem);
     musicTable->setItem(currentRow, 2, albumItem);
-    musicTable->setItem(currentRow, 3, yearItem);
 
 
     if (musicTable->selectedItems().isEmpty())
@@ -289,8 +375,8 @@ void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldSt
     else
     {
         musicTable->resizeColumnsToContents();
-        if (musicTable->columnWidth(0) > 300)
-            musicTable->setColumnWidth(0, 300);
+/*        if (musicTable->columnWidth(0) > 300)
+            musicTable->setColumnWidth(0, 300);*/
     }
 }
 
@@ -333,20 +419,21 @@ void MainWindow::finished()
 
 void MainWindow::setupActions()
 {
-    playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this);
+    playAction = new QAction(QIcon (QPixmap (":images/play")), tr("Play"), this);
     playAction->setShortcut(tr("Crl+P"));
     playAction->setDisabled(true);
-    pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this);
+    pauseAction = new QAction(QIcon (QPixmap (":images/pause")), tr("Pause"), this);
     pauseAction->setShortcut(tr("Ctrl+A"));
     pauseAction->setDisabled(true);
-    stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this);
+    pauseAction->setVisible(false);
+    stopAction = new QAction(QIcon (QPixmap (":images/stop")), tr("Stop"), this);
     stopAction->setShortcut(tr("Ctrl+S"));
     stopAction->setDisabled(true);
-    nextAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipForward), tr("Next"), this);
+    nextAction = new QAction(QIcon (QPixmap (":images/next")), tr("Next"), this);
     nextAction->setShortcut(tr("Ctrl+N"));
-    previousAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipBackward), tr("Previous"), this);
+    previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
     previousAction->setShortcut(tr("Ctrl+R"));
-    repeatAction = new QAction(style()->standardIcon(QStyle::SP_BrowserReload), tr("Repeat"), this);
+    repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
     repeatAction->setCheckable(true);
     repeatAction->setChecked(repeat);
     repeatAction->setShortcut(tr("Ctrl+I"));
@@ -383,11 +470,13 @@ void MainWindow::setupActions()
     connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
     connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
     connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
-    connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolders()));
+    connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolder()));
     connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
     connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
     connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
     connect (clearPlaylistAction, SIGNAL (triggered()), this, SLOT (clearPlaylist()));
+    connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
+    connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
@@ -395,7 +484,7 @@ void MainWindow::setupActions()
 
 void MainWindow::savePlaylist()
 {
-    QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "*.m3u");
+    QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u)");
     if (filename.isEmpty())
         return;
     if (filename.length() < 4 || filename.right(4).toLower() != ".m3u")
@@ -406,10 +495,10 @@ void MainWindow::savePlaylist()
         f.open(QFile::WriteOnly);
         for (int i = 0; i < sources.size(); ++i)
         {
-            if (sources[i].type() == Phonon::MediaSource::Stream)
-                f.write(sources[i].url().toString().toAscii());
-            else
+            if (sources[i].type() == Phonon::MediaSource::LocalFile)
                 f.write (sources[i].fileName().toAscii());
+            else
+                f.write(sources[i].url().toString().toAscii());
             f.write ("\n");
         }
         f.close ();
@@ -431,6 +520,11 @@ void MainWindow::loadPlaylist()
     clearPlaylist();
     foreach (QString l, lines)
     {
+        if (l.isEmpty() || (!QFileInfo (l).exists() && (l.indexOf("http") != 0)))
+        {
+            qDebug () << "not loadable: " << l;\
+            continue;
+        }
         qDebug () << "Load " << l;
         Phonon::MediaSource source(l);
         sources.append(source);
@@ -474,11 +568,11 @@ void MainWindow::setupMenus()
     fileMenu->addAction(addFilesAction);
     fileMenu->addAction(addFoldersAction);
     fileMenu->addAction(addUrlAction);
+    fileMenu->addSeparator();
     fileMenu->addAction(savePlaylistAction);
     fileMenu->addAction(loadPlaylistAction);
     fileMenu->addAction(clearPlaylistAction);
-    fileMenu->addSeparator();
-    fileMenu->addAction(exitAction);
+//    fileMenu->addAction(exitAction);
 
     QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
     aboutMenu->addAction(aboutAction);
@@ -490,6 +584,7 @@ void MainWindow::setupUi()
     QToolBar *bar = new QToolBar;
 
     bar->setOrientation(Qt::Vertical);
+    bar->setStyleSheet("padding:7px");
     //bar->addAction(volumeAction);
 
     seekSlider = new Phonon::SeekSlider(this);
@@ -498,7 +593,7 @@ void MainWindow::setupUi()
     volumeSlider = new Phonon::VolumeSlider(this);
     volumeSlider->setAudioOutput(audioOutput);
     volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
-    volumeSlider->setOrientation(Qt::Vertical);
+    volumeSlider->setOrientation(Qt::Horizontal);
     volumeSlider->setMuteVisible(false);
 //    volumeAddedAction = bar->addWidget(volumeSlider);
 //    volumeAddedAction->setVisible(false);
@@ -507,6 +602,8 @@ void MainWindow::setupUi()
     bar->addAction(stopAction);
     bar->addAction(repeatAction);
     bar->addAction(shuffleAction);
+    bar->addAction(nextAction);
+    bar->addAction(previousAction);
 
 /*    QLabel *volumeLabel = new QLabel;
     volumeLabel->setPixmap(QPixmap("images/volume.png"));*/
@@ -518,19 +615,23 @@ void MainWindow::setupUi()
 //    timeLcd->setPalette(palette);
 
     QStringList headers;
-    headers << tr("Artist") << tr("Title") << tr("Album") << tr("Year");
+    headers << tr("Artist") << tr("Title") << tr("Album");
 
-    musicTable = new QTableWidget(0, 4);
+    musicTable = new QTableWidget(0, 3);
     musicTable->setHorizontalHeaderLabels(headers);
     musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
     musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
     connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
         this, SLOT(tableClicked(int,int)));
+    connect(musicTable, SIGNAL(cellClicked(int,int)),
+        this, SLOT(cellClicked(int,int)));
+    musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
 
     QHBoxLayout *seekerLayout = new QHBoxLayout;
     QToolBar* bar2 = new QToolBar;
     bar2->addAction(volumeAction);
     seekerLayout->addWidget(bar2);
+    seekerLayout->addWidget(volumeSlider);
     seekerLayout->addWidget(seekSlider);
     seekerLayout->addWidget(timeLcd);
 
@@ -547,7 +648,6 @@ void MainWindow::setupUi()
     seekAndTableLayout->addLayout(seekerLayout);
 
     QHBoxLayout *mainLayout = new QHBoxLayout;
-    mainLayout->addWidget(volumeSlider);
     mainLayout->addLayout(seekAndTableLayout);
     mainLayout->addLayout(playbackLayout);
 
@@ -556,16 +656,41 @@ void MainWindow::setupUi()
 
     setCentralWidget(widget);
     setWindowTitle("TomAmp");
-//    ui.setupUi(this);
+    qDebug () << "cucc: " << musicTable->columnWidth(1);
 }
 
+void MainWindow::cellClicked(int row, int)
+{
+    if (mediaObject->state() == Phonon::PlayingState)
+    {
+        int index = sources.indexOf(mediaObject->currentSource());
+        if (index >= 0)
+        {
+            musicTable->selectRow(index);
+        }
+    }
+    else if (row < sources.size())
+    {
+        mediaObject->setCurrentSource(sources[row]);
+        shuffleList.removeAll(row);
+        shuffleList.insert(0, row);
+        qDebug () << shuffleList;
+    }
+}
 
 void MainWindow::setupShuffleList()
 {
     QList<int> tmp;
+    int index = sources.indexOf(mediaObject->currentSource());
+    if (index < 0)
+        index = 0;
     for (int i = 0; i < sources.size(); ++i)
-        tmp.append(i);
+    {
+        if (i != index)
+            tmp.append(i);
+    }
     shuffleList.clear();
+    shuffleList.append (index);
     while (tmp.size ())
     {
         int ind = qrand () % tmp.size();
@@ -573,4 +698,5 @@ void MainWindow::setupShuffleList()
         tmp.removeAt(ind);
     }
     qDebug () << shuffleList;
+    qDebug () << shuffleList;
 }