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