hgcacheproxymodel/src/hgdataprovidermodel.cpp
changeset 8 15f034b8a3b5
parent 5 4fa04caf0f43
child 17 a10844a9914d
equal deleted inserted replaced
5:4fa04caf0f43 8:15f034b8a3b5
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 *  Version     : %version: 12 %
    16 *  Version     : %version: 15 %
    17 */
    17 */
    18 #include <e32debug.h>
    18 #include <e32debug.h>
    19 #include <QVariant>
    19 #include <QVariant>
    20 #include <HbIcon>
    20 #include <HbIcon>
    21 #include <qpixmapdata_p.h>
    21 #include <qpixmapdata_p.h>
    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 
   248     QList< QPair< QVariant, int > > list;
   249     QList< QPair< QVariant, int > > list;
   249     list.append(item);
   250     list.append(item);
   250     doInsertItem(pos, &list, silent);
   251     doInsertItem(pos, &list, silent);
   251 }
   252 }
   252 
   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 
   253 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)
   254 {
   270 {
   255     if (pos >mCache->count() || pos <0){
   271     if (pos >mCache->count() || pos <0){
   256         return;
   272         return;
   257     }
   273     }
   272         endInsertRows();
   288         endInsertRows();
   273     } 
   289     } 
   274 }
   290 }
   275 
   291 
   276 
   292 
   277 void HgDataProviderModel::removeItem(int pos)
   293 void HgDataProviderModel::removeItem(int pos, bool silent)
   278 {
   294 {
   279     removeItems(pos, 1);
   295     removeItems(pos, 1, silent);
   280 }
   296 }
   281 
   297 
   282 void HgDataProviderModel::removeItems(int pos, int size)
   298 void HgDataProviderModel::removeItems(int pos, int size, bool silent)
   283 {
   299 {
   284     if (pos >=mCache->count()){
   300     if (pos >=mCache->count()){
   285         return;
   301         return;
   286     } else if (pos <0){
   302     } else if (pos <0){
   287         size = size + pos; //pos <0
   303         size = size + pos; //pos <0
   288         pos = 0;
   304         pos = 0;
   289     }
   305     }
   290     
   306     
   291     if (size >mCache->count()){
   307     if (size >mCache->count()){
   292         size = mCache->count();
   308         size = mCache->count();
   293     } else if (size <0){
   309     }
       
   310     if (size <=0){
   294         return;
   311         return;
   295     }
   312     }
   296     
   313     if (!silent){
   297     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     
   298     mDataLock.lock();
   319     mDataLock.lock();
   299     for (int i=0; i<size && pos<mCache->count(); i++){
   320     for (int i=0; i<size && pos<mCache->count(); i++){
   300         mCache->removeAt(pos);
   321         mCache->removeAt(pos);
   301     }
   322     }
   302     mDataLock.unlock();
   323     mDataLock.unlock();
   303     endRemoveRows();
   324     
   304 }
   325     if (!silent)
   305         
   326         endRemoveRows();
       
   327 }
       
   328 
   306 void HgDataProviderModel::resetModel() 
   329 void HgDataProviderModel::resetModel() 
   307 {
   330 {
   308     beginResetModel();
   331     beginResetModel();
   309     doResetModel();
   332     doResetModel();
   310     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;
   311 }
   344 }
   312 
   345 
   313 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent)
   346 void HgDataProviderModel::emitDataChanged(int from, int to, bool silent)
   314 {
   347 {
   315     if ( !silent ){
   348     if ( !silent ){
   346 void HgDataProviderModel::releasePixmap(int idx)
   379 void HgDataProviderModel::releasePixmap(int idx)
   347 {
   380 {
   348     mQPixmapsLock.lock();
   381     mQPixmapsLock.lock();
   349     if (mUsedPixmaps.contains(idx)) {
   382     if (mUsedPixmaps.contains(idx)) {
   350         QPixmap* pix = mUsedPixmaps.take(idx);
   383         QPixmap* pix = mUsedPixmaps.take(idx);
   351         if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) > ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
   384         if ( ( mFreePixmaps.count() + mUsedPixmaps.count() + mUnallocatedPixmaps ) >= ( mCacheSize + KQPixmapCacheEmergencyBuffer ) ) {
   352             delete pix; //we have too many pixmaps
   385             delete pix; //we have too many pixmaps
   353         } else {
   386         } else {
   354             mFreePixmaps.append(pix);
   387             mFreePixmaps.append(pix);
   355         }
   388         }
   356     } else {
   389     } else {
   360 }
   393 }
   361 
   394 
   362 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   395 QVariant HgDataProviderModel::createIcon(int index, QPixmap aPixmap)
   363 {
   396 {
   364     TX_ENTRY
   397     TX_ENTRY
       
   398     QVariant res;
   365 	QPixmap* pix = getPixmap(index);
   399 	QPixmap* pix = getPixmap(index);
   366 	if (pix){
   400 	if (pix){
   367 		if ( pix->pixmapData() ) {
   401 		if ( pix->pixmapData() ) {
   368 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   402 			pix->pixmapData()->fromImage(aPixmap.toImage(), Qt::AutoColor );  
   369 		} else {
   403 		} else {
   370 			*pix = aPixmap;
   404 			*pix = aPixmap;
   371 		}
   405 		}
   372 		mQPixmapsLock.lock();
   406 		mQPixmapsLock.lock();
   373 		mUsedPixmaps.insert(index, pix);
   407 		mUsedPixmaps.insert(index, pix);
   374 		mQPixmapsLock.unlock();
   408 		mQPixmapsLock.unlock();
   375 		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 		}
   376 	}
   425 	}
   377 	TX_EXIT_ARGS( QString("No pixmap avilable"));
   426 	
   378 	return QVariant();
   427 	if (res.isNull()){
       
   428 	    TX_EXIT_ARGS( QString("No pixmap avilable"));
       
   429 	}
       
   430 	
       
   431 	return res;
   379 }
   432 }
   380 
   433 
   381 QPixmap* HgDataProviderModel::getPixmap(int idx)
   434 QPixmap* HgDataProviderModel::getPixmap(int idx)
   382 {
   435 {
   383 //    TX_ENTRY
   436 //    TX_ENTRY