show options dialog
[tomamp] / mainwindow.cpp
index 1b17c7c..8bcd24f 100644 (file)
@@ -3,13 +3,17 @@
 #include <QInputDialog>
 
 #include "mainwindow.h"
+#include "optiondialog.h"
 #include "time.h"
 
 //#define AVOID_INPUT_DIALOG 0
 
 MainWindow::MainWindow()
-    : plman (this), settings (tr ("TomAmp"), "TomAmp")
+    : plman (this), settings (tr ("TomAmp"), "TomAmp"), isPlaying (false)
 {
+#ifdef Q_WS_MAEMO_5
+    setAttribute(Qt::WA_Maemo5AutoOrientation, true);
+#endif
     audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
     mediaObject = new Phonon::MediaObject(this);
 
@@ -22,6 +26,7 @@ MainWindow::MainWindow()
     connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
     connect (&plman, SIGNAL (playlistChanged (int)), this, SLOT (playlistChanged(int)));
     connect (&plman, SIGNAL (itemUpdated(int)), this, SLOT (itemUpdated (int)));
+    connect (&plman, SIGNAL (itemRemoved(int)), this, SLOT (itemRemoved (int)));
 
     Phonon::createPath(mediaObject, audioOutput);
 
@@ -32,18 +37,15 @@ MainWindow::MainWindow()
     setupActions();
     setupMenus();
     setupUi();
-    timeLcd->display("00:00");
+    show ();
+    timeLcd->display("00:00:00");
     plman.addStringList(settings.value("lastPlaylist").toStringList());
-//    shuffleList = settings.value("shuffleList", QList<int>()).toList();
-    QList<QVariant> tmp = settings.value("shuffleList").toList();
-    for (int i = 0; i < tmp.size (); ++i)
-        shuffleList.append (tmp[i].toInt());
-    if (!shuffleList.size())
-        setupShuffleList();
+    setupShuffleList();
     int curind = settings.value("currentIndex", -1).toInt ();
     if (curind >= 0)
-        setItem (curind);
+        setItem (curind, false);
     audioOutput->setVolume(settings.value("volume", .5).toReal());
+    QApplication::setWindowIcon(QIcon (QPixmap (":images/tomamp")));
 }
 
 MainWindow::~MainWindow()
@@ -53,10 +55,11 @@ MainWindow::~MainWindow()
     settings.setValue("lastPlaylist", plman.playlistStrings());
     settings.setValue("volume", audioOutput->volume());
     settings.setValue("currentIndex", plman.indexOf(mediaObject->currentSource()));
-    QList<QVariant> tmp;
-    for (int i = 0; i < shuffleList.size(); ++i)
-        tmp.append (i);
-    settings.setValue("shuffleList", tmp);
+    for (int i = 0; i < musicTable->columnCount(); ++i)
+    {
+        QString lab = QString ("colWidth_%1").arg (i);
+        settings.setValue(lab, musicTable->columnWidth(i));
+    }
 }
 
 void MainWindow::addFiles()
@@ -65,7 +68,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;
@@ -75,7 +78,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);
 }
@@ -89,11 +95,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;
@@ -108,6 +118,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);
@@ -120,12 +132,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:
@@ -142,20 +153,23 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
             next ();
             break;
         case Phonon::PlayingState:
-            setWindowTitle(mediaObject->metaData().value("TITLE") + " - TomAmp");
+            setWindowTitle(mediaObject->metaData().value("TITLE") + "(" + mediaObject->metaData().value("ARTIST") + ") - TomAmp");
             pauseAction->setVisible(true);
             playAction->setVisible (false);
             playAction->setEnabled(false);
             pauseAction->setEnabled(true);
             stopAction->setEnabled(true);
+            //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:
             pauseAction->setEnabled(false);
@@ -163,17 +177,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()));
-                mediaObject->play();
-            }
-            mediaObject->clearQueue();
             break;
         case Phonon::BufferingState:
-            qDebug () << "Buffering";
             break;
         default:
         ;
@@ -182,228 +187,166 @@ 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;
     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);
