src/hbcore/image/hbbadgeicon.cpp
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 7 923ff622b8b9
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    21 ** If you have questions regarding the use of this file, please contact
    21 ** If you have questions regarding the use of this file, please contact
    22 ** Nokia at developer.feedback@nokia.com.
    22 ** Nokia at developer.feedback@nokia.com.
    23 **
    23 **
    24 ****************************************************************************/
    24 ****************************************************************************/
    25 
    25 
       
    26 #include "hbbadgeicon_p.h"
    26 #include "hbicon.h"
    27 #include "hbicon.h"
    27 #include "hbbadgeiconinfo_p.h"
    28 #include "hbbadgeiconinfo_p.h"
    28 #include "hbbadgeicon_p.h"
       
    29 #include <QPainter>
    29 #include <QPainter>
    30 
    30 
    31 /*!
    31 /*!
    32   @beta
    32   @beta
    33   @hbcore
    33   @hbcore
    71   implementation provides no scaling of the badge icon.
    71   implementation provides no scaling of the badge icon.
    72   \internal
    72   \internal
    73  */
    73  */
    74 void HbBadgeIcon::addBadge(Qt::Alignment alignment,
    74 void HbBadgeIcon::addBadge(Qt::Alignment alignment,
    75                            const HbIcon &icon,
    75                            const HbIcon &icon,
    76                            int zValue)
    76                            int zValue,
    77 {
    77                            const QSizeF &sizeFactor,
    78     HbBadgeIconInfo info(icon, alignment, zValue);
    78                            Qt::AspectRatioMode aspectRatio)
    79     int size = mBadgeIcons.size();
    79 {
    80     if (size == 0) {
    80     HbBadgeIconInfo info(icon, alignment, zValue, sizeFactor, aspectRatio);
       
    81     int badgeCount = mBadgeIcons.count();
       
    82     if (badgeCount == 0) {
    81         mBadgeIcons.append(info);
    83         mBadgeIcons.append(info);
    82     }
    84     } else {
    83     else {
       
    84         bool added = false;
    85         bool added = false;
    85         //Find a spot to insert the badgeinfo in the list.
    86         //Find a spot to insert the badgeinfo in the list.
    86         for (int i = size - 1; i >= 0; i--){
    87         for (int i = badgeCount - 1; i >= 0; i--) {
    87             if (mBadgeIcons[i].zValue() > zValue){
    88             if (mBadgeIcons[i].zValue() > zValue) {
    88                 mBadgeIcons.insert(i + 1, info);
    89                 mBadgeIcons.insert(i + 1, info);
    89                 added = true;
    90                 added = true;
    90                 break;
    91                 break;
    91             }
    92             }
    92         }
    93         }
   145 /*!
   146 /*!
   146   \internal
   147   \internal
   147   Paint all badges in z-order.
   148   Paint all badges in z-order.
   148 */
   149 */
   149 void HbBadgeIcon::paint(QPainter *painter,
   150 void HbBadgeIcon::paint(QPainter *painter,
   150                         const QRectF& rect,
   151                         const QRectF &rect,
   151                         QIcon::Mode mode,
   152                         QIcon::Mode mode,
   152                         QIcon::State state,
   153                         QIcon::State state,
   153                         bool mirror)
   154                         bool mirror)
   154 {
   155 {
   155     int count = mBadgeIcons.count();
   156     int count = mBadgeIcons.count();
   156 
   157 
   157     for (int i = count - 1; i >= 0; i--) {
   158     for (int i = count - 1; i >= 0; i--) {
   158         HbBadgeIconInfo aIcon = mBadgeIcons[i];
   159         HbBadgeIconInfo badge = mBadgeIcons.at(i);
   159         Qt::Alignment align = aIcon.alignment();
   160         // Fix the alignment.
       
   161         Qt::Alignment align = badge.alignment();
   160         Qt::Alignment absAlign = align;
   162         Qt::Alignment absAlign = align;
   161         if (mirror) {
   163         if (mirror) {
   162             absAlign = align & ~(Qt::AlignRight | Qt::AlignLeft);
   164             absAlign = align & ~(Qt::AlignRight | Qt::AlignLeft);
   163 
       
   164             if (align & Qt::AlignLeft) {
   165             if (align & Qt::AlignLeft) {
   165                 absAlign |= Qt::AlignRight;
   166                 absAlign |= Qt::AlignRight;
   166             }
   167             }
   167             if (align & Qt::AlignRight) {
   168             if (align & Qt::AlignRight) {
   168                 absAlign |= Qt::AlignLeft;
   169                 absAlign |= Qt::AlignLeft;
   169             }
   170             }
   170         }
   171         }
   171         // ... and then draw at the specified location.
   172         // Update the size.
   172         aIcon.icon().paint(painter,
   173         QSizeF sizeFactor = badge.sizeFactor();
   173                  rect,
   174         if (!sizeFactor.isNull()) {
   174                  Qt::KeepAspectRatio,
   175             QSizeF targetSizeF(sizeFactor.width() * rect.width(), sizeFactor.height() * rect.height());
   175                  absAlign,
   176             QSize targetSize = targetSizeF.toSize();
   176                  mode,
   177             HbIcon icon = badge.icon();
   177                  state);
   178             if (targetSize != icon.size().toSize()) {
       
   179                 icon.setSize(targetSize);
       
   180                 badge.setIcon(icon);
       
   181                 mBadgeIcons[i] = badge;
       
   182             }
       
   183         }
       
   184         // And finally draw the badge.
       
   185         badge.icon().paint(painter,
       
   186                            rect,
       
   187                            badge.aspectRatio(),
       
   188                            absAlign,
       
   189                            mode,
       
   190                            state);
   178     }
   191     }
   179 }
   192 }
   180 
   193 
   181 /*!
   194 /*!
   182   \internal
   195   \internal
   183  */
   196  */
   184 void HbBadgeIcon::externalize(QDataStream& stream)
   197 void HbBadgeIcon::externalize(QDataStream &stream)
   185 {
   198 {
   186     int size = mBadgeIcons.size();
   199     int count = mBadgeIcons.count();
   187     // Write out how many badges we'll save first
   200     // Write out how many badges we'll save first
   188     stream << size;
   201     stream << count;
   189     // And write each item
   202     // And write each item
   190     for (int i = 0; i < size; i++) {
   203     for (int i = 0; i < count; i++) {
   191         HbBadgeIconInfo aIcon = mBadgeIcons[i];
   204         const HbBadgeIconInfo &badge(mBadgeIcons.at(i));
   192         stream << aIcon.icon();
   205         stream << badge.icon();
   193         stream << (qint32)(aIcon.alignment());
   206         stream << (qint32) badge.alignment();
   194         stream << aIcon.zValue();
   207         stream << badge.zValue();
       
   208         stream << badge.sizeFactor();
       
   209         stream << (qint32) badge.aspectRatio();
   195     }
   210     }
   196 }
   211 }
   197 
   212 
   198 
   213 
   199 /*!
   214 /*!
   200 \Internal
   215 \Internal
   201 */
   216 */
   202 void HbBadgeIcon::internalize(QDataStream& stream)
   217 void HbBadgeIcon::internalize(QDataStream &stream)
   203 {
   218 {
   204     int howManyBadges;
   219     int howManyBadges;
   205     stream >> howManyBadges;
   220     stream >> howManyBadges;
   206     for (int i = 0; i<howManyBadges; ++i) {
   221     for (int i = 0; i < howManyBadges; ++i) {
   207         qint32 align;
   222         qint32 align;
   208         qint32 zValue;
   223         qint32 zValue;
   209         HbIcon icon;
   224         HbIcon icon;
       
   225         QSizeF sizeFactor;
       
   226         qint32 aspectRatio;
   210         stream >> icon;
   227         stream >> icon;
   211         stream >> align;
   228         stream >> align;
   212         stream >> zValue;
   229         stream >> zValue;
   213 
   230         stream >> sizeFactor;
   214         HbBadgeIconInfo info(icon, (Qt::Alignment)align, zValue);
   231         stream >> aspectRatio;
       
   232 
       
   233         HbBadgeIconInfo info(icon, (Qt::Alignment) align, zValue,
       
   234                              sizeFactor, (Qt::AspectRatioMode) aspectRatio);
   215         mBadgeIcons.append(info);
   235         mBadgeIcons.append(info);
   216     };
   236     }
   217 }
   237 }