src/hbcore/image/hbbadgeicon.cpp
changeset 0 16d8024aca5e
child 5 627c4a0fd0e7
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     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 "hbicon.h"
       
    27 #include "hbbadgeiconinfo_p.h"
       
    28 #include "hbbadgeicon_p.h"
       
    29 #include <QPainter>
       
    30 
       
    31 /*!
       
    32   @beta
       
    33   @hbcore
       
    34   \internal
       
    35   \class HbBadgeIcon
       
    36   \brief HbBadgeIcon represents zero or more badge icons to be drawn
       
    37   over an HbIcon.
       
    38 
       
    39   This class uses a QList<HbBadgeIconInfo> in z-order to store the badges
       
    40   that must be applied when rendering an HbIcon.
       
    41 
       
    42   This class is to be used only internally within HbCore. Furthermore,
       
    43   This class is not intended to be a superclass for anything else.
       
    44 */
       
    45 
       
    46 /*!
       
    47   Default constructor; needs to do nothing.
       
    48   \internal
       
    49 */
       
    50 HbBadgeIcon::HbBadgeIcon()
       
    51 {
       
    52     // This space intentionally left blank
       
    53 }
       
    54 
       
    55 
       
    56 /*!
       
    57   Destructor; no explicit work needs to be done here.
       
    58 \internal
       
    59 */
       
    60 HbBadgeIcon::~HbBadgeIcon()
       
    61 {
       
    62     // This space intentionally left blank
       
    63 }
       
    64 
       
    65 /*!
       
    66   Add an icon to be drawn at the specified alignment in the
       
    67   given z-plane after scaling a predetermined amount. The
       
    68   badges are stored in increasing z-order value.
       
    69 
       
    70   Note that scaling is here for API compatibility later, but this
       
    71   implementation provides no scaling of the badge icon.
       
    72   \internal
       
    73  */
       
    74 void HbBadgeIcon::addBadge(Qt::Alignment alignment,
       
    75                            const HbIcon &icon,
       
    76                            int zValue)
       
    77 {
       
    78     HbBadgeIconInfo info(icon, alignment, zValue);
       
    79     int size = mBadgeIcons.size();
       
    80     if (size == 0) {
       
    81         mBadgeIcons.append(info);
       
    82     }
       
    83     else {
       
    84         bool added = false;
       
    85         //Find a spot to insert the badgeinfo in the list.
       
    86         for (int i = size - 1; i >= 0; i--){
       
    87             if (mBadgeIcons[i].zValue() > zValue){
       
    88                 mBadgeIcons.insert(i + 1, info);
       
    89                 added = true;
       
    90                 break;
       
    91             }
       
    92         }
       
    93         //Not added anywhere so put it at the top.
       
    94         if (!added) {
       
    95             mBadgeIcons.prepend(info);
       
    96         }
       
    97     }
       
    98 }
       
    99 
       
   100 /*!
       
   101   Removes all badges with a specific \a _iconName as the HbIcon::iconName().
       
   102   \internal
       
   103  */
       
   104 bool HbBadgeIcon::removeBadge(const HbIcon &badge)
       
   105 {
       
   106     bool result = false;
       
   107     // Could make this a binary search, but complexity isn't worth it for normal use cases.
       
   108     int count = mBadgeIcons.count();
       
   109     for (int i = count - 1; i >= 0; --i) {
       
   110         const HbBadgeIconInfo &info = mBadgeIcons.at(i);
       
   111         if (info.icon() == badge) {
       
   112             mBadgeIcons.removeAt(i);
       
   113             result = true;
       
   114         }
       
   115     }
       
   116     return result;
       
   117 }
       
   118 
       
   119 /*!
       
   120   Indicate whether or not this icon is badged.
       
   121   \internal
       
   122 */
       
   123 bool HbBadgeIcon::isBadged() const
       
   124 {
       
   125     return mBadgeIcons.size() > 0;
       
   126 }
       
   127 
       
   128 /*!
       
   129   Remove all badge icons from this icon
       
   130   \internal
       
   131 */
       
   132 void HbBadgeIcon::removeAllBadges()
       
   133 {
       
   134     mBadgeIcons.clear();
       
   135 }
       
   136 
       
   137 /*!
       
   138     Returns the list of all badge icons
       
   139 */
       
   140 const QList<HbBadgeIconInfo> HbBadgeIcon::badges() const
       
   141 {
       
   142     return mBadgeIcons;
       
   143 }
       
   144 
       
   145 /*!
       
   146   \internal
       
   147   Paint all badges in z-order.
       
   148 */
       
   149 void HbBadgeIcon::paint(QPainter *painter,
       
   150                         const QRectF& rect,
       
   151                         QIcon::Mode mode,
       
   152                         QIcon::State state,
       
   153                         bool mirror)
       
   154 {
       
   155     int count = mBadgeIcons.count();
       
   156 
       
   157     for (int i = count - 1; i >= 0; i--) {
       
   158         HbBadgeIconInfo aIcon = mBadgeIcons[i];
       
   159         Qt::Alignment align = aIcon.alignment();
       
   160         Qt::Alignment absAlign = align;
       
   161         if (mirror) {
       
   162             absAlign = align & ~(Qt::AlignRight | Qt::AlignLeft);
       
   163 
       
   164             if (align & Qt::AlignLeft) {
       
   165                 absAlign |= Qt::AlignRight;
       
   166             }
       
   167             if (align & Qt::AlignRight) {
       
   168                 absAlign |= Qt::AlignLeft;
       
   169             }
       
   170         }
       
   171         // ... and then draw at the specified location.
       
   172         aIcon.icon().paint(painter,
       
   173                  rect,
       
   174                  Qt::KeepAspectRatio,
       
   175                  absAlign,
       
   176                  mode,
       
   177                  state);
       
   178     }
       
   179 }
       
   180 
       
   181 /*!
       
   182   \internal
       
   183  */
       
   184 void HbBadgeIcon::externalize(QDataStream& stream)
       
   185 {
       
   186     int size = mBadgeIcons.size();
       
   187     // Write out how many badges we'll save first
       
   188     stream << size;
       
   189     // And write each item
       
   190     for (int i = 0; i < size; i++) {
       
   191         HbBadgeIconInfo aIcon = mBadgeIcons[i];
       
   192         stream << aIcon.icon();
       
   193         stream << (qint32)(aIcon.alignment());
       
   194         stream << aIcon.zValue();
       
   195     }
       
   196 }
       
   197 
       
   198 
       
   199 /*!
       
   200 \Internal
       
   201 */
       
   202 void HbBadgeIcon::internalize(QDataStream& stream)
       
   203 {
       
   204     int howManyBadges;
       
   205     stream >> howManyBadges;
       
   206     for (int i = 0; i<howManyBadges; ++i) {
       
   207         qint32 align;
       
   208         qint32 zValue;
       
   209         HbIcon icon;
       
   210         stream >> icon;
       
   211         stream >> align;
       
   212         stream >> zValue;
       
   213 
       
   214         HbBadgeIconInfo info(icon, (Qt::Alignment)align, zValue);
       
   215         mBadgeIcons.append(info);
       
   216     };
       
   217 }