Merge branch 'alpha' of tomsrv:tomamp into alpha
authortmarki <tmarki@gmail.com>
Thu, 19 Aug 2010 15:58:25 +0000 (17:58 +0200)
committertmarki <tmarki@gmail.com>
Thu, 19 Aug 2010 15:58:25 +0000 (17:58 +0200)
1  2 
tomamp/mainwindow.cpp
tomamp/mainwindow.h
tomamp/playlistmanager.cpp

diff --combined tomamp/mainwindow.cpp
@@@ -1,7 -1,9 +1,9 @@@
  #include <QtGui>
  #include <QtDebug>
  #include <QInputDialog>
+ #ifdef Q_WS_MAEMO_5
+ #include <QtMaemo5/QMaemo5InformationBox>
+ #endif
  #include "mainwindow.h"
  #include "optiondialog.h"
  #include "time.h"
@@@ -11,9 -13,7 +13,7 @@@
  MainWindow::MainWindow()
      : plman (this), settings (tr ("TomAmp"), "TomAmp"), isPlaying (false)
  {
- #ifdef Q_WS_MAEMO_5
-     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
- #endif
+     setOrientation();
      audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
      mediaObject = new Phonon::MediaObject(this);
  
@@@ -33,6 -33,7 +33,7 @@@
      qsrand (time (NULL));
      repeat = settings.value("repeat", false).toBool();
      shuffle = settings.value("shuffle", false).toBool();
+     headers = settings.value ("headers", QStringList()).toStringList();
      setupShuffleList();
      setupActions();
      setupMenus();
@@@ -55,6 -56,7 +56,7 @@@ MainWindow::~MainWindow(
      settings.setValue("lastPlaylist", plman.playlistStrings());
      settings.setValue("volume", audioOutput->volume());
      settings.setValue("currentIndex", plman.indexOf(mediaObject->currentSource()));
+     settings.setValue("headers", headers);
      for (int i = 0; i < musicTable->columnCount(); ++i)
      {
          QString lab = QString ("colWidth_%1").arg (i);
      }
  }
  
+ 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
+ }
  void MainWindow::addFiles()
  {
      QString folder = settings.value("LastFolder").toString();
@@@ -132,8 -147,7 +147,8 @@@ 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 http://itweek.deviantart.com/"));
 +        "Icons by http://itweek.deviantart.com/\n\n"
 +        "Thanks to Attila Csipa for the Diablo build"));
  }
  
  void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
