hgcacheproxymodel/src/hgdataprovidermodel.cpp
changeset 5 4fa04caf0f43
parent 3 c863538fcbb6
child 6 1cdcc61142d2
equal deleted inserted replaced
3:c863538fcbb6 5:4fa04caf0f43
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 *  Version     : %version: 9 %
    16 *  Version     : %version: 12 %
    17 */
    17 */
    18 #include <e32debug.h>
    18 #include <e32debug.h>
    19 #include <QVariant>
    19 #include <QVariant>
    20 #include <HbIcon.h>
    20 #include <HbIcon>
    21 #include <qpixmapdata_p.h>
    21 #include <qpixmapdata_p.h>
    22 #include <hgwidgets/hgdataprovidermodel.h>
    22 #include <hgwidgets/hgdataprovidermodel.h>
    23 #include <hgwidgets/hgcacheproxymodel.h>
    23 #include <hgwidgets/hgcacheproxymodel.h>
    24 #include "hglogger.h"
    24 #include "hglogger.h"
    25 
    25 
   130 QVariant HgDataProviderModel::data(int idx, int role) const
   130 QVariant HgDataProviderModel::data(int idx, int role) const
   131 {
   131 {
   132     QVariant res;
   132     QVariant res;
   133     if ( containsRole(idx, role)){
   133     if ( containsRole(idx, role)){
   134         res = mCache->at(idx)->value(role);
   134         res = mCache->at(idx)->value(role);
   135     } else if (role == Qt::DecorationRole ){
   135     } else if (isIndexValid(idx)){
   136         res = defaultIcon();
   136         if (role == Qt::DecorationRole ){
       
   137             res = defaultIcon();
       
   138         } else {
       
   139             res = getData(idx,role);
       
   140         }
       
   141         
   137     }
   142     }
   138     return res;
   143     return res;
   139 }
   144 }
   140 
   145 
   141 QMap<int, QVariant> HgDataProviderModel::itemData(const QModelIndex &index) const
   146 QMap<int, QVariant> HgDataProviderModel::itemData(const QModelIndex &index) const
   163 }
   168 }
   164 
   169 
   165 bool HgDataProviderModel::update(int pos, QList< QPair< QVariant, int > >* list, bool silent)
   170 bool HgDataProviderModel::update(int pos, QList< QPair< QVariant, int > >* list, bool silent)
   166 {
   171 {
   167     bool change(false);
   172     bool change(false);
   168     if (list && list->count() && pos >=0 && pos<count() && mCache->at(pos)) {
   173     if (list && list->count() && isIndexValid(pos)) {
   169         while(list->count()>0){
   174         while(list->count()>0){
   170             QPair< QVariant, int > pair = list->takeFirst();
   175             QPair< QVariant, int > pair = list->takeFirst();
   171             change = update(pos, pair.first, pair.second, true)|change;
   176             change = update(pos, pair.first, pair.second, true)|change;
   172         }
   177         }
   173         if ( !silent && change ){
   178         if ( !silent && change ){
   180 bool HgDataProviderModel::update(int pos, QVariant obj, int role, bool silent)
   185 bool HgDataProviderModel::update(int pos, QVariant obj, int role, bool silent)
   181 {
   186 {
   182     bool change(false);
   187     bool change(false);
   183     
   188     
   184     if ( isIndexValid(pos)){
   189     if ( isIndexValid(pos)){
       
   190         mDataLock.lock();
   185         mCache->at(pos)->insert(role, obj); //this will remove old one if needed
   191         mCache->at(pos)->insert(role, obj); //this will remove old one if needed
       
   192         mDataLock.unlock();        
   186         change = true;
   193         change = true;
   187     }
   194     }
   188     
   195     
   189     if ( !silent && change ){
   196     if ( !silent && change ){
   190         emitDataChanged(pos, pos, false);
   197         emitDataChanged(pos, pos, false);
   194 
   201 
   195 bool HgDataProviderModel::updateIcon(int pos, QVariant obj, bool silent)
   202 bool HgDataProviderModel::updateIcon(int pos, QVariant obj, bool silent)
   196 {
   203 {
   197     bool change(false);
   204     bool change(false);
   198     if ( obj.isValid() && !obj.isNull() && isIndexValid(pos) ){
   205     if ( obj.isValid() && !obj.isNull() && isIndexValid(pos) ){
       
   206         mDataLock.lock();
   199         mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed
   207         mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed
       
   208         mDataLock.unlock();        
   200         change = true;
   209         change = true;
   201         if (!silent){
   210         if (!silent){
   202             TX_LOG_ARGS(QString("pos:%1").arg( pos ) );
   211             TX_LOG_ARGS(QString("pos:%1").arg( pos ) );
   203             if ( mObserver){
   212             if ( mObserver){
   204                 mObserver->dataUpdated(pos,pos);
   213                 mObserver->dataUpdated(pos,pos);
   249     
   258     
   250     if ( !silent){
   259     if ( !silent){
   251         beginInsertRows(QModelIndex(), pos, pos);
   260         beginInsertRows(QModelIndex(), pos, pos);
   252     }
   261     }
   253     
   262     
       
   263     mDataLock.lock();    
   254     mCache->insert(pos, new QMap<int, QVariant>());
   264     mCache->insert(pos, new QMap<int, QVariant>());
       
   265     mDataLock.unlock();
       
   266     
   255     if (list && list->count()){
   267     if (list && list->count()){
   256         update(pos, list, true);
   268         update(pos, list, true);
   257     }
   269     }
   258     
   270     
   259     if ( !silent){
   271     if ( !silent){
   281     } else if (size <0){
   293     } else if (size <0){
   282         return;
   294         return;
   283     }
   295     }
   284     
   296     
   285     beginRemoveRows(QModelIndex(),pos, pos+size-1);
   297     beginRemoveRows(QModelIndex(),pos, pos+size-1);
       
   298     mDataLock.lock();
   286     for (int i=0; i<size && pos<mCache->count(); i++){
   299     for (int i=0; i<size && pos<mCache->count(); i++){
   287         mCache->removeAt(pos);
   300         mCache->removeAt(pos);
   288     }
   301     }
       
   302     mDataLock.unlock();
   289     endRemoveRows();
   303     endRemoveRows();
   290 }
   304 }
   291         
   305         
   292 void HgDataProviderModel::resetModel() 
   306 void HgDataProviderModel::resetModel() 
   293 {
   307 {
   306     }
   320     }
   307 }
   321 }
   308 
   322 
   309 void HgDataProviderModel::resizeQPixmapPool(int newSize)
   323 void HgDataProviderModel::resizeQPixmapPool(int newSize)
   310 {
   324 {
   311 //    mQPixmapsLock.lock();
   325     mQPixmapsLock.lock();
   312     int currentSize = mFreePixmaps.count() + mUsedPixmaps.count();
   326     int currentSize = mFreePixmaps.count() + mUsedPixmaps.count();
   313     int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer;
   327     int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer;
   314     mUnallocatedPixmaps = 0;
   328     mUnallocatedPixmaps = 0;
   315     while (diff != 0){
   329     while (diff != 0){
   316         if (diff < 0){
   330         if (diff < 0){
   323                 mFreePixmaps.removeLast();
   337                 mFreePixmaps.removeLast();
   324             } //else will be deleted with releasePixmap;
   338             } //else will be deleted with releasePixmap;
   325             diff--;
   339             diff--;
   326         }
   340         }
   327     }
   341     }
   328 //    mQPixmapsLock.unlock();
   342     mQPixmapsLock.unlock();
   329     mCacheSize = newSize;
   343     mCacheSize = newSize;
   330 }
   344 }
   331 
   345 
   332 void HgDataProviderModel::releasePixmap(int idx)
   346 void HgDataProviderModel::releasePixmap(int idx)
   333 {
   347 {
   334 //    mQPixmapsLock.lock();
   348     mQPixmapsLock.lock();
   335     if (mUsedPixmaps.contains(idx)) {
   349     if (mUsedPixmaps.contains(idx)) {
   336         QPixmap* pix = mUsedPixmaps.take(idx);
   350         QPixmap* pix = mUsedPixmaps.take(idx);
   337         if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) > ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
   351         if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) > ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
   338             delete pix; //we have too many pixmaps
   352             delete pix; //we have too many pixmaps
   339         } else {
   353         } else {
   340             mFreePixmaps.append(pix);
   354             mFreePixmaps.append(pix);
   341         }
   355         }
   342     } else {
   356     } else {
   343 //        TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx));
   357 //        TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx));
   344     }
   358     }
   345 //    mQPixmapsLock.unlock();    
   359     mQPixmapsLock.unlock();    
   346 }
   360 }
   347 
   361 
   348 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   362 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   349 {
   363 {
   350     TX_ENTRY
   364     TX_ENTRY
   353 		if ( pix->pixmapData() ) {
   367 		if ( pix->pixmapData() ) {
   354 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   368 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   355 		} else {
   369 		} else {
   356 			*pix = aPixmap;
   370 			*pix = aPixmap;
   357 		}
   371 		}
   358 //		mQPixmapsLock.lock();
   372 		mQPixmapsLock.lock();
   359 		mUsedPixmaps.insert(index, pix);
   373 		mUsedPixmaps.insert(index, pix);
   360 //		mQPixmapsLock.unlock();
   374 		mQPixmapsLock.unlock();
   361 		return HbIcon(QIcon(*pix));	
   375 		return HbIcon(QIcon(*pix));	
   362 	}
   376 	}
   363 	TX_EXIT_ARGS( QString("No pixmap avilable"));
   377 	TX_EXIT_ARGS( QString("No pixmap avilable"));
   364 	return QVariant();
   378 	return QVariant();
   365 }
   379 }
   366 
   380 
   367 QPixmap* HgDataProviderModel::getPixmap(int idx)
   381 QPixmap* HgDataProviderModel::getPixmap(int idx)
   368 {
   382 {
   369 //    TX_ENTRY
   383 //    TX_ENTRY
   370     QPixmap* res = NULL;
   384     QPixmap* res = NULL;
   371 //    mQPixmapsLock.lock();
   385     mQPixmapsLock.lock();
   372     if ( mUsedPixmaps.contains(idx)){
   386     if ( mUsedPixmaps.contains(idx)){
   373         res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap
   387         res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap
   374     } else {
   388     } else {
   375         if (!mFreePixmaps.isEmpty()){
   389         if (!mFreePixmaps.isEmpty()){
   376             res = mFreePixmaps.takeFirst();
   390             res = mFreePixmaps.takeFirst();
   379             res = new QPixmap();
   393             res = new QPixmap();
   380         } else {
   394         } else {
   381             TX_LOG_ARGS(QString("no free pixmaps"));
   395             TX_LOG_ARGS(QString("no free pixmaps"));
   382         }
   396         }
   383     }
   397     }
   384 //    mQPixmapsLock.unlock();
   398     mQPixmapsLock.unlock();
   385 //    TX_EXIT
   399 //    TX_EXIT
   386     return res;
   400     return res;
   387 }
   401 }
   388 
   402 
   389 //eof
   403 //eof