added install code to .pro
[tomamp] / mainwindow.cpp
index 78fc6bb..78d23b0 100644 (file)
@@ -8,7 +8,7 @@
 //#define AVOID_INPUT_DIALOG 0
 
 MainWindow::MainWindow()
-    : plman (this), settings (tr ("TomAmp"), "TomAmp")
+    : plman (this), settings (tr ("TomAmp"), "TomAmp"), isPlaying (false)
 {
     audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
     mediaObject = new Phonon::MediaObject(this);
@@ -32,13 +32,14 @@ MainWindow::MainWindow()
     setupActions();
     setupMenus();
     setupUi();
-    timeLcd->display("00:00");
+    timeLcd->display("00:00:00");
     plman.addStringList(settings.value("lastPlaylist").toStringList());
     setupShuffleList();
     int curind = settings.value("currentIndex", -1).toInt ();
     if (curind >= 0)
         setItem (curind, false);
     audioOutput->setVolume(settings.value("volume", .5).toReal());
+    QApplication::setWindowIcon(QIcon (QPixmap (":images/tomamp")));
 }
 
 MainWindow::~MainWindow()
@@ -61,7 +62,7 @@ void MainWindow::addFiles()
     if (folder.isEmpty())
         folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
     QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Files To Add"),
-                    folder);
+                    folder, "Music files (*.mp3 *.ogg *.wav *.flac);;Playlists (*.m3u *.pls)");
 
     if (files.isEmpty())
         return;
@@ -71,7 +72,10 @@ void MainWindow::addFiles()
     QStringList toadd;
     foreach (QString string, files)
     {
-        toadd.append (string);
+        if (string.toLower().endsWith(".pls") || string.toLower().endsWith(".m3u"))
+            plman.addPlaylist(string);
+        else
+            toadd.append (string);
     }
     plman.addStringList(toadd);
 }
@@ -85,11 +89,15 @@ void MainWindow::addFolder()
             tr("Select Directory To Add"),
             folder);
 
+    if (dir.isEmpty())
+        return;
+
+    settings.setValue("LastFolder", dir);
+
     QStringList filters;
     QStringList files = QDir (dir).entryList(filters, QDir::AllDirs);
     files.removeAll(".");
     files.removeAll("..");
-    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;
@@ -104,6 +112,8 @@ void MainWindow::addUrl()
 #else
     QString url = QInputDialog::getText(this, "Get URL", "Please type in the stream URL");
 #endif
+    if (url.isEmpty() || !url.toLower().startsWith("http"))
+        return;
     QStringList toadd;
     toadd << url;
     plman.addStringList(toadd);
@@ -116,12 +126,11 @@ void MainWindow::about()
         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)"));
