src/hbcore/image/hbpixmapiconrenderer.cpp
changeset 1 f7ac710697a9
child 5 627c4a0fd0e7
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbpixmapiconrenderer_p.h"
       
    27 #include "hbmaskableiconimpl_p.h"
       
    28 
       
    29 #include <QStyleOption>
       
    30 #include <QApplication>
       
    31 
       
    32 struct HbPixmapIconMaskedData
       
    33 {
       
    34     QPixmap    currentPixmap;
       
    35 };
       
    36 
       
    37 HbPixmapIconRenderer::HbPixmapIconRenderer(const QPixmap &pixmap, HbIconImpl *impl)
       
    38         : iconMode(QIcon::Normal),
       
    39         iconPropertiesApplied(false),
       
    40         pixmapData(pixmap),
       
    41         iconImpl(impl)
       
    42 {
       
    43 }
       
    44 
       
    45 HbPixmapIconRenderer::~HbPixmapIconRenderer()
       
    46 {
       
    47 }
       
    48 
       
    49 void HbPixmapIconRenderer::draw(QPainter* painter,
       
    50                                 const QPointF &topLeft,
       
    51                                 const QPainterPath &clipPath,
       
    52                                 HbMaskableIconImpl * maskIconData)
       
    53 {
       
    54     if (!iconPropertiesApplied) {
       
    55         applyIconProperties();
       
    56     }
       
    57 
       
    58     QPixmap pixmapToDraw = pixmapData;
       
    59 
       
    60     if (maskIconData) {
       
    61         pixmapToDraw = getMaskedPixmap(maskIconData);
       
    62         if (pixmapToDraw.isNull()) {
       
    63             pixmapToDraw = pixmapData;
       
    64         }
       
    65     }
       
    66 
       
    67     doDraw(painter, topLeft, pixmapToDraw, clipPath);
       
    68 }
       
    69 
       
    70 void HbPixmapIconRenderer::doDraw(QPainter * painter,
       
    71                                   const QPointF & topLeft,
       
    72                                   const QPixmap & finalPixmap,
       
    73                                   const QPainterPath & clipPath)
       
    74 {
       
    75     if (!clipPath.isEmpty()) {
       
    76         QPainterPath oldPath;
       
    77         bool clipped = painter->hasClipping();
       
    78     
       
    79         if (!clipped) {
       
    80             painter->setClipping(true);
       
    81         }
       
    82         
       
    83         QRectF cliprect = clipPath.boundingRect();
       
    84         QPainterPath intersect(clipPath);
       
    85         if (clipped) {
       
    86             oldPath = painter->clipPath();
       
    87             QRectF oldrect = oldPath.boundingRect();
       
    88             intersect =  oldPath.intersected(clipPath);
       
    89             QRectF interrect = intersect.boundingRect();
       
    90         }
       
    91     
       
    92         painter->setClipPath(intersect, Qt::ReplaceClip);     
       
    93         painter->drawPixmap(topLeft, finalPixmap);
       
    94     
       
    95         if (!clipped) {
       
    96             painter->setClipPath(oldPath, Qt::NoClip);
       
    97         } else {
       
    98             painter->setClipPath(oldPath);
       
    99         }
       
   100         painter->setClipping(clipped);
       
   101     }  else {
       
   102         painter->drawPixmap(topLeft, finalPixmap);
       
   103     }
       
   104 }
       
   105 
       
   106 void HbPixmapIconRenderer::applyIconProperties()
       
   107 {
       
   108     if ((iconColor.isValid()) && (iconMode != QIcon::Disabled)) {
       
   109         if (!pixmapData.isNull()) {
       
   110             QPixmap mask = pixmapData.alphaChannel();
       
   111             pixmapData.fill(iconColor);
       
   112             pixmapData.setAlphaChannel(mask);
       
   113         }
       
   114     }
       
   115 
       
   116     // Apply the mode
       
   117     if (iconMode != QIcon::Normal) {
       
   118         QStyleOption opt(0);
       
   119         opt.palette = QApplication::palette();
       
   120         pixmapData = QApplication::style()->generatedIconPixmap(iconMode, pixmapData, &opt);
       
   121     }
       
   122     iconPropertiesApplied = true;
       
   123 }
       
   124 
       
   125 QPixmap HbPixmapIconRenderer::getMaskedPixmap(HbMaskableIconImpl * maskIconData)
       
   126 {
       
   127     QPixmap maskedPixmap;
       
   128 
       
   129     HbPixmapIconMaskedData * mi = (HbPixmapIconMaskedData *)maskIconData->implData();
       
   130     if (maskIconData->maskChanged()) {
       
   131         if (!mi) {
       
   132             mi = new HbPixmapIconMaskedData();
       
   133         }
       
   134 
       
   135         mi->currentPixmap = pixmapData;
       
   136         mi->currentPixmap.setMask(maskIconData->mask());
       
   137         maskIconData->setImplData(mi);
       
   138     }
       
   139 
       
   140     if (mi) {
       
   141         maskedPixmap = mi->currentPixmap;
       
   142     }
       
   143 
       
   144     return maskedPixmap;
       
   145 }
       
   146 
       
   147 void HbPixmapIconRenderer::destroyMaskedData(HbIconMaskedData *data)
       
   148 {
       
   149     delete((HbPixmapIconMaskedData*)data);
       
   150 }
       
   151