/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/#include "flippablepad.h"#include "padnavigator.h"#include "splashitem.h"#include <QtGui/QtGui>#ifndef QT_NO_OPENGL#include <QtOpenGL/QtOpenGL>#endif
PadNavigator::PadNavigator(constQSize&size,QWidget*parent)
: QGraphicsView(parent)
{
// Splash item
SplashItem *splash =new SplashItem;
splash->setZValue(1);
// Pad item
FlippablePad *pad =new FlippablePad(size);
QGraphicsRotation*flipRotation =newQGraphicsRotation(pad);
QGraphicsRotation*xRotation =newQGraphicsRotation(pad);
QGraphicsRotation*yRotation =newQGraphicsRotation(pad);
flipRotation->setAxis(Qt::YAxis);
xRotation->setAxis(Qt::YAxis);
yRotation->setAxis(Qt::XAxis);
pad->setTransformations(QList<QGraphicsTransform*>()
<< flipRotation
<< xRotation << yRotation);
// Back (proxy widget) itemQGraphicsProxyWidget*backItem =newQGraphicsProxyWidget(pad);
QWidget*widget =newQWidget;
form.setupUi(widget);
form.hostName->setFocus();
backItem->setWidget(widget);
backItem->setVisible(false);
backItem->setFocus();
backItem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
constQRectF r = backItem->rect();
backItem->setTransform(QTransform()
.rotate(180,Qt::YAxis)
.translate(-r.width()/2,-r.height()/2));
// Selection item
RoundRectItem *selectionItem =new RoundRectItem(QRectF(-60,-60,120,120),Qt::gray, pad);
selectionItem->setZValue(0.5);
// Splash animationsQPropertyAnimation*smoothSplashMove =newQPropertyAnimation(splash,"y");
QPropertyAnimation*smoothSplashOpacity =newQPropertyAnimation(splash,"opacity");
smoothSplashMove->setEasingCurve(QEasingCurve::InQuad);
smoothSplashMove->setDuration(250);
smoothSplashOpacity->setDuration(250);
// Selection animationQPropertyAnimation*smoothXSelection =newQPropertyAnimation(selectionItem,"x");
QPropertyAnimation*smoothYSelection =newQPropertyAnimation(selectionItem,"y");
QPropertyAnimation*smoothXRotation =newQPropertyAnimation(xRotation,"angle");
QPropertyAnimation*smoothYRotation =newQPropertyAnimation(yRotation,"angle");
smoothXSelection->setDuration(125);
smoothYSelection->setDuration(125);
smoothXRotation->setDuration(125);
smoothYRotation->setDuration(125);
smoothXSelection->setEasingCurve(QEasingCurve::InOutQuad);
smoothYSelection->setEasingCurve(QEasingCurve::InOutQuad);
smoothXRotation->setEasingCurve(QEasingCurve::InOutQuad);
smoothYRotation->setEasingCurve(QEasingCurve::InOutQuad);
// Flip animation setupQPropertyAnimation*smoothFlipRotation =newQPropertyAnimation(flipRotation,"angle");
QPropertyAnimation*smoothFlipScale =newQPropertyAnimation(pad,"scale");
QPropertyAnimation*smoothFlipXRotation =newQPropertyAnimation(xRotation,"angle");
QPropertyAnimation*smoothFlipYRotation =newQPropertyAnimation(yRotation,"angle");
QParallelAnimationGroup*flipAnimation =newQParallelAnimationGroup(this);
smoothFlipScale->setDuration(500);
smoothFlipRotation->setDuration(500);
smoothFlipXRotation->setDuration(500);
smoothFlipYRotation->setDuration(500);
smoothFlipScale->setEasingCurve(QEasingCurve::InOutQuad);
smoothFlipRotation->setEasingCurve(QEasingCurve::InOutQuad);
smoothFlipXRotation->setEasingCurve(QEasingCurve::InOutQuad);
smoothFlipYRotation->setEasingCurve(QEasingCurve::InOutQuad);
smoothFlipScale->setKeyValueAt(0, qvariant_cast<qreal>(1.0));
smoothFlipScale->setKeyValueAt(0.5, qvariant_cast<qreal>(0.7));
smoothFlipScale->setKeyValueAt(1, qvariant_cast<qreal>(1.0));
flipAnimation->addAnimation(smoothFlipRotation);
flipAnimation->addAnimation(smoothFlipScale);
flipAnimation->addAnimation(smoothFlipXRotation);
flipAnimation->addAnimation(smoothFlipYRotation);
// Flip animation delayed property assignmentQSequentialAnimationGroup*setVariablesSequence =newQSequentialAnimationGroup;
QPropertyAnimation*setFillAnimation =newQPropertyAnimation(pad,"fill");
QPropertyAnimation*setBackItemVisibleAnimation =newQPropertyAnimation(backItem,"visible");
QPropertyAnimation*setSelectionItemVisibleAnimation =newQPropertyAnimation(selectionItem,"visible");
setFillAnimation->setDuration(0);
setBackItemVisibleAnimation->setDuration(0);
setSelectionItemVisibleAnimation->setDuration(0);
setVariablesSequence->addPause(250);
setVariablesSequence->addAnimation(setBackItemVisibleAnimation);
setVariablesSequence->addAnimation(setSelectionItemVisibleAnimation);
setVariablesSequence->addAnimation(setFillAnimation);
flipAnimation->addAnimation(setVariablesSequence);
// Build the state machineQStateMachine*stateMachine =newQStateMachine(this);
QState*splashState =newQState(stateMachine);
QState*frontState =newQState(stateMachine);
QHistoryState*historyState =newQHistoryState(frontState);
QState*backState =newQState(stateMachine);
frontState->assignProperty(pad,"fill",false);
frontState->assignProperty(splash,"opacity",0.0);
frontState->assignProperty(backItem,"visible",false);
frontState->assignProperty(flipRotation,"angle", qvariant_cast<qreal>(0.0));
frontState->assignProperty(selectionItem,"visible",true);
backState->assignProperty(pad,"fill",true);
backState->assignProperty(backItem,"visible",true);
backState->assignProperty(xRotation,"angle", qvariant_cast<qreal>(0.0));
backState->assignProperty(yRotation,"angle", qvariant_cast<qreal>(0.0));
backState->assignProperty(flipRotation,"angle", qvariant_cast<qreal>(180.0));
backState->assignProperty(selectionItem,"visible",false);
stateMachine->addDefaultAnimation(smoothXRotation);
stateMachine->addDefaultAnimation(smoothYRotation);
stateMachine->addDefaultAnimation(smoothXSelection);
stateMachine->addDefaultAnimation(smoothYSelection);
stateMachine->setInitialState(splashState);
// TransitionsQEventTransition*anyKeyTransition =newQEventTransition(this,QEvent::KeyPress, splashState);
anyKeyTransition->setTargetState(frontState);
anyKeyTransition->addAnimation(smoothSplashMove);
anyKeyTransition->addAnimation(smoothSplashOpacity);
QKeyEventTransition*enterTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Enter, backState);
QKeyEventTransition*returnTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Return, backState);
QKeyEventTransition*backEnterTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Enter, frontState);
QKeyEventTransition*backReturnTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Return, frontState);
enterTransition->setTargetState(historyState);
returnTransition->setTargetState(historyState);
backEnterTransition->setTargetState(backState);
backReturnTransition->setTargetState(backState);
enterTransition->addAnimation(flipAnimation);
returnTransition->addAnimation(flipAnimation);
backEnterTransition->addAnimation(flipAnimation);
backReturnTransition->addAnimation(flipAnimation);
// Create substates for each icon; store in temporary grid.int columns = size.width();
int rows = size.height();
QVector<QVector<QState*>> stateGrid;
stateGrid.resize(rows);
for (int y =0; y < rows; ++y) {
stateGrid[y].resize(columns);
for (int x =0; x < columns; ++x)
stateGrid[y][x]=newQState(frontState);
}
frontState->setInitialState(stateGrid[0][0]);
selectionItem->setPos(pad->iconAt(0,0)->pos());
// Enable key navigation using state transitionsfor (int y =0; y < rows; ++y) {
for (int x =0; x < columns; ++x) {
QState*state = stateGrid[y][x];
QKeyEventTransition*rightTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Right, state);
QKeyEventTransition*leftTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Left, state);
QKeyEventTransition*downTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Down, state);
QKeyEventTransition*upTransition =newQKeyEventTransition(this,QEvent::KeyPress,Qt::Key_Up, state);
rightTransition->setTargetState(stateGrid[y][(x +1) % columns]);
leftTransition->setTargetState(stateGrid[y][((x -1) + columns) % columns]);
downTransition->setTargetState(stateGrid[(y +1) % rows][x]);
upTransition->setTargetState(stateGrid[((y -1) + rows) % rows][x]);
RoundRectItem *icon = pad->iconAt(x, y);
state->assignProperty(xRotation,"angle",-icon->x() /6.0);
state->assignProperty(yRotation,"angle", icon->y() /6.0);
state->assignProperty(selectionItem,"x", icon->x());
state->assignProperty(selectionItem,"y", icon->y());
frontState->assignProperty(icon,"visible",true);
backState->assignProperty(icon,"visible",false);
QPropertyAnimation*setIconVisibleAnimation =newQPropertyAnimation(icon,"visible");
setIconVisibleAnimation->setDuration(0);
setVariablesSequence->addAnimation(setIconVisibleAnimation);
}
}
// SceneQGraphicsScene*scene =newQGraphicsScene(this);
scene->setBackgroundBrush(QPixmap(":/images/blue_angle_swirl.jpg"));
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
scene->addItem(pad);
scene->setSceneRect(scene->itemsBoundingRect());
setScene(scene);
// Adjust splash item to scene contentsconstQRectF sbr = splash->boundingRect();
splash->setPos(-sbr.width() /2, scene->sceneRect().top() -2);
frontState->assignProperty(splash,"y", splash->y() -100.0);
scene->addItem(splash);
// View
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setMinimumSize(50,50);
setViewportUpdateMode(FullViewportUpdate);
setCacheMode(CacheBackground);
setRenderHints(QPainter::Antialiasing |QPainter::SmoothPixmapTransform |QPainter::TextAntialiasing);
#ifndef QT_NO_OPENGL
setViewport(newQGLWidget(QGLFormat(QGL::SampleBuffers)));
#endif
stateMachine->start();
}
void PadNavigator::resizeEvent(QResizeEvent*event)
{
QGraphicsView::resizeEvent(event);
fitInView(scene()->sceneRect(),Qt::KeepAspectRatio);
}
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !