Merge branch 'master' of https://vcs.maemo.org/git/tomamp
[tomamp] / tomamp / mainwindow.cpp
index f2a3cd0..15bf4e0 100644 (file)
@@ -13,7 +13,6 @@
 MainWindow::MainWindow()
     : plman (this), settings (tr ("TomAmp"), "TomAmp"), isPlaying (false)
 {
-    setOrientation();
     audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
     mediaObject = new Phonon::MediaObject(this);
 
@@ -33,11 +32,26 @@ MainWindow::MainWindow()
     qsrand (time (NULL));
     repeat = settings.value("repeat", false).toBool();
     shuffle = settings.value("shuffle", false).toBool();
-    headers = settings.value ("headers", QStringList()).toStringList();
+    QStringList allowedHeaders;
+    allowedHeaders << "Artist" << "Title" << "Album" << "Controls";
+    foreach (QString h, settings.value ("headers", QStringList()).toStringList())
+    {
+        if (allowedHeaders.indexOf(h) >= 0)
+            headers << h;
+    }
+    if (!headers.size())
+        headers << "Artist" << "Title" << "Album";
+    
     setupShuffleList();
     setupActions();
     setupMenus();
-    setupUi();
+/*    foreach (QString s, Phonon::BackendCapabilities::availableMimeTypes())
+        qDebug () << s;*/
+    
+/*    if (settings.value("uiflipped", false).toBool())
+        setupUiFlipped();
+    else*/
+    setupUi ();
     show ();
     timeLcd->display("00:00:00");
     plman.addStringList(settings.value("lastPlaylist").toStringList());
@@ -47,6 +61,7 @@ MainWindow::MainWindow()
         setItem (curind, false);
     audioOutput->setVolume(settings.value("volume", .5).toReal());
     QApplication::setWindowIcon(QIcon (QPixmap (":images/tomamp")));
+    setOrientation();
 }
 
 MainWindow::~MainWindow()
@@ -69,11 +84,17 @@ void MainWindow::setOrientation ()
 #ifdef Q_WS_MAEMO_5
     QString orient = settings.value("orientation", "Automatic").toString();
     if (orient == "Portrait")
+    {
         setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
+    }
     else if (orient == "Landscape")
+    {
         setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
+    }
     else
+    {
         setAttribute(Qt::WA_Maemo5AutoOrientation, true);
+    }
 #endif
 }
 
@@ -82,8 +103,10 @@ void MainWindow::addFiles()
     QString folder = settings.value("LastFolder").toString();
     if (folder.isEmpty())
         folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
+    QString ext = "*." + plman.allowedExt().join(" *.");
+    ext = "Music files (" + ext + ");;Playlists (*.m3u *.pls)";
     QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Files To Add"),
-                    folder, "Music files (*.mp3 *.ogg *.wav *.flac);;Playlists (*.m3u *.pls)");
+                                                      folder, ext, 0, QFileDialog::DontUseNativeDialog);
 
     if (files.isEmpty())
         return;
@@ -143,11 +166,12 @@ void MainWindow::addUrl()
 
 void MainWindow::about()
 {
-    QMessageBox::information(this, tr("About TomAmp v0.1"),
+    QMessageBox::information(this, tr("About TomAmp v0.2"),
         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 http://itweek.deviantart.com/"));
+        "Icons by http://itweek.deviantart.com/\n\n"
+        "Special thanks to Attila Csipa"));
 }
 
 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
@@ -174,7 +198,10 @@ void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState
             playAction->setEnabled(false);
             pauseAction->setEnabled(true);
             stopAction->setEnabled(true);
-            //lastPlayed = plman.indexOf(mediaObject->currentSource());
+            unhighlightRow(lastPlayed);
+            lastPlayed = plman.indexOf(mediaObject->currentSource());
+            highlightRow(plman.indexOf(mediaObject->currentSource()));
+            musicTable->selectRow(plman.indexOf(mediaObject->currentSource()));
             break;
         case Phonon::StoppedState:
             setWindowTitle("TomAmp");
@@ -205,6 +232,12 @@ void MainWindow::next()
     bool wasPlaying = isPlaying;
     if (mediaObject->state () == Phonon::ErrorState)
         wasPlaying = true;