+        "Icons by http://itweek.deviantart.com/"));
 }
 
 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
 {
-    qDebug () << "State: " << newState;
     switch (newState)
     {
         case Phonon::ErrorState:
@@ -144,15 +153,16 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
             playAction->setEnabled(false);
             pauseAction->setEnabled(true);
             stopAction->setEnabled(true);
-            lastPlayed = plman.indexOf(mediaObject->currentSource());
+            //lastPlayed = plman.indexOf(mediaObject->currentSource());
             break;
         case Phonon::StoppedState:
+            setWindowTitle("TomAmp");
             stopAction->setEnabled(false);
             playAction->setEnabled(true);
             pauseAction->setVisible(false);
             playAction->setVisible(true);
             pauseAction->setEnabled(false);
-            timeLcd->display("00:00");
+            timeLcd->display("00:00:00");
             unhighlightRow(plman.indexOf(mediaObject->currentSource()));
             break;
         case Phonon::PausedState:
@@ -161,18 +171,8 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
             pauseAction->setVisible(false);
             playAction->setVisible(true);
             playAction->setEnabled(true);
-            qDebug () << "Queue size: " << mediaObject->queue().size ();
-            if (mediaObject->queue().size ())
-            {
-/*                mediaObject->setCurrentSource(mediaObject->queue()[0]);
-                musicTable->selectRow(plman.indexOf(mediaObject->currentSource()));*/
-                setItem (plman.indexOf(mediaObject->queue()[0]), true);
-//                mediaObject->play();
-            }
-            mediaObject->clearQueue();
             break;
         case Phonon::BufferingState:
-            qDebug () << "Buffering";
             break;
         default:
         ;
@@ -181,24 +181,17 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
 
 void MainWindow::next()
 {
-    bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
+    bool wasPlaying = isPlaying;
     if (mediaObject->state () == Phonon::ErrorState)
         wasPlaying = true;
-    qDebug () << "NEXT, repeat=" << repeat << ", shuffle=" << shuffle << ", ShuffleLis size=" << shuffleList.size ();
-    if (shuffleList.size() != plman.size())
-        qDebug () << "WHOA STRANGE SHUFFLE: " << shuffleList;
     int index = plman.indexOf(mediaObject->currentSource());
-    qDebug () << "Current index is " << index;
     if (shuffle)
     {
-        qDebug () << "Shuffle next " << index;
         index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
         while (index < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
         {
             index += 1;
-            qDebug () << "Index increase a " << index;
         }
-        qDebug () << "Shuffle next 2 " << index;
         if (index < shuffleList.size ())
         {
             setItem (index, wasPlaying);
@@ -208,46 +201,32 @@ void MainWindow::next()
             index = 0;
             while ((index) < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
             {
-                qDebug () << "Index increase 2a " << index;
                 index += 1;
             }
             setItem (index, wasPlaying);
         }
-/*        if (index >= shuffleList.size ())
-            wasPlaying = false;*/
-
     }
     else
     {
         index++;
-        qDebug () << "Normal next";
         while ((index) < plman.size () && !plman.getItem(index).playable)
         {
             index += 1;
-            qDebug () << "Index increase " << index;
         }
-        qDebug () << "Normal next 2 " << index;
         if (index < plman.size())
         {
             setItem (index, wasPlaying);
         }
         else if (repeat)
         {
-            qDebug () << "Repeat on";
             index = 0;
             while ((index) < plman.size () && !plman.getItem(index).playable)
             {
                 index += 1;
-                qDebug () << "Index increase " << index;
             }
             setItem (index, wasPlaying);
         }
-/*        if (index >= shuffleList.size ())
-            wasPlaying = false;*/
     }
-/*    if (wasPlaying)
-        mediaObject->play();*/
-    qDebug () << "wasPlaying: " << wasPlaying << ", playbutton visible: " << playAction->isVisible();
 }
 
 void MainWindow::setItem(int i, bool doplay)
@@ -258,44 +237,34 @@ void MainWindow::setItem(int i, bool doplay)
             unhighlightRow(lastPlayed);
         if (shuffle)
         {
-            qDebug () << "SetItem with shuffle, index = " << i << " real index is " << shuffleList[i];
-            qDebug () << "ShuffleList: " << shuffleList;
-            if (doplay)
-                highlightRow(shuffleList[i]);
             mediaObject->setCurrentSource(plman.at (shuffleList[i]));
-//            musicTable->selectRow (shuffleList[i]);
         }
         else
         {
-            qDebug () << "SetItem without shuffle, index = " << i;
-            if (doplay)
-                highlightRow(i);
             mediaObject->setCurrentSource(plman.at(i));
-//            musicTable->selectRow (i);
         }
     }
     if (doplay && mediaObject->currentSource().type() != Phonon::MediaSource::Invalid)
-        mediaObject->play();
+    {
+        play();
+    }
+    else
+        stop ();
 }
 
 void MainWindow::previous()
 {
-    bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
+    bool wasPlaying = isPlaying;//(mediaObject->state () == Phonon::PlayingState);
     if (mediaObject->state () == Phonon::ErrorState)
         wasPlaying = true;
-    qDebug () << "PREVIOUS, repeat=" << repeat << ", shuffle=" << shuffle;
     int index = plman.indexOf(mediaObject->currentSource());
-    qDebug () << "Current index is " << index;
     if (shuffle)
     {
-        qDebug () << "Shuffle next";
         index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) - 1;
         while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
         {
             index--;
-            qDebug () << "Index increase a " << index;
         }
-        qDebug () << "Shuffle next 2 " << index;
         if (index >= 0)
         {
             setItem (index, wasPlaying);
@@ -305,7 +274,6 @@ void MainWindow::previous()
             index = plman.size () - 1;
             while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
             {
-                qDebug () << "Index increase 2a " << index;
                 index--;
             }
             setItem (index, wasPlaying);
@@ -317,36 +285,24 @@ void MainWindow::previous()
     else
     {
         index--;
-        qDebug () << "Normal next";
         while ((index) >= 0 && !plman.getItem(index).playable)
         {
             index--;
-            qDebug () << "Index increase " << index;
         }
-        qDebug () << "Normal next 2 " << index;
         if (index >= 0)
         {
             setItem (index, wasPlaying);
         }
         else if (repeat)
         {
-            qDebug () << "Repeat on";
             index = plman.size() - 1;
             while ((index) >= 0 && !plman.getItem(index).playable)
             {
                 index--;
-                qDebug () << "Index increase " << index;
             }
             setItem (index, wasPlaying);
         }
-/*        if (index < 0)
-            wasPlaying = false;*/
     }
-/*    if (wasPlaying)
-        mediaObject->play();*/
-    qDebug () << "wasPlaying: " << wasPlaying << ", playbutton visible: " << playAction->isVisible();
-
-
 }
 
 void MainWindow::highlightRow (int i)
@@ -382,9 +338,9 @@ void MainWindow::unhighlightRow (int i)
 
 void MainWindow::tick(qint64 time)
 {
-    QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
+    QTime displayTime((time / 3600000), (time / 60000) % 60, (time / 1000) % 60);
 
-    timeLcd->display(displayTime.toString("mm:ss"));
+    timeLcd->display(displayTime.toString("HH:mm:ss"));
 }
 
 void MainWindow::tableClicked(int row, int /* column */)
@@ -429,14 +385,17 @@ void MainWindow::tableClicked(int row, int /* column */)
 
 void MainWindow::sourceChanged(const Phonon::MediaSource &source)
 {
-    musicTable->selectRow(plman.indexOf(source));
-    timeLcd->display("00:00");
+    int ind = plman.indexOf(source);
+    highlightRow(ind);
+    unhighlightRow(lastPlayed);
+    lastPlayed = ind;
+    musicTable->selectRow(ind);
+    timeLcd->display("00:00:00");
 }
 
 
 void MainWindow::aboutToFinish()
 {
-    qDebug () << "Abouttotfinish";
     int index = plman.indexOf(mediaObject->currentSource()) + 1;
     if (shuffle)
     {
@@ -456,46 +415,49 @@ void MainWindow::aboutToFinish()
         if (plman.size() > index)
         {
             mediaObject->enqueue(plman.at(index));
-            qDebug () << "Enqueue " << index << " pfm " << mediaObject->prefinishMark();
         }
         else if (repeat)
         {
             mediaObject->enqueue(plman.at(0));
-            qDebug () << "Enqueue " << 0 << " pfm " << mediaObject->prefinishMark();
         }
     }
 }
 
 void MainWindow::finished()
 {
-    qDebug () << "Finished";
 }
 
 void MainWindow::setupActions()
 {
-    playAction = new QAction(QIcon (QPixmap (":images/play")), tr("Play"), this);
+    playAction = new QAction(QIcon (QPixmap (":images/play")), "", this);
     playAction->setShortcut(tr("Crl+P"));
     playAction->setDisabled(true);
-    pauseAction = new QAction(QIcon (QPixmap (":images/pause")), tr("Pause"), this);
+    pauseAction = new QAction(QIcon (QPixmap (":images/pause")), "", this);
     pauseAction->setShortcut(tr("Ctrl+A"));
     pauseAction->setDisabled(true);
     pauseAction->setVisible(false);
-    stopAction = new QAction(QIcon (QPixmap (":images/stop")), tr("Stop"), this);
+    stopAction = new QAction(QIcon (QPixmap (":images/stop")), "", this);
     stopAction->setShortcut(tr("Ctrl+S"));
     stopAction->setDisabled(true);
-    nextAction = new QAction(QIcon (QPixmap (":images/next")), tr("Next"), this);
+    nextAction = new QAction(QIcon (QPixmap (":images/next")), "", this);
     nextAction->setShortcut(tr("Ctrl+N"));
-    previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
+    previousAction = new QAction(QIcon (QPixmap (":images/previous")), "", this);
     previousAction->setShortcut(tr("Ctrl+R"));
-    repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
+    if (repeat)
+        repeatAction = new QAction(QIcon (QPixmap (":images/repeatActive")), "", this);
+    else
+        repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), "", this);
     repeatAction->setCheckable(true);
     repeatAction->setChecked(repeat);
     repeatAction->setShortcut(tr("Ctrl+I"));
-    shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
+    if (shuffle)
+        shuffleAction = new QAction(QIcon (QPixmap (":images/shuffleActive")), "", this);
+    else
+        shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), "", this);
     shuffleAction->setCheckable(true);
     shuffleAction->setChecked(shuffle);
     shuffleAction->setShortcut(tr("Ctrl+H"));
