hgcacheproxymodel/src/hgdataprovidermodel.cpp
branchGCC_SURGE
changeset 10 c348b9772569
parent 8 15f034b8a3b5
child 17 a10844a9914d
--- a/hgcacheproxymodel/src/hgdataprovidermodel.cpp	Fri Jun 11 16:25:05 2010 +0100
+++ b/hgcacheproxymodel/src/hgdataprovidermodel.cpp	Thu Jul 22 16:36:56 2010 +0100
@@ -13,11 +13,11 @@
 *
 * Description:
 *
-*  Version     : %version: 6 %
+*  Version     : %version: 15 %
 */
 #include <e32debug.h>
 #include <QVariant>
-#include <HbIcon.h>
+#include <HbIcon>
 #include <qpixmapdata_p.h>
 #include <hgwidgets/hgdataprovidermodel.h>
 #include <hgwidgets/hgcacheproxymodel.h>
@@ -31,7 +31,8 @@
     mCache(new QList<QMap<int, QVariant>*>()),
     mCacheSize(0),
     mUnallocatedPixmaps(0),
-    mObserver(0)
+    mObserver(0),
+    mIconMode(HgDataProviderIconHbIcon)
 {
     TX_ENTRY
     TX_EXIT
@@ -132,8 +133,13 @@
     QVariant res;
     if ( containsRole(idx, role)){
         res = mCache->at(idx)->value(role);
-    } else if (role == Qt::DecorationRole ){
-        res = defaultIcon();
+    } else if (isIndexValid(idx)){
+        if (role == Qt::DecorationRole ){
+            res = defaultIcon();
+        } else {
+            res = getData(idx,role);
+        }
+        
     }
     return res;
 }
@@ -149,6 +155,10 @@
 
 void HgDataProviderModel::clearCache()
 {
+    for ( int i=0; i<count(); i++){
+        releasePixmap(i);
+    }
+    
     qDeleteAll( mCache->begin(), mCache->end() );
     mCache->clear();
 }
