mpdata/src/mpcollectiondatamodel.cpp
changeset 29 8192e5b5c935
parent 22 ecf06a08d4d9
child 37 eb79a7c355bf
--- a/mpdata/src/mpcollectiondatamodel.cpp	Fri May 14 15:49:53 2010 +0300
+++ b/mpdata/src/mpcollectiondatamodel.cpp	Thu May 27 12:49:57 2010 +0300
@@ -17,7 +17,7 @@
 
 #include <QIcon>
 #include <QList>
-#include <qmimedata.h>
+#include <QMimeData>
 #include <hbicon.h>
 #include <hbnamespace.h>
 
@@ -53,14 +53,26 @@
  */
 
 /*!
+    \fn void dataReloaded()
+
+    This signal is emitted when there has been a changed in the data represented
+    by this model. This signal is emitted so that the view owning this model can
+    refresh the view. This can happen after operations like delete and several
+    playlist related operations (save, rearrange, remove).
+ */
+
+/*!
  Constructs the collection data model.
  */
 MpCollectionDataModel::MpCollectionDataModel( MpMpxCollectionData *data, QObject *parent )
     : QAbstractListModel(parent),
       mCollectionData(data),
-      mRowCount(0)
+      mRowCount(0),
+      mAlbumIndexOffset(0)
 {
     TX_ENTRY
+    connect( mCollectionData, SIGNAL(contextChanged(TCollectionContext)), this, SLOT(setContext(TCollectionContext)) );
+    connect( mCollectionData, SIGNAL(dataChanged()), this, SLOT(reloadData()) );
     mAlbumArtManager = new MpCollectionAlbumArtManager(mCollectionData);
     connect( mAlbumArtManager, SIGNAL(albumArtReady(int)), this, SLOT(updateAlbumArt(int)) );
     TX_EXIT
@@ -89,7 +101,7 @@
 {
     TX_LOG
     Q_UNUSED(parent);
-    return mRowCount;
+    return mRowCount - mAlbumIndexOffset;
 }
 
 /*!
@@ -105,27 +117,51 @@
         return returnValue;
     }
 
-    int row = index.row();
+    int row = index.row() + mAlbumIndexOffset;
     TX_LOG_ARGS("index=" << row << ", role=" << role);
     TCollectionContext context = mCollectionData->context();
     if ( role == Qt::DisplayRole ) {
         QStringList display;
         // Fetch the primary text, which is the title, if available.
         QString primaryText;
-        primaryText = mCollectionData->itemData(row, MpMpxCollectionData::Title);
-        if ( !primaryText.isEmpty() ) {
-            display << primaryText;
+        switch ( context ) {
+            case ECollectionContextAllSongs:
+            case ECollectionContextArtists:
+            case ECollectionContextAlbums:
+            case ECollectionContextArtistAlbums:
+            case ECollectionContextPlaylists:
+            case ECollectionContextPlaylistSongs:
+            case ECollectionContextAlbumsTBone:
+            case ECollectionContextArtistAlbumsTBone:
+            case ECollectionContextArtistAllSongs:
+                primaryText = mCollectionData->itemData(row, MpMpxCollectionData::Title);
+                if ( !primaryText.isEmpty() ) {
+                    display << primaryText;
+                }
+                else {
+                    display << hbTrId("txt_mus_other_unknown4");
+                }
+                break;
+            case ECollectionContextAlbumsMediaWall:
+                primaryText = mCollectionData->itemData(row, MpMpxCollectionData::Artist);
+                if ( !primaryText.isEmpty() ) {
+                    display << primaryText;
+                }
+                else {
+                    display << hbTrId("txt_mus_other_unknown4");
+                }    
+                break;
+            default:
+                break;
         }
-        else {
-            display << hbTrId("txt_mus_other_unknown4");
-        }
-
+        
         // Fetch the secondary text, which depends on the current context, if available.
         QString secondaryText;
         switch ( context ) {
             case ECollectionContextAllSongs:
             case ECollectionContextAlbums:
             case ECollectionContextPlaylistSongs:
+            case ECollectionContextAlbumsTBone:
                 secondaryText = mCollectionData->itemData(row, MpMpxCollectionData::Artist);
                 if ( !secondaryText.isEmpty() ) {
                     display << secondaryText;
@@ -134,7 +170,16 @@
                     display << hbTrId("txt_mus_other_unknown3");
                 }
                 break;
-            case ECollectionContextArtistSongs: 
+            case ECollectionContextArtistAlbumsTBone:
+                secondaryText = mCollectionData->collectionTitle();
+                if ( !secondaryText.isEmpty() ) {
+                    display << secondaryText;
+                }
+                else {
+                    display << hbTrId("txt_mus_other_unknown3");
+                }
+                break;
+            case ECollectionContextArtistAllSongs:
                 secondaryText = mCollectionData->itemData(row, MpMpxCollectionData::Album);
                 if ( !secondaryText.isEmpty() ) {
                     display << secondaryText;
@@ -143,6 +188,15 @@
                     display << hbTrId("txt_mus_other_unknown4");
                 }
                 break;
+            case ECollectionContextAlbumsMediaWall:
+                secondaryText = mCollectionData->itemData(row, MpMpxCollectionData::Title);
+                if ( !secondaryText.isEmpty() ) {
+                    display << secondaryText;
+                }
+                else {
+                    display << hbTrId("txt_mus_other_unknown3");
+                }
+                break;
             default:
                 break;
         }
@@ -151,16 +205,41 @@
     else if ( role == Qt::DecorationRole ) {
         switch ( context ) {
             case ECollectionContextAlbums:
+            case ECollectionContextAlbumsMediaWall:
+            case ECollectionContextArtistAlbumsTBone:
+            case ECollectionContextAlbumsTBone:
+                    returnValue = mAlbumArtManager->albumArt( row ) ;
+                break;
             case ECollectionContextArtistAlbums:
-                const QIcon *icon = mAlbumArtManager->albumArt(row);
-                QVariant iconVariant(QVariant::Icon, icon);
-                returnValue = iconVariant;
+                if ( row == 0 ) {
+                    returnValue = HbIcon( "qtg_small_sound" );
+                }
+                else {
+                    returnValue = mAlbumArtManager->albumArt( row );
+                }
                 break;
         }
     }
     else if ( role == Hb::IndexFeedbackRole ) {
         QString feedbackIndex;
-        feedbackIndex = mCollectionData->itemData(row, MpMpxCollectionData::Title);
+        switch ( context ) {
+            case ECollectionContextAllSongs:
+            case ECollectionContextArtists:
+            case ECollectionContextAlbums:
+            case ECollectionContextArtistAlbums:
+            case ECollectionContextPlaylists:
+            case ECollectionContextPlaylistSongs:
+            case ECollectionContextAlbumsTBone:
+            case ECollectionContextArtistAlbumsTBone:
+            case ECollectionContextArtistAllSongs:
+                feedbackIndex = mCollectionData->itemData(row, MpMpxCollectionData::Title);
+                break;
+            case ECollectionContextAlbumsMediaWall:
+                feedbackIndex = mCollectionData->itemData(row, MpMpxCollectionData::Artist);
+                break;
+            default:
+                break;
+        }
         returnValue = feedbackIndex;
     }
     TX_EXIT
@@ -276,11 +355,49 @@
 }
 
 /*!
+ Slot to be called when collection context is changed.
+ */
+void MpCollectionDataModel::setContext( TCollectionContext context )
+{
+    TX_ENTRY_ARGS( "context=" << context );
+    // Reset the album index offset for navigation
+    mAlbumIndexOffset = 0;
+    switch ( context ) {
+        case ECollectionContextArtistAlbums:
+        case ECollectionContextAlbums:
+            mAlbumArtManager->setThumbnailSize( MpCommon::ListThumb );
+            mAlbumArtManager->enableDefaultArt( true );
+            break;
+        case ECollectionContextArtistAlbumsTBone:
+            if ( mCollectionData->count() > 1 ) {
+                // Selected artist has more than 1 album and therefore the
+                // artist's "All songs" exist. Since we don't show artist's
+                // "All songs" in TBone, we need to set an offset.
+                mAlbumIndexOffset = 1;
+            }
+            //intentional fallthrough
+       case ECollectionContextAlbumsTBone:
+            mAlbumArtManager->setThumbnailSize( MpCommon::TBoneThumb );
+            mAlbumArtManager->enableDefaultArt( false );
+            break;
+        case ECollectionContextAlbumsMediaWall:
+            mAlbumArtManager->setThumbnailSize( MpCommon::MediaWallThumb );
+            mAlbumArtManager->enableDefaultArt( false );
+            break;
+        default:
+            break;
+    }
+    TX_EXIT
+}
+
+/*!
  Slot to be called when album art for the \a index needs to be updated.
  */
 void MpCollectionDataModel::updateAlbumArt( int index )
 {
     TX_ENTRY_ARGS("index=" << index);
+
+    index -= mAlbumIndexOffset;
     if ( index >= 0 && index < mRowCount ) {
         QModelIndex modelIndex = QAbstractItemModel::createIndex(index, 0);
         emit dataChanged(modelIndex, modelIndex);
@@ -300,7 +417,9 @@
     mRowCount = mCollectionData->count();
 
     TCollectionContext context = mCollectionData->context();
-    if ( context == ECollectionContextAlbums || ECollectionContextArtistAlbums ) {
+    if ( context == ECollectionContextAlbums || 
+         context == ECollectionContextArtistAlbums ||
+         context == ECollectionContextAlbumsMediaWall ) {
         // Before providing the new data to the view (list, grid, etc.), we want
         // to make sure that we have enough album arts for the first screen.
         mAlbumArtManager->cacheFirstScreen();
@@ -309,3 +428,17 @@
     TX_EXIT
 }
 
+/*!
+ Slot to be called when data has changed (same context) and model needs to reload
+ the data.
+ */
+void MpCollectionDataModel::reloadData()
+{
+    TX_ENTRY
+    mAlbumArtManager->cancel();
+    mRowCount = mCollectionData->count();
+    reset();
+    emit dataReloaded();
+    TX_EXIT
+}
+