examples/animation/easing/window.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "window.h"
       
    43 
       
    44 Window::Window(QWidget *parent)
       
    45     : QWidget(parent), m_iconSize(64, 64)
       
    46 {
       
    47     m_ui.setupUi(this);
       
    48     QButtonGroup *buttonGroup = qFindChild<QButtonGroup *>(this);     // ### workaround for uic in 4.4
       
    49     m_ui.easingCurvePicker->setIconSize(m_iconSize);
       
    50     m_ui.easingCurvePicker->setMinimumHeight(m_iconSize.height() + 50);
       
    51     buttonGroup->setId(m_ui.lineRadio, 0);
       
    52     buttonGroup->setId(m_ui.circleRadio, 1);
       
    53 
       
    54     QEasingCurve dummy;
       
    55     m_ui.periodSpinBox->setValue(dummy.period());
       
    56     m_ui.amplitudeSpinBox->setValue(dummy.amplitude());
       
    57     m_ui.overshootSpinBox->setValue(dummy.overshoot());
       
    58 
       
    59     connect(m_ui.easingCurvePicker, SIGNAL(currentRowChanged(int)), this, SLOT(curveChanged(int)));
       
    60     connect(buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(pathChanged(int)));
       
    61     connect(m_ui.periodSpinBox, SIGNAL(valueChanged(double)), this, SLOT(periodChanged(double)));
       
    62     connect(m_ui.amplitudeSpinBox, SIGNAL(valueChanged(double)), this, SLOT(amplitudeChanged(double)));
       
    63     connect(m_ui.overshootSpinBox, SIGNAL(valueChanged(double)), this, SLOT(overshootChanged(double)));
       
    64     createCurveIcons();
       
    65 
       
    66     QPixmap pix(QLatin1String(":/images/qt-logo.png"));
       
    67     m_item = new PixmapItem(pix);
       
    68     m_scene.addItem(m_item);
       
    69     m_ui.graphicsView->setScene(&m_scene);
       
    70 
       
    71     m_anim = new Animation(m_item, "pos");
       
    72     m_anim->setEasingCurve(QEasingCurve::OutBounce);
       
    73     m_ui.easingCurvePicker->setCurrentRow(int(QEasingCurve::OutBounce));
       
    74 
       
    75     startAnimation();
       
    76 }
       
    77 
       
    78 void Window::createCurveIcons()
       
    79 {
       
    80     QPixmap pix(m_iconSize);
       
    81     QPainter painter(&pix);
       
    82     QLinearGradient gradient(0,0, 0, m_iconSize.height());
       
    83     gradient.setColorAt(0.0, QColor(240, 240, 240));
       
    84     gradient.setColorAt(1.0, QColor(224, 224, 224));
       
    85     QBrush brush(gradient);
       
    86     const QMetaObject &mo = QEasingCurve::staticMetaObject;
       
    87     QMetaEnum metaEnum = mo.enumerator(mo.indexOfEnumerator("Type"));
       
    88     // Skip QEasingCurve::Custom
       
    89     for (int i = 0; i < QEasingCurve::NCurveTypes - 1; ++i) {
       
    90         painter.fillRect(QRect(QPoint(0, 0), m_iconSize), brush);
       
    91         QEasingCurve curve((QEasingCurve::Type)i);
       
    92         painter.setPen(QColor(0, 0, 255, 64));
       
    93         qreal xAxis = m_iconSize.height()/1.5;
       
    94         qreal yAxis = m_iconSize.width()/3;
       
    95         painter.drawLine(0, xAxis, m_iconSize.width(),  xAxis);
       
    96         painter.drawLine(yAxis, 0, yAxis, m_iconSize.height());
       
    97 
       
    98         qreal curveScale = m_iconSize.height()/2;
       
    99 
       
   100         painter.setPen(Qt::NoPen);
       
   101 
       
   102         // start point
       
   103         painter.setBrush(Qt::red);
       
   104         QPoint start(yAxis, xAxis - curveScale * curve.valueForProgress(0));
       
   105         painter.drawRect(start.x() - 1, start.y() - 1, 3, 3);
       
   106 
       
   107         // end point
       
   108         painter.setBrush(Qt::blue);
       
   109         QPoint end(yAxis + curveScale, xAxis - curveScale * curve.valueForProgress(1));
       
   110         painter.drawRect(end.x() - 1, end.y() - 1, 3, 3);
       
   111 
       
   112         QPainterPath curvePath;
       
   113         curvePath.moveTo(start);
       
   114         for (qreal t = 0; t <= 1.0; t+=1.0/curveScale) {
       
   115             QPoint to;
       
   116             to.setX(yAxis + curveScale * t);
       
   117             to.setY(xAxis - curveScale * curve.valueForProgress(t));
       
   118             curvePath.lineTo(to);
       
   119         }
       
   120         painter.setRenderHint(QPainter::Antialiasing, true);
       
   121         painter.strokePath(curvePath, QColor(32, 32, 32));
       
   122         painter.setRenderHint(QPainter::Antialiasing, false);
       
   123         QListWidgetItem *item = new QListWidgetItem;
       
   124         item->setIcon(QIcon(pix));
       
   125         item->setText(metaEnum.key(i));
       
   126         m_ui.easingCurvePicker->addItem(item);
       
   127     }
       
   128 }
       
   129 
       
   130 void Window::startAnimation()
       
   131 {
       
   132     m_anim->setStartValue(QPointF(0, 0));
       
   133     m_anim->setEndValue(QPointF(100, 100));
       
   134     m_anim->setDuration(2000);
       
   135     m_anim->setLoopCount(-1); // forever
       
   136     m_anim->start();
       
   137 }
       
   138 
       
   139 void Window::curveChanged(int row)
       
   140 {
       
   141     QEasingCurve::Type curveType = (QEasingCurve::Type)row;
       
   142     m_anim->setEasingCurve(curveType);
       
   143     m_anim->setCurrentTime(0);
       
   144 
       
   145     bool isElastic = curveType >= QEasingCurve::InElastic && curveType <= QEasingCurve::OutInElastic;
       
   146     bool isBounce = curveType >= QEasingCurve::InBounce && curveType <= QEasingCurve::OutInBounce;
       
   147     m_ui.periodSpinBox->setEnabled(isElastic);
       
   148     m_ui.amplitudeSpinBox->setEnabled(isElastic || isBounce);
       
   149     m_ui.overshootSpinBox->setEnabled(curveType >= QEasingCurve::InBack && curveType <= QEasingCurve::OutInBack);
       
   150 }
       
   151 
       
   152 void Window::pathChanged(int index)
       
   153 {
       
   154     m_anim->setPathType((Animation::PathType)index);
       
   155 }
       
   156 
       
   157 void Window::periodChanged(double value)
       
   158 {
       
   159     QEasingCurve curve = m_anim->easingCurve();
       
   160     curve.setPeriod(value);
       
   161     m_anim->setEasingCurve(curve);
       
   162 }
       
   163 
       
   164 void Window::amplitudeChanged(double value)
       
   165 {
       
   166     QEasingCurve curve = m_anim->easingCurve();
       
   167     curve.setAmplitude(value);
       
   168     m_anim->setEasingCurve(curve);
       
   169 }
       
   170 
       
   171 void Window::overshootChanged(double value)
       
   172 {
       
   173     QEasingCurve curve = m_anim->easingCurve();
       
   174     curve.setOvershoot(value);
       
   175     m_anim->setEasingCurve(curve);
       
   176 }
       
   177