hgcacheproxymodel/src/hgdataprovidermodel.cpp
branchGCC_SURGE
changeset 10 c348b9772569
parent 8 15f034b8a3b5
child 17 a10844a9914d
equal deleted inserted replaced
4:463f1934e292 10:c348b9772569
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 *  Version     : %version: 6 %
    16 *  Version     : %version: 15 %
    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 
    29 HgDataProviderModel::HgDataProviderModel(QObject *parent) : 
    29 HgDataProviderModel::HgDataProviderModel(QObject *parent) : 
    30     QAbstractItemModel(parent),
    30     QAbstractItemModel(parent),
    31     mCache(new QList<QMap<int, QVariant>*>()),
    31     mCache(new QList<QMap<int, QVariant>*>()),
    32     mCacheSize(0),
    32     mCacheSize(0),
    33     mUnallocatedPixmaps(0),
    33     mUnallocatedPixmaps(0),
    34     mObserver(0)
    34     mObserver(0),
       
    35     mIconMode(HgDataProviderIconHbIcon)
    35 {
    36 {
    36     TX_ENTRY
    37     TX_ENTRY
    37     TX_EXIT
    38     TX_EXIT
    38 }
    39 }
    39 
    40 
   130 QVariant HgDataProviderModel::data(int idx, int role) const
   131 QVariant HgDataProviderModel::data(int idx, int role) const
   131 {
   132 {
   132     QVariant res;
   133     QVariant res;
   133     if ( containsRole(idx, role)){
   134     if ( containsRole(idx, role)){
   134         res = mCache->at(idx)->value(role);
   135         res = mCache->at(idx)->value(role);
   135     } else if (role == Qt::DecorationRole ){
   136     } else if (isIndexValid(idx)){
   136         res = defaultIcon();
   137         if (role == Qt::DecorationRole ){
       
   138             res = defaultIcon();
       
   139         } else {
       
   140             res = getData(idx,role);
       
   141         }
       
   142         
   137     }
   143     }
   138     return res;
   144     return res;
   139 }
   145 }
   140 
   146 
   141 QMap<int, QVariant> HgDataProviderModel::itemData(const QModelIndex &index) const
   147 QMap<int, QVariant> HgDataProviderModel::itemData(const QModelIndex &index) const
   147     return res;
   153     return res;
   148 }
   154 }
   149 
   155 
   150 void HgDataProviderModel::clearCache()
   156 void HgDataProviderModel::clearCache()
   151 {
   157 {
       
   158     for ( int i=0; i<count(); i++){
       
   159         releasePixmap(i);
       
   160     }
       
   161     
   152     qDeleteAll( mCache->begin(), mCache->end() );
   162     qDeleteAll( mCache->begin(), mCache->end() );
   153     mCache->clear();
   163     mCache->clear();
   154 }
   164 }
   155 
   165 
   156 int HgDataProviderModel::count() const
   166 int HgDataProviderModel::count() const
   159 }
   169 }
   160 
   170 
   161 bool HgDataProviderModel::update(int pos, QList< QPair< QVariant, int > >* list, bool silent)
   171 bool HgDataProviderModel::update(int pos, QList< QPair< QVariant, int > >* list, bool silent)
   162 {
   172 {
   163     bool change(false);
   173     bool change(false);
   164     if (list && list->count() && pos >=0 && pos<count() && mCache->at(pos)) {
   174     if (list && list->count() && isIndexValid(pos)) {
   165         while(list->count()){
   175         while(list->count()>0){
   166             QPair< QVariant, int > pair = list->takeFirst();
   176             QPair< QVariant, int > pair = list->takeFirst();
   167             change = update(pos, pair.first, pair.second, true)|change;
   177             change = update(pos, pair.first, pair.second, true)|change;
   168         }
   178         }
   169         if ( !silent && change ){
   179         if ( !silent && change ){
   170             emitDataChanged(pos, pos, false);
   180             emitDataChanged(pos, pos, false);
   176 bool HgDataProviderModel::update(int pos, QVariant obj, int role, bool silent)
   186 bool HgDataProviderModel::update(int pos, QVariant obj, int role, bool silent)
   177 {
   187 {
   178     bool change(false);
   188     bool change(false);
   179     
   189     
   180     if ( isIndexValid(pos)){
   190     if ( isIndexValid(pos)){
       
   191         mDataLock.lock();
   181         mCache->at(pos)->insert(role, obj); //this will remove old one if needed
   192         mCache->at(pos)->insert(role, obj); //this will remove old one if needed
       
   193         mDataLock.unlock();        
   182         change = true;
   194         change = true;
   183     }
   195     }
   184     
   196     
   185     if ( !silent && change ){
   197     if ( !silent && change ){
   186         emitDataChanged(pos, pos, false);
   198         emitDataChanged(pos, pos, false);
   190 
   202 
   191 bool HgDataProviderModel::updateIcon(int pos, QVariant obj, bool silent)
   203 bool HgDataProviderModel::updateIcon(int pos, QVariant obj, bool silent)
   192 {
   204 {
   193     bool change(false);
   205     bool change(false);
   194     if ( obj.isValid() && !obj.isNull() && isIndexValid(pos) ){
   206     if ( obj.isValid() && !obj.isNull() && isIndexValid(pos) ){
       
   207         mDataLock.lock();
   195         mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed
   208         mCache->at(pos)->insert(Qt::DecorationRole, obj); //will remove old if needed
       
   209         mDataLock.unlock();        
   196         change = true;
   210         change = true;
   197         if (!silent){
   211         if (!silent){
   198             TX_LOG_ARGS(QString("pos:%1").arg( pos ) );
   212             TX_LOG_ARGS(QString("pos:%1").arg( pos ) );
   199             if ( mObserver){
   213             if ( mObserver){
   200                 mObserver->dataUpdated(pos,pos);
   214                 mObserver->dataUpdated(pos,pos);
   235     QList< QPair< QVariant, int > > list;
   249     QList< QPair< QVariant, int > > list;
   236     list.append(item);
   250     list.append(item);
   237     doInsertItem(pos, &list, silent);
   251     doInsertItem(pos, &list, silent);
   238 }
   252 }
   239 
   253 
       
   254 void HgDataProviderModel::clearItem(int pos, bool silent)
       
   255 {
       
   256     bool change = false;
       
   257     if ( isIndexValid(pos)){
       
   258         mDataLock.lock();    
       
   259         mCache->at(pos)->clear();
       
   260         mDataLock.unlock();
       
   261         change = true;
       
   262     }
       
   263     
       
   264     if ( change && !silent){
       
   265         emit dataChanged( index(pos, 0), index(pos, 0) );
       
   266     }
       
   267 }
       
   268 
   240 void HgDataProviderModel::doInsertItem(int pos, QList< QPair< QVariant, int > >* list, bool silent)
   269 void HgDataProviderModel::doInsertItem(int pos, QList< QPair< QVariant, int > >* list, bool silent)
   241 {
   270 {
   242     if (pos >mCache->count()){
   271     if (pos >mCache->count() || pos <0){
   243         pos = mCache->count();
   272         return;
   244     } else if (pos <0){
       
   245         pos = 0;
       
   246     }
   273     }
   247     
   274     
   248     if ( !silent){
   275     if ( !silent){
   249         beginInsertRows(QModelIndex(), pos, pos);
   276         beginInsertRows(QModelIndex(), pos, pos);
   250     }
   277     }
   251     
   278     
       
   279     mDataLock.lock();    
   252     mCache->insert(pos, new QMap<int, QVariant>());
   280     mCache->insert(pos, new QMap<int, QVariant>());
       
   281     mDataLock.unlock();
       
   282     
   253     if (list && list->count()){
   283     if (list && list->count()){
   254         update(pos, list, true);
   284         update(pos, list, true);
   255     }
   285     }
   256     
   286     
   257     if ( !silent){
   287     if ( !silent){
   258         endInsertRows();
   288         endInsertRows();
   259     } 
   289     } 
   260 }
   290 }
   261 
   291 
   262 
   292 
   263 void HgDataProviderModel::removeItem(int pos)
   293 void HgDataProviderModel::removeItem(int pos, bool silent)
   264 {
   294 {
   265     removeItems(pos, 1);
   295     removeItems(pos, 1, silent);
   266 }
   296 }
   267 
   297 
   268 void HgDataProviderModel::removeItems(int pos, int size)
   298 void HgDataProviderModel::removeItems(int pos, int size, bool silent)
   269 {
   299 {
   270     if (pos >mCache->count())
   300     if (pos >=mCache->count()){
   271         return;
   301         return;
   272     else if (pos <0){
   302     } else if (pos <0){
   273         size = size + pos; //pos <0
   303         size = size + pos; //pos <0
   274         pos = 0;
   304         pos = 0;
   275     }
   305     }
   276     
   306     
   277     if (size >mCache->count()){
   307     if (size >mCache->count()){
   278         size = mCache->count();
   308         size = mCache->count();
   279     } else if (size <0){
   309     }
       
   310     if (size <=0){
   280         return;
   311         return;
   281     }
   312     }
   282     
   313     if (!silent){
   283     beginRemoveRows(QModelIndex(),pos, pos+size-1);
   314         beginRemoveRows(QModelIndex(),pos, pos+size-1);
       
   315     } else {
       
   316         qWarning("Removing items without notifying might be danger.");
       
   317     }
       
   318     
       
   319     mDataLock.lock();
   284     for (int i=0; i<size && pos<mCache->count(); i++){
   320     for (int i=0; i<size && pos<mCache->count(); i++){
   285         mCache->removeAt(pos);
   321         mCache->removeAt(pos);
   286     }
   322     }
   287     endRemoveRows();
   323     mDataLock.unlock();
   288 }
   324     
   289         
   325     if (!silent)
       
   326         endRemoveRows();
       
   327 }
       
   328 
   290 void HgDataProviderModel::resetModel() 
   329 void HgDataProviderModel::resetModel() 
   291 {
   330 {
   292     beginResetModel();
   331     beginResetModel();
   293     doResetModel();
   332     doResetModel();
   294     endResetModel();
   333     endResetModel();
       
   334 }
       
   335 
       
   336 void HgDataProviderModel::setIconMode(HgDataProviderModel::HgDataProviderIconMode mode)
       
   337 {
       
   338     mIconMode = mode;
       
   339 }
       
   340 
       
   341 HgDataProviderModel::HgDataProviderIconMode HgDataProviderModel::iconMode()
       
   342 {
       
   343     return mIconMode;
   295 }
   344 }
   296 
   345 
   297 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent)
   346 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent)
   298 {
   347 {
   299     if ( !silent ){
   348     if ( !silent ){
   304     }
   353     }
   305 }
   354 }
   306 
   355 
   307 void HgDataProviderModel::resizeQPixmapPool(int newSize)
   356 void HgDataProviderModel::resizeQPixmapPool(int newSize)
   308 {
   357 {
   309 //    mQPixmapsLock.lock();
   358     mQPixmapsLock.lock();
   310     int currentSize = mFreePixmaps.count() + mUsedPixmaps.count();
   359     int currentSize = mFreePixmaps.count() + mUsedPixmaps.count();
   311     int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer;
   360     int diff = currentSize - newSize - KQPixmapCacheEmergencyBuffer;
   312     mUnallocatedPixmaps = 0;
   361     mUnallocatedPixmaps = 0;
   313     while (diff != 0){
   362     while (diff != 0){
   314         if (diff < 0){
   363         if (diff < 0){
   321                 mFreePixmaps.removeLast();
   370                 mFreePixmaps.removeLast();
   322             } //else will be deleted with releasePixmap;
   371             } //else will be deleted with releasePixmap;
   323             diff--;
   372             diff--;
   324         }
   373         }
   325     }
   374     }
   326 //    mQPixmapsLock.unlock();
   375     mQPixmapsLock.unlock();
   327     mCacheSize = newSize;
   376     mCacheSize = newSize;
   328 }
   377 }
   329 
   378 
   330 void HgDataProviderModel::releasePixmap(int idx)
   379 void HgDataProviderModel::releasePixmap(int idx)
   331 {
   380 {
   332 //    mQPixmapsLock.lock();
   381     mQPixmapsLock.lock();
   333     if (mUsedPixmaps.contains(idx)) {
   382     if (mUsedPixmaps.contains(idx)) {
   334         QPixmap* pix = mUsedPixmaps.take(idx);
   383         QPixmap* pix = mUsedPixmaps.take(idx);
   335         if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) > ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
   384         if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) >= ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
   336             delete pix; //we have too many pixmaps
   385             delete pix; //we have too many pixmaps
   337         } else {
   386         } else {
   338             mFreePixmaps.append(pix);
   387             mFreePixmaps.append(pix);
   339         }
   388         }
   340     } else {
   389     } else {
   341 //        TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx));
   390 //        TX_LOG_ARGS( QString("can't release pixmap for idx=%0").arg(idx));
   342     }
   391     }
   343 //    mQPixmapsLock.unlock();    
   392     mQPixmapsLock.unlock();    
   344 }
   393 }
   345 
   394 
   346 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   395 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   347 {
   396 {
   348     TX_ENTRY
   397     TX_ENTRY
       
   398     QVariant res;
   349 	QPixmap* pix = getPixmap(index);
   399 	QPixmap* pix = getPixmap(index);
   350 	if (pix){
   400 	if (pix){
   351 		if ( pix->pixmapData() ) {
   401 		if ( pix->pixmapData() ) {
   352 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   402 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   353 		} else {
   403 		} else {
   354 			*pix = aPixmap;
   404 			*pix = aPixmap;
   355 		}
   405 		}
   356 //		mQPixmapsLock.lock();
   406 		mQPixmapsLock.lock();
   357 		mUsedPixmaps.insert(index, pix);
   407 		mUsedPixmaps.insert(index, pix);
   358 //		mQPixmapsLock.unlock();
   408 		mQPixmapsLock.unlock();
   359 		return HbIcon(QIcon(*pix));	
   409 		switch (mIconMode){
       
   410             case HgDataProviderIconHbIcon : 
       
   411                 res = HbIcon(QIcon(*pix));  
       
   412                 break;
       
   413 		    case HgDataProviderIconQIcon : 
       
   414                 res = QIcon(*pix);
       
   415                 break;
       
   416 		    case HgDataProviderIconQImage : 
       
   417                 res = pix->toImage();
       
   418                 break;
       
   419 		    case HgDataProviderIconQPixmap:		    
       
   420 		        res = *pix;  
       
   421 		        break;
       
   422             default:
       
   423                 break;
       
   424 		}
   360 	}
   425 	}
   361 	TX_EXIT_ARGS( QString("No pixmap avilable"));
   426 	
   362 	return QVariant();
   427 	if (res.isNull()){
       
   428 	    TX_EXIT_ARGS( QString("No pixmap avilable"));
       
   429 	}
       
   430 	
       
   431 	return res;
   363 }
   432 }
   364 
   433 
   365 QPixmap* HgDataProviderModel::getPixmap(int idx)
   434 QPixmap* HgDataProviderModel::getPixmap(int idx)
   366 {
   435 {
   367 //    TX_ENTRY
   436 //    TX_ENTRY
   368     QPixmap* res = NULL;
   437     QPixmap* res = NULL;
   369 //    mQPixmapsLock.lock();
   438     mQPixmapsLock.lock();
   370     if ( mUsedPixmaps.contains(idx)){
   439     if ( mUsedPixmaps.contains(idx)){
   371         res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap
   440         res = mUsedPixmaps.take(idx);//let's just replace pixmapdata for that pixmap
   372     } else {
   441     } else {
   373         if (!mFreePixmaps.isEmpty()){
   442         if (!mFreePixmaps.isEmpty()){
   374             res = mFreePixmaps.takeFirst();
   443             res = mFreePixmaps.takeFirst();
   377             res = new QPixmap();
   446             res = new QPixmap();
   378         } else {
   447         } else {
   379             TX_LOG_ARGS(QString("no free pixmaps"));
   448             TX_LOG_ARGS(QString("no free pixmaps"));
   380         }
   449         }
   381     }
   450     }
   382 //    mQPixmapsLock.unlock();
   451     mQPixmapsLock.unlock();
   383 //    TX_EXIT
   452 //    TX_EXIT
   384     return res;
   453     return res;
   385 }
   454 }
   386 
   455 
   387 //eof
   456 //eof