Qt 4.8
qeasingcurve.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 /*
43 
44 | *property* | *Used for type* |
45 | period | QEasingCurve::{In,Out,InOut,OutIn}Elastic |
46 | amplitude | QEasingCurve::{In,Out,InOut,OutIn}Bounce, QEasingCurve::{In,Out,InOut,OutIn}Elastic |
47 | overshoot | QEasingCurve::{In,Out,InOut,OutIn}Back |
48 
49 */
50 
51 
52 
53 
318 #include "qeasingcurve.h"
319 
320 #ifndef QT_NO_DEBUG_STREAM
321 #include <QtCore/qdebug.h>
322 #include <QtCore/qstring.h>
323 #endif
324 
325 #ifndef QT_NO_DATASTREAM
326 #include <QtCore/qdatastream.h>
327 #endif
328 
330 
332 {
333  return type >= QEasingCurve::InElastic
334  && type <= QEasingCurve::OutInBounce;
335 }
336 
338 {
339 public:
340  enum Type { In, Out, InOut, OutIn };
341 
343  qreal overshoot = 1.70158)
344  : _t(type), _p(period), _a(amplitude), _o(overshoot)
345  { }
347  virtual qreal value(qreal t);
348  virtual QEasingCurveFunction *copy() const;
349  bool operator==(const QEasingCurveFunction& other);
350 
355 };
356 
358 {
359  return t;
360 }
361 
363 {
364  return new QEasingCurveFunction(_t, _p, _a, _o);
365 }
366 
368 {
369  return _t == other._t &&
370  qFuzzyCompare(_p, other._p) &&
371  qFuzzyCompare(_a, other._a) &&
372  qFuzzyCompare(_o, other._o);
373 }
374 
376 #include "../../3rdparty/easing/easing.cpp"
378 
380 {
381 public:
383  : type(QEasingCurve::Linear),
384  config(0),
385  func(&easeNone)
386  { }
387  ~QEasingCurvePrivate() { delete config; }
388  void setType_helper(QEasingCurve::Type);
389 
393 };
394 
396 {
398  : QEasingCurveFunction(type, qreal(0.3), qreal(1.0))
399  { }
400 
402  {
403  ElasticEase *rv = new ElasticEase(_t);
404  rv->_p = _p;
405  rv->_a = _a;
406  return rv;
407  }
408 
410  {
411  qreal p = (_p < 0) ? qreal(0.3) : _p;
412  qreal a = (_a < 0) ? qreal(1.0) : _a;
413  switch(_t) {
414  case In:
415  return easeInElastic(t, a, p);
416  case Out:
417  return easeOutElastic(t, a, p);
418  case InOut:
419  return easeInOutElastic(t, a, p);
420  case OutIn:
421  return easeOutInElastic(t, a, p);
422  default:
423  return t;
424  }
425  }
426 };
427 
429 {
431  : QEasingCurveFunction(type, qreal(0.3), qreal(1.0))
432  { }
433 
435  {
436  BounceEase *rv = new BounceEase(_t);
437  rv->_a = _a;
438  return rv;
439  }
440 
442  {
443  qreal a = (_a < 0) ? qreal(1.0) : _a;
444  switch(_t) {
445  case In:
446  return easeInBounce(t, a);
447  case Out:
448  return easeOutBounce(t, a);
449  case InOut:
450  return easeInOutBounce(t, a);
451  case OutIn:
452  return easeOutInBounce(t, a);
453  default:
454  return t;
455  }
456  }
457 };
458 
460 {
462  : QEasingCurveFunction(type, qreal(0.3), qreal(1.0), qreal(1.70158))
463  { }
464 
466  {
467  BackEase *rv = new BackEase(_t);
468  rv->_o = _o;
469  return rv;
470  }
471 
473  {
474  qreal o = (_o < 0) ? qreal(1.70158) : _o;
475  switch(_t) {
476  case In:
477  return easeInBack(t, o);
478  case Out:
479  return easeOutBack(t, o);
480  case InOut:
481  return easeInOutBack(t, o);
482  case OutIn:
483  return easeOutInBack(t, o);
484  default:
485  return t;
486  }
487  }
488 };
489 
491 {
492  switch(curve) {
494  return &easeNone;
496  return &easeInQuad;
498  return &easeOutQuad;
500  return &easeInOutQuad;
502  return &easeOutInQuad;
504  return &easeInCubic;
506  return &easeOutCubic;
508  return &easeInOutCubic;
510  return &easeOutInCubic;
512  return &easeInQuart;
514  return &easeOutQuart;
516  return &easeInOutQuart;
518  return &easeOutInQuart;
520  return &easeInQuint;
522  return &easeOutQuint;
524  return &easeInOutQuint;
526  return &easeOutInQuint;
528  return &easeInSine;
530  return &easeOutSine;
532  return &easeInOutSine;
534  return &easeOutInSine;
536  return &easeInExpo;
538  return &easeOutExpo;
540  return &easeInOutExpo;
542  return &easeOutInExpo;
544  return &easeInCirc;
546  return &easeOutCirc;
548  return &easeInOutCirc;
550  return &easeOutInCirc;
551  // Internal for, compatibility with QTimeLine only ??
553  return &easeInCurve;
555  return &easeOutCurve;
557  return &easeSineCurve;
559  return &easeCosineCurve;
560  default:
561  return 0;
562  };
563 }
564 
566 {
567  QEasingCurveFunction *curveFunc = 0;
568  switch(type) {
570  curveFunc = new ElasticEase(ElasticEase::In);
571  break;
573  curveFunc = new ElasticEase(ElasticEase::Out);
574  break;
576  curveFunc = new ElasticEase(ElasticEase::InOut);
577  break;
579  curveFunc = new ElasticEase(ElasticEase::OutIn);
580  break;
582  curveFunc = new BounceEase(BounceEase::Out);
583  break;
585  curveFunc = new BounceEase(BounceEase::In);
586  break;
588  curveFunc = new BounceEase(BounceEase::OutIn);
589  break;
591  curveFunc = new BounceEase(BounceEase::InOut);
592  break;
594  curveFunc = new BackEase(BackEase::In);
595  break;
597  curveFunc = new BackEase(BackEase::Out);
598  break;
600  curveFunc = new BackEase(BackEase::InOut);
601  break;
603  curveFunc = new BackEase(BackEase::OutIn);
604  break;
605  default:
606  curveFunc = new QEasingCurveFunction(QEasingCurveFunction::In, qreal(0.3), qreal(1.0), qreal(1.70158));
607  }
608 
609  return curveFunc;
610 }
611 
616  : d_ptr(new QEasingCurvePrivate)
617 {
618  setType(type);
619 }
620 
626 {
627  // ### non-atomic, requires malloc on shallow copy
628  *d_ptr = *other.d_ptr;
629  if (other.d_ptr->config)
630  d_ptr->config = other.d_ptr->config->copy();
631 }
632 
638 {
639  delete d_ptr;
640 }
641 
646 {
647  // ### non-atomic, requires malloc on shallow copy
648  if (d_ptr->config) {
649  delete d_ptr->config;
650  d_ptr->config = 0;
651  }
652 
653  *d_ptr = *other.d_ptr;
654  if (other.d_ptr->config)
655  d_ptr->config = other.d_ptr->config->copy();
656 
657  return *this;
658 }
659 
664 bool QEasingCurve::operator==(const QEasingCurve &other) const
665 {
666  bool res = d_ptr->func == other.d_ptr->func
667  && d_ptr->type == other.d_ptr->type;
668  if (res) {
669  if (d_ptr->config && other.d_ptr->config) {
670  // catch the config content
671  res = d_ptr->config->operator==(*(other.d_ptr->config));
672 
673  } else if (d_ptr->config || other.d_ptr->config) {
674  // one one has a config object, which could contain default values
675  res = qFuzzyCompare(amplitude(), other.amplitude()) &&
676  qFuzzyCompare(period(), other.period()) &&
677  qFuzzyCompare(overshoot(), other.overshoot());
678  }
679  }
680  return res;
681 }
682 
702 {
703  return d_ptr->config ? d_ptr->config->_a : qreal(1.0);
704 }
705 
714 {
715  if (!d_ptr->config)
717  d_ptr->config->_a = amplitude;
718 }
719 
726 {
727  return d_ptr->config ? d_ptr->config->_p : qreal(0.3);
728 }
729 
738 {
739  if (!d_ptr->config)
741  d_ptr->config->_p = period;
742 }
743 
750 {
751  return d_ptr->config ? d_ptr->config->_o : qreal(1.70158) ;
752 }
753 
762 {
763  if (!d_ptr->config)
765  d_ptr->config->_o = overshoot;
766 }
767 
772 {
773  return d_ptr->type;
774 }
775 
777 {
778  qreal amp = -1.0;
779  qreal period = -1.0;
780  qreal overshoot = -1.0;
781 
782  if (config) {
783  amp = config->_a;
784  period = config->_p;
785  overshoot = config->_o;
786  delete config;
787  config = 0;
788  }
789 
790  if (isConfigFunction(newType) || (amp != -1.0) || (period != -1.0) || (overshoot != -1.0)) {
791  config = curveToFunctionObject(newType);
792  if (amp != -1.0)
793  config->_a = amp;
794  if (period != -1.0)
795  config->_p = period;
796  if (overshoot != -1.0)
797  config->_o = overshoot;
798  func = 0;
799  } else if (newType != QEasingCurve::Custom) {
800  func = curveToFunc(newType);
801  }
802  Q_ASSERT((func == 0) == (config != 0));
803  type = newType;
804 }
805 
810 {
811  if (d_ptr->type == type)
812  return;
813  if (type < Linear || type >= NCurveTypes - 1) {
814  qWarning("QEasingCurve: Invalid curve type %d", type);
815  return;
816  }
817 
818  d_ptr->setType_helper(type);
819 }
820 
833 {
834  if (!func) {
835  qWarning("Function pointer must not be null");
836  return;
837  }
838  d_ptr->func = func;
840 }
841 
848 {
849  return d_ptr->type == Custom ? d_ptr->func : 0;
850 }
851 
859 {
860  progress = qBound<qreal>(0, progress, 1);
861  if (d_ptr->func)
862  return d_ptr->func(progress);
863  else if (d_ptr->config)
864  return d_ptr->config->value(progress);
865  else
866  return progress;
867 }
868 
869 #ifndef QT_NO_DEBUG_STREAM
871 {
872  debug << "type:" << item.d_ptr->type
873  << "func:" << item.d_ptr->func;
874  if (item.d_ptr->config) {
875  debug << QString::fromAscii("period:%1").arg(item.d_ptr->config->_p, 0, 'f', 20)
876  << QString::fromAscii("amp:%1").arg(item.d_ptr->config->_a, 0, 'f', 20)
877  << QString::fromAscii("overshoot:%1").arg(item.d_ptr->config->_o, 0, 'f', 20);
878  }
879  return debug;
880 }
881 #endif // QT_NO_DEBUG_STREAM
882 
883 #ifndef QT_NO_DATASTREAM
884 
898 {
899  stream << quint8(easing.d_ptr->type);
900  stream << quint64(quintptr(easing.d_ptr->func));
901 
902  bool hasConfig = easing.d_ptr->config;
903  stream << hasConfig;
904  if (hasConfig) {
905  stream << easing.d_ptr->config->_p;
906  stream << easing.d_ptr->config->_a;
907  stream << easing.d_ptr->config->_o;
908  }
909  return stream;
910 }
911 
926 {
928  quint8 int_type;
929  stream >> int_type;
930  type = static_cast<QEasingCurve::Type>(int_type);
931  easing.setType(type);
932 
933  quint64 ptr_func;
934  stream >> ptr_func;
935  easing.d_ptr->func = QEasingCurve::EasingFunction(quintptr(ptr_func));
936 
937  bool hasConfig;
938  stream >> hasConfig;
939  if (hasConfig) {
941  stream >> config->_p;
942  stream >> config->_a;
943  stream >> config->_o;
944  easing.d_ptr->config = config;
945  }
946  return stream;
947 }
948 #endif // QT_NO_DATASTREAM
949 
QEasingCurveFunction * copy() const
BounceEase(Type type)
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
QEasingCurveFunction(QEasingCurveFunction::Type type=In, qreal period=0.3, qreal amplitude=1.0, qreal overshoot=1.70158)
ElasticEase(Type type)
int type
Definition: qmetatype.cpp:239
qreal valueForProgress(qreal progress) const
Return the effective progress for the easing curve at progress.
double qreal
Definition: qglobal.h:1193
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
qreal overshoot() const
Returns the overshoot.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
The QEasingCurve class provides easing curves for controlling animation.
Definition: qeasingcurve.h:55
qreal period() const
Returns the period.
bool operator==(const QEasingCurve &other) const
Compare this easing curve with other and returns true if they are equal.
friend Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QEasingCurve &item)
QEasingCurveFunction * copy() const
void setType_helper(QEasingCurve::Type)
void setCustomType(EasingFunction func)
Sets a custom easing curve that is defined by the user in the function func.
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
long ASN1_INTEGER_get ASN1_INTEGER * a
unsigned char quint8
Definition: qglobal.h:934
static QEasingCurve::EasingFunction curveToFunc(QEasingCurve::Type curve)
EasingFunction customType() const
Returns the function pointer to the custom easing curve.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
qreal amplitude() const
Returns the amplitude.
QEasingCurve::EasingFunction func
QEasingCurve & operator=(const QEasingCurve &other)
Copy other.
bool operator==(const QEasingCurveFunction &other)
Type type() const
Returns the type of the easing curve.
void setPeriod(qreal period)
Sets the period to period.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static FILE * stream
unsigned __int64 quint64
Definition: qglobal.h:943
QEasingCurvePrivate * d_ptr
Definition: qeasingcurve.h:102
QEasingCurveFunction * config
static QEasingCurveFunction * curveToFunctionObject(QEasingCurve::Type type)
Q_CORE_EXPORT void qWarning(const char *,...)
void setOvershoot(qreal overshoot)
Sets the overshoot to overshoot.
virtual qreal value(qreal t)
QEasingCurve(Type type=Linear)
Constructs an easing curve of the given type.
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
BackEase(Type type)
qreal value(qreal t)
QEasingCurveFunction * copy() const
~QEasingCurve()
Destructor.
void setAmplitude(qreal amplitude)
Sets the amplitude to amplitude.
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
virtual ~QEasingCurveFunction()
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
Type
The type of easing curve.
Definition: qeasingcurve.h:60
friend Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QEasingCurve &)
static bool isConfigFunction(QEasingCurve::Type type)
virtual QEasingCurveFunction * copy() const
QEasingCurve::Type type
qreal value(qreal t)
void setType(Type type)
Sets the type of the easing curve to type.
qreal value(qreal t)
qreal(* EasingFunction)(qreal progress)
This is a typedef for a pointer to a function with the following signature:
Definition: qeasingcurve.h:96