+    if (mediaObject->queue().size())
+    {
+        setItem (plman.indexOf(mediaObject->queue()[0]), wasPlaying);
+        mediaObject->queue().clear();
+        return;
+    }
     int index = plman.indexOf(mediaObject->currentSource());
     if (shuffle)
     {
@@ -254,8 +287,8 @@ void MainWindow::setItem(int i, bool doplay)
 {
     if (i < plman.size() && i >= 0)
     {
-        if (lastPlayed >= 0)
-            unhighlightRow(lastPlayed);
+/*        if (lastPlayed >= 0)
+            unhighlightRow(lastPlayed);*/
         if (shuffle)
         {
             mediaObject->setCurrentSource(plman.at (shuffleList[i]));
@@ -483,6 +516,7 @@ void MainWindow::setupActions()
     shuffleAction->setCheckable(true);
     shuffleAction->setChecked(shuffle);
     shuffleAction->setShortcut(tr("Ctrl+H"));
+    enqueueActionButton = new QAction (tr ("E"), this);
     volumeAction = new QAction(QIcon (QPixmap (":images/volume")), "", this);
     volumeAction->setCheckable(true);
     volumeAction->setShortcut(tr("Ctrl+V"));
@@ -515,6 +549,7 @@ void MainWindow::setupActions()
     connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
     connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
     connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
+    connect(enqueueActionButton, SIGNAL(triggered()), this, SLOT(enqueueSelected()));
 
     connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
     connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolder()));
@@ -586,7 +621,7 @@ void MainWindow::volumeToggle ()
 void MainWindow::play()
 {
     mediaObject->play();
-    lastPlayed = plman.indexOf(mediaObject->currentSource());
+//    lastPlayed = plman.indexOf(mediaObject->currentSource());
     highlightRow(lastPlayed);
     isPlaying = true;
 }
@@ -619,21 +654,27 @@ void MainWindow::setupMenus()
 void MainWindow::setupUi()
 {
     QToolBar *bar = new QToolBar;
+    bool flip = settings.value("uiflipped", false).toBool();
 
-    bar->setOrientation(Qt::Vertical);
+    if(!flip) bar->setOrientation(Qt::Vertical);
     bar->setStyleSheet("padding:7px");
-    //bar->addAction(volumeAction);
 
     seekSlider = new Phonon::SeekSlider(this);
     seekSlider->setMediaObject(mediaObject);
+    if (flip) seekSlider->setOrientation(Qt::Vertical);
 
     volumeSlider = new Phonon::VolumeSlider(this);
     volumeSlider->setAudioOutput(audioOutput);
-    volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
-    volumeSlider->setOrientation(Qt::Horizontal);
+    if (flip)
+    {
+        volumeSlider->setOrientation(Qt::Vertical);
+        volumeSlider->setMinimumHeight(150);
+    }
+    else
+    {
+        volumeSlider->setMinimumWidth(150);
+    }
     volumeSlider->setMuteVisible(false);
-//    volumeAddedAction = bar->addWidget(volumeSlider);
-//    volumeAddedAction->setVisible(false);
     bar->addAction(playAction);
     bar->addAction(pauseAction);
     bar->addAction(stopAction);
@@ -644,6 +685,7 @@ void MainWindow::setupUi()
     bar->addAction(upAction);
     bar->addAction(downAction);
     bar->addAction(delAction);
+    bar->addAction(enqueueActionButton);
 
     contextMenu = new QMenu (this);
     enqueueAction = contextMenu->addAction(tr ("Enqueue"));
@@ -666,6 +708,8 @@ void MainWindow::setupUi()
     musicTable->setHorizontalHeaderLabels(headers);
     musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
     musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
+//    musicTable->setAlternatingRowColors(true);
+    musicTable->verticalHeader ()->hide();
     connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
         this, SLOT(tableClicked(int,int)));
     connect(musicTable, SIGNAL(cellClicked(int,int)),
@@ -679,26 +723,45 @@ void MainWindow::setupUi()
     }
 
 
-    QHBoxLayout *seekerLayout = new QHBoxLayout;
+    QLayout *seekerLayout;
+    QLayout *playbackLayout;
+    if (flip)
+    {
+        seekerLayout = new QVBoxLayout;
+        playbackLayout = new QHBoxLayout;
+        bar->addWidget(timeLcd);
+    }
+    else
+    {
+        seekerLayout = new QHBoxLayout;
+        playbackLayout = new QVBoxLayout;
+    }
     QToolBar* bar2 = new QToolBar;
     bar2->addAction(volumeAction);
     seekerLayout->addWidget(bar2);
     seekerLayout->addWidget(volumeSlider);
     seekerLayout->addWidget(seekSlider);
-    seekerLayout->addWidget(timeLcd);
-
-    QVBoxLayout *playbackLayout = new QVBoxLayout;
+    if (!flip)
+        seekerLayout->addWidget(timeLcd);
     volumeSlider->hide ();
     playbackLayout->addWidget(bar);
 
     QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
 
     seekAndTableLayout->addWidget(musicTable);
-    seekAndTableLayout->addLayout(seekerLayout);
-
     QHBoxLayout *mainLayout = new QHBoxLayout;
     mainLayout->addLayout(seekAndTableLayout);
-    mainLayout->addLayout(playbackLayout);
+    if (flip)
+    {
+        seekAndTableLayout->addLayout(playbackLayout);
+        mainLayout->addLayout(seekerLayout);
+    }
+    else
+    {
+        seekAndTableLayout->addLayout(seekerLayout);
+        mainLayout->addLayout(playbackLayout);
+    }
+
 
     QWidget *widget = new QWidget;
     widget->setLayout(mainLayout);
@@ -714,12 +777,13 @@ void MainWindow::cellClicked(int /*row*/, int)
 void MainWindow::enqueueSelected()
 {
     int sel = musicTable->currentRow();
+    qDebug () << "Enqueue on " << sel;
     if (sel >= 0)
     {
         mediaObject->queue().clear();
         mediaObject->enqueue(plman.at(sel));
 #ifdef Q_WS_MAEMO_5
-        QMaemo5InformationBox::information(this, tr ("Song enqueued as next song"),
+        QMaemo5InformationBox::information(this, plman.getItem(sel).title + tr (" enqueued as next song"),
         QMaemo5InformationBox::DefaultTimeout);
 #endif
 
@@ -762,7 +826,16 @@ void MainWindow::savePlaylist ()
     QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u *.pls)");
     if (filename.isEmpty())
         return;
-    plman.savePlaylist(filename);
+    if (!plman.savePlaylist(filename))
+    {
+#ifdef Q_WS_MAEMO_5
+        QMaemo5InformationBox::information(this, tr ("Error writing playlist file"),
+        QMaemo5InformationBox::DefaultTimeout);
+#else
+        QMessageBox::critical(this, "Write error", "Error writing playlist file", QMessageBox::Ok);
+#endif
+    }
+
 }
 
 void MainWindow::loadPlaylist ()
@@ -837,6 +910,7 @@ void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
         {
             QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
             item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
+
             musicTable->setItem(row, col, item3);
         }
     }
@@ -845,7 +919,7 @@ void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
     if (controlCol >= 0 && !musicTable->cellWidget(row, controlCol))
     {
         QLabel* label = new QLabel;
-        label->setText(QString::fromUtf8("<b><a style='text-decoration:none' href='up'>▲</a> <a style='text-decoration:none' href='down'>▼</a> <a style='text-decoration:none' href='del'>╳</a> <a style='text-decoration:none' href='info'>i</a></b>"));
+        label->setText(QString::fromUtf8("<b><a style='text-decoration:none' href='up'>▲</a> <a style='text-decoration:none' href='down'>▼</a> <a style='text-decoration:none' href='del'>╳</a> <a style='text-decoration:none' href='enq'>E</a></b>"));
         label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
         label->setProperty("row", row);
         musicTable->setCellWidget(row, controlCol, label);
@@ -856,13 +930,17 @@ void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
 void MainWindow::playlistControl (const QString& con)
 {
     int i = sender ()->property("row").toInt();
-    qDebug () << "Playlist control: " << con << " on " << i;
     if (con == "up")
         buttonUp(i);
     else if (con == "down")
         buttonDown(i);
     else if (con == "del")
         buttonDel (i);
+    else if (con == "enq")
+    {
+        musicTable->selectRow(i);
+        enqueueSelected();
+    }
     else
         QMessageBox::information(this, tr ("Coming up..."), tr ("This feature is not implemented yet."));
 }
@@ -870,7 +948,6 @@ void MainWindow::playlistControl (const QString& con)
 
 void MainWindow::buttonUp(int i)
 {
-    qDebug () << "Presses up on " << i;
     if (i)
     {
         plman.moveItemUp(i);
@@ -888,7 +965,6 @@ void MainWindow::buttonUp(int i)
 
 void MainWindow::buttonDown(int i)
 {
-    qDebug () << "Presses down on " << i;
     if (i < plman.size() - 1)
     {
         plman.moveItemDown(i);
@@ -906,7 +982,6 @@ void MainWindow::buttonDown(int i)
 
 void MainWindow::buttonDel(int i)
 {
-    qDebug () << "Presses del on " << i;
     if (QMessageBox::question(this, "Confirm remove", "Are you sure you want to remove this item?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
         return;
     if (i < plman.size())
@@ -960,9 +1035,15 @@ void MainWindow::downSelected()
 
 void MainWindow::showOptions ()
 {
+    bool flip = settings.value("uiflipped", false).toBool();
     OptionDialog* dlg = new OptionDialog (this, settings);
     dlg->exec();
     delete dlg;
+    if (flip != settings.value("uiflipped", false).toBool())
+    {
+        delete centralWidget();
+        setupUi ();
+    }
     setOrientation ();
     if (headers != settings.value("headers", QStringList ()).toStringList())
     {
@@ -971,4 +1052,7 @@ void MainWindow::showOptions ()
         musicTable->setHorizontalHeaderLabels(headers);
         playlistChanged(0);
     }
+    int curitem = plman.indexOf(mediaObject->currentSource());
+    if (curitem >= 0 && isPlaying)
+        highlightRow(curitem);
 }