|
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 #include <e32debug.h> |
|
19 #include <QVariant> |
|
20 #include <HbIcon.h> |
|
21 #include <qpixmapdata_p.h> |
|
22 #include <hgwidgets/hgdataprovidermodel.h> |
|
23 #include <hgwidgets/hgcacheproxymodel.h> |
|
24 #include "hglogger.h" |
|
25 |
|
26 |
|
27 const int KQPixmapCacheEmergencyBuffer = 5; |
|
28 |
|
29 HgDataProviderModel::HgDataProviderModel(QObject *parent) : |
|
30 QAbstractItemModel(parent), |
|
31 mCache(new QList<QMap<int, QVariant>*>()), |
|
32 mCacheSize(0), |
|
33 mUnallocatedPixmaps(0), |
|
34 mObserver(0) |
|
35 { |
|
36 TX_ENTRY |
|
37 TX_EXIT |
|
38 } |
|
39 |
|
40 HgDataProviderModel::~HgDataProviderModel() |
|
41 { |
|
42 TX_ENTRY |
|
43 clearCache(); |
|
44 delete mCache; |
|
45 qDeleteAll( mFreePixmaps.begin(), mFreePixmaps.end() ); |
|
46 mFreePixmaps.clear(); |
|
47 qDeleteAll( mUsedPixmaps.begin(), mUsedPixmaps.end() ); |
|
48 mUsedPixmaps.clear(); |
|
49 TX_EXIT |
|
50 } |
|
51 |
|
52 void HgDataProviderModel::release(QList<int> list, bool silent) |
|
53 { |
|
54 TX_ENTRY |
|
55 int i=0; |
|
56 int min = count(); |
|
57 int max = 0; |
|
58 |
|
59 for ( int idx = 0; idx < list.count(); idx++){ |
|
60 i = list[idx]; |
|
61 if ( i >=0 && i<count()){ |
|
62 if ( i < min) |
|
63 min = i; |
|
64 if ( i > max) |
|
65 max = i; |
|
66 resetIcon(i); |
|
67 } |
|
68 } |
|
69 |
|
70 doReleaseData(list, silent); |
|
71 |
|
72 if (min<max){ //min<max is true if at least one item is in range <0,count()) |
|
73 emitDataChanged(min, max, silent); |
|
74 } |
|
75 TX_EXIT |
|
76 } |
|
77 |
|
78 void HgDataProviderModel::request(QList<int> list, bool silent) |
|
79 { |
|
80 doRequestData(list, silent); |
|
81 } |
|
82 |
|
83 void HgDataProviderModel::registerObserver(HgDataProviderModelObserver* obs) |
|
84 { |
|
85 TX_ENTRY |
|
86 mObserver = obs; |
|
87 TX_EXIT |
|
88 } |
|
89 |
|
90 QModelIndex HgDataProviderModel::index(int row, int column, |
|
91 const QModelIndex &parent) const |
|
92 { |
|
93 Q_UNUSED(parent); |
|
94 if ( row >= rowCount() ){ |
|
95 row = -1; |
|
96 } |
|
97 if ( column >= columnCount() ){ |
|
98 column = -1; |
|
99 } |
|
100 |
|
101 return QAbstractItemModel::createIndex(row, column); |
|
102 } |
|
103 |
|
104 QModelIndex HgDataProviderModel::parent(const QModelIndex &child) const |
|
105 { |
|
106 Q_UNUSED(child); |
|
107 return QModelIndex(); //returns always invalid model index |
|
108 } |
|
109 |
|
110 int HgDataProviderModel::rowCount(const QModelIndex &parent) const |
|
111 { |
|
112 Q_UNUSED(parent); |
|
113 return count(); |
|
114 } |
|
115 |
|
116 int HgDataProviderModel::columnCount(const QModelIndex &parent) const |
|
117 { |
|
118 Q_UNUSED(parent); |
|
119 return 1; |
|
120 } |
|
121 |
|
122 QVariant HgDataProviderModel::data(const QModelIndex &index, int role) const |
|
123 { |
|
124 return data(index.row(), role); |
|
125 } |
|
126 |
|
127 QVariant HgDataProviderModel::data(int idx, int role) const |
|
128 { |
|
129 QVariant res; |
|
130 if ( containsRole(idx, role)){ |
|
131 res = mCache->at(idx)->value(role); |
|
132 } else if (role == Qt::DecorationRole ){ |
|
133 res = defaultIcon(); |
|
134 } |
|
135 return res; |
|
136 } |
|
137 |
|
138 QMap<int, QVariant> HgDataProviderModel::itemData(const QModelIndex &index) const |
|
139 { |
|
140 QMap<int, QVariant> res; |
|
141 if ( index.row()>=0 && index.row()<count() ){ |
|
142 res = QMap<int, QVariant>(*mCache->at(index.row())); |
|
143 } |
|
144 return res; |
|
145 } |
|
146 |
|
147 void HgDataProviderModel::clearCache() |
|
148 { |
|
149 qDeleteAll( mCache->begin(), mCache->end() ); |
|
150 mCache->clear(); |
|
151 } |
|
152 |
|
153 int HgDataProviderModel::count() const |
|
154 { |
|
155 return mCache->count(); |
|
156 } |
|
157 |
|
158 bool HgDataProviderModel::update(int pos, QList< QPair< QVariant, int > >* list, bool silent) |
|
159 { |
|
160 bool change(false); |
|
161 if (list && list->count() && pos >=0 && pos<count() && mCache->at(pos)) { |
|
162 while(list->count()){ |
|
163 QPair< QVariant, int > pair = list->takeFirst(); |
|
164 change = update(pos, pair.first, pair.second, true)|change; |
|
165 } |
|
166 if ( !silent && change ){ |
|
167 emitDataChanged(pos, pos, false); |
|
168 } |
|
169 } |
|
170 return change; |
|
171 } |
|
172 |
|
173 bool HgDataProviderModel::update(int pos, QVariant obj, int role, bool silent) |
|
174 { |
|
175 bool change(false); |
|
176 |
|
177 if ( pos >=0 && pos<count() && mCache->at(pos)){ |
|
178 mCache->at(pos)->insert(role, obj); //this will remove old one if needed |
|
179 change = true; |
|
180 } |
|
181 |
|
182 if ( !silent && change ){ |
|
183 emitDataChanged(pos, pos, false); |
|
184 } |
|
185 return change; |
|
186 } |
|
187 |
|
188 bool HgDataProviderModel::updateIcon(int pos, QVariant obj, bool silent) |
|
189 { |
|
190 if (obj.isValid()==false) |
|
191 return false; |
|
192 |
|
193 bool change(false); |
|
194 if ( pos >=0 && pos<count() && mCache->at(pos)){ |
|
195 mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed |
|
196 change = true; |
|
197 } |
|
198 |
|
199 if (!silent && change){ |
|
200 TX_LOG |
|
201 if ( mObserver){ |
|
202 mObserver->dataUpdated(pos,pos); |
|
203 } else { |
|
204 QModelIndex topLeft = index(pos, 0); |
|
205 QModelIndex bottomRight = index(pos, 0); |
|
206 emit dataChanged(topLeft,bottomRight); |
|
207 } |
|
208 } |
|
209 return change; |
|
210 } |
|
211 |
|
212 void HgDataProviderModel::resetIcon(int pos) |
|
213 { |
|
214 if ( containsRole(pos, Qt::DecorationRole)){ |
|
215 mCache->at(pos)->remove(Qt::DecorationRole); |
|
216 } |
|
217 } |
|
218 |
|
219 void HgDataProviderModel::newItem(QList< QPair< QVariant, int > >* list, bool silent) |
|
220 { |
|
221 insertItem(mCache->count(), list, silent); |
|
222 } |
|
223 |
|
224 void HgDataProviderModel::newItem(QPair< QVariant, int > item, bool silent) |
|
225 { |
|
226 insertItem(mCache->count(), item, silent); |
|
227 } |
|
228 |
|
229 void HgDataProviderModel::insertItem(int pos, QList< QPair< QVariant, int > >* list, bool silent) |
|
230 { |
|
231 doInsertItem(pos, list, silent); |
|
232 } |
|
233 |
|
234 void HgDataProviderModel::insertItem(int pos, QPair< QVariant, int > item, bool silent) |
|
235 { |
|
236 QList< QPair< QVariant, int > > list; |
|
237 list.append(item); |
|
238 doInsertItem(pos, &list, silent); |
|
239 } |
|
240 |
|
241 void HgDataProviderModel::doInsertItem(int pos, QList< QPair< QVariant, int > >* list, bool silent) |
|
242 { |
|
243 if (pos >mCache->count()){ |
|
244 pos = mCache->count(); |
|
245 } else if (pos <0){ |
|
246 pos = 0; |
|
247 } |
|
248 |
|
249 if ( !silent){ |
|
250 beginInsertRows(QModelIndex(), pos, pos); |
|
251 } |
|
252 |
|
253 mCache->insert(pos, new QMap<int, QVariant>()); |
|
254 if (list && list->count()){ |
|
255 update(pos, list, true); |
|
256 } |
|
257 |
|
258 if ( !silent){ |
|
259 endInsertRows(); |
|
260 } |
|
261 } |
|
262 |
|
263 |
|
264 void HgDataProviderModel::removeItem(int pos) |
|
265 { |
|
266 removeItems(pos, 1); |
|
267 } |
|
268 |
|
269 void HgDataProviderModel::removeItems(int pos, int size) |
|
270 { |
|
271 if (pos >mCache->count()) |
|
272 return; |
|
273 else if (pos <0){ |
|
274 size = size + pos; //pos <0 |
|
275 pos = 0; |
|
276 } |
|
277 |
|
278 if (size >mCache->count()){ |
|
279 size = mCache->count(); |
|
280 } else if (size <0){ |
|
281 return; |
|
282 } |
|
283 |
|
284 beginRemoveRows(QModelIndex(),pos, pos+size-1); |
|
285 for (int i=0; i<size && pos<mCache->count(); i++){ |
|
286 mCache->removeAt(pos); |
|
287 } |
|
288 endRemoveRows(); |
|
289 } |
|
290 |
|
291 void HgDataProviderModel::resetModel() |
|
292 { |
|
293 beginResetModel(); |
|
294 doResetModel(); |
|
295 endResetModel(); |
|
296 } |
|
297 |
|
298 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent) |
|
299 { |
|
300 if ( !silent ){ |
|
301 TX_LOG |
|
302 QModelIndex topLeft = index(from, 0); |
|
303 QModelIndex bottomRight = index(to, 0); |
|
304 emit dataChanged(topLeft,bottomRight); |
|
305 } |
|
306 } |
|
307 |
|
308 void HgDataProviderModel::resizeQPixmapPool(int newSize) |
|
309 { |
|
310 mQPixmapsLock.lock(); |
|
311 int currentSize = mFreePixmaps.count() + mUsedPixmaps.count(); |
|
312 int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer; |
|
313 mUnallocatedPixmaps = 0; |
|
314 while (diff != 0){ |
|
315 if (diff < 0){ |
|
316 mUnallocatedPixmaps++; |
|
317 diff++; |
|
318 }else{ |
|
319 if (mUnallocatedPixmaps>0){ |
|
320 mUnallocatedPixmaps--; |
|
321 } else if (mFreePixmaps.count()){ |
|
322 mFreePixmaps.removeLast(); |
|
323 } //else will be deleted with releasePixmap; |
|
324 diff--; |
|
325 } |
|
326 } |
|
327 mQPixmapsLock.unlock(); |
|
328 mCacheSize = newSize; |
|
329 } |
|
330 |
|
331 void HgDataProviderModel::releasePixmap(int idx) |
|
332 { |
|
333 mQPixmapsLock.lock(); |
|
334 if (mUsedPixmaps.contains(idx)){ |
|
335 QPixmap* pix = mUsedPixmaps.take(idx); |
|
336 if ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps> mCacheSize + KQPixmapCacheEmergencyBuffer){ |
|
337 delete pix; //we have too many pixmaps |
|
338 }else{ |
|
339 mFreePixmaps.append(pix); |
|
340 } |
|
341 }else{ |
|
342 TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx)); |
|
343 } |
|
344 mQPixmapsLock.unlock(); |
|
345 } |
|
346 |
|
347 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap) |
|
348 { |
|
349 QPixmap* pix = getPixmap(index); |
|
350 if (pix){ |
|
351 if ( pix->pixmapData() ){ |
|
352 pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor ); |
|
353 } else { |
|
354 *pix = aPixmap; |
|
355 } |
|
356 mQPixmapsLock.lock(); |
|
357 mUsedPixmaps.insert(index, pix); |
|
358 mQPixmapsLock.unlock(); |
|
359 return HbIcon(QIcon(*pix)); |
|
360 } |
|
361 TX_EXIT_ARGS( QString("No pixmap avilable")); |
|
362 return QVariant(); |
|
363 } |
|
364 |
|
365 QPixmap* HgDataProviderModel::getPixmap(int idx) |
|
366 { |
|
367 TX_ENTRY |
|
368 QPixmap* res = NULL; |
|
369 mQPixmapsLock.lock(); |
|
370 if ( mUsedPixmaps.contains(idx)){ |
|
371 res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap |
|
372 } else { |
|
373 if (!mFreePixmaps.isEmpty()){ |
|
374 res = mFreePixmaps.takeFirst(); |
|
375 }else if (mUnallocatedPixmaps){ |
|
376 mUnallocatedPixmaps--; |
|
377 res = new QPixmap(); |
|
378 } else { |
|
379 TX_LOG_ARGS(QString("no free pixmaps")); |
|
380 } |
|
381 } |
|
382 mQPixmapsLock.unlock(); |
|
383 TX_EXIT |
|
384 return res; |
|
385 } |
|
386 |
|
387 //eof |