videocollection/videocollectionwrapper/src/videosortfilterproxymodel.cpp
changeset 17 69946d1824c4
parent 15 cf5481c2bc0b
child 20 b9e04db066d4
--- a/videocollection/videocollectionwrapper/src/videosortfilterproxymodel.cpp	Fri Apr 16 14:59:52 2010 +0300
+++ b/videocollection/videocollectionwrapper/src/videosortfilterproxymodel.cpp	Mon May 03 12:32:50 2010 +0300
@@ -15,6 +15,9 @@
 *
 */
 
+// Version : %version: 63 %
+
+// INCLUDE FILES
 #include <qstringlist.h>
 #include <qset.h>
 #include <qtimer.h>
@@ -27,23 +30,26 @@
 #include "videolistdatamodel.h"
 #include "videocollectionclient.h"
 #include "videocollectionwrapper.h"
+#include "videocollectiontrace.h"
 
 // -----------------------------------------------------------------------------
 // VideoSortFilterProxyModel::VideoSortFilterProxyModel
 // -----------------------------------------------------------------------------
 //
-VideoSortFilterProxyModel::VideoSortFilterProxyModel(int type, QObject *parent) :
+VideoSortFilterProxyModel::VideoSortFilterProxyModel(VideoCollectionCommon::TModelType type, QObject *parent) :
 QSortFilterProxyModel(parent),
 mModel(0),
 mCollectionClient(0),
 mType(type),
-mLevel(-1),
+mLevel(VideoCollectionCommon::ELevelInvalid),
 mGenericFilterId(TMPXItemId::InvalidId()),
 mGenericFilterValue(false),
 mIdleSortTimer(0),
-mWantedSortRole(VideoCollectionCommon::KeyDateTime)
+mWantedSortRole(VideoCollectionCommon::KeyDateTime),
+mWantedSortOrder(Qt::AscendingOrder)
 {
-    // NOP
+	FUNC_LOG_ADDR(this);
+	INFO_2("VideoSortFilterProxyModel::VideoSortFilterProxyModel() [0x%x]: type: %d", this, type);
 }
 
 // -----------------------------------------------------------------------------
@@ -52,7 +58,7 @@
 //
 VideoSortFilterProxyModel::~VideoSortFilterProxyModel()
 {
-    // NOP
+	FUNC_LOG_ADDR(this);
 }
 
 // -----------------------------------------------------------------------------
