1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "easinggraph.h"
35 #include <QtGui/QPainter>
36 #include <QtGui/QStyleOptionGraphicsItem>
41 EasingGraph::EasingGraph(QWidget *parent):QWidget(parent),
42 m_color(Qt::magenta), m_zeroColor(Qt::gray),m_duration(0),
43 m_easingExtremes(QLatin1String("In"))
45 // setFlag(QGraphicsItem::ItemHasNoContents, false);
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);
95 EasingGraph::~EasingGraph()
99 QEasingCurve::Type EasingGraph::easingType() const
101 return m_curveFunction.type();
104 QEasingCurve EasingGraph::easingCurve() const
106 return m_curveFunction;
109 QString EasingGraph::easingShape() const
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);
119 void EasingGraph::setEasingShape(const QString &newShape)
121 if (easingShape() != newShape) {
122 if (newShape=="Linear")
123 setEasingName(newShape);
125 setEasingName(m_easingExtremes+newShape);
129 QString EasingGraph::easingExtremes() const
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";
139 void EasingGraph::setEasingExtremes(const QString &newExtremes)
141 if (m_easingExtremes != newExtremes) {
142 m_easingExtremes = newExtremes;
143 if (easingShape()!="Linear")
144 setEasingName(newExtremes+easingShape());
148 QString EasingGraph::easingName() const
150 return m_availableNames.key(m_curveFunction.type());
153 void EasingGraph::setEasingName(const QString &newName)
155 if (easingName() != newName) {
157 if (!m_availableNames.contains(newName)) return;
158 m_curveFunction = QEasingCurve(m_availableNames.value(newName));
159 emit easingNameChanged();
160 emit easingExtremesChanged();
161 emit easingShapeChanged();
166 qreal EasingGraph::overshoot() const
168 return m_curveFunction.overshoot();
171 void EasingGraph::setOvershoot(qreal newOvershoot)
173 if ((overshoot() != newOvershoot) && (easingShape()=="Back")) {
174 m_curveFunction.setOvershoot(newOvershoot);
175 emit overshootChanged();
179 qreal EasingGraph::amplitude() const
181 return m_curveFunction.amplitude();
184 void EasingGraph::setAmplitude(qreal newAmplitude)
186 if ((amplitude() != newAmplitude) && ((easingShape()=="Bounce") ||(easingShape()=="Elastic"))) {
187 m_curveFunction.setAmplitude(newAmplitude);
188 emit amplitudeChanged();
193 qreal EasingGraph::period() const
195 return m_curveFunction.period();
198 void EasingGraph::setPeriod(qreal newPeriod)
200 if ((period() != newPeriod) && (easingShape()=="Elastic")) {
201 m_curveFunction.setPeriod(newPeriod);
202 emit periodChanged();
207 qreal EasingGraph::duration() const
212 void EasingGraph::setDuration(qreal newDuration)
214 if (m_duration != newDuration) {
215 m_duration = newDuration;
216 emit durationChanged();
220 QColor EasingGraph::color() const
225 void EasingGraph::setColor(const QColor &newColor)
227 if (m_color != newColor) {
234 QColor EasingGraph::zeroColor() const{
238 void EasingGraph::setZeroColor(const QColor &newColor)
240 if (m_zeroColor != newColor) {
241 m_zeroColor = newColor;
242 emit zeroColorChanged();
247 QRectF EasingGraph::boundingRect() const
249 return QRectF(0, 0, width(), height());
252 void EasingGraph::paintEvent(QPaintEvent *event)
253 //void EasingGraph::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
255 QWidget::paintEvent(event);
257 QPainter *painter = new QPainter(this);
259 bool drawZero = false;
263 int length = width();
264 int breadth = height()-2;
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));
275 QRectF pathRect = path.controlPointRect();
276 if ( (pathRect.height()>breadth)) {
278 qreal scale = breadth/pathRect.height();
279 qreal displacement = -pathRect.top();
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));
296 painter->setBrush(Qt::transparent);
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());
305 painter->drawLine(0,y,length,y);
306 y = int(breadth/pathRect.height()*(breadth-pathRect.top()));
308 painter->drawLine(0,y,length,y);
311 painter->setPen(m_color);
312 painter->drawPath(path);