|
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: 3 % |
|
17 */ |
|
18 #ifndef HGDATAPROVIDERMODEL_H_ |
|
19 #define HGDATAPROVIDERMODEL_H_ |
|
20 |
|
21 #include <QObject> |
|
22 #include <QList> |
|
23 #include <QPair> |
|
24 #include <QPixmap.h> |
|
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 |
|
83 private: |
|
84 void doInsertItem(int pos, QList< QPair< QVariant, int > >* list, bool silent); |
|
85 |
|
86 protected: |
|
87 void removeItem(int pos); |
|
88 void removeItems(int pos, int size); |
|
89 |
|
90 virtual QVariant defaultIcon() const = 0; |
|
91 inline bool containsRole(int idx, int role) const; |
|
92 inline bool isIndexValid(int idx) const; |
|
93 |
|
94 public: |
|
95 void resetModel(); |
|
96 |
|
97 protected: |
|
98 virtual void doResetModel() {}; |
|
99 |
|
100 // helpers fot emits |
|
101 protected: |
|
102 void emitDataChanged(int start, int end, bool silent); |
|
103 |
|
104 //QPixmap pool |
|
105 public: |
|
106 void resizeQPixmapPool(int size); |
|
107 |
|
108 protected: |
|
109 void releasePixmap(int idx); |
|
110 QVariant createIcon(int index, QPixmap aPixmap); |
|
111 |
|
112 private: |
|
113 QPixmap* getPixmap(int idx); |
|
114 |
|
115 |
|
116 private: |
|
117 QList<QMap<int, QVariant>*>* mCache; |
|
118 int mCacheSize; //Number of cached Icons. Not same as mCache->count() |
|
119 QList< QPixmap* > mFreePixmaps; |
|
120 QMap< int, QPixmap* > mUsedPixmaps; |
|
121 int mUnallocatedPixmaps; |
|
122 QMutex mQPixmapsLock; |
|
123 HgDataProviderModelObserver *mObserver; |
|
124 }; |
|
125 |
|
126 inline bool HgDataProviderModel::isIndexValid(int idx) const |
|
127 { |
|
128 return ((0 <= idx) && |
|
129 (idx < mCache->size()) && |
|
130 (mCache->at(idx))); |
|
131 } |
|
132 |
|
133 inline bool HgDataProviderModel::containsRole(int idx, int role) const |
|
134 { |
|
135 return ( isIndexValid(idx) && |
|
136 mCache->at(idx)->contains(role) ); |
|
137 } |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 #endif // HGDATAPROVIDERMODEL_H_ |