working optins with orientation and column order/selection
authortmarki <tmarki@gmail.com>
Wed, 18 Aug 2010 00:06:05 +0000 (02:06 +0200)
committertmarki <tmarki@gmail.com>
Wed, 18 Aug 2010 00:06:05 +0000 (02:06 +0200)
enqueue (buggy, can cause crash)
ordering buttons (accessible in portrait mode)

tomamp/mainwindow.cpp
tomamp/mainwindow.h
tomamp/optiondialog.cpp
tomamp/optiondialog.h
tomamp/playlistmanager.cpp
tomamp/playlistmanager.h
tomamp/tomamp.pro

index ec30cbb..f2a3cd0 100644 (file)
@@ -1,7 +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 @@
 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 @@ MainWindow::MainWindow()
     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 @@ 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);
@@ -62,6 +64,19 @@ MainWindow::~MainWindow()
     }
 }
 
+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();
@@ -402,6 +417,8 @@ void MainWindow::sourceChanged(const Phonon::MediaSource &source)
 
 void MainWindow::aboutToFinish()
 {
+    if (mediaObject->queue().size())
+        return;
     int index = plman.indexOf(mediaObject->currentSource()) + 1;
     if (shuffle)
     {
@@ -447,6 +464,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)
@@ -502,9 +522,12 @@ void MainWindow::setupActions()
     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()));
@@ -618,20 +641,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);
@@ -639,20 +670,12 @@ void MainWindow::setupUi()
         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));
     }
 
 
@@ -688,6 +711,21 @@ void MainWindow::cellClicked(int /*row*/, int)
 {
 }
 
+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()))
@@ -763,33 +801,55 @@ void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
         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 ()));*/
     }
 }
 
@@ -810,15 +870,18 @@ void MainWindow::playlistControl (const QString& con)
 
 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);
     }
 }
@@ -831,8 +894,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);
     }
 }
@@ -840,6 +907,8 @@ 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())
     {
         plman.removeItem(i);
@@ -863,16 +932,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 ()
 {
-    qDebug () << "Options dialog";
-    OptionDialog dlg (this, settings);
-    dlg.exec();
+    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);
+    }
 }
index 59e247e..c8ffc48 100644 (file)
@@ -102,6 +102,9 @@ private slots:
     void itemRemoved (int i);
     void showOptions ();
     void playlistControl (const QString&);
+    void upSelected();
+    void downSelected();
+    void enqueueSelected();
 
 protected:
     void contextMenuEvent (QContextMenuEvent*e);
@@ -114,6 +117,7 @@ private:
     void buttonUp (int i);
     void buttonDown (int i);
     void buttonDel (int i);
+    void setOrientation ();
 
     Phonon::SeekSlider *seekSlider;
     Phonon::MediaObject *mediaObject;
@@ -131,6 +135,9 @@ private:
     QAction *volumeAddedAction;
     QAction *nextAction;
     QAction *previousAction;
+    QAction *upAction;
+    QAction *downAction;
+    QAction *delAction;
     QAction *addFilesAction;
     QAction *addFoldersAction;
     QAction *addUrlAction;
@@ -142,11 +149,13 @@ private:
     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;
index 54b1131..a0c7262 100644 (file)
 OptionDialog::OptionDialog(QWidget *parent, QSettings& set) :
     QDialog(parent), settings (set)
 {
+    availableHeaders = settings.value("headerOrder", QStringList()).toStringList();
+    if (!availableHeaders.size())
+        availableHeaders << "Artist" << "Title" << "Album" << "Controls";
     setupUi();
 }
 
