contentstorage/camificonengine/src/camificonengine.cpp
changeset 115 3ab5c078b490
equal deleted inserted replaced
109:e0aa398e6810 115:3ab5c078b490
       
     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: camificonengine.cpp
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QPainter>
       
    19 #include <QPixmap>
       
    20 #include <QFileInfo>
       
    21 #include <QDebug>
       
    22 
       
    23 #include <AknIconUtils.h> // avkon
       
    24 #include <apgicnfl.h> // fbsbitmap
       
    25 
       
    26 #include "camificonengine.h"
       
    27 
       
    28 const int cacheSize = 4;
       
    29 
       
    30 inline QString CaMifIconEngine::pmcKey(const QSize &size)
       
    31 { 
       
    32     return QString::number((size.width()<<11)|size.height(), 16);
       
    33 }
       
    34 
       
    35 CaMifIconEngine::CaMifIconEngine()
       
    36     : QIconEngineV2(),
       
    37       mMifFilename(),
       
    38       mCache(cacheSize),
       
    39       mBitmapCached(0),
       
    40       mMaskBitmapCached(0)
       
    41 {
       
    42 }
       
    43 
       
    44 CaMifIconEngine::CaMifIconEngine(const CaMifIconEngine &other) :
       
    45     QIconEngineV2(other),
       
    46     mMifFilename(other.mMifFilename),
       
    47     mCache(cacheSize),
       
    48     mBitmapCached(0),
       
    49     mMaskBitmapCached(0)
       
    50 {
       
    51 }
       
    52 
       
    53 CaMifIconEngine::~CaMifIconEngine()
       
    54 {
       
    55     mCache.clear();
       
    56     delete mBitmapCached;
       
    57     delete mMaskBitmapCached;
       
    58 }
       
    59 
       
    60 void CaMifIconEngine::cacheBitmapL()
       
    61 {
       
    62     CFbsBitmap *bitmap(0);
       
    63     CFbsBitmap *maskBitmap(0);
       
    64 
       
    65     TInt bitmapIndex = 0;
       
    66     TInt maskIndex = 1;
       
    67     TPtrC16 filename(
       
    68         reinterpret_cast<const TUint16*>(mMifFilename.utf16()));
       
    69     // it will change bitmap ids if it is mif (checking inside)
       
    70     AknIconUtils::ValidateLogicalAppIconId( filename,
       
    71         bitmapIndex, maskIndex );
       
    72     AknIconUtils::CreateIconLC( bitmap, maskBitmap, filename,
       
    73         bitmapIndex, maskIndex );
       
    74     mBitmapCached = bitmap;
       
    75     mMaskBitmapCached = maskBitmap;
       
    76     // bitmap and icon, AknsUtils::CreateIconLC doesn't specify the order
       
    77     CleanupStack::Pop(2);
       
    78 }
       
    79 void CaMifIconEngine::getPixmapFromBitmap(const QSize &size, QPixmap& pixmap)
       
    80 {
       
    81     if (!mBitmapCached || !mMaskBitmapCached) {
       
    82         TRAP_IGNORE(cacheBitmapL());
       
    83     }
       
    84     if (mBitmapCached && mMaskBitmapCached) {
       
    85         CFbsBitmap *bitmap = mBitmapCached;
       
    86         CFbsBitmap *maskBitmap = mMaskBitmapCached;
       
    87         AknIconUtils::SetSize(bitmap, TSize(size.width(), size.height()),
       
    88                         EAspectRatioPreservedAndUnusedSpaceRemoved);
       
    89     
       
    90         pixmap = pixmap.fromSymbianCFbsBitmap(bitmap);
       
    91         QPixmap mask;
       
    92         mask = mask.fromSymbianCFbsBitmap(maskBitmap);
       
    93         pixmap.setAlphaChannel(mask);
       
    94     }
       
    95 }
       
    96 
       
    97 QSize CaMifIconEngine::actualSize(const QSize &size,
       
    98     QIcon::Mode mode,
       
    99     QIcon::State state)
       
   100 {
       
   101     Q_UNUSED(mode);
       
   102     Q_UNUSED(state);
       
   103     QString key(pmcKey(size));
       
   104     qDebug() << "pmckey: " << key;
       
   105     QSize realSize;
       
   106             
       
   107     if (mCache.contains(key)) {
       
   108         realSize = size;
       
   109     } else {
       
   110         QPixmap pixmap;
       
   111         TPtrC16 filename(
       
   112             reinterpret_cast<const TUint16*>(mMifFilename.utf16()));
       
   113         getPixmapFromBitmap(size, pixmap);
       
   114         if (!pixmap.isNull()) {
       
   115             QPixmap *cachedPixmap = new QPixmap(pixmap);
       
   116             if (mCache.insert(key, cachedPixmap)) {
       
   117                 realSize = size;
       
   118                 // mCache has taken ownership of cachedPixmap. 
       
   119                 qDebug() << "pmckey INSERT";
       
   120             }
       
   121         }
       
   122     }
       
   123     return realSize;
       
   124 }
       
   125 
       
   126 QPixmap CaMifIconEngine::pixmap(const QSize &size,
       
   127     QIcon::Mode mode,
       
   128     QIcon::State state)
       
   129 {
       
   130     Q_UNUSED(mode);
       
   131     Q_UNUSED(state);
       
   132     QPixmap pixmap;
       
   133     actualSize(size, mode, state);
       
   134     QString key = pmcKey(size);
       
   135     if (mCache.contains(key)) {
       
   136         pixmap = *mCache[key];
       
   137     }
       
   138     return pixmap;
       
   139 }
       
   140 
       
   141 void CaMifIconEngine::addPixmap(const QPixmap &pixmap,
       
   142     QIcon::Mode mode,
       
   143     QIcon::State state)
       
   144 {
       
   145     Q_UNUSED(pixmap);
       
   146     Q_UNUSED(mode);
       
   147     Q_UNUSED(state);
       
   148 }
       
   149 
       
   150 void CaMifIconEngine::addFile(const QString &fileName,
       
   151     const QSize &size,
       
   152     QIcon::Mode mode,
       
   153     QIcon::State state)
       
   154 {
       
   155     Q_UNUSED(size);
       
   156     Q_UNUSED(mode);
       
   157     Q_UNUSED(state);
       
   158     // Mif file name is recorded only the first time "addFile" function
       
   159     // is invoked (from QIcon constructor).
       
   160     if (mMifFilename.isEmpty() && !fileName.isEmpty()) {
       
   161         
       
   162         QString abs = fileName;
       
   163         if (fileName.at(0) != QLatin1Char(':')) {
       
   164             abs = QFileInfo(fileName).absoluteFilePath();
       
   165         }
       
   166         if (abs.endsWith(QLatin1String(".mif"), Qt::CaseInsensitive))
       
   167         {
       
   168             mMifFilename = abs;
       
   169             mMifFilename.replace('/', '\\');
       
   170             TRAP_IGNORE(cacheBitmapL());
       
   171         } 
       
   172     }
       
   173 }
       
   174 
       
   175 void CaMifIconEngine::paint(QPainter *painter, const QRect &rect,
       
   176                            QIcon::Mode mode, QIcon::State state)
       
   177 {
       
   178     painter->drawPixmap(rect, pixmap(rect.size(), mode, state));
       
   179 }
       
   180 
       
   181 QString CaMifIconEngine::key() const
       
   182 {
       
   183     return QLatin1String("mif");
       
   184 }
       
   185 
       
   186 QIconEngineV2 *CaMifIconEngine::clone() const
       
   187 {
       
   188     return new CaMifIconEngine(*this);
       
   189 }
       
   190 
       
   191 
       
   192