+            setItem (index, wasPlaying);
         }
         else if (repeat)
         {
             index = 0;
             while ((index) < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
             {
-                qDebug () << "Index increase 2a " << index;
                 index += 1;
             }
-            setItem (index);
+            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);
+            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);
+            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)
+void MainWindow::setItem(int i, bool doplay)
 {
-    int curInd = plman.indexOf(mediaObject->currentSource());
-    for (int j = 0; j < 3 && curInd >= 0 && curInd < shuffleList.size (); ++j)
-    {
-        QTableWidgetItem* item = musicTable->item(curInd, j);
-        if (item)
-        {
-            QFont font = item->font();
-            font.setBold(false);
-            font.setItalic(false);
-            item->setFont(font);
-        }
-    }
     if (i < plman.size() && i >= 0)
     {
+        if (lastPlayed >= 0)
+            unhighlightRow(lastPlayed);
         if (shuffle)
         {
-            for (int j = 0; j < 3; ++j)
-            {
-                QTableWidgetItem* item = musicTable->item(shuffleList[i], j);
-                if (item)
-                {
-                    QFont font = item->font();
-                    font.setBold(true);
-                    font.setItalic(true);
-                    item->setFont(font);
-                }
-            }
             mediaObject->setCurrentSource(plman.at (shuffleList[i]));
-            musicTable->selectRow (shuffleList[i]);
         }
         else
         {
-            for (int j = 0; j < 3; ++j)
-            {
-                QTableWidgetItem* item = musicTable->item(i, j);
-                if (item)
-                {
-                    QFont font = item->font();
-                    font.setBold(true);
-                    font.setItalic(true);
-                    item->setFont(font);
-                }
-            }
             mediaObject->setCurrentSource(plman.at(i));
-            musicTable->selectRow (i);
         }
     }
+    if (doplay && mediaObject->currentSource().type() != Phonon::MediaSource::Invalid)
+    {
+        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);
+            setItem (index, wasPlaying);
         }
         else if (repeat)
         {
             index = plman.size () - 1;
             while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
             {
-                qDebug () << "Index increase 2a " << index;
                 index--;
             }
-            setItem (index);
+            setItem (index, wasPlaying);
         }
-        if (index < 0)
-            wasPlaying = false;
+/*        if (index < 0)
+            wasPlaying = false;*/
 
     }
     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);
+            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);
+            setItem (index, wasPlaying);
         }
-        if (index < 0)
-            wasPlaying = false;
     }
-    if (wasPlaying)
-        mediaObject->play();
-    qDebug () << "wasPlaying: " << wasPlaying << ", playbutton visible: " << playAction->isVisible();
+}
 
-/*    bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
-    int index = plman.indexOf(mediaObject->currentSource()) - 1;
-    if (shuffle)
+void MainWindow::highlightRow (int i)
+{
+    for (int j = 0; j < 3; ++j)
     {
-        index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) - 1;
-        if (index >= 0)
-        {
-            mediaObject->setCurrentSource(plman.at (shuffleList[index]));
-        }
-        else if (repeat)
+        QTableWidgetItem* item = musicTable->item(i, j);
+        if (item)
         {
-            mediaObject->setCurrentSource(plman.at (shuffleList[shuffleList.size() - 1]));
+            QFont font = item->font();
+            font.setBold(true);
+            font.setItalic(true);
+            item->setFont(font);
         }
-
     }
-    else
+}
+
+void MainWindow::unhighlightRow (int i)
+{
+    for (int j = 0; j < 3; ++j)
     {
-        if (index >= 0)
-        {
-            mediaObject->setCurrentSource(plman.at(index));
-        }
-        else if (repeat)
+        QTableWidgetItem* item = musicTable->item(i, j);
+        if (item)
         {
-            mediaObject->setCurrentSource(plman.at(plman.size() - 1));
+            QFont font = item->font();
+            font.setBold(false);
+            font.setItalic(false);
+            item->setFont(font);
         }
     }
-    if (wasPlaying)
-        mediaObject->play();*/
-
 }
 
+
 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 */)
@@ -425,8 +368,8 @@ void MainWindow::tableClicked(int row, int /* column */)
     {
         if (shuffle)
             index = shuffleList.indexOf(index);
-        setItem (index);
-        mediaObject->play();
+        setItem (index, true);
+//        mediaObject->play();
     }
     else
     {
@@ -439,8 +382,8 @@ void MainWindow::tableClicked(int row, int /* column */)
         {
             if (shuffle)
                 index = shuffleList.indexOf(index);
-            setItem (index);
-            mediaObject->play();
+            setItem (index, true);
+//            mediaObject->play();
         }
     }
 
@@ -448,14 +391,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)
     {
@@ -475,46 +421,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);
@@ -529,55 +478,100 @@ void MainWindow::setupActions()
     loadPlaylistAction->setShortcut(tr("Ctrl+L"));
     clearPlaylistAction = new QAction (tr("&Clear Playlist"), this);
     clearPlaylistAction->setShortcut(tr("Ctrl+C"));
+    optionAction = new QAction (tr("Op&tions"), this);
+    optionAction->setShortcut(tr("Ctrl+T"));
     exitAction = new QAction(tr("E&xit"), this);
     exitAction->setShortcut(tr("Ctrl+X"));
     aboutAction = new QAction(tr("A&bout"), this);
     aboutAction->setShortcut(tr("Ctrl+B"));
     aboutQtAction = new QAction(tr("About &Qt"), this);
     aboutQtAction->setShortcut(tr("Ctrl+Q"));
