hgcacheproxymodel/src/hgdataprovidermodel.cpp
changeset 2 49c70dcc3f17
parent 1 e48454f237ca
child 3 c863538fcbb6
equal deleted inserted replaced
1:e48454f237ca 2:49c70dcc3f17
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 *  Version     : %version: 3 %
    16 *  Version     : %version: 6 %
    17 */
    17 */
    18 #include <e32debug.h>
    18 #include <e32debug.h>
    19 #include <QVariant>
    19 #include <QVariant>
    20 #include <HbIcon.h>
    20 #include <HbIcon.h>
    21 #include <qpixmapdata_p.h>
    21 #include <qpixmapdata_p.h>
    44     delete mCache;
    44     delete mCache;
    45     qDeleteAll( mFreePixmaps.begin(), mFreePixmaps.end() );
    45     qDeleteAll( mFreePixmaps.begin(), mFreePixmaps.end() );
    46     mFreePixmaps.clear();
    46     mFreePixmaps.clear();
    47     qDeleteAll( mUsedPixmaps.begin(), mUsedPixmaps.end() );
    47     qDeleteAll( mUsedPixmaps.begin(), mUsedPixmaps.end() );
    48     mUsedPixmaps.clear();
    48     mUsedPixmaps.clear();
    49     TX_EXIT    
    49     TX_EXIT
    50 }
    50 }
    51 
    51 
    52 void HgDataProviderModel::release(QList<int> list, bool silent)
    52 void HgDataProviderModel::release(QList<int> list, bool silent)
    53 {
    53 {
    54     TX_ENTRY    
    54     TX_ENTRY    
    57     int max = 0;
    57     int max = 0;
    58     
    58     
    59     for ( int idx = 0; idx < list.count(); idx++){
    59     for ( int idx = 0; idx < list.count(); idx++){
    60         i = list[idx];
    60         i = list[idx];
    61         if ( i >=0 && i<count()){
    61         if ( i >=0 && i<count()){
    62             if ( i < min)
    62             if ( i < min){
    63                 min = i;
    63                 min = i;
    64             if ( i > max)
    64             } if ( i > max){
    65                 max = i;
    65                 max = i;
       
    66             }
    66             resetIcon(i);
    67             resetIcon(i);
    67         }
    68         }
    68     }
    69     }
    69     
    70     
    70     doReleaseData(list, silent);
    71     doReleaseData(list, silent);
    71     
    72     
    72     if (min<max){ //min<max is true if at least one item is in range <0,count())
    73     if (min<max){ //min<max is true if at least one item is in range <0,count())
    73         emitDataChanged(min, max, silent);
    74         emitDataChanged(min, max, silent);
    74     }
    75     }
       
    76     TX_EXIT
       
    77 }
       
    78 
       
    79 void HgDataProviderModel::request(QList<int> list, bool silent)
       
    80 {
       
    81     TX_ENTRY
       
    82     doRequestData(list, silent); 
    75     TX_EXIT    
    83     TX_EXIT    
    76 }
       
    77 
       
    78 void HgDataProviderModel::request(QList<int> list, bool silent)
       
    79 {
       
    80     doRequestData(list, silent); 
       
    81 }
    84 }
    82 
    85 
    83 void HgDataProviderModel::registerObserver(HgDataProviderModelObserver* obs)
    86 void HgDataProviderModel::registerObserver(HgDataProviderModelObserver* obs)
    84 {
    87 {
    85     TX_ENTRY
    88     TX_ENTRY
   172 
   175 
   173 bool HgDataProviderModel::update(int pos, QVariant obj, int role, bool silent)
   176 bool HgDataProviderModel::update(int pos, QVariant obj, int role, bool silent)
   174 {
   177 {
   175     bool change(false);
   178     bool change(false);
   176     
   179     
   177     if ( pos >=0 && pos<count() && mCache->at(pos)){
   180     if ( isIndexValid(pos)){
   178         mCache->at(pos)->insert(role, obj); //this will remove old one if needed
   181         mCache->at(pos)->insert(role, obj); //this will remove old one if needed
   179         change = true;
   182         change = true;
   180     }
   183     }
   181     
   184     
   182     if ( !silent && change ){
   185     if ( !silent && change ){
   185     return change;
   188     return change;
   186 }
   189 }
   187 
   190 
   188 bool HgDataProviderModel::updateIcon(int pos, QVariant obj, bool silent)
   191 bool HgDataProviderModel::updateIcon(int pos, QVariant obj, bool silent)
   189 {
   192 {
   190     if (obj.isValid()==false)
       
   191         return false;
       
   192     
       
   193     bool change(false);
   193     bool change(false);
   194     if ( pos >=0 && pos<count() && mCache->at(pos)){
   194     if ( obj.isValid() && !obj.isNull() && isIndexValid(pos) ){
   195         mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed
   195         mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed
   196         change = true;
   196         change = true;
   197     }
   197         if (!silent){
   198     
   198             TX_LOG_ARGS(QString("pos:%1").arg( pos ) );
   199     if (!silent && change){
   199             if ( mObserver){
   200         TX_LOG
   200                 mObserver->dataUpdated(pos,pos);
   201         if ( mObserver){
   201             } else { //if no observer, let's emit signal
   202             mObserver->dataUpdated(pos,pos);
   202                 QModelIndex topLeft = index(pos, 0);
   203         } else {
   203                 QModelIndex bottomRight = index(pos, 0);
   204             QModelIndex topLeft = index(pos, 0);
   204                 emit dataChanged(topLeft,bottomRight);
   205             QModelIndex bottomRight = index(pos, 0);
   205             }
   206             emit dataChanged(topLeft,bottomRight);
       
   207         }
   206         }
   208     }
   207     }
   209     return change;
   208     return change;
   210 }
   209 }
   211 
   210 
   296 }
   295 }
   297 
   296 
   298 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent)
   297 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent)
   299 {
   298 {
   300     if ( !silent ){
   299     if ( !silent ){
   301         TX_LOG    
   300 //        TX_LOG
   302         QModelIndex topLeft = index(from, 0);
   301         QModelIndex topLeft = index(from, 0);
   303         QModelIndex bottomRight = index(to, 0);
   302         QModelIndex bottomRight = index(to, 0);
   304         emit dataChanged(topLeft,bottomRight);
   303         emit dataChanged(topLeft,bottomRight);
   305     }
   304     }
   306 }
   305 }
   307 
   306 
   308 void HgDataProviderModel::resizeQPixmapPool(int newSize)
   307 void HgDataProviderModel::resizeQPixmapPool(int newSize)
   309 {
   308 {
   310     mQPixmapsLock.lock();
   309 //    mQPixmapsLock.lock();
   311     int currentSize = mFreePixmaps.count() + mUsedPixmaps.count();
   310     int currentSize = mFreePixmaps.count() + mUsedPixmaps.count();
   312     int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer;
   311     int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer;
   313     mUnallocatedPixmaps = 0;
   312     mUnallocatedPixmaps = 0;
   314     while (diff != 0){
   313     while (diff != 0){
   315         if (diff < 0){
   314         if (diff < 0){
   322                 mFreePixmaps.removeLast();
   321                 mFreePixmaps.removeLast();
   323             } //else will be deleted with releasePixmap;
   322             } //else will be deleted with releasePixmap;
   324             diff--;
   323             diff--;
   325         }
   324         }
   326     }
   325     }
   327     mQPixmapsLock.unlock();
   326 //    mQPixmapsLock.unlock();
   328     mCacheSize = newSize;
   327     mCacheSize = newSize;
   329 }
   328 }
   330 
   329 
   331 void HgDataProviderModel::releasePixmap(int idx)
   330 void HgDataProviderModel::releasePixmap(int idx)
   332 {
   331 {
   333     mQPixmapsLock.lock();
   332 //    mQPixmapsLock.lock();
   334     if (mUsedPixmaps.contains(idx)){
   333     if (mUsedPixmaps.contains(idx)) {
   335         QPixmap* pix = mUsedPixmaps.take(idx);
   334         QPixmap* pix = mUsedPixmaps.take(idx);
   336         if ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps> mCacheSize + KQPixmapCacheEmergencyBuffer){
   335         if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) > ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
   337             delete pix; //we have too many pixmaps
   336             delete pix; //we have too many pixmaps
   338         }else{
   337         } else {
   339             mFreePixmaps.append(pix);
   338             mFreePixmaps.append(pix);
   340         }
   339         }
   341     }else{
   340     } else {
   342         TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx));
   341 //        TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx));
   343     }
   342     }
   344     mQPixmapsLock.unlock();    
   343 //    mQPixmapsLock.unlock();    
   345 }
   344 }
   346 
   345 
   347 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   346 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   348 {
   347 {
       
   348     TX_ENTRY
   349 	QPixmap* pix = getPixmap(index);
   349 	QPixmap* pix = getPixmap(index);
   350 	if (pix){
   350 	if (pix){
   351 		if ( pix->pixmapData() ){
   351 		if ( pix->pixmapData() ) {
   352 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   352 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   353 		} else {
   353 		} else {
   354 			*pix = aPixmap;
   354 			*pix = aPixmap;
   355 		}
   355 		}
   356 		mQPixmapsLock.lock();
   356 //		mQPixmapsLock.lock();
   357 		mUsedPixmaps.insert(index, pix);
   357 		mUsedPixmaps.insert(index, pix);
   358 		mQPixmapsLock.unlock();
   358 //		mQPixmapsLock.unlock();
   359 		return HbIcon(QIcon(*pix));	
   359 		return HbIcon(QIcon(*pix));	
   360 	}
   360 	}
   361 	TX_EXIT_ARGS( QString("No pixmap avilable"));
   361 	TX_EXIT_ARGS( QString("No pixmap avilable"));
   362 	return QVariant();
   362 	return QVariant();
   363 }
   363 }
   364 
   364 
   365 QPixmap* HgDataProviderModel::getPixmap(int idx)
   365 QPixmap* HgDataProviderModel::getPixmap(int idx)
   366 {
   366 {
   367     TX_ENTRY
   367 //    TX_ENTRY
   368     QPixmap* res = NULL;
   368     QPixmap* res = NULL;
   369     mQPixmapsLock.lock();
   369 //    mQPixmapsLock.lock();
   370     if ( mUsedPixmaps.contains(idx)){
   370     if ( mUsedPixmaps.contains(idx)){
   371         res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap
   371         res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap
   372     } else {
   372     } else {
   373         if (!mFreePixmaps.isEmpty()){
   373         if (!mFreePixmaps.isEmpty()){
   374             res = mFreePixmaps.takeFirst();
   374             res = mFreePixmaps.takeFirst();
   375         }else if (mUnallocatedPixmaps){
   375         }else if (mUnallocatedPixmaps){
   376             mUnallocatedPixmaps--;
   376             mUnallocatedPixmaps--;
   377             res = new QPixmap();
   377             res = new QPixmap();
   378         } else {
   378         } else {
   379             TX_LOG_ARGS(QString("no free pixmaps"));
   379             TX_LOG_ARGS(QString("no free pixmaps"));
   380         }        
   380         }
   381     }
   381     }
   382     mQPixmapsLock.unlock();
   382 //    mQPixmapsLock.unlock();
   383     TX_EXIT    
   383 //    TX_EXIT
   384     return res;
   384     return res;
   385 }
   385 }
   386 
   386 
   387 //eof
   387 //eof