qhbstyle/qhbstyleanimation.cpp
branchRCL_3
changeset 9 5d007b20cfd0
equal deleted inserted replaced
8:885c2596c964 9:5d007b20cfd0
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 *
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 #include "qhbstyleanimation.h"
       
    22 #include <qpixmap.h>
       
    23 #include <qwidget.h>
       
    24 #include <qpainter.h>
       
    25 #include <hbicon.h>
       
    26 #include <qbitmap.h>
       
    27 
       
    28 #include "qhbstyle.h"
       
    29 #include "hbframedrawer.h"
       
    30 
       
    31 
       
    32 QHbStyleAnimation::QHbStyleAnimation(QWidget* target, QObject *parent) :
       
    33     QObject(parent), m_animationIcon(0), m_target(target), m_point(QPoint(0, 0)), m_mask(0)
       
    34 {
       
    35 }
       
    36 
       
    37 QHbStyleAnimation::~QHbStyleAnimation()
       
    38 {
       
    39 }
       
    40 
       
    41 const QWidget* QHbStyleAnimation::target()const
       
    42 {
       
    43     return m_target.data();
       
    44 }
       
    45 
       
    46 QPoint QHbStyleAnimation::point()
       
    47 {
       
    48     return m_point;
       
    49 }
       
    50 
       
    51 void QHbStyleAnimation::setPoint(const QPoint& point)
       
    52 {
       
    53     m_point = point;
       
    54     m_target->update();
       
    55 }
       
    56 
       
    57 void QHbStyleAnimation::createAnimationIcon(QStyle::ControlElement element, Qt::Orientations orientation)
       
    58 {
       
    59     //Create mask
       
    60     HbFrameDrawer drawer;
       
    61     if (orientation == Qt::Horizontal){
       
    62         drawer.setFrameGraphicsName("qtg_fr_progbar_h_mask");
       
    63         drawer.setFrameType(HbFrameDrawer::ThreePiecesHorizontal);
       
    64     }
       
    65     else {
       
    66         drawer.setFrameGraphicsName("qtg_fr_progbar_v_mask");
       
    67         drawer.setFrameType(HbFrameDrawer::ThreePiecesVertical);
       
    68     }
       
    69     drawer.setFillWholeRect(true);
       
    70     m_mask.reset(new QPixmap(m_target->rect().size()));
       
    71     m_mask->fill(Qt::transparent);
       
    72     QPainter p(m_mask.data());
       
    73     drawer.paint(&p, m_target->rect());
       
    74     p.end();
       
    75 
       
    76     //Create animated icon
       
    77     QString iconName;
       
    78     switch (element) {
       
    79         case QStyle::CE_ProgressBarContents: {
       
    80              if (orientation == Qt::Horizontal)
       
    81                 iconName = QLatin1String("qtg_graf_progbar_h_wait");
       
    82              else
       
    83                 iconName = QLatin1String("qtg_graf_progbar_v_wait");
       
    84             break;
       
    85         }
       
    86         default:
       
    87             break;
       
    88     }
       
    89 
       
    90     if (!iconName.isNull() && !m_target->rect().isEmpty()) {
       
    91         HbIcon* icon = new HbIcon(iconName);
       
    92         if(orientation == Qt::Horizontal)
       
    93             icon->setSize(QSize(icon->width(), m_target->rect().height()));
       
    94         else
       
    95             icon->setSize(QSize(m_target->rect().width(), icon->height()));
       
    96 
       
    97         const qreal rectWidth = m_target->rect().width();
       
    98         const qreal iconWidth = icon->width();
       
    99         const qreal rectHeight = m_target->rect().height();
       
   100         const qreal iconHeight = icon->height();
       
   101 
       
   102         const int animationWidth = (orientation == Qt::Horizontal) ?  int(rectWidth + iconWidth) : int(rectWidth);
       
   103         const int animationHeight = (orientation == Qt::Horizontal) ?  int(rectHeight) : int(rectHeight + iconHeight);
       
   104 
       
   105         m_animationIcon.reset(new QPixmap(animationWidth, animationHeight));
       
   106         m_animationIcon->fill(Qt::transparent);
       
   107         QPainter p(m_animationIcon.data());
       
   108 
       
   109         if (orientation == Qt::Horizontal) {
       
   110             if (iconWidth > 0)
       
   111                 for (qreal i = 0 ; i < (rectWidth + iconWidth); i += iconWidth)
       
   112                     icon->paint(&p, QRectF(i, 0, iconWidth, iconHeight), Qt::IgnoreAspectRatio, Qt::AlignCenter, QIcon::Normal, QIcon::On);
       
   113         } else {
       
   114             if (iconHeight > 0)
       
   115                 for(qreal i = 0 ; i < (rectHeight + iconHeight) ; i += iconHeight)
       
   116                     icon->paint(&p, QRectF(0, i, iconWidth, iconHeight), Qt::IgnoreAspectRatio, Qt::AlignCenter, QIcon::Normal, QIcon::On);
       
   117         }
       
   118         p.end();
       
   119     }
       
   120 }
       
   121 
       
   122 void QHbStyleAnimation::paintAnimation(QPainter *painter)
       
   123 {
       
   124     Q_ASSERT(m_animationIcon);
       
   125     Q_ASSERT(painter);
       
   126 
       
   127     //Take part from animation icon
       
   128     QPixmap icon(m_target->rect().size());
       
   129     icon.fill(Qt::transparent);
       
   130     QPainter p(&icon);
       
   131     p.setCompositionMode(QPainter::CompositionMode_SourceOver);
       
   132     p.drawPixmap(QPointF(0, 0), *m_mask);
       
   133     p.setCompositionMode(QPainter::CompositionMode_SourceIn);
       
   134     p.drawPixmap(m_point, *m_animationIcon, QRect(0, 0, m_target->rect().width() + m_point.rx() * -1, m_target->rect().height() + m_point.ry() * -1));
       
   135     p.end();
       
   136 
       
   137     //paint animation
       
   138     painter->drawPixmap(QPointF(0, 0), icon);
       
   139 }