@@@ -191,6 -205,11 +206,11 @@@ void MainWindow::next(
      bool wasPlaying = isPlaying;
      if (mediaObject->state () == Phonon::ErrorState)
          wasPlaying = true;
+     if (mediaObject->queue().size())
+     {
+         setItem (plman.indexOf(mediaObject->queue()[0]), wasPlaying);
+         return;
+     }
      int index = plman.indexOf(mediaObject->currentSource());
      if (shuffle)
      {
@@@ -403,6 -422,8 +423,8 @@@ void MainWindow::sourceChanged(const Ph
  
  void MainWindow::aboutToFinish()
  {
+     if (mediaObject->queue().size())
+         return;
      int index = plman.indexOf(mediaObject->currentSource()) + 1;
      if (shuffle)
      {
@@@ -448,6 -469,9 +470,9 @@@ void MainWindow::setupActions(
      stopAction->setDisabled(true);
      nextAction = new QAction(QIcon (QPixmap (":images/next")), "", this);
      nextAction->setShortcut(tr("Ctrl+N"));
+     upAction = new QAction (QString::fromUtf8("▲"), this);
+     downAction = new QAction (QString::fromUtf8("▼"), this);
+     delAction = new QAction (QString::fromUtf8("╳"), this);
      previousAction = new QAction(QIcon (QPixmap (":images/previous")), "", this);
      previousAction->setShortcut(tr("Ctrl+R"));
      if (repeat)
      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 (optionAction, SIGNAL (triggered()), this, SLOT (showOptions()));
      connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
      connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
+     connect (upAction, SIGNAL(triggered()), this, SLOT(upSelected()));
+     connect (downAction, SIGNAL(triggered()), this, SLOT(downSelected()));
+     connect (delAction, SIGNAL(triggered()), this, SLOT(removeSelectedItem()));
      connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
      connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
      connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
@@@ -619,20 -646,28 +647,28 @@@ void MainWindow::setupUi(
      bar->addAction(shuffleAction);
      bar->addAction(nextAction);
      bar->addAction(previousAction);
+     bar->addAction(upAction);
+     bar->addAction(downAction);
+     bar->addAction(delAction);
  
      contextMenu = new QMenu (this);
+     enqueueAction = contextMenu->addAction(tr ("Enqueue"));
      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()));
+     connect (enqueueAction, SIGNAL (triggered()), this, SLOT (enqueueSelected()));
  
  
      timeLcd = new QLCDNumber;
  
-     QStringList headers;
-     headers << tr("Artist") << tr("Title") << tr("Album") << "Controls";
+     if (!headers.size ())
+     {
+         headers << "Artist" << "Title" << "Album";
+         settings.setValue("headers", headers);
+     }
  
-     musicTable = new QTableWidget(0, 4);
+     musicTable = new QTableWidget(0, headers.size ());
      musicTable->setHorizontalHeaderLabels(headers);
      musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
      musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
          this, SLOT(tableClicked(int,int)));
      connect(musicTable, SIGNAL(cellClicked(int,int)),
          this, SLOT(cellClicked(int,int)));
- /*    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));
      }
  
  
@@@ -689,6 -716,21 +717,21 @@@ void MainWindow::cellClicked(int /*row*
  {
  }
  
+ void MainWindow::enqueueSelected()
+ {
+     int sel = musicTable->currentRow();
+     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::DefaultTimeout);
+ #endif
+     }
+ }
  void MainWindow::contextMenuEvent (QContextMenuEvent*e)
  {
      if (!childAt (e->pos()))
@@@ -764,33 -806,55 +807,55 @@@ void MainWindow::setRowFromItem (int ro
          return;
      if (item.artist.isEmpty() && item.title.isEmpty())
      {
-         QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
-         item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
-         musicTable->setItem(row, 1, item1);
+         int col = headers.indexOf("Title");
+         if (col >= 0)
+         {
+             QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
+             item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
+             musicTable->setItem(row, col, item1);
+         }
      }
      else
      {
-         QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
-         item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
-         musicTable->setItem(row, 0, item1);
-         QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
-         item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
-         musicTable->setItem(row, 1, item2);
-         QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
-         item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
-         musicTable->setItem(row, 2, item3);
+         int col = headers.indexOf("Artist");
+         if (col >= 0)
+         {
+             QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
+             item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
+             musicTable->setItem(row, col, item1);
+         }
+         col = headers.indexOf("Title");
+         if (col >= 0)
+         {
+             if (!musicTable->item (row, col))
+             {
+                 QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
+                 item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
+                 musicTable->setItem(row, col, item2);
+             }
+             else
+             {
+                 musicTable->item (row, col)->setText(item.title);
+             }
+         }
+         col = headers.indexOf("Album");
+         if (col >= 0)
+         {
+             QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
+             item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
+             musicTable->setItem(row, col, item3);
+         }
      }
  
-     if (!musicTable->cellWidget(row, 3))
+     int controlCol = headers.indexOf("Controls");
+     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->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
          label->setProperty("row", row);
-         musicTable->setCellWidget(row, 3, label);
+         musicTable->setCellWidget(row, controlCol, label);
          connect (label, SIGNAL (linkActivated (const QString&)),  this, SLOT (playlistControl (const QString&)));
- /*        connect (down, SIGNAL (linkActivated (const QString&)),  this, SLOT (buttonDown ()));
-         connect (del, SIGNAL (linkActivated (const QString&)),  this, SLOT (buttonDel ()));*/
      }
  }
  
@@@ -811,15 -875,18 +876,18 @@@ void MainWindow::playlistControl (cons
  
  void MainWindow::buttonUp(int i)
  {
-     //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);
+         int controlCol = headers.indexOf("Controls");
+         if (controlCol >= 0)
+         {
+             musicTable->cellWidget(i, controlCol)->setProperty("row", i);
+             musicTable->cellWidget(i - 1, controlCol)->setProperty("row", i - 1);
+         }
          musicTable->selectRow(i - 1);
      }
  }
@@@ -832,8 -899,12 +900,12 @@@ void MainWindow::buttonDown(int i
          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);
+         int controlCol = headers.indexOf("Controls");
+         if (controlCol >= 0)
+         {
+             musicTable->cellWidget(i, controlCol)->setProperty("row", i);
+             musicTable->cellWidget(i + 1, controlCol)->setProperty("row", i + 1);
+         }
          musicTable->selectRow(i + 1);
      }
  }
  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())
      {
          plman.removeItem(i);
@@@ -864,15 -937,43 +938,43 @@@ void MainWindow::itemUpdated(int index
  void MainWindow::itemRemoved (int i)
  {
      musicTable->removeRow(i);
+     int controlCol = headers.indexOf("Controls");
+     if (controlCol < 0)
+         return;
      for (int j = i ? (i - 1) : 0; j < musicTable->rowCount(); ++j)
      {
-         if (musicTable->cellWidget(j, 3))
-             musicTable->cellWidget(j, 3)->setProperty("row", j);
+         if (musicTable->cellWidget(j, controlCol))
+             musicTable->cellWidget(j, controlCol)->setProperty("row", j);
      }
  }
  
+ void MainWindow::upSelected()
+ {
+     int sel = musicTable->currentRow();
+     if (sel > 0)
+         buttonUp(sel);
+ }
+ void MainWindow::downSelected()
+ {
+     int sel = musicTable->currentRow();
+     if (sel >= 0)
+         buttonDown(sel);
+ }
  void MainWindow::showOptions ()
  {
-     OptionDialog dlg (this, settings);
-     dlg.show();
+     OptionDialog* dlg = new OptionDialog (this, settings);
+     dlg->exec();
+     delete dlg;
+     setOrientation ();
+     if (headers != settings.value("headers", QStringList ()).toStringList())
+     {
+         headers = settings.value("headers", QStringList ()).toStringList();
+         musicTable->setColumnCount(headers.size ());
+         musicTable->setHorizontalHeaderLabels(headers);
+         playlistChanged(0);
+     }
  }
diff --combined tomamp/mainwindow.h
  
  #include <QMainWindow>
  #include <QSettings>
 +#if QT_VERSION < 0x040600
 +#include <audiooutput.h>
 +#include <seekslider.h>
 +#include <mediaobject.h>
 +#include <volumeslider.h>
 +#include <backendcapabilities.h>
 +#else
  #include <phonon/audiooutput.h>
  #include <phonon/seekslider.h>
  #include <phonon/mediaobject.h>
  #include <phonon/volumeslider.h>
  #include <phonon/backendcapabilities.h>
 +#endif
  #include <QList>
  #include "playlistmanager.h"
  
@@@ -110,6 -102,9 +110,9 @@@ private slots
      void itemRemoved (int i);
      void showOptions ();
      void playlistControl (const QString&);
+     void upSelected();
+     void downSelected();
+     void enqueueSelected();
  
  protected:
      void contextMenuEvent (QContextMenuEvent*e);
@@@ -122,6 -117,7 +125,7 @@@ private
      void buttonUp (int i);
      void buttonDown (int i);
      void buttonDel (int i);
+     void setOrientation ();
  
      Phonon::SeekSlider *seekSlider;
      Phonon::MediaObject *mediaObject;
      QAction *volumeAddedAction;
      QAction *nextAction;
      QAction *previousAction;
+     QAction *upAction;
+     QAction *downAction;
+     QAction *delAction;
      QAction *addFilesAction;
      QAction *addFoldersAction;
      QAction *addUrlAction;
      QAction *aboutQtAction;
      QAction *removeSelected;
      QAction *removeAllButSelected;
+     QAction *enqueueAction;
      QAction *optionAction;
      QMenu   *contextMenu;
      QToolBar *bar;
      QLCDNumber *timeLcd;
      QTableWidget *musicTable;
+     QStringList headers;
      bool        repeat;
      bool        shuffle;
      QSettings settings;
@@@ -9,7 -9,7 +9,7 @@@ QStringList allowedExtensions
  PlaylistManager::PlaylistManager(QWidget* parent)
      : parentWidget (parent), lastMetaRead (-1)
  {
 -    allowedExtensions << "mp3" << "ogg" << "wav" << "wmv" << "wma";
 +    allowedExtensions << "mp3" << "ogg" << "wav" << "wmv" << "wma" << "flac";
  //    qDebug () << Phonon::BackendCapabilities::availableMimeTypes();
      metaInformationResolver = new Phonon::MediaObject(parent);
      connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
@@@ -116,12 -116,15 +116,15 @@@ void PlaylistManager::metaStateChanged(
      QMap<QString, QString> metaData = metaInformationResolver->metaData();
  
  
-     if (index >= 0 && newState != Phonon::ErrorState)
+     if (index >= 0 && newState != Phonon::ErrorState && index < items.size ())
      {
          items[index].artist = metaData.value("ARTIST");
          items[index].title = metaData.value("TITLE");
          items[index].album = metaData.value("ALBUM");
-         qDebug () << "Info is: " << items[index].artist << " - " << items[index].title;
+ /*        items[index].year = metaData.value("DATE");
+         items[index].genre = metaData.value("GENRE");
+         qDebug () << "Meta " << metaData;
+         qDebug () << "Info is: " << items[index].year << " - " << items[index].genre;*/
          if (metaData.isEmpty())
              qDebug () << "Detected to be empty: " << items[index].uri;
          else
@@@ -312,9 -315,7 +315,9 @@@ void PlaylistManager::removeItem(int i
  
  bool PlaylistManager::fileSupported (const QString& fname) const
  {
 -    QString ext = fname.right(3).toLower();
 +    if (fname.lastIndexOf('.') < 0)
 +        return false;
 +    QString ext = fname.right(fname.size() - fname.lastIndexOf('.') - 1).toLower();
      foreach (QString e, allowedExtensions)
      {
          if (ext == e)