ganeswidgets/tsrc/fute/HgWidgetTest/src/hgwidgettestalbumartmanager.cpp
changeset 0 89c329efa980
equal deleted inserted replaced
-1:000000000000 0:89c329efa980
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <thumbnailmanager_qt.h>
       
    19 #include <QTimer>
       
    20 
       
    21 #include "hgwidgettestalbumartmanager.h"
       
    22 
       
    23 const int KMaxThumbnailReq = 1;
       
    24 
       
    25 /*!
       
    26     \class HgWidgetTestAlbumArtManager
       
    27     \brief Music Player collection album art manager.
       
    28 
       
    29     Collection album art manager provides access to album art needed for
       
    30     display in certain collection views. It hides interface to the thumbnail
       
    31     manager and also implements a caching mechanism for performance reasons.
       
    32 */
       
    33 
       
    34 /*!
       
    35     \fn void albumArtReady( int index )
       
    36 
       
    37     This signal is emitted when album art for \a index is ready.
       
    38 
       
    39     \sa getAlbumArt()
       
    40  */
       
    41 
       
    42 /*!
       
    43     \fn void albumCacheReady()
       
    44 
       
    45     This signal is emitted when album art cache is ready.
       
    46 
       
    47     \sa cacheAlbumArt()
       
    48  */
       
    49 
       
    50 /*!
       
    51  Constructs the album art manager.
       
    52  */
       
    53 HgWidgetTestAlbumArtManager::HgWidgetTestAlbumArtManager(QObject *parent)
       
    54     : QObject(parent),
       
    55       mCachingInProgress(false),
       
    56       mRequestCount(0),
       
    57       mMask()
       
    58 {
       
    59     mThumbnailManager = new ThumbnailManager(this);
       
    60     mThumbnailManager->setMode(ThumbnailManager::Default);
       
    61     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality);
       
    62 //    mThumbnailManager->setMode(ThumbnailManager::CreateQImages);
       
    63     QPixmap qpixmap(":/icons/teardrop_mask.png");
       
    64     mMask = QBitmap(qpixmap);
       
    65 
       
    66 //    connect( mThumbnailManager, SIGNAL(thumbnailReady(QImage, void *, int, int)),
       
    67 //             this, SLOT(thumbnailReady(QImage, void *, int, int)) );
       
    68     connect( mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
       
    69              this, SLOT(thumbnailReady(QPixmap, void *, int, int)) );
       
    70 }
       
    71 
       
    72 /*!
       
    73  Destructs the album art manager.
       
    74  */
       
    75 HgWidgetTestAlbumArtManager::~HgWidgetTestAlbumArtManager()
       
    76 {
       
    77     cancel();
       
    78     mImageCache.clear();
       
    79 }
       
    80 
       
    81 void HgWidgetTestAlbumArtManager::setThumbnailSize(ThumbnailManager::ThumbnailSize size)
       
    82 {
       
    83     cancel();
       
    84     mImageCache.clear();
       
    85     mThumbnailManager->setThumbnailSize(size);
       
    86 }
       
    87 
       
    88 /*!
       
    89  Returns the album art for the given \a albumArtUri. If the album art is not
       
    90  available in its cache, an asynchronous request is made to the thumbnail manager
       
    91  and a null icon is returned.
       
    92 
       
    93  \sa signal albumArtReady
       
    94  */
       
    95 QImage HgWidgetTestAlbumArtManager::albumArt( const QString& albumArtUri, int index )
       
    96 {
       
    97     QImage icon;
       
    98     if ( mImageCache.contains(albumArtUri) ) {
       
    99         icon = mImageCache.value(albumArtUri);
       
   100         // If you want to cache all images disable this line. However, with large 
       
   101         // amount of items OOM will occure.
       
   102 //        mImageCache.remove(albumArtUri);
       
   103     }
       
   104     else {
       
   105         if ( mRequestCount < KMaxThumbnailReq ) {
       
   106             // Using negative index as priority will ensure that thumbnail requests
       
   107             // are processed in the order they were requested.
       
   108             int *clientData = new int(index);
       
   109             int reqId = mThumbnailManager->getThumbnail( albumArtUri, clientData, -1 );
       
   110             if ( reqId != -1 ) {
       
   111                 mTnmReqMap.insert( reqId, albumArtUri );
       
   112                 mRequestCount++;
       
   113             }
       
   114             else {
       
   115             }
       
   116         }
       
   117         else {
       
   118             mRequestQueue.enqueue( qMakePair(albumArtUri, index) );
       
   119         }
       
   120     }
       
   121     return icon;
       
   122 }
       
   123 
       
   124 /*!
       
   125  Request to cache the album art for the items specified in \a albumArtList.
       
   126  Returns 'true' if caching is started. If all items already exist in cache,
       
   127  'false' is returned.
       
   128 
       
   129  \sa signal albumCacheReady
       
   130  */
       
   131 bool HgWidgetTestAlbumArtManager::cacheAlbumArt( const QStringList albumArtList )
       
   132 {
       
   133     int allAvailable = true;
       
   134     if ( !albumArtList.empty() ) {
       
   135         QString albumArtUri;
       
   136         int reqId;
       
   137         QStringListIterator iter(albumArtList);
       
   138         while ( iter.hasNext() ) {
       
   139             albumArtUri = iter.next();
       
   140             if ( !mImageCache.contains(albumArtUri) ) {
       
   141                 reqId = mThumbnailManager->getThumbnail( albumArtUri );
       
   142                 if ( reqId != -1 ) {
       
   143                     mTnmReqMap.insert( reqId, albumArtUri );
       
   144                     mRequestCount++;
       
   145                     allAvailable = false;
       
   146                 }
       
   147                 else {
       
   148                 }
       
   149             }
       
   150         }
       
   151     }
       
   152 
       
   153     if ( allAvailable ) {
       
   154         return false;
       
   155     }
       
   156     else {
       
   157         mCachingInProgress = true;
       
   158         return true;
       
   159     }
       
   160 }
       
   161 
       
   162 /*!
       
   163  Cancels all outstanding album art requests.
       
   164 
       
   165  \sa getAlbumArt, cacheAlbumArt
       
   166  */
       
   167 void HgWidgetTestAlbumArtManager::cancel()
       
   168 {
       
   169     if ( !mTnmReqMap.empty() ) {
       
   170         QMapIterator<int, QString> iter(mTnmReqMap);
       
   171         while ( iter.hasNext() ) {
       
   172             iter.next();
       
   173             bool result = mThumbnailManager->cancelRequest(iter.key());
       
   174         }
       
   175     }
       
   176     mTnmReqMap.clear();
       
   177     mRequestQueue.clear();
       
   178     mRequestCount = 0;
       
   179     mCachingInProgress = false;
       
   180 }
       
   181 
       
   182 
       
   183 /*!
       
   184  Slot to be called when thumbnail bitmap generation or loading is complete.
       
   185  */
       
   186 void HgWidgetTestAlbumArtManager::thumbnailReady( const QPixmap& pixmap, void *data, int id, int error )
       
   187 {
       
   188     // Find the index
       
   189     if ( mTnmReqMap.contains(id) ) {
       
   190         // Remove the request whether it completed successfully or with error.
       
   191         QString albumArtUri = mTnmReqMap[id];
       
   192         mTnmReqMap.remove( id );
       
   193         mRequestCount--;
       
   194 
       
   195         if ( mCachingInProgress ) {
       
   196             if ( error == 0 ) {
       
   197 //                QPixmap art(pixmap);
       
   198 //                applyMask(art);
       
   199 //                QIcon qicon(art);
       
   200 //                mImageCache.insert(albumArtUri, pixmap);
       
   201             }
       
   202             else {
       
   203             }
       
   204             if ( mTnmReqMap.empty() ) {
       
   205                 mCachingInProgress = false;
       
   206                 emit albumCacheReady();
       
   207                 return;
       
   208             }
       
   209         }
       
   210         else {
       
   211             if ( error == 0 ) {
       
   212                 int *clientData = (int *)data;
       
   213                 int index = *clientData;
       
   214                 delete clientData;
       
   215                 QImage image = pixmap.toImage().convertToFormat(QImage::Format_RGB16);
       
   216                 mImageCache.insert(albumArtUri, image);
       
   217                 emit albumArtReady(index);
       
   218                 QTimer::singleShot(0, this, SLOT(executeNext()));
       
   219             }
       
   220             else {
       
   221             }
       
   222         }
       
   223     }
       
   224 }
       
   225 
       
   226 /*!
       
   227  Slot to be called when thumbnail bitmap generation or loading is complete.
       
   228  */
       
   229 void HgWidgetTestAlbumArtManager::thumbnailReady( const QImage& image, void *data, int id, int error )
       
   230 {
       
   231     // Find the index
       
   232     if ( mTnmReqMap.contains(id) ) {
       
   233         // Remove the request whether it completed successfully or with error.
       
   234         QString albumArtUri = mTnmReqMap[id];
       
   235         mTnmReqMap.remove( id );
       
   236         mRequestCount--;
       
   237 
       
   238         if ( mCachingInProgress ) {
       
   239             if ( error == 0 ) {
       
   240 //                QPixmap art(pixmap);
       
   241 //                applyMask(art);
       
   242 //                QIcon qicon(art);
       
   243                 mImageCache.insert(albumArtUri, image);
       
   244             }
       
   245             else {
       
   246             }
       
   247             if ( mTnmReqMap.empty() ) {
       
   248                 mCachingInProgress = false;
       
   249                 emit albumCacheReady();
       
   250                 return;
       
   251             }
       
   252         }
       
   253         else {
       
   254             if ( error == 0 ) {
       
   255                 int *clientData = (int *)data;
       
   256                 int index = *clientData;
       
   257                 delete clientData;
       
   258                 QSize size = image.size();
       
   259                 QImage newImage;
       
   260                 newImage = image.copy();
       
   261                 mImageCache.insert(albumArtUri, newImage);
       
   262                 emit albumArtReady(index);
       
   263                 QTimer::singleShot(0, this, SLOT(executeNext()));
       
   264             }
       
   265             else {
       
   266             }
       
   267         }
       
   268     }
       
   269 }
       
   270 
       
   271 
       
   272 /*!
       
   273  Applies mask on the thumbnail.
       
   274  */
       
   275 void HgWidgetTestAlbumArtManager::applyMask( QPixmap& pixmap )
       
   276 {
       
   277     pixmap.setMask(mMask);
       
   278 }
       
   279 
       
   280 void HgWidgetTestAlbumArtManager::executeNext()
       
   281 {
       
   282     // Check to see if any request is pending in the queue
       
   283     while ( !mRequestQueue.isEmpty()
       
   284             && (mRequestCount < KMaxThumbnailReq) ) {
       
   285         QPair<QString, int> req = mRequestQueue.dequeue();
       
   286         QString albumArtUri = req.first;
       
   287         int index = req.second;
       
   288 
       
   289         // Using negative index as priority will ensure that thumbnail requests
       
   290         // are processed in the order they were requested.
       
   291         int *clientData = new int(index);
       
   292         int reqId = mThumbnailManager->getThumbnail( albumArtUri, clientData, -1 );
       
   293         if ( reqId != -1 ) {
       
   294             mTnmReqMap.insert( reqId, albumArtUri );
       
   295             mRequestCount++;
       
   296         }
       
   297         else {
       
   298         }
       
   299     }
       
   300 
       
   301 }