Beginnings of *proper* playlist support
authorElias Woods <EliasWoods@gmail.com>
Sat, 8 Jan 2011 07:23:24 +0000 (02:23 -0500)
committerElias Woods <EliasWoods@gmail.com>
Sat, 8 Jan 2011 07:23:24 +0000 (02:23 -0500)
groove.cpp
playlist.cpp
playlist.h
pwin.cpp
pwin.h
pwin.ui

index eaf319f..0ad01d5 100644 (file)
@@ -119,6 +119,7 @@ groove::groove(QWidget *parent) :
     stack->addWidget(pwindow);
     stack->setCurrentWidget(resultView);
     connect(bBar,SIGNAL(list()),this,SLOT(togglePlaylist()));
+    pwindow->setModel(pl);
 
 }
 void groove::togglePlaylist()
@@ -133,6 +134,7 @@ void groove::performSearch(QString s)
 {
     qDebug() << s;
     resultView->setModel(gs->getSongModel(s));
+    this->stack->setCurrentWidget(this->resultView);
 }
 
 void groove::search()
@@ -200,7 +202,7 @@ void groove::play()
         if(item == 0)
             return;
         //gs->getSong();
-        player->play(pl->addSong(item));
+        player->play(pl->addSong(item,model->item(selected.first().row(),0)->text()));
     }
     //selected.
     //if
@@ -216,12 +218,12 @@ void groove::addSongPlaylist()
         //gs->getSong();
         if(pl->currentplaying() == -1)
         {
-            player->play(pl->addSong(item));
+            player->play(pl->addSong(item,model->item(selected.first().row(),0)->text()));
         }
         else
-            pl->addSong(item);
+            pl->addSong(item,model->item(selected.first().row(),0)->text());
         model->item(selected.first().row(),1)->setText("Added to Playlist");;
-        pwindow->addSong(model->item(selected.first().row(),0)->text());
+        //pwindow->addSong(model->item(selected.first().row(),0)->text());
     }
 
 }
index 5d867fd..f088b4f 100644 (file)
@@ -1,7 +1,7 @@
 #include "playlist.h"
 
 playlist::playlist(QObject *parent) :
-    QObject(parent)
+    QAbstractTableModel(parent)
 {
    manager = new QNetworkAccessManager();
    this->currentdownloaditem = -1;
@@ -10,6 +10,62 @@ playlist::playlist(QObject *parent) :
    this->currentSkeyItem = -1;
    this->reply = NULL;
 }
+
+//Implemented model class information
+QVariant playlist::data(const QModelIndex &index, int role) const
+{
+    QVariant dat;
+    playlist* play = (playlist *)index.model();
+    if(play->existAt(index.row()))
+    {
+        if (!index.isValid())
+            return QVariant();
+        if (role == Qt::TextAlignmentRole) {
+            return int(Qt::AlignRight | Qt::AlignVCenter);
+        } else if (role == Qt::DisplayRole) {
+            switch(index.column())
+            {
+            case sName:
+                dat = QVariant(*play->pList->at(index.row())->name);
+                break;
+            case sID:
+                dat = QVariant(*play->pList->at(index.row())->songId);
+                break;
+            case sKey:
+                dat = QVariant(*play->pList->at(index.row())->streamkey);
+                break;
+            case sDownloaded:
+                dat = QVariant(play->pList->at(index.row())->downloaded);
+                break;
+            case sReady:
+                dat = QVariant(play->pList->at(index.row())->bufferready);
+                break;
+            case sURL:
+                dat = QVariant(play->pList->at(index.row())->server->toString());
+                break;
+            case sPlayed:
+                dat = QVariant(play->pList->at(index.row())->played);
+                break;
+            default:
+                dat = QVariant();
+            }
+        } else
+            dat = QVariant();
+    }
+    else
+        dat = QVariant();
+    return dat;
+}
+int playlist::rowCount(const QModelIndex &) const
+{
+    return pList->size();
+}
+int playlist::columnCount(const QModelIndex &) const
+{
+    return PLAYLISTENUMS;
+}
+
+
 QList<playlist::songElement *>* playlist::getList()
 {
     return pList;
@@ -131,9 +187,10 @@ void playlist::skeyFound()
     this->currentSkeyItem = -1;
 }
 