-    volumeAction = new QAction(QIcon (QPixmap (":images/volume")), tr("Volume"), this);
+    volumeAction = new QAction(QIcon (QPixmap (":images/volume")), "", this);
     volumeAction->setCheckable(true);
     volumeAction->setShortcut(tr("Ctrl+V"));
     addFilesAction = new QAction(tr("Add &File"), this);
@@ -519,9 +481,9 @@ void MainWindow::setupActions()
 /*    removeSelected = new QAction (tr("&Delete from playlist"));
     removeSelected->setShortcut(tr ("Ctrl+D"));*/
 
-    connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
+    connect(playAction, SIGNAL(triggered()), this, SLOT(play()));
     connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
-    connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
+    connect(stopAction, SIGNAL(triggered()), this, SLOT(stop()));
     connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
     connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
     connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
@@ -542,7 +504,8 @@ void MainWindow::setupActions()
 
 void MainWindow::removeSelectedItem()
 {
-    qDebug () << "Remove Selected!";
+    if (QMessageBox::question(this, "Confirm remove", "Are you sure you want to remove this item?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
+        return;
     int row = musicTable->currentRow();
     if (row >= 0)
         plman.removeItem(row);
@@ -550,7 +513,8 @@ void MainWindow::removeSelectedItem()
 
 void MainWindow::removeAllButSelectedItem()
 {
-    qDebug () << "Remove Selected!";
+    if (QMessageBox::question(this, "Confirm remove", "Are you sure you want to remove all other items?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
+        return;
     int row = musicTable->currentRow();
     if (row >= 0)
     {
@@ -565,25 +529,40 @@ void MainWindow::removeAllButSelectedItem()
 void MainWindow::repeatToggle ()
 {
     repeat = !repeat;
-    qDebug() << "Repeat toggled to " << repeat;
     settings.setValue("repeat", QVariant (repeat));
+    if (repeat)
+        repeatAction->setIcon(QIcon (QPixmap (":images/repeatActive")));
+    else
+        repeatAction->setIcon(QIcon (QPixmap (":images/repeat")));
 }
 
 void MainWindow::shuffleToggle ()
 {
     shuffle = !shuffle;
     settings.setValue("shuffle", QVariant (shuffle));
+    if (shuffle)
+        shuffleAction->setIcon(QIcon (QPixmap (":images/shuffleActive")));
+    else
+        shuffleAction->setIcon(QIcon (QPixmap (":images/shuffle")));
 }
 
 void MainWindow::volumeToggle ()
 {
-    qDebug () << "Volumetoggle: " << volumeAction->isChecked();
     volumeSlider->setVisible(volumeAction->isChecked());
 }
 
 void MainWindow::play()
 {
+    mediaObject->play();
+    lastPlayed = plman.indexOf(mediaObject->currentSource());
+    highlightRow(lastPlayed);
+    isPlaying = true;
+}
 
+void MainWindow::stop()
+{
+    mediaObject->stop();
+    isPlaying = false;
 }
 
 
@@ -640,9 +619,9 @@ void MainWindow::setupUi()
     timeLcd = new QLCDNumber;
 
     QStringList headers;
-    headers << tr("Artist") << tr("Title") << tr("Album");
+    headers << tr("Artist") << tr("Title") << tr("Album") << "Controls";
 
-    musicTable = new QTableWidget(0, 3);
+    musicTable = new QTableWidget(0, 4);
     musicTable->setHorizontalHeaderLabels(headers);
     musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
     musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
@@ -693,7 +672,6 @@ void MainWindow::setupUi()
 
     setCentralWidget(widget);
     setWindowTitle("TomAmp");
-    qDebug () << "cucc: " << musicTable->columnWidth(1);
 }
 
 void MainWindow::cellClicked(int /*row*/, int)
@@ -702,7 +680,10 @@ void MainWindow::cellClicked(int /*row*/, int)
 
 void MainWindow::contextMenuEvent (QContextMenuEvent*e)
 {
-    qDebug () << "Context menu event!";
+    if (!childAt (e->pos()))
+        return;
+    if (childAt (e->pos())->parentWidget() != musicTable)
+        return;
     contextMenu->popup(e->globalPos());
 }
 
@@ -726,18 +707,21 @@ void MainWindow::setupShuffleList()
         shuffleList.append(tmp[ind]);
         tmp.removeAt(ind);
     }
-    qDebug () << shuffleList;
 }
 
 void MainWindow::savePlaylist ()
 {
-    QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u)");
-    plman.loadPlaylist(filename);
+    QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u *.pls)");
+    if (filename.isEmpty())
+        return;
+    plman.savePlaylist(filename);
 }
 
 void MainWindow::loadPlaylist ()
 {
-    QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u");
+    QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u *.pls");
+    if (filename.isEmpty())
+        return;
     plman.loadPlaylist (filename);
 }
 
@@ -756,10 +740,10 @@ void MainWindow::playlistChanged(int from)
         musicTable->insertRow(currentRow);
         setRowFromItem (currentRow, plman.getItem(i));
     }
-    if (plman.indexOf(mediaObject->currentSource()) < 0)
+/*    if (plman.indexOf(mediaObject->currentSource()) < 0)
     {
         setItem (firstGood, false);
-    }
+    }*/
     setupShuffleList();
 }
 
@@ -785,6 +769,25 @@ void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
         item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
         musicTable->setItem(row, 2, item3);
     }
+    qDebug () << "Widget: " << musicTable->cellWidget(row, 3);
+
+    if (!musicTable->cellWidget(row, 3))
+    {
+        QToolBar* bar = new QToolBar;
+        QPushButton* up = new QPushButton;
+        up->setText("up");
+        up->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+        bar->setProperty("row", row);
+        bar->addWidget(up);
+        musicTable->setCellWidget(row, 3, bar);
+        connect (up, SIGNAL (clicked ()),  this, SLOT (buttonUp ()));
+    }
+}
+
+void MainWindow::buttonUp()
+{
+    qDebug () << "Presses up on " << sender()->parent()->property("row");
+    plman.moveItemUp(sender()->parent()->property("row").toInt());
 }
 
 void MainWindow::itemUpdated(int index)