hgwidgets_plat/hgcacheproxymodel_api/inc/hgdataprovidermodel.h
changeset 20 a60f8b6b1d32
parent 17 a10844a9914d
child 21 53314abf04ed
equal deleted inserted replaced
17:a10844a9914d 20:a60f8b6b1d32
     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:
       
    15 *
       
    16 *  Version     : %version: 11 %
       
    17 */
       
    18 #ifndef HGDATAPROVIDERMODEL_H_
       
    19 #define HGDATAPROVIDERMODEL_H_
       
    20 
       
    21 #include <QObject>
       
    22 #include <QList>
       
    23 #include <QPair>
       
    24 #include <QPixmap>
       
    25 #include <QMap>
       
    26 #include <QMutex>
       
    27 #include <QAbstractItemModel>
       
    28 
       
    29 class HbIcon;
       
    30 class QVariant;
       
    31 class HgCacheProxyModel;
       
    32 
       
    33 #ifdef BUILD_CACHEPROXYMODEL
       
    34 #define CACHEPROXYMODEL_EXPORT Q_DECL_EXPORT
       
    35 #else
       
    36 #define CACHEPROXYMODEL_EXPORT Q_DECL_IMPORT
       
    37 #endif
       
    38 
       
    39 class HgDataProviderModelObserver
       
    40 {
       
    41 public:
       
    42     virtual void dataUpdated(int from, int to) = 0;    
       
    43 };
       
    44 
       
    45 class CACHEPROXYMODEL_EXPORT HgDataProviderModel : public QAbstractItemModel
       
    46 {
       
    47     Q_OBJECT
       
    48 
       
    49 public:
       
    50     HgDataProviderModel(QObject *parent = 0);
       
    51     ~HgDataProviderModel();   
       
    52 //to cooperate with buffer manager    
       
    53     void release(QList<int> list, bool silent = true);
       
    54     void request(QList<int> list, bool silent = false);  
       
    55     virtual void doReleaseData(QList<int> list, bool silent) = 0;
       
    56     virtual void doRequestData(QList<int> list, bool silent) = 0;  
       
    57     void registerObserver(HgDataProviderModelObserver* obs);
       
    58 
       
    59 public:
       
    60 //from QAbstractItemModel
       
    61     QModelIndex index(int row, int column,
       
    62                               const QModelIndex &parent = QModelIndex()) const;
       
    63     virtual QModelIndex parent(const QModelIndex &child) const;
       
    64     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
       
    65     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
       
    66     virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
       
    67     virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
       
    68     
       
    69 //cache management    
       
    70 protected:
       
    71     QVariant data(int idx, int role) const;
       
    72     void clearCache();
       
    73     int count() const;
       
    74     bool update(int pos, QVariant obj, int role, bool silent = false);
       
    75     bool update(int pos, QList< QPair< QVariant, int > >* list, bool silent = false);    
       
    76     bool updateIcon(int pos, QVariant obj, bool silent = false);
       
    77     void resetIcon(int idx);
       
    78     void newItem(QList< QPair< QVariant, int > >* list = NULL, bool silent = true);
       
    79     void newItem(QPair< QVariant, int > item, bool silent = true);
       
    80     void insertItem(int pos, QList< QPair< QVariant, int > >* list = NULL, bool silent = true);
       
    81     void insertItem(int pos, QPair< QVariant, int > item, bool silent = true);
       
    82     void clearItem(int pos, bool silent = false);
       
    83 
       
    84 private:
       
    85     void doInsertItem(int pos, QList< QPair< QVariant, int > >* list, bool silent);
       
    86 
       
    87 protected:
       
    88     void removeItem(int pos, bool silent = false);
       
    89     void removeItems(int pos, int size, bool silent = false);
       
    90     
       
    91     virtual QVariant defaultIcon() const = 0;
       
    92     inline bool containsRole(int idx, int role) const;
       
    93     inline bool isIndexValid(int idx) const;
       
    94 
       
    95 public:    
       
    96     void resetModel();
       
    97     
       
    98     enum HgDataProviderIconMode {
       
    99         HgDataProviderIconHbIcon,
       
   100         HgDataProviderIconQIcon,
       
   101         HgDataProviderIconQImage,
       
   102         HgDataProviderIconQPixmap
       
   103     };
       
   104     
       
   105     void setIconMode(HgDataProviderIconMode mode);
       
   106     HgDataProviderIconMode iconMode();
       
   107     
       
   108 protected:    
       
   109     virtual void doResetModel() {};
       
   110     virtual QVariant getData(int idx, int role) const {Q_UNUSED(idx); Q_UNUSED(role); return QVariant(); };
       
   111     
       
   112 // helpers fot emits
       
   113 protected:  
       
   114     void emitDataChanged(int start, int end, bool silent);
       
   115     
       
   116 //QPixmap pool
       
   117 public:
       
   118     void resizeQPixmapPool(int size);
       
   119     
       
   120 protected:  
       
   121     void releasePixmap(int idx);
       
   122     QVariant createIcon(int index, QPixmap aPixmap);
       
   123 
       
   124 private:
       
   125     QPixmap* getPixmap(int idx);
       
   126 
       
   127     
       
   128 private:
       
   129     QList<QMap<int, QVariant>*>* mCache;   
       
   130     int mCacheSize; //Number of cached Icons. Not same as mCache->count()
       
   131     QList< QPixmap* > mFreePixmaps;
       
   132     QMap< int, QPixmap* > mUsedPixmaps;    
       
   133     int mUnallocatedPixmaps;
       
   134     QMutex mQPixmapsLock;
       
   135     QMutex mDataLock;    
       
   136     HgDataProviderModelObserver *mObserver;
       
   137     HgDataProviderIconMode mIconMode;
       
   138 };
       
   139 
       
   140 inline bool HgDataProviderModel::isIndexValid(int idx) const
       
   141 {
       
   142     return ((idx >= 0) && 
       
   143             (idx < mCache->size()) && 
       
   144             (mCache->at(idx))); 
       
   145 }
       
   146 
       
   147 inline bool HgDataProviderModel::containsRole(int idx, int role) const
       
   148 {
       
   149     return ( isIndexValid(idx) && 
       
   150              mCache->at(idx)->contains(role) ); 
       
   151 }
       
   152 
       
   153 #endif // HGDATAPROVIDERMODEL_H_