-int playlist::addSong(QStandardItem *item)
+int playlist::addSong(QStandardItem *item, QString name)
 {
     playlist::songElement *newelement = new playlist::songElement;
+    newelement->name = new QString(name);
     newelement->buffer = new QBuffer();
     newelement->downloaded =false;
     newelement->songId = new QString(item->text());
@@ -144,7 +201,7 @@ int playlist::addSong(QStandardItem *item)
     newelement->type = playlist::EStream;
     pList->append(newelement);
     gs->getSong(item->text());
-
+    emit this->rowsInserted(QModelIndex(),pList->size(),pList->size());
     this->currentSkeyItem = pList->size()-1;
     emit this->freeze(true);
     return pList->size()-1;
index 7d3c215..bcf9aea 100644 (file)
@@ -8,7 +8,7 @@
 #include <QList>
 #include <QSignalMapper>
 #include <gscom.h>
-class playlist : public QObject
+class playlist : public QAbstractTableModel
 {
     Q_OBJECT
 public:
@@ -23,8 +23,24 @@ public:
         Aborted = 1,
         Other =2
     };
+    //this Enum MUST BE sequantial
+    //And when changed must update the length
+#define PLAYLISTENUMS 8
+    enum coulmnStruct
+    {
+        sName,
+        sID,
+        sKey,
+        sURL,
+        sBuff,
+        sDownloaded,
+        sPlayed,
+        sReady,
+    };
+
     struct songElement
     {
+        QString *name;
         QString *songId;
         QString *streamkey;
         QUrl *server;
@@ -37,8 +53,7 @@ public:
     };
     QList<songElement *>* getList();
     explicit playlist(QObject *parent = 0);
-    int addSong(QStandardItem *item);
-    QList<QStandardItem *> getPlaylist();
+    int addSong(QStandardItem *item,QString name);
     void removeSong(int position);
     void getSong(int position);
     QIODevice * getBuffer(int position);
@@ -54,12 +69,19 @@ public:
     void beginDownload(int position);
     bool existAt(int position);
 
+    //Implemented virtual classes for Model View
+    QVariant data(const QModelIndex &index, int role) const;
+    int columnCount(const QModelIndex &parent) const;
+    int rowCount(const QModelIndex &parent) const;
+
 signals:
     void downloadProgress(int position, qint64 d, qint64 t);
     void bufferReady(int position);
     void sFailure(int position,failType);
     void downloadComplete(int position);
     void freeze(bool);
+    //void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
+    void rowsInserted ( const QModelIndex & parent, int start, int end );
 
 public slots:
 private slots:
index 6f55cd5..8b566ac 100644 (file)
--- a/pwin.cpp
+++ b/pwin.cpp
@@ -1,4 +1,5 @@
 #include "pwin.h"
+#include "playlist.h"
 #include "ui_pwin.h"
 
 pWin::pWin(QWidget *parent) :
@@ -6,13 +7,22 @@ pWin::pWin(QWidget *parent) :
     ui(new Ui::pWin)
 {
     ui->setupUi(this);
+
+
 }
 
 pWin::~pWin()
 {
     delete ui;
 }
-void pWin::addSong(QString name)
+void pWin::setModel(QAbstractItemModel *m)
 {
-    ui->listWidget->addItem(name);
+    ui->tabv->setModel(m);
+    //By Default hide all columns
+    for(int i = 0; i < PLAYLISTENUMS; i++)
+    {
+        ui->tabv->hideColumn(i);
+    }
+    //Only show the song name column
+    ui->tabv->showColumn(playlist::sName);
 }
diff --git a/pwin.h b/pwin.h
index afb99a0..07448df 100644 (file)
--- a/pwin.h
+++ b/pwin.h
@@ -2,6 +2,7 @@
 #define PWIN_H
 
 #include <QWidget>
+#include <QtGui>
 
 namespace Ui {
     class pWin;
@@ -12,7 +13,7 @@ class pWin : public QWidget
     Q_OBJECT
 
 public:
-    void addSong(QString name);
+    void setModel(QAbstractItemModel *name);
     explicit pWin(QWidget *parent = 0);
     ~pWin();
 
diff --git a/pwin.ui b/pwin.ui
index 4b0aeb7..9bfd07c 100644 (file)
--- a/pwin.ui
+++ b/pwin.ui
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
-    <widget class="QListWidget" name="listWidget">
-     <property name="dragDropMode">
-      <enum>QAbstractItemView::InternalMove</enum>
-     </property>
-     <property name="selectionBehavior">
-      <enum>QAbstractItemView::SelectRows</enum>
-     </property>
-     <item>
-      <property name="text">
-       <string>New Item</string>
-      </property>
-      <property name="icon">
-       <iconset resource="res.qrc">
-        <normaloff>:/groove/icons/general_forward.png</normaloff>:/groove/icons/general_forward.png</iconset>
-      </property>
-     </item>
-    </widget>
-   </item>
-   <item>
-    <layout class="QVBoxLayout" name="verticalLayout"/>
+    <widget class="QTableView" name="tabv"/>
    </item>
   </layout>
  </widget>
- <resources>
-  <include location="res.qrc"/>
- </resources>
+ <resources/>
  <connections/>
 </ui>