ganeswidgets/tsrc/fute/HgWidgetTest/src/hgflipwidget.cpp
changeset 0 89c329efa980
child 6 1cdcc61142d2
equal deleted inserted replaced
-1:000000000000 0:89c329efa980
       
     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:  Another view for test application.
       
    15 *
       
    16 */
       
    17 #include "hgflipwidget.h"
       
    18 
       
    19 #include <qgraphicslinearlayout.h>
       
    20 #include <qgraphicssceneresizeevent>
       
    21 #include <hbaction.h>
       
    22 #include <hbinstance.h>
       
    23 #include <hblabel.h>
       
    24 #include <hbmainwindow.h>
       
    25 #include <hblistwidget.h>
       
    26 #include <hbpushbutton.h>
       
    27 #include <qpropertyanimation>
       
    28 #include <qstate.h>
       
    29 #include <qabstracttransition>
       
    30 #include <qstatemachine>
       
    31 #include <qsignaltransition>
       
    32 #include "trace.h"
       
    33 
       
    34 HgFlipWidget::HgFlipWidget(const QString &title1, const QString &title2, const QPixmap &pixmap, QGraphicsItem *parent) :
       
    35     HbWidget(parent), mPixmap(pixmap), mYRotation(0)
       
    36 {
       
    37     FUNC_LOG;
       
    38     
       
    39     Q_UNUSED(title1)
       
    40     Q_UNUSED(title2)
       
    41     
       
    42     mStack = new HbStackedWidget();
       
    43     
       
    44     HbWidget* front = new HbWidget();       
       
    45     {
       
    46         QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
       
    47         mIconLabel = new HbLabel;
       
    48         HANDLE_ERROR_NULL(mIconLabel);
       
    49         if (mIconLabel)
       
    50         {
       
    51             mIconLabel->setIcon(HbIcon(mPixmap));
       
    52             mIconLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
       
    53             layout->setContentsMargins(0,0,0,0);
       
    54             layout->addItem(mIconLabel);
       
    55         }    
       
    56         front->setLayout(layout);
       
    57     }
       
    58         
       
    59     HbWidget* back = new HbWidget();
       
    60     {
       
    61         QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
       
    62         HbListWidget* widget = new HbListWidget;
       
    63         widget->addItem(QString("1. First item"));
       
    64         widget->addItem(QString("2. Second item"));
       
    65         widget->addItem(QString("3. Third item"));
       
    66         widget->addItem(QString("4. Fourth item"));
       
    67         HbPushButton* button = new HbPushButton("close");        
       
    68         layout->addItem(widget);
       
    69         layout->addItem(button);
       
    70         QObject::connect(button, SIGNAL(clicked(bool)), this, SLOT(buttonClicked(bool)));
       
    71         layout->setContentsMargins(0,0,0,0);
       
    72         back->setLayout(layout);
       
    73     }
       
    74     
       
    75     mStack->addWidget(front);
       
    76     mStack->addWidget(back);
       
    77     
       
    78     QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(this);
       
    79     layout->addItem(mStack);
       
    80     layout->setAlignment(mStack, Qt::AlignHCenter|Qt::AlignVCenter);
       
    81     layout->setContentsMargins(0,0,0,0);
       
    82     setLayout(layout);
       
    83 
       
    84 
       
    85     mState = StateOpening1;
       
    86     mAnimation = new QPropertyAnimation(this, "yRotation");
       
    87     mAnimation->setDuration(500);
       
    88     mAnimation->setStartValue(qreal(0));
       
    89     mAnimation->setEndValue(qreal(90));
       
    90     QObject::connect(mAnimation, SIGNAL(finished()), this, SLOT(animationFinished()));
       
    91     
       
    92     mAnimation->start();
       
    93         
       
    94 }
       
    95 
       
    96 void HgFlipWidget::togglePage()
       
    97 {
       
    98     FUNC_LOG;
       
    99     
       
   100     mStack->setCurrentIndex(mStack->currentIndex() ^ 1);
       
   101 }
       
   102 
       
   103 void HgFlipWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   104 {
       
   105     FUNC_LOG;
       
   106 
       
   107     if (mIconLabel && event)
       
   108     {
       
   109         QSize iconSize(event->newSize().width(), event->newSize().height());
       
   110         mIconLabel->setIcon(HbIcon(mPixmap.scaled(iconSize, Qt::IgnoreAspectRatio)));
       
   111     }
       
   112     
       
   113 }
       
   114 
       
   115 void HgFlipWidget::setYRotation(qreal a)
       
   116 {
       
   117     mYRotation = a;
       
   118     qreal s = 1.0f + (qAbs(a) / 90.0f * 0.5f);
       
   119     QTransform t;
       
   120     t.translate(rect().width()/2, rect().height()/2);
       
   121     t.scale(s, s);
       
   122     t.rotate(a, Qt::YAxis);
       
   123     t.translate(-rect().width()/2, -rect().height()/2);
       
   124     setTransform(t);
       
   125     mStack->setTransform(t);
       
   126 }
       
   127 
       
   128 qreal HgFlipWidget::yRotation()
       
   129 {
       
   130     return mYRotation;
       
   131 }
       
   132 
       
   133 void HgFlipWidget::animationFinished()
       
   134 {
       
   135     if (mState == StateOpening1)
       
   136     {
       
   137         mAnimation->setStartValue(qreal(-90));
       
   138         mAnimation->setEndValue(qreal(0));
       
   139         mAnimation->start();
       
   140         mState = StateOpening2;
       
   141         togglePage();
       
   142     }
       
   143     else if (mState == StateOpening2)
       
   144     {
       
   145         mState = StateOpened;
       
   146     }
       
   147     else if (mState == StateClosing1)
       
   148     {
       
   149         mAnimation->setStartValue(-90);
       
   150         mAnimation->setEndValue(0);
       
   151         mAnimation->start();
       
   152         mState = StateClosing2;
       
   153         togglePage();
       
   154     }
       
   155     else if (mState == StateClosing2)
       
   156     {
       
   157         mState = StateClosed;
       
   158         emit closed();
       
   159     }
       
   160 }
       
   161 
       
   162 void HgFlipWidget::close()
       
   163 {
       
   164     if (mState == StateOpened)
       
   165     {
       
   166         mState = StateClosing1;
       
   167         mAnimation->setStartValue(0);
       
   168         mAnimation->setEndValue(90);
       
   169         mAnimation->start();
       
   170     }
       
   171 }
       
   172 
       
   173 void HgFlipWidget::buttonClicked(bool checked)
       
   174 {
       
   175     Q_UNUSED(checked)
       
   176     
       
   177     close();
       
   178 }