OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmleditorwidgets / easingpane / easinggraph.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
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 #include "easinggraph.h"
34
35 #include <QtGui/QPainter>
36 #include <QtGui/QStyleOptionGraphicsItem>
37 #include <math.h>
38
39 QT_BEGIN_NAMESPACE
40
41 EasingGraph::EasingGraph(QWidget *parent):QWidget(parent),
42     m_color(Qt::magenta), m_zeroColor(Qt::gray),m_duration(0),
43     m_easingExtremes(QLatin1String("In"))
44 {
45 //    setFlag(QGraphicsItem::ItemHasNoContents, false);
46
47     // populate the hash
48     m_availableNames.insert(QLatin1String("Linear"), QEasingCurve::Linear);
49     m_availableNames.insert(QLatin1String("InQuad"), QEasingCurve::InQuad);
50     m_availableNames.insert(QLatin1String("OutQuad"), QEasingCurve::OutQuad);
51     m_availableNames.insert(QLatin1String("InOutQuad"), QEasingCurve::InOutQuad);
52     m_availableNames.insert(QLatin1String("OutInQuad"), QEasingCurve::OutInQuad);
53     m_availableNames.insert(QLatin1String("InCubic"), QEasingCurve::InCubic);
54     m_availableNames.insert(QLatin1String("OutCubic"), QEasingCurve::OutCubic);
55     m_availableNames.insert(QLatin1String("InOutCubic"), QEasingCurve::InOutCubic);
56     m_availableNames.insert(QLatin1String("OutInCubic"), QEasingCurve::OutInCubic);
57     m_availableNames.insert(QLatin1String("InQuart"), QEasingCurve::InQuart);
58     m_availableNames.insert(QLatin1String("OutQuart"), QEasingCurve::OutQuart);
59     m_availableNames.insert(QLatin1String("InOutQuart"), QEasingCurve::InOutQuart);
60     m_availableNames.insert(QLatin1String("OutInQuart"), QEasingCurve::OutInQuart);
61     m_availableNames.insert(QLatin1String("InQuint"), QEasingCurve::InQuint);
62     m_availableNames.insert(QLatin1String("OutQuint"), QEasingCurve::OutQuint);
63     m_availableNames.insert(QLatin1String("InOutQuint"), QEasingCurve::InOutQuint);
64     m_availableNames.insert(QLatin1String("OutInQuint"), QEasingCurve::OutInQuint);
65     m_availableNames.insert(QLatin1String("InSine"), QEasingCurve::InSine);
66     m_availableNames.insert(QLatin1String("OutSine"), QEasingCurve::OutSine);
67     m_availableNames.insert(QLatin1String("InOutSine"), QEasingCurve::InOutSine);
68     m_availableNames.insert(QLatin1String("OutInSine"), QEasingCurve::OutInSine);
69     m_availableNames.insert(QLatin1String("InExpo"), QEasingCurve::InExpo);
70     m_availableNames.insert(QLatin1String("OutExpo"), QEasingCurve::OutExpo);
71     m_availableNames.insert(QLatin1String("InOutExpo"), QEasingCurve::InOutExpo);
72     m_availableNames.insert(QLatin1String("OutInExpo"), QEasingCurve::OutInExpo);
73     m_availableNames.insert(QLatin1String("InCirc"), QEasingCurve::InCirc);
74     m_availableNames.insert(QLatin1String("OutCirc"), QEasingCurve::OutCirc);
75     m_availableNames.insert(QLatin1String("InOutCirc"), QEasingCurve::InOutCirc);
76     m_availableNames.insert(QLatin1String("OutInCirc"), QEasingCurve::OutInCirc);
77     m_availableNames.insert(QLatin1String("InElastic"), QEasingCurve::InElastic);
78     m_availableNames.insert(QLatin1String("OutElastic"), QEasingCurve::OutElastic);
79     m_availableNames.insert(QLatin1String("InOutElastic"), QEasingCurve::InOutElastic);
80     m_availableNames.insert(QLatin1String("OutInElastic"), QEasingCurve::OutInElastic);
81     m_availableNames.insert(QLatin1String("InBack"), QEasingCurve::InBack);
82     m_availableNames.insert(QLatin1String("OutBack"), QEasingCurve::OutBack);
83     m_availableNames.insert(QLatin1String("InOutBack"), QEasingCurve::InOutBack);
84     m_availableNames.insert(QLatin1String("OutInBack"), QEasingCurve::OutInBack);
85     m_availableNames.insert(QLatin1String("InBounce"), QEasingCurve::InBounce);
86     m_availableNames.insert(QLatin1String("OutBounce"), QEasingCurve::OutBounce);
87     m_availableNames.insert(QLatin1String("InOutBounce"), QEasingCurve::InOutBounce);
88     m_availableNames.insert(QLatin1String("OutInBounce"), QEasingCurve::OutInBounce);
89     m_availableNames.insert(QLatin1String("InCurve"), QEasingCurve::InCurve);
90     m_availableNames.insert(QLatin1String("OutCurve"), QEasingCurve::OutCurve);
91     m_availableNames.insert(QLatin1String("SineCurve"), QEasingCurve::SineCurve);
92     m_availableNames.insert(QLatin1String("CosineCurve"), QEasingCurve::CosineCurve);
93 }
94
95 EasingGraph::~EasingGraph()
96 {
97 }
98
99 QEasingCurve::Type EasingGraph::easingType() const
100 {
101     return m_curveFunction.type();
102 }
103
104 QEasingCurve EasingGraph::easingCurve() const
105 {
106     return m_curveFunction;
107 }
108
109 QString EasingGraph::easingShape() const
110 {
111     QString name = easingName();
112     if (name.left(5)=="InOut") return name.right(name.length()-5);
113     if (name.left(5)=="OutIn") return name.right(name.length()-5);
114     if (name.left(3)=="Out") return name.right(name.length()-3);
115     if (name.left(2)=="In") return name.right(name.length()-2);
116     return name;
117 }
118
119 void EasingGraph::setEasingShape(const QString &newShape)
120 {
121     if (easingShape() != newShape) {
122         if (newShape=="Linear")
123             setEasingName(newShape);
124         else
125             setEasingName(m_easingExtremes+newShape);
126     }
127 }
128
129 QString EasingGraph::easingExtremes() const
130 {
131     QString name = easingName();
132     if (name.left(5)=="InOut") return "InOut";
133     if (name.left(5)=="OutIn") return "OutIn";
134     if (name.left(3)=="Out") return "Out";
135     if (name.left(2)=="In") return "In";
136     return QString();
137 }
138
139 void EasingGraph::setEasingExtremes(const QString &newExtremes)
140 {
141     if (m_easingExtremes != newExtremes) {
142         m_easingExtremes = newExtremes;
143         if (easingShape()!="Linear")
144             setEasingName(newExtremes+easingShape());
145     }
146 }
147
148 QString EasingGraph::easingName() const
149 {
150     return m_availableNames.key(m_curveFunction.type());
151 }
152
153 void EasingGraph::setEasingName(const QString &newName)
154 {
155     if (easingName() != newName) {
156
157         if (!m_availableNames.contains(newName)) return;
158         m_curveFunction = QEasingCurve(m_availableNames.value(newName));
159         emit easingNameChanged();
160         emit easingExtremesChanged();
161         emit easingShapeChanged();
162         update();
163     }
164 }
165
166 qreal EasingGraph::overshoot() const
167 {
168     return m_curveFunction.overshoot();
169 }
170
171 void EasingGraph::setOvershoot(qreal newOvershoot)
172 {
173     if ((overshoot() != newOvershoot) && (easingShape()=="Back")) {
174         m_curveFunction.setOvershoot(newOvershoot);
175         emit overshootChanged();
176         update();
177     }
178 }
179 qreal EasingGraph::amplitude() const
180 {
181     return m_curveFunction.amplitude();
182 }
183
184 void EasingGraph::setAmplitude(qreal newAmplitude)
185 {
186     if ((amplitude() != newAmplitude) && ((easingShape()=="Bounce") ||(easingShape()=="Elastic"))) {
187         m_curveFunction.setAmplitude(newAmplitude);
188         emit amplitudeChanged();
189         update();
190     }
191 }
192
193 qreal EasingGraph::period() const
194 {
195     return m_curveFunction.period();
196 }
197
198 void EasingGraph::setPeriod(qreal newPeriod)
199 {
200     if ((period() != newPeriod) && (easingShape()=="Elastic")) {
201         m_curveFunction.setPeriod(newPeriod);
202         emit periodChanged();
203         update();
204     }
205 }
206
207 qreal EasingGraph::duration() const
208 {
209     return m_duration;
210 }
211
212 void EasingGraph::setDuration(qreal newDuration)
213 {
214     if (m_duration != newDuration) {
215         m_duration = newDuration;
216         emit durationChanged();
217     }
218 }
219
220 QColor EasingGraph::color() const
221 {
222     return m_color;
223 }
224
225 void EasingGraph::setColor(const QColor &newColor)
226 {
227     if (m_color != newColor) {
228         m_color = newColor;
229         emit colorChanged();
230         update();
231     }
232 }
233
234 QColor EasingGraph::zeroColor() const{
235     return m_zeroColor;
236 }
237
238 void EasingGraph::setZeroColor(const QColor &newColor)
239 {
240     if (m_zeroColor != newColor) {
241         m_zeroColor = newColor;
242         emit zeroColorChanged();
243         update();
244     }
245 }
246
247 QRectF EasingGraph::boundingRect() const
248 {
249     return QRectF(0, 0, width(), height());
250 }
251
252 void EasingGraph::paintEvent(QPaintEvent *event)
253 //void EasingGraph::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
254 {
255     QWidget::paintEvent(event);
256
257     QPainter *painter = new QPainter(this);
258     painter->save();
259     bool drawZero = false;
260
261     // no background
262
263     int length = width();
264     int breadth = height()-2;
265     QPainterPath path;
266     path.moveTo(0,int((1-m_curveFunction.valueForProgress(0))*breadth));
267     for (int i=0;i<length;i++) {
268         qreal progress = i/qreal(length);
269         qreal value = m_curveFunction.valueForProgress(progress);
270         int x = int(length*progress);
271         int y = int(breadth*(1-value));
272         path.lineTo(x,y);
273     }
274
275     QRectF pathRect = path.controlPointRect();
276     if ( (pathRect.height()>breadth)) {
277         // scale vertically
278         qreal scale = breadth/pathRect.height();
279         qreal displacement = -pathRect.top();
280
281         // reset path and recompute scaled version
282         path = QPainterPath();
283         path.moveTo(0,int(scale*((1-m_curveFunction.valueForProgress(0))*breadth+displacement)));
284         for (int i=0;i<length;i++) {
285             qreal progress = i/qreal(length);
286             qreal value = m_curveFunction.valueForProgress(progress);
287             int x = int(length*progress);
288             int y = int(scale*(breadth*(1-value)+displacement));
289             path.lineTo(x,y);
290         }
291
292         drawZero = true;
293     }
294
295
296     painter->setBrush(Qt::transparent);
297
298     if (drawZero) {
299         // "zero" and "one" lines
300         QPen zeroPen = QPen(m_zeroColor);
301         zeroPen.setStyle(Qt::DashLine);
302         painter->setPen(zeroPen);
303         int y = int(-pathRect.top()*breadth/pathRect.height());
304         if (y>0)
305             painter->drawLine(0,y,length,y);
306         y = int(breadth/pathRect.height()*(breadth-pathRect.top()));
307         if (y<breadth)
308             painter->drawLine(0,y,length,y);
309     }
310
311     painter->setPen(m_color);
312     painter->drawPath(path);
313     painter->restore();
314     delete painter;
315 }
316
317 QT_END_NAMESPACE