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