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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "window.h"
43
44Window::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
78void 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
130void 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
139void 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
152void Window::pathChanged(int index)
153{
154 m_anim->setPathType((Animation::PathType)index);
155}
156
157void Window::periodChanged(double value)
158{
159 QEasingCurve curve = m_anim->easingCurve();
160 curve.setPeriod(value);
161 m_anim->setEasingCurve(curve);
162}
163
164void Window::amplitudeChanged(double value)
165{
166 QEasingCurve curve = m_anim->easingCurve();
167 curve.setAmplitude(value);
168 m_anim->setEasingCurve(curve);
169}
170
171void Window::overshootChanged(double value)
172{
173 QEasingCurve curve = m_anim->easingCurve();
174 curve.setOvershoot(value);
175 m_anim->setEasingCurve(curve);
176}
177
Note: See TracBrowser for help on using the repository browser.