source: trunk/examples/animation/easing/window.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "window.h"
42
43Window::Window(QWidget *parent)
44 : QWidget(parent), m_iconSize(64, 64)
45{
46 m_ui.setupUi(this);
47 QButtonGroup *buttonGroup = qFindChild<QButtonGroup *>(this); // ### workaround for uic in 4.4
48 m_ui.easingCurvePicker->setIconSize(m_iconSize);
49 m_ui.easingCurvePicker->setMinimumHeight(m_iconSize.height() + 50);
50 buttonGroup->setId(m_ui.lineRadio, 0);
51 buttonGroup->setId(m_ui.circleRadio, 1);
52
53 QEasingCurve dummy;
54 m_ui.periodSpinBox->setValue(dummy.period());
55 m_ui.amplitudeSpinBox->setValue(dummy.amplitude());
56 m_ui.overshootSpinBox->setValue(dummy.overshoot());
57
58 connect(m_ui.easingCurvePicker, SIGNAL(currentRowChanged(int)), this, SLOT(curveChanged(int)));
59 connect(buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(pathChanged(int)));
60 connect(m_ui.periodSpinBox, SIGNAL(valueChanged(double)), this, SLOT(periodChanged(double)));
61 connect(m_ui.amplitudeSpinBox, SIGNAL(valueChanged(double)), this, SLOT(amplitudeChanged(double)));
62 connect(m_ui.overshootSpinBox, SIGNAL(valueChanged(double)), this, SLOT(overshootChanged(double)));
63 createCurveIcons();
64
65 QPixmap pix(QLatin1String(":/images/qt-logo.png"));
66 m_item = new PixmapItem(pix);
67 m_scene.addItem(m_item);
68 m_ui.graphicsView->setScene(&m_scene);
69
70 m_anim = new Animation(m_item, "pos");
71 m_anim->setEasingCurve(QEasingCurve::OutBounce);
72 m_ui.easingCurvePicker->setCurrentRow(int(QEasingCurve::OutBounce));
73
74 startAnimation();
75}
76
77void Window::createCurveIcons()
78{
79 QPixmap pix(m_iconSize);
80 QPainter painter(&pix);
81 QLinearGradient gradient(0,0, 0, m_iconSize.height());
82 gradient.setColorAt(0.0, QColor(240, 240, 240));
83 gradient.setColorAt(1.0, QColor(224, 224, 224));
84 QBrush brush(gradient);
85 const QMetaObject &mo = QEasingCurve::staticMetaObject;
86 QMetaEnum metaEnum = mo.enumerator(mo.indexOfEnumerator("Type"));
87 // Skip QEasingCurve::Custom
88 for (int i = 0; i < QEasingCurve::NCurveTypes - 1; ++i) {
89 painter.fillRect(QRect(QPoint(0, 0), m_iconSize), brush);
90 QEasingCurve curve((QEasingCurve::Type)i);
91 painter.setPen(QColor(0, 0, 255, 64));
92 qreal xAxis = m_iconSize.height()/1.5;
93 qreal yAxis = m_iconSize.width()/3;
94 painter.drawLine(0, xAxis, m_iconSize.width(), xAxis);
95 painter.drawLine(yAxis, 0, yAxis, m_iconSize.height());
96
97 qreal curveScale = m_iconSize.height()/2;
98
99 painter.setPen(Qt::NoPen);
100
101 // start point
102 painter.setBrush(Qt::red);
103 QPoint start(yAxis, xAxis - curveScale * curve.valueForProgress(0));
104 painter.drawRect(start.x() - 1, start.y() - 1, 3, 3);
105
106 // end point
107 painter.setBrush(Qt::blue);
108 QPoint end(yAxis + curveScale, xAxis - curveScale * curve.valueForProgress(1));
109 painter.drawRect(end.x() - 1, end.y() - 1, 3, 3);
110
111 QPainterPath curvePath;
112 curvePath.moveTo(start);
113 for (qreal t = 0; t <= 1.0; t+=1.0/curveScale) {
114 QPoint to;
115 to.setX(yAxis + curveScale * t);
116 to.setY(xAxis - curveScale * curve.valueForProgress(t));
117 curvePath.lineTo(to);
118 }
119 painter.setRenderHint(QPainter::Antialiasing, true);
120 painter.strokePath(curvePath, QColor(32, 32, 32));
121 painter.setRenderHint(QPainter::Antialiasing, false);
122 QListWidgetItem *item = new QListWidgetItem;
123 item->setIcon(QIcon(pix));
124 item->setText(metaEnum.key(i));
125 m_ui.easingCurvePicker->addItem(item);
126 }
127}
128
129void Window::startAnimation()
130{
131 m_anim->setStartValue(QPointF(0, 0));
132 m_anim->setEndValue(QPointF(100, 100));
133 m_anim->setDuration(2000);
134 m_anim->setLoopCount(-1); // forever
135 m_anim->start();
136}
137
138void Window::curveChanged(int row)
139{
140 QEasingCurve::Type curveType = (QEasingCurve::Type)row;
141 m_anim->setEasingCurve(curveType);
142 m_anim->setCurrentTime(0);
143
144 bool isElastic = curveType >= QEasingCurve::InElastic && curveType <= QEasingCurve::OutInElastic;
145 bool isBounce = curveType >= QEasingCurve::InBounce && curveType <= QEasingCurve::OutInBounce;
146 m_ui.periodSpinBox->setEnabled(isElastic);
147 m_ui.amplitudeSpinBox->setEnabled(isElastic || isBounce);
148 m_ui.overshootSpinBox->setEnabled(curveType >= QEasingCurve::InBack && curveType <= QEasingCurve::OutInBack);
149}
150
151void Window::pathChanged(int index)
152{
153 m_anim->setPathType((Animation::PathType)index);
154}
155
156void Window::periodChanged(double value)
157{
158 QEasingCurve curve = m_anim->easingCurve();
159 curve.setPeriod(value);
160 m_anim->setEasingCurve(curve);
161}
162
163void Window::amplitudeChanged(double value)
164{
165 QEasingCurve curve = m_anim->easingCurve();
166 curve.setAmplitude(value);
167 m_anim->setEasingCurve(curve);
168}
169
170void Window::overshootChanged(double value)
171{
172 QEasingCurve curve = m_anim->easingCurve();
173 curve.setOvershoot(value);
174 m_anim->setEasingCurve(curve);
175}
176
Note: See TracBrowser for help on using the repository browser.