@@ -161,8 +171,8 @@
 bool HgDataProviderModel::update(int pos, QList< QPair< QVariant, int > >* list, bool silent)
 {
     bool change(false);
-    if (list && list->count() && pos >=0 && pos<count() && mCache->at(pos)) {
-        while(list->count()){
+    if (list && list->count() && isIndexValid(pos)) {
+        while(list->count()>0){
             QPair< QVariant, int > pair = list->takeFirst();
             change = update(pos, pair.first, pair.second, true)|change;
         }
@@ -178,7 +188,9 @@
     bool change(false);
     
     if ( isIndexValid(pos)){
+        mDataLock.lock();
         mCache->at(pos)->insert(role, obj); //this will remove old one if needed
+        mDataLock.unlock();        
         change = true;
     }
     
@@ -192,7 +204,9 @@
 {
     bool change(false);
     if ( obj.isValid() && !obj.isNull() && isIndexValid(pos) ){
+        mDataLock.lock();
         mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed
+        mDataLock.unlock();        
         change = true;
         if (!silent){
             TX_LOG_ARGS(QString("pos:%1").arg( pos ) );
@@ -237,19 +251,35 @@
     doInsertItem(pos, &list, silent);
 }
 
+void HgDataProviderModel::clearItem(int pos, bool silent)
+{
+    bool change = false;
+    if ( isIndexValid(pos)){
+        mDataLock.lock();    
+        mCache->at(pos)->clear();
+        mDataLock.unlock();
+        change = true;
+    }
+    
+    if ( change && !silent){
+        emit dataChanged( index(pos, 0), index(pos, 0) );
+    }
+}
+
 void HgDataProviderModel::doInsertItem(int pos, QList< QPair< QVariant, int > >* list, bool silent)
 {
-    if (pos >mCache->count()){
-        pos = mCache->count();
-    } else if (pos <0){
-        pos = 0;
+    if (pos >mCache->count() || pos <0){
+        return;
     }
     
     if ( !silent){
         beginInsertRows(QModelIndex(), pos, pos);
     }
     
+    mDataLock.lock();    
     mCache->insert(pos, new QMap<int, QVariant>());
+    mDataLock.unlock();
+    
     if (list && list->count()){
         update(pos, list, true);
     }
@@ -260,33 +290,42 @@
 }
 
 
-void HgDataProviderModel::removeItem(int pos)
+void HgDataProviderModel::removeItem(int pos, bool silent)
 {
-    removeItems(pos, 1);
+    removeItems(pos, 1, silent);
 }
 
-void HgDataProviderModel::removeItems(int pos, int size)
+void HgDataProviderModel::removeItems(int pos, int size, bool silent)
 {
-    if (pos >mCache->count())
+    if (pos >=mCache->count()){
         return;
-    else if (pos <0){
+    } else if (pos <0){
         size = size + pos; //pos <0
         pos = 0;
     }
     
     if (size >mCache->count()){
         size = mCache->count();
-    } else if (size <0){
+    }
+    if (size <=0){
         return;
     }
+    if (!silent){
+        beginRemoveRows(QModelIndex(),pos, pos+size-1);
+    } else {
+        qWarning("Removing items without notifying might be danger.");
+    }
     
-    beginRemoveRows(QModelIndex(),pos, pos+size-1);
+    mDataLock.lock();
     for (int i=0; i<size && pos<mCache->count(); i++){
         mCache->removeAt(pos);
     }
-    endRemoveRows();
+    mDataLock.unlock();
+    
+    if (!silent)
+        endRemoveRows();
 }
-        
+
 void HgDataProviderModel::resetModel() 
 {
     beginResetModel();
@@ -294,6 +333,16 @@
     endResetModel();
 }
 
+void HgDataProviderModel::setIconMode(HgDataProviderModel::HgDataProviderIconMode mode)
+{
+    mIconMode = mode;
+}
+
+HgDataProviderModel::HgDataProviderIconMode HgDataProviderModel::iconMode()
+{
+    return mIconMode;
+}
+
 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent)
 {
     if ( !silent ){
@@ -306,7 +355,7 @@
 
 void HgDataProviderModel::resizeQPixmapPool(int newSize)
 {
-//    mQPixmapsLock.lock();
+    mQPixmapsLock.lock();
     int currentSize = mFreePixmaps.count() + mUsedPixmaps.count();
     int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer;
     mUnallocatedPixmaps = 0;
@@ -323,16 +372,16 @@
             diff--;
         }
     }
-//    mQPixmapsLock.unlock();
+    mQPixmapsLock.unlock();
     mCacheSize = newSize;
 }
 
 void HgDataProviderModel::releasePixmap(int idx)
 {
-//    mQPixmapsLock.lock();
+    mQPixmapsLock.lock();
     if (mUsedPixmaps.contains(idx)) {
         QPixmap* pix = mUsedPixmaps.take(idx);
-        if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) > ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
+        if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) >= ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
             delete pix; //we have too many pixmaps
         } else {
             mFreePixmaps.append(pix);
@@ -340,12 +389,13 @@
     } else {
 //        TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx));
     }
-//    mQPixmapsLock.unlock();    
+    mQPixmapsLock.unlock();    
 }
 
 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
 {
     TX_ENTRY
+    QVariant res;
 	QPixmap* pix = getPixmap(index);
 	if (pix){
 		if ( pix->pixmapData() ) {
@@ -353,20 +403,39 @@
 		} else {
 			*pix = aPixmap;
 		}
-//		mQPixmapsLock.lock();
+		mQPixmapsLock.lock();
 		mUsedPixmaps.insert(index, pix);
-//		mQPixmapsLock.unlock();
-		return HbIcon(QIcon(*pix));	
+		mQPixmapsLock.unlock();
+		switch (mIconMode){
+            case HgDataProviderIconHbIcon : 
+                res = HbIcon(QIcon(*pix));  
+                break;
+		    case HgDataProviderIconQIcon : 
+                res = QIcon(*pix);
+                break;
+		    case HgDataProviderIconQImage : 
+                res = pix->toImage();
+                break;
+		    case HgDataProviderIconQPixmap:		    
+		        res = *pix;  
+		        break;
+            default:
+                break;
+		}
 	}
-	TX_EXIT_ARGS( QString("No pixmap avilable"));
-	return QVariant();
+	
+	if (res.isNull()){
+	    TX_EXIT_ARGS( QString("No pixmap avilable"));
+	}
+	
+	return res;
 }
 
 QPixmap* HgDataProviderModel::getPixmap(int idx)
 {
 //    TX_ENTRY
     QPixmap* res = NULL;
-//    mQPixmapsLock.lock();
+    mQPixmapsLock.lock();
     if ( mUsedPixmaps.contains(idx)){
         res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap
     } else {
@@ -379,7 +448,7 @@
             TX_LOG_ARGS(QString("no free pixmaps"));
         }
     }
-//    mQPixmapsLock.unlock();
+    mQPixmapsLock.unlock();
 //    TX_EXIT
     return res;
 }