+/*    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()));
+
     connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
     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()), &plman, SLOT (clearPlaylist()));
+    connect (optionAction, SIGNAL (triggered()), &plman, SLOT (showOptions()));
     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()));
+//    connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
 }
 
+void MainWindow::removeSelectedItem()
+{
+    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);
+}
+
+void MainWindow::removeAllButSelectedItem()
+{
+    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)
+    {
+        QString uri = plman.getItem(row).uri;
+        QStringList lst;
+        lst << uri;
+        plman.clearPlaylist();
+        plman.addStringList(lst);
+    }
+}
 
 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;
 }
 
 
@@ -591,6 +585,7 @@ void MainWindow::setupMenus()
     fileMenu->addAction(savePlaylistAction);
     fileMenu->addAction(loadPlaylistAction);
     fileMenu->addAction(clearPlaylistAction);
+    fileMenu->addAction(optionAction);
 //    fileMenu->addAction(exitAction);
 
     QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
@@ -624,19 +619,19 @@ void MainWindow::setupUi()
     bar->addAction(nextAction);
     bar->addAction(previousAction);
 
-/*    QLabel *volumeLabel = new QLabel;
-    volumeLabel->setPixmap(QPixmap("images/volume.png"));*/
+    contextMenu = new QMenu (this);
+    removeSelected = contextMenu->addAction(tr ("Remove selected"));
+    removeAllButSelected = contextMenu->addAction(tr("Remove all but selected"));
+    connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
+    connect (removeAllButSelected, SIGNAL (triggered()), this, SLOT (removeAllButSelectedItem()));
 
-/*    QPalette palette;
-    palette.setBrush(QPalette::Light, Qt::darkGray);*/
 
     timeLcd = new QLCDNumber;
-//    timeLcd->setPalette(palette);
 
     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);
@@ -644,7 +639,22 @@ void MainWindow::setupUi()
         this, SLOT(tableClicked(int,int)));
     connect(musicTable, SIGNAL(cellClicked(int,int)),
         this, SLOT(cellClicked(int,int)));
-    musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
+/*    for (int i = 0; i < 3; ++i)
+    {
+        if (!musicTable->horizontalHeaderItem(i))
+            continue;
+        musicTable->horizontalHeaderItem(i)->setBackgroundColor(QColor (128, 128, 255));;
+        musicTable->horizontalHeaderItem(i)->setForeground(QColor (255, 255, 255));
+    }*/
+    for (int i = 0; i < musicTable->columnCount(); ++i)
+    {
+        QString lab = QString ("colWidth_%1").arg (i);
+        int val = settings.value(lab, 0).toInt();
+        if (val)
+            musicTable->setColumnWidth(i, val);
+//        settings.setValue(lab, musicTable->columnWidth(i));
+    }
+
 
     QHBoxLayout *seekerLayout = new QHBoxLayout;
     QToolBar* bar2 = new QToolBar;
@@ -657,9 +667,6 @@ void MainWindow::setupUi()
     QVBoxLayout *playbackLayout = new QVBoxLayout;
     volumeSlider->hide ();
     playbackLayout->addWidget(bar);
-//    playbackLayout->addStretch();
-//    playbackLayout->addWidget(volumeSlider);
-//    playbackLayout->addWidget(volumeLabel);
 
     QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
 
@@ -675,28 +682,22 @@ void MainWindow::setupUi()
 
     setCentralWidget(widget);
     setWindowTitle("TomAmp");
-    qDebug () << "cucc: " << musicTable->columnWidth(1);
 }
 
 void MainWindow::cellClicked(int /*row*/, int)
 {
-    /*if (mediaObject->state() == Phonon::PlayingState)
-    {
-        int index = plman.indexOf(mediaObject->currentSource());
-        if (index >= 0)
-        {
-            musicTable->selectRow(index);
-        }
-    }
-    else if (row < plman.size())
-    {
-        mediaObject->setCurrentSource(plman.at(row));
-        shuffleList.removeAll(row);
-        shuffleList.insert(0, row);
-        qDebug () << shuffleList;
-    }*/
 }
 
+void MainWindow::contextMenuEvent (QContextMenuEvent*e)
+{
+    if (!childAt (e->pos()))
+        return;
+    if (childAt (e->pos())->parentWidget() != musicTable)
+        return;
+    contextMenu->popup(e->globalPos());
+}
+
+
 void MainWindow::setupShuffleList()
 {
     QList<int> tmp;
@@ -716,19 +717,21 @@ void MainWindow::setupShuffleList()
         shuffleList.append(tmp[ind]);
         tmp.removeAt(ind);
     }
