ganeswidgets/tsrc/fute/HgWidgetTest/src/hgwidgettestdatamodel.cpp
changeset 2 49c70dcc3f17
parent 1 e48454f237ca
child 5 4fa04caf0f43
equal deleted inserted replaced
1:e48454f237ca 2:49c70dcc3f17
    53  */
    53  */
    54 HgWidgetTestDataModel::HgWidgetTestDataModel(QObject *parent)
    54 HgWidgetTestDataModel::HgWidgetTestDataModel(QObject *parent)
    55     : QAbstractListModel(parent),
    55     : QAbstractListModel(parent),
    56       mCachingInProgress(false),
    56       mCachingInProgress(false),
    57       mImageType(ImageTypeNone),
    57       mImageType(ImageTypeNone),
    58       mDefaultIcon((":/images/default.svg")),
    58 //      mDefaultIcon((":/images/default.svg")),
    59       mUseLowResImages(false),
    59       mUseLowResImages(false),
    60       mWrapper( new ThumbnailManager() ),
    60       mWrapper( new ThumbnailManager() ),
    61       mThumbnailRequestPending(false),
    61       mThumbnailRequestPending(false),
    62       mThumbnailRequestIndex(-1),
    62       mThumbnailRequestIndex(-1),
    63       mThumbnailRequestID(-1),
    63       mThumbnailRequestID(-1),
    64       mBufferManager(0)
    64       mBufferManager(0),
       
    65       mSilentDataFetch(false)
    65 {
    66 {
    66     FUNC_LOG;
    67     FUNC_LOG;
    67     mWrapper->setQualityPreference( ThumbnailManager::OptimizeForPerformance );
    68     mWrapper->setQualityPreference( ThumbnailManager::OptimizeForPerformance );
    68 
    69 
    69     mWrapper->setProperty("qimageSupport","true");
    70     mWrapper->setProperty("qimageSupport","true");
    87 }
    88 }
    88 
    89 
    89 void HgWidgetTestDataModel::setThumbnailSize(ThumbnailManager::ThumbnailSize size)
    90 void HgWidgetTestDataModel::setThumbnailSize(ThumbnailManager::ThumbnailSize size)
    90 {
    91 {
    91     mWrapper->setThumbnailSize(size);
    92     mWrapper->setThumbnailSize(size);
       
    93 }
       
    94 
       
    95 void HgWidgetTestDataModel::timeOut()
       
    96 {
       
    97     for (int i = 0; i < mFileInfoList.size(); ++i){
       
    98         QFileInfo fileInfo = mFileInfoList.at(i);
       
    99         if (fileInfo.isFile()){
       
   100             QString s = fileInfo.filePath();
       
   101             if (s.indexOf(QString(".jpg"),0,Qt::CaseInsensitive)>0){
       
   102                 mFiles.append(s);
       
   103                 mImages.append(QImage());
       
   104                 mVisibility.append(true);
       
   105             }
       
   106         }
       
   107     }
       
   108 
       
   109     if (mBufferManager) {
       
   110         mBufferManager->resetBuffer(0,mFiles.count());
       
   111     }            
       
   112     
       
   113     if (mFiles.count()>0) {
       
   114         // photo's behaves this way so for testing purposes lets follow their practice.
       
   115         beginInsertRows(QModelIndex(), 0, mFiles.count()-1);
       
   116         endInsertRows();
       
   117     }
    92 }
   118 }
    93 
   119 
    94 void HgWidgetTestDataModel::init()
   120 void HgWidgetTestDataModel::init()
    95 {
   121 {
    96     FUNC_LOG;
   122     FUNC_LOG;
   102     dir.setPath(QString("c:/data/images"));
   128     dir.setPath(QString("c:/data/images"));
   103 #else
   129 #else
   104     dir.setPath(QString("f:/data/images"));
   130     dir.setPath(QString("f:/data/images"));
   105 #endif
   131 #endif
   106 
   132 
   107     QFileInfoList list = dir.entryInfoList();
   133     mFileInfoList = dir.entryInfoList();
   108     for (int i = 0; i < list.size(); ++i){
       
   109         QFileInfo fileInfo = list.at(i);
       
   110         if (fileInfo.isFile()){
       
   111             QString s = fileInfo.filePath();
       
   112             if (s.indexOf(QString(".jpg"),0,Qt::CaseInsensitive)>0){
       
   113                 mFiles.append(s);
       
   114                 mImages.append(QImage());
       
   115                 mVisibility.append(true);
       
   116             }
       
   117         }
       
   118     }
       
   119 
       
   120     QPixmap pixmap(":/images/default.svg");
   134     QPixmap pixmap(":/images/default.svg");
       
   135     mDefaultPixmap = pixmap;
   121     if (!pixmap.isNull()){
   136     if (!pixmap.isNull()){
   122         mQIcon = QIcon(pixmap);
   137         mQIcon = QIcon(pixmap);
   123         if (!mQIcon.isNull()){
   138         if (!mQIcon.isNull()){
   124             mHbIcon = HbIcon(mQIcon);
   139             mHbIcon = HbIcon(mQIcon);
   125         }
   140         }
   126     }
   141     }
       
   142     
       
   143     QTimer::singleShot(0, this, SLOT(timeOut()));
   127 }
   144 }
   128 
   145 
   129 /*!
   146 /*!
   130  Returns the number of rows under the given \a parent.
   147  Returns the number of rows under the given \a parent.
   131 
   148 
   139 {
   156 {
   140     Q_UNUSED(parent);
   157     Q_UNUSED(parent);
   141     return mFiles.count();
   158     return mFiles.count();
   142 }
   159 }
   143 
   160 
       
   161 
       
   162 QVariant HgWidgetTestDataModel::silentData(const QModelIndex &index, int role)
       
   163 {
       
   164     mSilentDataFetch = true;
       
   165     QVariant variant = data(index,role);
       
   166     mSilentDataFetch = false;
       
   167     return variant;
       
   168 }
       
   169 
   144 /*!
   170 /*!
   145  Returns the data stored for the item referred to by the \a index.
   171  Returns the data stored for the item referred to by the \a index.
   146 
   172 
   147  \reimp
   173  \reimp
   148  */
   174  */
   157 
   183 
   158     if( row < 0 && row >= mFiles.count() ){
   184     if( row < 0 && row >= mFiles.count() ){
   159         return returnValue;
   185         return returnValue;
   160     }
   186     }
   161 
   187 
   162     if( mBufferManager )
   188     if( mBufferManager && !mSilentDataFetch)
   163         mBufferManager->setPosition( row );
   189         mBufferManager->setPosition( row );
   164 
   190 
   165     switch ( role )
   191     switch ( role )
   166         {
   192         {
   167     case HgWidget::HgVisibilityRole:
   193     case HgWidget::HgVisibilityRole:
   180             returnValue = texts;
   206             returnValue = texts;
   181             break;
   207             break;
   182             }
   208             }
   183         case Qt::DecorationRole:
   209         case Qt::DecorationRole:
   184             {
   210             {
   185             // INFO("Requesting model item" << row << ", " << mFiles.at(row));
   211                 if (mFiles.at(row).isEmpty())
   186             if (mFiles.at(row).isEmpty()) {
   212                 {
   187                 returnValue = mDefaultIcon;
   213                     
   188             }
   214                     switch (mImageType)
   189             else {
       
   190                 QImage icon = mImages.at(row);
       
   191                 if ( !icon.isNull() )
       
   192                     {
   215                     {
   193                     if (mUseLowResImages) {
   216                     case ImageTypeHbIcon:
   194                         QSize size = icon.size();
   217                         returnValue = mHbIcon;
   195                         icon = icon.scaled(QSize(size.width()/4, size.height()/4));
   218                         break;
       
   219                     case ImageTypeQImage:
       
   220                         returnValue = mDefaultIcon;
       
   221                         break;
       
   222                     case ImageTypeQIcon:
       
   223                         returnValue = mQIcon;
       
   224                         break;
       
   225                     case ImageTypeQPixmap:
       
   226                         returnValue = mDefaultPixmap;
       
   227                         break;
   196                     }
   228                     }
   197 
   229                 
   198                     switch(mImageType)
   230                 }
       
   231                 else
       
   232                 {
       
   233                     switch (mImageType)
       
   234                     {
       
   235                     case ImageTypeQImage:
       
   236                     {
       
   237                         QImage image = mImages.at(row);
       
   238                         if (image.isNull())
   199                         {
   239                         {
   200                         case ImageTypeHbIcon:
   240                             returnValue = mDefaultIcon;
       
   241                         }
       
   242                         else
       
   243                         {
       
   244                             if (mUseLowResImages)
   201                             {
   245                             {
   202                             returnValue = mHbIcon;
   246                                 QSize size = image.size();
   203                             break;
   247                                 image = image.scaled(QSize(size.width()/4, size.height()/4));
       
   248                                 returnValue = image;
   204                             }
   249                             }
   205                         case ImageTypeQImage:
   250                             else
   206                             {
   251                             {
   207                             returnValue = icon;
   252                                 returnValue = image;
   208                             break;
       
   209                             }
   253                             }
   210                         case ImageTypeQIcon:
       
   211                             {
       
   212                             returnValue = mQIcon;
       
   213                             break;
       
   214                             }
       
   215                         default:
       
   216                             break;
       
   217                         }
   254                         }
   218 
       
   219                     }
   255                     }
   220                 else
   256                     break;
       
   257                     case ImageTypeQPixmap:
   221                     {
   258                     {
   222                     returnValue = mDefaultIcon;
   259                         QPixmap pixmap = mPixmaps.at(row);
       
   260                         if (pixmap.isNull())
       
   261                         {
       
   262                             returnValue = mDefaultPixmap;
       
   263                         }
       
   264                         else
       
   265                         {
       
   266                             returnValue = pixmap;
       
   267                         }
   223                     }
   268                     }
       
   269                     break;
       
   270                     case ImageTypeHbIcon:
       
   271                     {
       
   272                         returnValue = mHbIcon;
       
   273                     }
       
   274                     break;
       
   275                     case ImageTypeQIcon:
       
   276                     {
       
   277                         returnValue = mQIcon;
       
   278                     }
       
   279                     break;
       
   280                     }                    
   224                 }
   281                 }
   225             break;
   282             break;
   226             }
   283             }
   227         case Hb::IndexFeedbackRole:
   284         case Hb::IndexFeedbackRole:
   228             {
   285             {
   360 }
   417 }
   361 
   418 
   362 void HgWidgetTestDataModel::reset()
   419 void HgWidgetTestDataModel::reset()
   363 {
   420 {
   364     emit beginResetModel();
   421     emit beginResetModel();
   365     mImages.removeAt(0);
       
   366     mFiles.removeAt(0);
   422     mFiles.removeAt(0);
       
   423     if (mImageType == ImageTypeQPixmap)
       
   424     {
       
   425         mPixmaps.removeAt(0);
       
   426     }
       
   427     else
       
   428         mImages.removeAt(0);
   367     emit endResetModel();
   429     emit endResetModel();
   368 }
   430 }
   369 
   431 
   370 /*!
   432 /*!
   371  Slot to be called when album art for the \a index needs to be updated.
   433  Slot to be called when album art for the \a index needs to be updated.
   445     mThumbnailRequestPending = false;
   507     mThumbnailRequestPending = false;
   446 
   508 
   447     delete mBufferManager;
   509     delete mBufferManager;
   448     mBufferManager = 0;
   510     mBufferManager = 0;
   449     mBufferManager = new BufferManager(this, buffer, treshhold, 0, mFiles.count());
   511     mBufferManager = new BufferManager(this, buffer, treshhold, 0, mFiles.count());
   450     for (int i = 0; i<mImages.count();i++) {
   512     if (mImageType == ImageTypeQPixmap)
   451         mImages.replace(i, QImage());
   513     {
       
   514         for (int i = 0; i<mPixmaps.count();i++) {
       
   515             mPixmaps.replace(i, QPixmap());
       
   516         }        
       
   517 
       
   518     }
       
   519     else
       
   520     {
       
   521         for (int i = 0; i<mImages.count();i++) {
       
   522             mImages.replace(i, QImage());
       
   523         }
       
   524 
   452     }
   525     }
   453 }
   526 }
   454 
   527 
   455 void HgWidgetTestDataModel::release(int start, int end)
   528 void HgWidgetTestDataModel::release(int start, int end)
   456 {
   529 {
   468         mWaitingThumbnails.removeOne(mFiles.at(i));
   541         mWaitingThumbnails.removeOne(mFiles.at(i));
   469         if (mThumbnailRequestIndex==i && mThumbnailRequestID!=-1){
   542         if (mThumbnailRequestIndex==i && mThumbnailRequestID!=-1){
   470             mWrapper->cancelRequest(mThumbnailRequestID);
   543             mWrapper->cancelRequest(mThumbnailRequestID);
   471             requestNew = true;
   544             requestNew = true;
   472         }
   545         }
   473         mImages.replace(i,QImage());
   546         if (mImageType == ImageTypeQPixmap)
       
   547         {
       
   548             mPixmaps.replace(i, QPixmap());
       
   549         }
       
   550         else
       
   551         {
       
   552             mImages.replace(i,QImage());
       
   553         }
   474     }
   554     }
   475 
   555 
   476     if (requestNew){
   556     if (requestNew){
   477         mThumbnailRequestIndex = -1;
   557         mThumbnailRequestIndex = -1;
   478         mThumbnailRequestID = -1;
   558         mThumbnailRequestID = -1;
   507 
   587 
   508 void HgWidgetTestDataModel::thumbnailReady( QPixmap pixmap, void* data, int /*id*/, int error )
   588 void HgWidgetTestDataModel::thumbnailReady( QPixmap pixmap, void* data, int /*id*/, int error )
   509 {
   589 {
   510     if (!error && !pixmap.isNull() ){
   590     if (!error && !pixmap.isNull() ){
   511 //        int idx = reinterpret_cast<int>(data);
   591 //        int idx = reinterpret_cast<int>(data);
   512         mImages.replace(mThumbnailRequestIndex,pixmap.toImage().convertToFormat(QImage::Format_RGB16));
   592         if (mImageType == ImageTypeQPixmap)
       
   593         {
       
   594             mPixmaps.replace(mThumbnailRequestIndex, pixmap);
       
   595         }
       
   596         else {
       
   597             mImages.replace(mThumbnailRequestIndex,pixmap.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied));//.convertToFormat(QImage::Format_RGB16));            
       
   598         }
   513         QModelIndex modelIndex = QAbstractItemModel::createIndex(mThumbnailRequestIndex, 0);
   599         QModelIndex modelIndex = QAbstractItemModel::createIndex(mThumbnailRequestIndex, 0);
   514         emit dataChanged(modelIndex, modelIndex);
   600         emit dataChanged(modelIndex, modelIndex);
   515     }
   601     }
   516     mThumbnailRequestIndex = -1;
   602     mThumbnailRequestIndex = -1;
   517     mThumbnailRequestID = -1;
   603     mThumbnailRequestID = -1;