@@ -61,6 +67,7 @@
 //
 int VideoSortFilterProxyModel::initialize(VideoListDataModel *sourceModel)
 {
+	FUNC_LOG_ADDR(this);
     if(!sourceModel)
     {
         return -1;
@@ -71,6 +78,7 @@
         mModel = sourceModel;
         if(!connectSignals()) 
         {
+            ERROR_1(-1, "VideoSortFilterProxyModel::initialize() [0x%x] failed to connect signals.", this);
             disconnectSignals();
             mModel = 0;
             return -1;
@@ -87,6 +95,7 @@
 //
 bool VideoSortFilterProxyModel::connectSignals()
 {
+	FUNC_LOG_ADDR(this);
     if(!connect(mModel, SIGNAL(modelReady()),
             this, SIGNAL(modelReady()))) 
     {
@@ -97,7 +106,7 @@
     {
         return false;
     }
-    if(mType == VideoCollectionWrapper::ECollectionContent)
+    if(mType == VideoCollectionCommon::EModelTypeCollectionContent)
     {
         if(!connect(mModel, SIGNAL(albumChanged()),
                     this, SLOT(albumChangedSlot()))) 
@@ -105,6 +114,14 @@
             return false;
         }
     }
+    if(mType == VideoCollectionCommon::EModelTypeCollections)
+    {
+        if(!connect(mModel, SIGNAL(itemModified(const TMPXItemId &)),
+                    this, SLOT(itemModifiedSlot(const TMPXItemId &)))) 
+        {
+            return false;
+        }
+    }
     return true;
 }
    
@@ -114,21 +131,29 @@
 //
 void VideoSortFilterProxyModel::disconnectSignals()
 {
+	FUNC_LOG_ADDR(this);
 	disconnect(mModel, SIGNAL(modelReady()), this, SIGNAL(modelReady()));
     disconnect(mModel, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
-    if(mType == VideoCollectionWrapper::ECollectionContent)
+    if(mType == VideoCollectionCommon::EModelTypeCollectionContent)
     {
         disconnect(mModel, SIGNAL(albumChanged()), this, SLOT(albumChangedSlot()));
     }
-
+    if(mType == VideoCollectionCommon::EModelTypeCollections)
+    {
+        disconnect(mModel, SIGNAL(itemModified(const TMPXItemId &)), 
+                   this, SLOT(itemModifiedSlot(const TMPXItemId &)));
+    }
 }
 
 // -----------------------------------------------------------------------------
 // VideoSortFilterProxyModel::open
 // -----------------------------------------------------------------------------
 //
-int VideoSortFilterProxyModel::open(int level)
+int VideoSortFilterProxyModel::open(VideoCollectionCommon::TCollectionLevels level)
 {
+	FUNC_LOG_ADDR(this);
+	INFO_2("VideoSortFilterProxyModel::open() [0x%x] level: %d", this, level);
+	
     if(!mCollectionClient)
     {
         return -1;
@@ -136,6 +161,7 @@
     
     if(mLevel != level) 
     {
+        INFO_1("VideoSortFilterProxyModel::open() [0x%x] opening different level, invalidating.", this);
         mLevel = level;
         invalidateFilter();
     }
@@ -151,12 +177,23 @@
 void VideoSortFilterProxyModel::doSorting(int sortingRole, Qt::SortOrder order, 
         bool async)
 {
-    if((sortingRole != Qt::DisplayRole) &&
-       (sortingRole != VideoCollectionCommon::KeySizeValue) &&
-       (sortingRole != VideoCollectionCommon::KeyDateTime))
+	FUNC_LOG_ADDR(this);
+	INFO_3("VideoSortFilterProxyModel::doSorting() [0x%x] sortingRole: %d, order: %d", this, sortingRole, order);
+	
+    if(sortingRole != VideoCollectionCommon::KeyTitle &&
+       sortingRole != VideoCollectionCommon::KeySizeValue &&
+       sortingRole != VideoCollectionCommon::KeyDateTime &&
+       sortingRole != VideoCollectionCommon::KeyNumberOfItems)
     {
-        // default sorting order is by date
-        mWantedSortRole = VideoCollectionCommon::KeyDateTime;
+        // default sorting order is by date for other models but collections.
+        if(mType != VideoCollectionCommon::EModelTypeCollections)
+        {   
+            mWantedSortRole = VideoCollectionCommon::KeyDateTime;
+        }
+        else
+        {
+            mWantedSortRole = VideoCollectionCommon::KeyTitle;
+        }
     }
     else
     {
@@ -180,7 +217,9 @@
             connect(mIdleSortTimer, SIGNAL(timeout()), this, SLOT(processSorting()));
         }
         mIdleSortTimer->start(0);
-    } else {
+    }
+    else
+    {
         processSorting();
     }
 }
@@ -190,6 +229,7 @@
 //
 void VideoSortFilterProxyModel::getSorting(int &sortingRole, Qt::SortOrder &order)
 {
+	FUNC_LOG_ADDR(this);
 	sortingRole = mWantedSortRole;
 	order       = mWantedSortOrder;
 }
@@ -200,6 +240,7 @@
 //
 int VideoSortFilterProxyModel::deleteItems(const QModelIndexList &indexList)
 {
+	FUNC_LOG_ADDR(this);
     if(mModel)
     {
         QModelIndexList mappedList;
@@ -224,6 +265,7 @@
 //
 int VideoSortFilterProxyModel::openItem(TMPXItemId mediaId)
 {
+	FUNC_LOG_ADDR(this);
     if(mediaId != TMPXItemId::InvalidId() && mCollectionClient)
     {
         if(mCollectionClient->openItem(mediaId) == 0)
@@ -232,6 +274,7 @@
             { 
                 mLevel = VideoCollectionCommon::ELevelAlbum;
                 mModel->setAlbumInUse(mediaId);
+                INFO_1("VideoSortFilterProxyModel::open() [0x%x] opening album or category, invalidating.", this);
                 invalidateFilter();
             } 
             return 0;
@@ -246,6 +289,7 @@
 //
 int VideoSortFilterProxyModel::back()
 {
+	FUNC_LOG_ADDR(this);
     if(mCollectionClient && mCollectionClient->back() == 0)
     {
         if(mLevel == VideoCollectionCommon::ELevelAlbum)
@@ -267,6 +311,7 @@
 //
 int VideoSortFilterProxyModel::fetchItemDetails(const QModelIndex &index)
 {
+	FUNC_LOG_ADDR(this);
     // no need to map index beforehand, because 
 	// getMediaIdAtIndex maps it
     TMPXItemId mpxId1 = getMediaIdAtIndex(index);
@@ -274,7 +319,6 @@
     {
         if(mCollectionClient->getVideoDetails(mpxId1) == 0)
         {
-
             emit shortDetailsReady(mpxId1);
             return 0;
         }
@@ -288,6 +332,7 @@
 //
 void VideoSortFilterProxyModel::processSorting()
 {
+	FUNC_LOG_ADDR(this);
     if(mIdleSortTimer)
     {
         mIdleSortTimer->stop();
@@ -330,7 +375,7 @@
     TMPXItemId rightId = mModel->mediaIdAtIndex(right.row());
     
     // Default categories are always first in the following order:
-    // Recently played (missing currently
+    // Recently played (missing currently)
     // Captured
     // Downloaded
     // Podcasts (missing currently)
@@ -368,8 +413,9 @@
     }
     
     // Do comparisation based on the role:
-    //  Qt::DisplayRole,
+    //  VideoCollectionCommon::KeyTitle,
     //  VideoCollectionCommon::KeySizeValue,
+    //  VideoCollectionCommon::KeyNumberOfItems,
     //  VideoCollectionCommon::KeyDateValue
     // 
     // If role does not match, do not sort
@@ -381,13 +427,14 @@
     {
         return false;
     }
-    if(sRole == Qt::DisplayRole)
+    if(sRole == VideoCollectionCommon::KeyTitle)
     {
-        QString leftString = leftData.toStringList().first().toUpper();
-        QString rightString = rightData.toStringList().first().toUpper();
+        QString leftString = leftData.toString().toUpper();
+        QString rightString = rightData.toString().toUpper();
         return QString::localeAwareCompare(leftString, rightString) < 0;
     }
-    else if(sRole == VideoCollectionCommon::KeySizeValue)
+    else if(sRole == VideoCollectionCommon::KeySizeValue ||
+            sRole == VideoCollectionCommon::KeyNumberOfItems)
     {
         quint32 leftSize = leftData.toUInt();
         quint32 rightSize = rightData.toUInt();
@@ -412,7 +459,9 @@
     Q_UNUSED(source_parent);
     
     if(!sourceModel())
+    {
         return false;
+    }
 
     if(source_row < 0 || source_row >= sourceModel()->rowCount())
     {
@@ -424,21 +473,21 @@
         return false;
     }
     
-    if (mType == VideoCollectionWrapper::EAllVideos)
+    if (mType == VideoCollectionCommon::EModelTypeAllVideos)
     {
         if(id.iId2 == KVcxMvcMediaTypeVideo)
         {
             return true;
         }
     }
-    else if(mType == VideoCollectionWrapper::ECollections)
+    else if(mType == VideoCollectionCommon::EModelTypeCollections)
     {
         if(mLevel == VideoCollectionCommon::ELevelCategory && id.iId2 != KVcxMvcMediaTypeVideo)
         {
             return true;
         }       
     }
-    else if (mType == VideoCollectionWrapper::ECollectionContent)
+    else if (mType == VideoCollectionCommon::EModelTypeCollectionContent)
     {
         // if item belongs to the open album, accept it
         if (mModel->belongsToAlbum(id))
@@ -446,7 +495,7 @@
             return true;
         }
     }
-    else if(mType == VideoCollectionWrapper::EGeneric)
+    else if(mType == VideoCollectionCommon::EModelTypeGeneric)
     {
         if(mLevel == VideoCollectionCommon::ELevelVideos && id.iId2 == KVcxMvcMediaTypeVideo)
         {
@@ -459,7 +508,6 @@
             {
                 return true;
             }
-            
         }
         else if(mLevel == VideoCollectionCommon::ELevelCategory && id.iId2 == KVcxMvcMediaTypeAlbum)    
         {
@@ -521,6 +569,7 @@
 //
 TMPXItemId VideoSortFilterProxyModel::addNewAlbum(const QString &title)
 {
+	FUNC_LOG_ADDR(this);
     TMPXItemId id = TMPXItemId::InvalidId();
     
     if (mCollectionClient)
@@ -537,6 +586,7 @@
 //
 int VideoSortFilterProxyModel::removeAlbums(const QModelIndexList &indexList)
 {
+	FUNC_LOG_ADDR(this);
 	int err(-1);
     
 	if (mCollectionClient)
@@ -569,13 +619,14 @@
 QString VideoSortFilterProxyModel::resolveAlbumName(
     const QString& albumName) const
 {
+	FUNC_LOG_ADDR(this);
     QString resolvedName = albumName.trimmed();
 
     // for checking names, we need to use collection list proxy model 
     // to get all existing album names including default ones
     VideoSortFilterProxyModel *collectionModel = 
                        VideoCollectionWrapper::instance().getModel(
-                                   VideoCollectionWrapper::ECollections);
+                                   VideoCollectionCommon::EModelTypeCollections);
     if(!collectionModel || !mModel)
     {
         return resolvedName;
@@ -588,11 +639,10 @@
     // create set of existing names
     while (proxyIndex.isValid())
     {
-        data = mModel->data(collectionModel->mapToSource(proxyIndex), Qt::DisplayRole);
+        data = mModel->data(collectionModel->mapToSource(proxyIndex), VideoCollectionCommon::KeyTitle);
         if (data.isValid())
         {
-            QStringList stringList = data.toStringList();
-            names.insert(stringList.at(0));
+            names.insert(data.toString());
         }
         proxyIndex = collectionModel->index(++i, 0, QModelIndex());
     }
@@ -618,6 +668,7 @@
 int VideoSortFilterProxyModel::addItemsInAlbum(TMPXItemId &albumId,
         const QList<TMPXItemId> &mediaIds)
 {
+	FUNC_LOG_ADDR(this);
     int err(-1);
     
     if (mCollectionClient)
@@ -636,6 +687,7 @@
 int VideoSortFilterProxyModel::removeItemsFromAlbum(TMPXItemId &albumId, 
         const QList<TMPXItemId> &mediaIds)
 {
+	FUNC_LOG_ADDR(this);
     int err(-1);
        
     if (mModel)
@@ -645,6 +697,7 @@
         if(err > 0)
         {
             // if there really were items to be removed, invalid filter
+            INFO_1("VideoSortFilterProxyModel::removeItemsFromAlbum() [0x%x] items removed, invaliding.", this);
             invalidateFilter();
             err = 0;
         }
@@ -653,22 +706,41 @@
 }
 
 // -----------------------------------------------------------------------------
+// VideoSortFilterProxyModel::renameAlbum()
+// -----------------------------------------------------------------------------
+//
+int VideoSortFilterProxyModel::renameAlbum(const TMPXItemId &albumId, const QString &newTitle)
+{
+	FUNC_LOG_ADDR(this);
+    int err(-1);
+
+    if(mCollectionClient)
+    {
+        return mCollectionClient->renameAlbum(albumId, newTitle);
+    }
+    return err;
+}
+
+// -----------------------------------------------------------------------------
 // VideoSortFilterProxyModel::getOpenItem()
 // -----------------------------------------------------------------------------
 //
 TMPXItemId VideoSortFilterProxyModel::getOpenItem() const
 {
+	FUNC_LOG_ADDR(this);
     TMPXItemId itemId = TMPXItemId::InvalidId();
     
     if(mModel && mCollectionClient)
     {
-        if(mType == VideoCollectionWrapper::EAllVideos)
+        if(mType == VideoCollectionCommon::EModelTypeAllVideos)
         {
+            INFO_1("VideoSortFilterProxyModel::getOpenItem() [0x%x] all videos is open.", this);
             itemId.iId1 = KVcxMvcCategoryIdAll;
-            itemId.iId2 = KVcxMvcMediaTypeCategory;        
+            itemId.iId2 = KVcxMvcMediaTypeCategory;
         }
-        else if(mType == VideoCollectionWrapper::ECollectionContent)
+        else if(mType == VideoCollectionCommon::EModelTypeCollectionContent)
         {
+            INFO_1("VideoSortFilterProxyModel::getOpenItem() [0x%x] category or album is open.", this);
             itemId = mModel->albumInUse();
         }
     }
@@ -682,10 +754,13 @@
 //
 void VideoSortFilterProxyModel::setGenericIdFilter(TMPXItemId itemId, bool filterValue)
 {
-    if(mType == VideoCollectionWrapper::EGeneric)
+	FUNC_LOG_ADDR(this);
+    if(mType == VideoCollectionCommon::EModelTypeGeneric)
     {
+        INFO_3("VideoSortFilterProxyModel::setGenericIdFilter() [0x%x] itemId.iId2: %d, filterValue: %d", this, itemId.iId2, filterValue);
         mGenericFilterId = itemId;
         mGenericFilterValue = filterValue;
+        INFO_1("VideoSortFilterProxyModel::setGenericIdFilter() [0x%x] invalidating.", this);
         invalidateFilter();
     }
 }
@@ -696,8 +771,13 @@
 //
 void VideoSortFilterProxyModel::setAlbumInUse(TMPXItemId albumId)
 {
-    mModel->setAlbumInUse(albumId);
-    invalidateFilter();
+	FUNC_LOG_ADDR(this);
+    if(mModel)
+    {
+        INFO_1("VideoSortFilterProxyModel::setAlbumInUse() [0x%x] invalidating.", this);
+        mModel->setAlbumInUse(albumId);
+        invalidateFilter();
+    }
 }
 
 // -----------------------------------------------------------------------------
@@ -706,12 +786,41 @@
 //
 void VideoSortFilterProxyModel::albumChangedSlot()
 {
+	FUNC_LOG_ADDR(this);
     // ignore if not collection content model
-    if (mType == VideoCollectionWrapper::ECollectionContent)
+    if (mType == VideoCollectionCommon::EModelTypeCollectionContent)
     {
-        invalidateFilter();
+        INFO_1("VideoSortFilterProxyModel::albumChangedSlot() [0x%x] invalidating.", this);
+	    //otherwise newle created album content won't sort
+        setSortRole(mWantedSortRole);
+        sort(0, mWantedSortOrder);
+    	invalidateFilter();
     }
 }
 
-// end of file
+// -----------------------------------------------------------------------------
+// VideoSortFilterProxyModel::itemModifiedSlot()
+// -----------------------------------------------------------------------------
+//
+void VideoSortFilterProxyModel::itemModifiedSlot(const TMPXItemId &itemId)
+{
+	FUNC_LOG_ADDR(this);
+    if(mType == VideoCollectionCommon::EModelTypeCollections &&
+       (itemId.iId2 == KVcxMvcMediaTypeAlbum || itemId.iId2 == KVcxMvcMediaTypeCategory))
+    {
+        INFO_1("VideoSortFilterProxyModel::itemModifiedSlot() [0x%x] invalidating.", this);
+        invalidate();
+    }
+}
 
+// -----------------------------------------------------------------------------
+// VideoSortFilterProxyModel::getType()
+// -----------------------------------------------------------------------------
+//
+VideoCollectionCommon::TModelType VideoSortFilterProxyModel::getType()
+{
+	FUNC_LOG_ADDR(this);
+	return mType;
+}
+
+// End of file