videocollection/videocollectionwrapper/inc/videosortfilterproxymodel.h
changeset 62 0e1e938beb1a
parent 59 a76e86df7ccd
child 65 a9d57bd8d7b7
equal deleted inserted replaced
59:a76e86df7ccd 62:0e1e938beb1a
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Videolist sort filter proxy implementation
       
    15 * 
       
    16 */
       
    17 
       
    18 #ifndef VIDEOSORTFILTERPROXYMODEL_H
       
    19 #define VIDEOSORTFILTERPROXYMODEL_H
       
    20 
       
    21 #include <qsortfilterproxymodel.h>
       
    22 #include <e32const.h>
       
    23 #include <mpxitemid.h>
       
    24 #include "videocollectionexport.h"
       
    25 #include "videocollectioncommon.h"
       
    26 
       
    27 class QTimer;
       
    28 class VideoListDataModel;
       
    29 class VideoCollectionClient;
       
    30 
       
    31 class VIDEOCOLLECTION_DLL_EXPORT VideoSortFilterProxyModel : public QSortFilterProxyModel 
       
    32 {
       
    33     Q_OBJECT
       
    34     
       
    35 public: 
       
    36     
       
    37     /**
       
    38      * Contructor.
       
    39      *
       
    40      * @param parent parent of this widget
       
    41      * @param client Collection client pointer to use.
       
    42      */
       
    43     VideoSortFilterProxyModel(VideoCollectionCommon::TModelType type, QObject *parent=0);
       
    44     
       
    45     /**
       
    46      * Destructor.
       
    47      *
       
    48      */
       
    49     virtual ~VideoSortFilterProxyModel();
       
    50     
       
    51     /**
       
    52      * Initializes model, calls initialize to source model, and sets
       
    53      * the given sourceModel as source model for this proxy model.
       
    54      * 
       
    55      * @param collection mpx video collectionobject
       
    56      * @param sourceModel the source model
       
    57      * 
       
    58      * @return int: 0 if everything ok
       
    59      */
       
    60     int initialize(VideoListDataModel *sourceModel);
       
    61 
       
    62 public:
       
    63            
       
    64     /**
       
    65      * First call opens the collection at the defined level. Subsequent calls only change the level.
       
    66      * This starts populating the model data from the MyVideos collection plugin.
       
    67      * 
       
    68      * @param level The level where the collection is opened.
       
    69      * @return 0 if opening was successful, negative if not.
       
    70      */
       
    71     int open(VideoCollectionCommon::TCollectionLevels level);
       
    72     
       
    73     /**
       
    74      * Starts sorting. If async parameter is defined as true uses idle timer: 
       
    75      * starts timer with zero interval. When system has time to run timer sort()
       
    76      * -method is called. If async is false, then the sort() -method is called
       
    77      * immediatelly.
       
    78      * 
       
    79      * @param sortingRole The role that sorting is done by.
       
    80      * @param sortingOrder The sorting order, e.g. ascending or descending.
       
    81      * @param async Defines if the sorting is done asyncronously or not. Default
       
    82      *      is true.
       
    83      */
       
    84     void doSorting(int sortingRole, Qt::SortOrder order, bool async = true);
       
    85 
       
    86     /**
       
    87      * To get current actice sorting parameters of the model
       
    88      * 
       
    89      * @param sortingRole The role that sorting is done by.
       
    90      * @param sortingOrder The sorting order, e.g. ascending or descending.
       
    91      */
       
    92     void getSorting(int &sortingRole, Qt::SortOrder &order);
       
    93 
       
    94     
       
    95     /**
       
    96      * Method to delete items at provided indeces
       
    97      *
       
    98      * @param indexList list of indeces.
       
    99      * @return int 0 succeeds, -1 does not succeed.
       
   100      */
       
   101     int deleteItems(const QModelIndexList &indexList);
       
   102     
       
   103     /**
       
   104     * Opens the defined item. This is quite abstract as depending
       
   105     * on the type of the item, the behaviour or result could be
       
   106     * quite different.
       
   107     * 
       
   108     * @param item id id of the item to be opened
       
   109     * @return int 0 succeeds, <0 if opening does not succeed.
       
   110     */    
       
   111     int openItem(TMPXItemId mediaId);
       
   112 
       
   113     /**
       
   114     * Return to collection level
       
   115     *
       
   116     */    
       
   117     int back();
       
   118 
       
   119     /**
       
   120      * Starts fetching full details for the defined item.
       
   121      * 
       
   122      * @param index index of the item from where to get details from
       
   123      * @return int 0 succeeds, < 0 if detail fetching startup does not succeed.
       
   124      */   
       
   125     int fetchItemDetails(const QModelIndex &index);
       
   126 
       
   127     /**
       
   128      * Method checks that model exists and asks for mpx id from
       
   129      * given object.
       
   130      * 
       
   131      * @param index index of the item in UI.
       
   132      * @return TMPXItemId mpx id if succeeds TMPXItemId::InvalidId() in case of error
       
   133      *  
       
   134      */
       
   135     const TMPXItemId& getMediaIdAtIndex(const QModelIndex &index) const;
       
   136   
       
   137     /**
       
   138      * returns qmodelindex of item id provided.
       
   139      * 
       
   140      * @param item id
       
   141      * @return QModelIndex;
       
   142      */
       
   143     QModelIndex indexOfId(const TMPXItemId &id);
       
   144     
       
   145     /**
       
   146      * Method checks that model exists and asks for file path from
       
   147      * given object.
       
   148      * 
       
   149      * @param mediaId id of the item.
       
   150      * @return QString file path if succeeds, empty string in case of error.
       
   151      *  
       
   152      */
       
   153     QString getMediaFilePathForId(const TMPXItemId &mediaId);
       
   154         
       
   155     /**
       
   156      * Add a new album.
       
   157      * 
       
   158      * @param title, Album title.
       
   159      * @param mediaIds, Media items to add in the album.
       
   160      * @return TMPXItemId id of created album TMPXItemId:::InvalidId() in case of failure
       
   161      */
       
   162     TMPXItemId addNewAlbum(const QString &title);
       
   163 	
       
   164     /**
       
   165      * Remove an album.
       
   166      * 
       
   167      * @param indexList, list of album indexes to be removed.
       
   168      * @return 0 if no errors.
       
   169      */
       
   170     int removeAlbums(const QModelIndexList &indexList);
       
   171 
       
   172 	/**
       
   173 	 * Add items in an existing album.
       
   174 	 * 
       
   175 	 * @param albumId, Album where to add items.
       
   176 	 * @param mediaIds, list of item ids
       
   177 	 * @return 0 if no errors.
       
   178 	 */
       
   179     int addItemsInAlbum(TMPXItemId &albumId, const QList<TMPXItemId> &mediaIds);    
       
   180     
       
   181     /**
       
   182      * Removes items from existing album.
       
   183      * 
       
   184      * @param albumId, Album where to add items.
       
   185      * @param mediaIds, list of item ids
       
   186      * @return 0 if no errors.
       
   187      */
       
   188     int removeItemsFromAlbum(TMPXItemId &albumId, const QList<TMPXItemId> &mediaIds);    
       
   189 	
       
   190     /**
       
   191      * Renames a video or user defined colletion.
       
   192      * 
       
   193      * @param itemId, id of item  to be renamed.
       
   194      * @param newTitle, New title for the item.
       
   195      * @return 0 if no errors.
       
   196      */
       
   197     int renameItem(const TMPXItemId &itemId, const QString &newTitle);
       
   198 
       
   199     /**
       
   200 	 * Resolves duplicate album names and returns the resolved name.
       
   201 	 * 'New collection' -> 'New collection (1)' -> 'New collection (2)', etc.
       
   202 	 * 
       
   203 	 * @param albumName, Name specified by the user.
       
   204 	 * @return Resolved name given to the album.
       
   205 	 */
       
   206     QString resolveAlbumName(const QString& albumName) const;
       
   207 	
       
   208     /**
       
   209      * sets item id filter used in generic proxy model
       
   210      * while filtering rows. 
       
   211      * 
       
   212      * If provided filterValue -flag is true, we filter off items that 
       
   213      * do not exist in container indicated as itemId. 
       
   214      * If false, filtering works other way around. 
       
   215      * 
       
   216      * 
       
   217      * @param filterValue item id used as filter
       
   218      * @param filterValue
       
   219      */
       
   220     void setGenericIdFilter(const TMPXItemId &itemId, bool filterValue);
       
   221     
       
   222     /**
       
   223      * Set album in use and invalidates filtering.
       
   224      * 
       
   225      * @param albumId album id
       
   226      */
       
   227     void setAlbumInUse(const TMPXItemId &albumId);
       
   228     
       
   229 	/**
       
   230 	 * Gets the currently opened item.
       
   231 	 * 
       
   232 	 * @param None.
       
   233 	 * @return TMPXItemId.
       
   234 	 */
       
   235     TMPXItemId getOpenItem() const;
       
   236 	
       
   237 	/**
       
   238 	 * Gets the type of model.
       
   239 	 * 
       
   240 	 * @param None.
       
   241 	 * @return TModelType.
       
   242 	 */
       
   243     VideoCollectionCommon::TModelType getType();
       
   244     
       
   245 signals:
       
   246 
       
   247     /**
       
   248     * Signal to be emitted if detail fetching started ok
       
   249     * 
       
   250     * @param index, index of the item
       
   251     */ 
       
   252     void shortDetailsReady(TMPXItemId itemId);
       
   253     
       
   254     /**
       
   255      * Signals that the model is ready, ie. that all data has been
       
   256      * loaded from myvideoscollection.
       
   257      */
       
   258     void modelReady();
       
   259     
       
   260     /**
       
   261      * Notifies that model's physical data structure has changed:
       
   262      * - item inserted
       
   263      * - item removed 
       
   264      */
       
   265     void modelChanged();
       
   266  
       
   267     /**
       
   268      * Notifies that model has been sorted.
       
   269      */
       
   270     void modelSorted();
       
   271 
       
   272 protected: // from QSortFilterProxyModel
       
   273     
       
   274     /**
       
   275      * Compares items based in the role setted before sorting.
       
   276      *  
       
   277      */
       
   278     bool lessThan ( const QModelIndex & left, const QModelIndex & right ) const;
       
   279     
       
   280     /**
       
   281      * Filtering functionality. Called by the fw.
       
   282      *  
       
   283      * @param source_row row to chexk
       
   284      * @param source_parent row's parent
       
   285      * 
       
   286      * @return bool row accepted(true) or not accepted(false)
       
   287      */
       
   288     bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const;
       
   289 
       
   290 private slots:
       
   291 
       
   292     /**
       
   293      * Timeout slot for zero timer. Stops timer and calls sort(). 
       
   294      * After call is finished, emits sortingReady.
       
   295      *  
       
   296      */
       
   297     void processSorting();
       
   298     
       
   299     /**
       
   300      * signaled when particular album content has changed and 
       
   301      * refiltering is required
       
   302      */
       
   303     void albumChangedSlot();
       
   304     
       
   305 private:
       
   306     
       
   307     /**
       
   308      * disabled contructor
       
   309      */
       
   310     VideoSortFilterProxyModel(QObject *parent=0);
       
   311     
       
   312     /**
       
   313      * connects all signals emitted from or throught this object
       
   314      * 
       
   315      * @return bool
       
   316      */
       
   317     bool connectSignals();
       
   318     
       
   319     /**
       
   320      * disconnects all signals
       
   321      */
       
   322     void disconnectSignals();
       
   323     
       
   324 private:
       
   325     
       
   326     /**
       
   327      * Pointer to the actual model.
       
   328      * Not owned.
       
   329      */
       
   330     VideoListDataModel *mModel;
       
   331     
       
   332     /**
       
   333      * Collection client.
       
   334      * Not owned. 
       
   335      */
       
   336     VideoCollectionClient *mCollectionClient;
       
   337     
       
   338     /**
       
   339      * type of data excepted
       
   340      */
       
   341     VideoCollectionCommon::TModelType mType;
       
   342     
       
   343     /**
       
   344      * Currently open level.
       
   345      */
       
   346     VideoCollectionCommon::TCollectionLevels mLevel;
       
   347     
       
   348     /**
       
   349      * item id used as filter if model type is generic model
       
   350      */
       
   351     TMPXItemId mGenericFilterId;
       
   352     
       
   353     /**
       
   354      * flag used 
       
   355      */
       
   356     bool mGenericFilterValue;
       
   357     
       
   358     /**
       
   359      * Timer object. Owned.
       
   360      */
       
   361     QTimer  *mIdleSortTimer;
       
   362     
       
   363     /**
       
   364      * sort role to be setted after idle timer time outs for sorting to be able to be executed
       
   365      */
       
   366     int mWantedSortRole;
       
   367     
       
   368     /**
       
   369      * sort order (ascending or descending).
       
   370      */
       
   371     Qt::SortOrder mWantedSortOrder;
       
   372     
       
   373 };
       
   374 
       
   375 #endif  //VIDEOSORTFILTERPROXYMODEL_H