ginebra/graphicsitemflipper.cpp
changeset 0 1450b09d0cfd
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <assert.h>
       
    20 #include "graphicsitemflipper.h"
       
    21 #include "utilities.h"
       
    22 
       
    23 GraphicsItemFlipper::GraphicsItemFlipper(QGraphicsItem *parentItem, QGraphicsScene *scene, int duration,
       
    24                                          uint updateInterval, int zValue)
       
    25   : m_timeLine(duration)
       
    26 {
       
    27     m_timeLine.setUpdateInterval(updateInterval);
       
    28     m_timeLine.setCurveShape(QTimeLine::EaseInOutCurve);
       
    29     safe_connect(&m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(updateFlipStep(qreal)));
       
    30     safe_connect(&m_timeLine, SIGNAL(finished()), this, SLOT(onFinished()));
       
    31 
       
    32     m_pixmapItem = new QGraphicsPixmapItem(parentItem);
       
    33     scene->addItem(m_pixmapItem);
       
    34     m_pixmapItem->setZValue(zValue);
       
    35 }
       
    36 
       
    37 GraphicsItemFlipper::~GraphicsItemFlipper()
       
    38 {
       
    39     delete m_pixmapItem;
       
    40     if(m_sourcePixmap) delete m_sourcePixmap;
       
    41     if(m_targetPixmap) delete m_targetPixmap;
       
    42 }
       
    43 
       
    44 void GraphicsItemFlipper::start(QGraphicsView *view, QGraphicsWidget *sourceWidget, QGraphicsWidget *targetWidget,
       
    45                                 bool forward)
       
    46 {
       
    47     emit starting();
       
    48 
       
    49     getPixmaps(view, sourceWidget, targetWidget);
       
    50 
       
    51     m_pixmapItem->setPixmap(forward ? *m_sourcePixmap : *m_targetPixmap);
       
    52 
       
    53     m_pixmapItem->show();
       
    54 
       
    55     m_timeLine.setDirection(forward ? QTimeLine::Forward : QTimeLine::Backward);
       
    56     m_timeLine.start();
       
    57 }
       
    58 
       
    59 void GraphicsItemFlipper::getPixmaps(QGraphicsView *view, QGraphicsWidget *sourceWidget, QGraphicsWidget *targetWidget)
       
    60 {
       
    61     Q_UNUSED(view)
       
    62     QStyleOptionGraphicsItem options;
       
    63 
       
    64     m_sourceRect = sourceWidget->geometry();
       
    65     m_targetRect = targetWidget->geometry();
       
    66 
       
    67     // Get a snapshot of the source widget.
       
    68     m_sourcePixmap = new QPixmap(m_sourceRect.size().toSize());
       
    69     QPainter painter(m_sourcePixmap);
       
    70     options.exposedRect = m_sourceRect;
       
    71     sourceWidget->paint(&painter, &options);
       
    72     painter.end();
       
    73 
       
    74     // Get a snapshot of the target widget and flip it left to right.
       
    75     QPixmap tmpPixmap(m_targetRect.size().toSize());
       
    76     QPainter targetPainter(&tmpPixmap);
       
    77     options.exposedRect = m_targetRect;
       
    78     targetWidget->paint(&targetPainter, &options);
       
    79     targetPainter.end();
       
    80     m_targetPixmap = new QPixmap(tmpPixmap.transformed(QTransform().rotate(180, Qt::YAxis)));
       
    81 }
       
    82 
       
    83 void GraphicsItemFlipper::updateFlipStep(qreal value)  // slot
       
    84 {
       
    85     //qreal scale = 1 - sin(3.14 * value) * 0.5;
       
    86     qreal sourceW = m_sourceRect.width();
       
    87     qreal sourceH = m_sourceRect.height();
       
    88     qreal deltaW = m_targetRect.width() - sourceW;
       
    89     qreal deltaH = m_targetRect.height() - sourceH;
       
    90     qreal deltaX = m_targetRect.left() - m_sourceRect.left();
       
    91     qreal deltaY = m_targetRect.top() - m_sourceRect.top();
       
    92     qreal xScale = (sourceW + (deltaW * value))/sourceW;
       
    93     qreal yScale = (sourceH + (deltaH * value))/sourceH;
       
    94     qreal newW = sourceW * xScale;
       
    95     qreal newH = sourceH * yScale;
       
    96     qreal dx = m_pixmapItem->pos().x() + (deltaX * value) + (newW/2);
       
    97 
       
    98 //    qDebug() << "GraphicsItemFlipper::updateFlipStep: value=" << value;
       
    99 //            << " xScale=" << xScale
       
   100 //            << " newW=" << newW
       
   101 //            << " deltaX=" << deltaX;
       
   102 
       
   103     // Set the appropriate pixmap.  If we've gone past the halfway point, ie. the item is edge-on,
       
   104     // switch pixmaps.
       
   105     // To do: Only set the pixmap twice, once when the animation starts and once at the halfway
       
   106     //        point.  Need to be sure resolution is correct at the end of the animation.
       
   107     if(value > 0.5) {
       
   108         m_pixmapItem->setPixmap(m_targetPixmap->scaled(int(newW), int(newH)));
       
   109     }
       
   110     else {
       
   111         m_pixmapItem->setPixmap(m_sourcePixmap->scaled(int(newW), int(newH)));
       
   112     }
       
   113 
       
   114     QTransform transform;
       
   115     transform.translate(dx, 0);
       
   116     transform.rotate(180 * value, Qt::YAxis);
       
   117     transform.translate(-dx, 0);
       
   118     transform.translate(deltaX * value, deltaY * value);
       
   119     m_pixmapItem->setTransform(transform);
       
   120 }
       
   121 
       
   122 void GraphicsItemFlipper::onFinished()
       
   123 {
       
   124     m_pixmapItem->hide();
       
   125     emit finished();
       
   126 }