qhbstyle/qhbstyleanimation.cpp
changeset 4 90517678cc4f
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     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     delete m_animationIcon;
       
    40     delete m_mask;
       
    41 }
       
    42 
       
    43 const QWidget* QHbStyleAnimation::target()const
       
    44 {
       
    45     return m_target;
       
    46 }
       
    47 
       
    48 QPoint QHbStyleAnimation::point()
       
    49 {
       
    50     return m_point;
       
    51 }
       
    52 
       
    53 void QHbStyleAnimation::setPoint(const QPoint& point)
       
    54 {
       
    55     m_point = point;
       
    56     m_target->update();
       
    57 }
       
    58 
       
    59 void QHbStyleAnimation::createAnimationIcon(QStyle::ControlElement element, Qt::Orientations orientation)
       
    60 {
       
    61     //Create mask
       
    62     HbFrameDrawer drawer;
       
    63     if (orientation == Qt::Horizontal){
       
    64         drawer.setFrameGraphicsName("qtg_fr_progbar_h_mask");
       
    65         drawer.setFrameType(HbFrameDrawer::ThreePiecesHorizontal);
       
    66     }
       
    67     else {
       
    68         drawer.setFrameGraphicsName("qtg_fr_progbar_v_mask");
       
    69         drawer.setFrameType(HbFrameDrawer::ThreePiecesVertical);
       
    70     }
       
    71     drawer.setFillWholeRect(true);
       
    72     if (m_mask)
       
    73         delete m_mask;
       
    74     m_mask = new QPixmap(m_target->rect().size());
       
    75     m_mask->fill(Qt::transparent);
       
    76     QPainter p(m_mask);
       
    77     drawer.paint(&p, m_target->rect());
       
    78     p.end();
       
    79 
       
    80     //Create animated icon
       
    81     QString iconName;
       
    82     switch (element) {
       
    83         case QStyle::CE_ProgressBarContents: {
       
    84              if (orientation == Qt::Horizontal)
       
    85                 iconName = QLatin1String("qtg_graf_progbar_h_wait");
       
    86              else
       
    87                 iconName = QLatin1String("qtg_graf_progbar_v_wait");
       
    88             break;
       
    89         }
       
    90         default:
       
    91             break;
       
    92     }
       
    93 
       
    94     if (!iconName.isNull() && !m_target->rect().isEmpty()) {
       
    95         HbIcon* icon = q_check_ptr(new HbIcon(iconName));
       
    96         if(orientation == Qt::Horizontal)
       
    97             icon->setSize(QSize(icon->width(), m_target->rect().height()));
       
    98         else
       
    99             icon->setSize(QSize(m_target->rect().width(), icon->height()));
       
   100 
       
   101         const qreal rectWidth = m_target->rect().width();
       
   102         const qreal iconWidth = icon->width();
       
   103         const qreal rectHeight = m_target->rect().height();
       
   104         const qreal iconHeight = icon->height();
       
   105 
       
   106         if (m_animationIcon)
       
   107             delete m_animationIcon;
       
   108 
       
   109         const int animationWidth = (orientation == Qt::Horizontal) ?  int(rectWidth + iconWidth) : int(rectWidth);
       
   110         const int animationHeight = (orientation == Qt::Horizontal) ?  int(rectHeight) : int(rectHeight + iconHeight);
       
   111 
       
   112         m_animationIcon = q_check_ptr(new QPixmap(animationWidth, animationHeight));
       
   113         m_animationIcon->fill(Qt::transparent);
       
   114         QPainter p(m_animationIcon);
       
   115 
       
   116         if (orientation == Qt::Horizontal) {
       
   117             if (iconWidth > 0)
       
   118                 for (qreal i = 0 ; i < (rectWidth + iconWidth); i += iconWidth)
       
   119                     icon->paint(&p, QRectF(i, 0, iconWidth, iconHeight), Qt::IgnoreAspectRatio, Qt::AlignCenter, QIcon::Normal, QIcon::On);
       
   120         } else {
       
   121             if (iconHeight > 0)
       
   122                 for(qreal i = 0 ; i < (rectHeight + iconHeight) ; i += iconHeight)
       
   123                     icon->paint(&p, QRectF(0, i, iconWidth, iconHeight), Qt::IgnoreAspectRatio, Qt::AlignCenter, QIcon::Normal, QIcon::On);
       
   124         }
       
   125         p.end();
       
   126     }
       
   127 }
       
   128 
       
   129 void QHbStyleAnimation::paintAnimation(QPainter *painter)
       
   130 {
       
   131     Q_ASSERT(m_animationIcon);
       
   132     Q_ASSERT(painter);
       
   133 
       
   134     //Take part from animation icon
       
   135     QPixmap icon(m_target->rect().size());
       
   136     icon.fill(Qt::transparent);
       
   137     QPainter p(&icon);
       
   138     p.setCompositionMode(QPainter::CompositionMode_SourceOver);
       
   139     p.drawPixmap(QPointF(0, 0), *m_mask);
       
   140     p.setCompositionMode(QPainter::CompositionMode_SourceIn);
       
   141     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));
       
   142     p.end();
       
   143 
       
   144     //paint animation
       
   145     painter->drawPixmap(QPointF(0, 0), icon);
       
   146 }