+OptionDialog::~OptionDialog ()
+{
+#ifdef Q_WS_MAEMO_5
+    settings.setValue("orientation", orient->currentText());
+#endif
+    settings.setValue("headerOrder", availableHeaders);
+    QStringList h;
+    foreach (QObject* child, children())
+    {
+        QCheckBox* cb = qobject_cast<QCheckBox*>(child);
+        if (cb && cb->isChecked ())
+        {
+            h << cb->text ();
+        }
+    }
+    settings.setValue("headers", h);
+}
+
 
 void OptionDialog::setupUi()
 {
-    QHBoxLayout *mainLayout = new QHBoxLayout;
-//    QWidget *widget = new QWidget;
-//    widget->setLayout(mainLayout);
-
-//    setCentralWidget(widget);
+    QVBoxLayout *mainLayout = new QVBoxLayout;
+#ifdef Q_WS_MAEMO_5
+    QString ori = settings.value("orientation", "Automatic").toString();
+    orient = new QComboBox();
+    orient->addItem(tr ("Automatic"));
+    orient->addItem(tr ("Landscape"));
+    orient->addItem(tr ("Portrait"));
+    if (ori == "Landscape")
+        orient->setCurrentIndex(1);
+    if (ori == "Portrait")
+        orient->setCurrentIndex(2);
+    QLabel *label = new QLabel (tr ("Orientation"));
+    QHBoxLayout *subLayout = new QHBoxLayout;
+    subLayout->addWidget(label);
+    subLayout->addWidget(orient);
+    mainLayout->addLayout(subLayout);
+#endif
+    QLabel* lab = new QLabel (tr ("Enabled columns: "));
+    QHBoxLayout *sub = new QHBoxLayout;
+    sub->addWidget(lab);
+    headerLayout = new QVBoxLayout;
+    QStringList headers = settings.value ("headers", QStringList ()).toStringList();
+    qDebug () << "Available headers: " << availableHeaders;
+    foreach (QString str, availableHeaders)
+    {\
+        QHBoxLayout *cont = new QHBoxLayout;
+        QCheckBox* box = new QCheckBox (str);
+        if (headers.indexOf(str) >= 0)
+            box->setChecked(true);
+        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></b>"));
+        label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+        label->setProperty("row", str);
+        cont->addWidget(box);
+        cont->addWidget(label);
+        headerLayout->addLayout(cont);
+        connect (label, SIGNAL(linkActivated(QString)), this, SLOT (orderControl(QString)));
+    }
+    sub->addLayout(headerLayout);
+    mainLayout->addLayout(sub);
     setLayout(mainLayout);
-    setWindowTitle("TomAmp Settings");
+    setWindowTitle("Settings");
+}
+
+void OptionDialog::orderControl (QString link)
+{
+    QString str = sender ()->property("row").toString();
+    qDebug () << "Col action " << link << " on " << str;
+    int i = availableHeaders.indexOf(str);
+    if (link == "up" && i > 0)
+    {
+        QString tmp = availableHeaders[i];
+        availableHeaders[i] = availableHeaders[i - 1];
+        availableHeaders[i - 1] = tmp;
+    }
+    else if (link == "down" && i < availableHeaders.size() - 1)
+    {
+        QString tmp = availableHeaders[i];
+        availableHeaders[i] = availableHeaders[i + 1];
+        availableHeaders[i + 1] = tmp;
+    }
+    QStringList h;
+    foreach (QObject* child, children())
+    {
+        QCheckBox* cb = qobject_cast<QCheckBox*>(child);
+        if (cb && cb->isChecked ())
+        {
+            h << cb->text ();
+        }
+    }
+    settings.setValue("headers", h);
+    delete layout ();
+    foreach (QObject* child, children ())
+        delete child;
+//    update ();
+    setupUi();
+}
+
+void OptionDialog::upColumn (int i)
+{
+}
+
+void OptionDialog::downColumn (int i)
+{
 }
 
index fc60fa7..1c5a280 100644 (file)
@@ -4,18 +4,29 @@
 #include <QDialog>
 #include <QSettings>
 
+class QComboBox;
+class QVBoxLayout;
+
 class OptionDialog : public QDialog
 {
     Q_OBJECT
 public:
     explicit OptionDialog(QWidget *parent, QSettings& set);
+    ~OptionDialog ();
 
 signals:
 
 public slots:
+private slots:
+    void orderControl (QString);
 private:
+    void upColumn (int i);
+    void downColumn (int i);
     void    setupUi ();
     QSettings& settings;
+    QComboBox* orient;
+    QStringList availableHeaders;
+    QVBoxLayout *headerLayout;
 };
 
 #endif // OPTIONDIALOG_H
index 61681a2..3c0cb95 100644 (file)
@@ -121,7 +121,10 @@ void PlaylistManager::metaStateChanged(Phonon::State newState, Phonon::State old
         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
index d1bbaba..395a066 100644 (file)
@@ -16,6 +16,9 @@ struct PlaylistItem
     QString title;
     QString artist;
     QString album;
+/*    QString year;
+    QString genre;
+    QString length;*/
     PlaylistItem (const QString& uri) : source (Phonon::MediaSource (uri)), uri (uri), playable (false), localFile (false) {  }
     bool operator ==(const Phonon::MediaSource& s) const { return source == s; }
 };
index 13eaa0f..e932ac8 100644 (file)
@@ -6,6 +6,8 @@
 
 QT       += core gui phonon
 
+maemo5 { QT += maemo5 }
+
 TARGET = tomamp
 TEMPLATE = app