-    qDebug () << shuffleList;
-    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);
 }
 
@@ -738,12 +741,19 @@ void MainWindow::playlistChanged(int from)
     {
         musicTable->removeRow(musicTable->rowCount () - 1);
     }
+    int firstGood = -1;
     for (int i = from; i < plman.size (); ++i)
     {
+        if (firstGood < 0 && plman.getItem (i).playable)
+            firstGood = i;
         int currentRow = musicTable->rowCount();
         musicTable->insertRow(currentRow);
         setRowFromItem (currentRow, plman.getItem(i));
     }
+/*    if (plman.indexOf(mediaObject->currentSource()) < 0)
+    {
+        setItem (firstGood, false);
+    }*/
     setupShuffleList();
 }
 
@@ -769,11 +779,98 @@ void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
         item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
         musicTable->setItem(row, 2, item3);
     }
+
+    if (!musicTable->cellWidget(row, 3))
+    {
+        QToolBar* bar = new QToolBar;
+        QLabel* up = new QLabel;
+        up->setText(QString::fromUtf8("<b><a href='up'>▲</a></b>"));
+        up->setStyleSheet("padding-right:3px;");
+        up->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+        bar->addWidget(up);
+        QLabel* down = new QLabel;
+        down->setText(QString::fromUtf8("<b><a href='down'>▼</a></b>"));
+        down->setStyleSheet("padding-right:3px;");
+        bar->addWidget(down);
+        QLabel* del = new QLabel;
+        del->setText(QString::fromUtf8("<b><a href='del'>╳</a></b>"));
+        del->setStyleSheet("padding-right:3px;");
+        bar->addWidget(del);
+        down->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+        bar->setProperty("row", row);
+        musicTable->setCellWidget(row, 3, bar);
+        connect (up, SIGNAL (linkActivated (const QString&)),  this, SLOT (buttonUp ()));
+        connect (down, SIGNAL (linkActivated (const QString&)),  this, SLOT (buttonDown ()));
+        connect (del, SIGNAL (linkActivated (const QString&)),  this, SLOT (buttonDel ()));
+    }
+}
+
+void MainWindow::buttonUp()
+{
+    int i = sender()->parent()->property("row").toInt();
+    qDebug () << "Presses up on " << i;
+    if (i)
+    {
+        plman.moveItemUp(i);
+        setRowFromItem (i, plman.getItem(i));
+        setRowFromItem (i - 1, plman.getItem(i - 1));
+        musicTable->cellWidget(i, 3)->setProperty("row", i);
+        musicTable->cellWidget(i - 1, 3)->setProperty("row", i - 1);
+        musicTable->selectRow(i - 1);
+    }
+}
+
+void MainWindow::buttonDown()
+{
+    int i = sender()->parent()->property("row").toInt();
+    qDebug () << "Presses down on " << i;
+    if (i < plman.size() - 1)
+    {
+        plman.moveItemDown(i);
+        setRowFromItem (i, plman.getItem(i));
+        setRowFromItem (i + 1, plman.getItem(i + 1));
+        musicTable->cellWidget(i, 3)->setProperty("row", i);
+        musicTable->cellWidget(i + 1, 3)->setProperty("row", i + 1);
+        musicTable->selectRow(i + 1);
+    }
+}
+
+void MainWindow::buttonDel()
+{
+    int i = sender()->parent()->property("row").toInt();
+    qDebug () << "Presses del on " << i;
+    if (i < plman.size())
+    {
+        plman.removeItem(i);
+    }
 }
 
 void MainWindow::itemUpdated(int index)
 {
-    setRowFromItem (index, plman.getItem(index));
     if (plman.indexOf(mediaObject->currentSource()) < 0 && plman.getItem (index).playable)
-        mediaObject->setCurrentSource(plman.getItem(index).source);
+    {
+        setItem (index, false);
+    }
+    setRowFromItem (index, plman.getItem(index));
+    if (plman.indexOf(mediaObject->currentSource()) == index)
+    {
+        if (shuffle) index = shuffleList.indexOf(index);
+        setItem (index, false);
+    }
+}
+
+void MainWindow::itemRemoved (int i)
+{
+    musicTable->removeRow(i);
+    for (int j = i ? (i - 1) : 0; j < musicTable->rowCount(); ++j)
+    {
+        if (musicTable->cellWidget(j, 3))
+            musicTable->cellWidget(j, 3)->setProperty("row", j);
+    }
+}
+
+void MainWindow::showOptions ()
+{
+    OptionDialog dlg (this, settings);
+    dlg.show();
 }