| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:BSD$ | 
| 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 The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** BSD License Usage | 
| 18 | ** Alternatively, you may use this file under the terms of the BSD license | 
| 19 | ** as follows: | 
| 20 | ** | 
| 21 | ** "Redistribution and use in source and binary forms, with or without | 
| 22 | ** modification, are permitted provided that the following conditions are | 
| 23 | ** met: | 
| 24 | **   * Redistributions of source code must retain the above copyright | 
| 25 | **     notice, this list of conditions and the following disclaimer. | 
| 26 | **   * Redistributions in binary form must reproduce the above copyright | 
| 27 | **     notice, this list of conditions and the following disclaimer in | 
| 28 | **     the documentation and/or other materials provided with the | 
| 29 | **     distribution. | 
| 30 | **   * Neither the name of The Qt Company Ltd nor the names of its | 
| 31 | **     contributors may be used to endorse or promote products derived | 
| 32 | **     from this software without specific prior written permission. | 
| 33 | ** | 
| 34 | ** | 
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
| 46 | ** | 
| 47 | ** $QT_END_LICENSE$ | 
| 48 | ** | 
| 49 | ****************************************************************************/ | 
| 50 |  | 
| 51 | #include "window.h" | 
| 52 |  | 
| 53 | Window::Window(QWidget *parent) | 
| 54 |     : QWidget(parent), | 
| 55 |     m_iconSize(64, 64) | 
| 56 | { | 
| 57 |     m_ui.setupUi(this); | 
| 58 |     m_ui.easingCurvePicker->setIconSize(m_iconSize); | 
| 59 |     m_ui.easingCurvePicker->setMinimumHeight(m_iconSize.height() + 50); | 
| 60 |     m_ui.buttonGroup->setId(button: m_ui.lineRadio, id: 0); | 
| 61 |     m_ui.buttonGroup->setId(button: m_ui.circleRadio, id: 1); | 
| 62 |  | 
| 63 |     QEasingCurve dummy; | 
| 64 |     m_ui.periodSpinBox->setValue(dummy.period()); | 
| 65 |     m_ui.amplitudeSpinBox->setValue(dummy.amplitude()); | 
| 66 |     m_ui.overshootSpinBox->setValue(dummy.overshoot()); | 
| 67 |  | 
| 68 |     connect(sender: m_ui.easingCurvePicker, signal: &QListWidget::currentRowChanged, | 
| 69 |             receiver: this, slot: &Window::curveChanged); | 
| 70 |     connect(sender: m_ui.buttonGroup, signal: QOverload<QAbstractButton *>::of(ptr: &QButtonGroup::buttonClicked), | 
| 71 |             receiver: this, slot: &Window::pathChanged); | 
| 72 |     connect(sender: m_ui.periodSpinBox, signal: QOverload<double>::of(ptr: &QDoubleSpinBox::valueChanged), | 
| 73 |             receiver: this, slot: &Window::periodChanged); | 
| 74 |     connect(sender: m_ui.amplitudeSpinBox, signal: QOverload<double>::of(ptr: &QDoubleSpinBox::valueChanged), | 
| 75 |             receiver: this, slot: &Window::amplitudeChanged); | 
| 76 |     connect(sender: m_ui.overshootSpinBox, signal: QOverload<double>::of(ptr: &QDoubleSpinBox::valueChanged), | 
| 77 |             receiver: this, slot: &Window::overshootChanged); | 
| 78 |     createCurveIcons(); | 
| 79 |  | 
| 80 |     QPixmap pix(QLatin1String(":/images/qt-logo.png" )); | 
| 81 |     m_item = new PixmapItem(pix); | 
| 82 |     m_scene.addItem(item: m_item); | 
| 83 |     m_ui.graphicsView->setScene(&m_scene); | 
| 84 |  | 
| 85 |     m_anim = new Animation(m_item, "pos" ); | 
| 86 |     m_anim->setEasingCurve(QEasingCurve::OutBounce); | 
| 87 |     m_ui.easingCurvePicker->setCurrentRow(int(QEasingCurve::OutBounce)); | 
| 88 |  | 
| 89 |     startAnimation(); | 
| 90 | } | 
| 91 |  | 
| 92 | QEasingCurve createEasingCurve(QEasingCurve::Type curveType) | 
| 93 | { | 
| 94 |     QEasingCurve curve(curveType); | 
| 95 |  | 
| 96 |     if (curveType == QEasingCurve::BezierSpline) { | 
| 97 |         curve.addCubicBezierSegment(c1: QPointF(0.4, 0.1), c2: QPointF(0.6, 0.9), endPoint: QPointF(1.0, 1.0)); | 
| 98 |  | 
| 99 |     } else if (curveType == QEasingCurve::TCBSpline) { | 
| 100 |         curve.addTCBSegment(nextPoint: QPointF(0.0, 0.0), t: 0, c: 0, b: 0); | 
| 101 |         curve.addTCBSegment(nextPoint: QPointF(0.3, 0.4), t: 0.2, c: 1, b: -0.2); | 
| 102 |         curve.addTCBSegment(nextPoint: QPointF(0.7, 0.6), t: -0.2, c: 1, b: 0.2); | 
| 103 |         curve.addTCBSegment(nextPoint: QPointF(1.0, 1.0), t: 0, c: 0, b: 0); | 
| 104 |     } | 
| 105 |  | 
| 106 |     return curve; | 
| 107 | } | 
| 108 |  | 
| 109 | void Window::createCurveIcons() | 
| 110 | { | 
| 111 |     QPixmap pix(m_iconSize); | 
| 112 |     QPainter painter(&pix); | 
| 113 |     QLinearGradient gradient(0,0, 0, m_iconSize.height()); | 
| 114 |     gradient.setColorAt(pos: 0.0, color: QColor(240, 240, 240)); | 
| 115 |     gradient.setColorAt(pos: 1.0, color: QColor(224, 224, 224)); | 
| 116 |     QBrush brush(gradient); | 
| 117 |     const QMetaObject &mo = QEasingCurve::staticMetaObject; | 
| 118 |     QMetaEnum metaEnum = mo.enumerator(index: mo.indexOfEnumerator(name: "Type" )); | 
| 119 |     // Skip QEasingCurve::Custom | 
| 120 |     for (int i = 0; i < QEasingCurve::NCurveTypes - 1; ++i) { | 
| 121 |         painter.fillRect(QRect(QPoint(0, 0), m_iconSize), brush); | 
| 122 |         QEasingCurve curve = createEasingCurve(curveType: (QEasingCurve::Type) i); | 
| 123 |         painter.setPen(QColor(0, 0, 255, 64)); | 
| 124 |         qreal xAxis = m_iconSize.height()/1.5; | 
| 125 |         qreal yAxis = m_iconSize.width()/3; | 
| 126 |         painter.drawLine(x1: 0, y1: xAxis, x2: m_iconSize.width(),  y2: xAxis); | 
| 127 |         painter.drawLine(x1: yAxis, y1: 0, x2: yAxis, y2: m_iconSize.height()); | 
| 128 |  | 
| 129 |         qreal curveScale = m_iconSize.height()/2; | 
| 130 |  | 
| 131 |         painter.setPen(Qt::NoPen); | 
| 132 |  | 
| 133 |         // start point | 
| 134 |         painter.setBrush(Qt::red); | 
| 135 |         QPoint start(yAxis, xAxis - curveScale * curve.valueForProgress(progress: 0)); | 
| 136 |         painter.drawRect(x: start.x() - 1, y: start.y() - 1, w: 3, h: 3); | 
| 137 |  | 
| 138 |         // end point | 
| 139 |         painter.setBrush(Qt::blue); | 
| 140 |         QPoint end(yAxis + curveScale, xAxis - curveScale * curve.valueForProgress(progress: 1)); | 
| 141 |         painter.drawRect(x: end.x() - 1, y: end.y() - 1, w: 3, h: 3); | 
| 142 |  | 
| 143 |         QPainterPath curvePath; | 
| 144 |         curvePath.moveTo(p: start); | 
| 145 |         for (qreal t = 0; t <= 1.0; t+=1.0/curveScale) { | 
| 146 |             QPoint to; | 
| 147 |             to.setX(yAxis + curveScale * t); | 
| 148 |             to.setY(xAxis - curveScale * curve.valueForProgress(progress: t)); | 
| 149 |             curvePath.lineTo(p: to); | 
| 150 |         } | 
| 151 |         painter.setRenderHint(hint: QPainter::Antialiasing, on: true); | 
| 152 |         painter.strokePath(path: curvePath, pen: QColor(32, 32, 32)); | 
| 153 |         painter.setRenderHint(hint: QPainter::Antialiasing, on: false); | 
| 154 |         QListWidgetItem *item = new QListWidgetItem; | 
| 155 |         item->setIcon(QIcon(pix)); | 
| 156 |         item->setText(metaEnum.key(index: i)); | 
| 157 |         m_ui.easingCurvePicker->addItem(aitem: item); | 
| 158 |     } | 
| 159 | } | 
| 160 |  | 
| 161 | void Window::startAnimation() | 
| 162 | { | 
| 163 |     m_anim->setStartValue(QPointF(0, 0)); | 
| 164 |     m_anim->setEndValue(QPointF(100, 100)); | 
| 165 |     m_anim->setDuration(2000); | 
| 166 |     m_anim->setLoopCount(-1); // forever | 
| 167 |     m_anim->start(); | 
| 168 | } | 
| 169 |  | 
| 170 | void Window::curveChanged(int row) | 
| 171 | { | 
| 172 |     QEasingCurve::Type curveType = (QEasingCurve::Type)row; | 
| 173 |     m_anim->setEasingCurve(createEasingCurve(curveType)); | 
| 174 |     m_anim->setCurrentTime(0); | 
| 175 |  | 
| 176 |     bool isElastic = curveType >= QEasingCurve::InElastic && curveType <= QEasingCurve::OutInElastic; | 
| 177 |     bool isBounce = curveType >= QEasingCurve::InBounce && curveType <= QEasingCurve::OutInBounce; | 
| 178 |     m_ui.periodSpinBox->setEnabled(isElastic); | 
| 179 |     m_ui.amplitudeSpinBox->setEnabled(isElastic || isBounce); | 
| 180 |     m_ui.overshootSpinBox->setEnabled(curveType >= QEasingCurve::InBack && curveType <= QEasingCurve::OutInBack); | 
| 181 | } | 
| 182 |  | 
| 183 | void Window::pathChanged(QAbstractButton *button) | 
| 184 | { | 
| 185 |     const int index = m_ui.buttonGroup->id(button); | 
| 186 |     m_anim->setPathType(Animation::PathType(index)); | 
| 187 | } | 
| 188 |  | 
| 189 | void Window::periodChanged(double value) | 
| 190 | { | 
| 191 |     QEasingCurve curve = m_anim->easingCurve(); | 
| 192 |     curve.setPeriod(value); | 
| 193 |     m_anim->setEasingCurve(curve); | 
| 194 | } | 
| 195 |  | 
| 196 | void Window::amplitudeChanged(double value) | 
| 197 | { | 
| 198 |     QEasingCurve curve = m_anim->easingCurve(); | 
| 199 |     curve.setAmplitude(value); | 
| 200 |     m_anim->setEasingCurve(curve); | 
| 201 | } | 
| 202 |  | 
| 203 | void Window::overshootChanged(double value) | 
| 204 | { | 
| 205 |     QEasingCurve curve = m_anim->easingCurve(); | 
| 206 |     curve.setOvershoot(value); | 
| 207 |     m_anim->setEasingCurve(curve); | 
| 208 | } | 
| 209 |  | 
| 210 |  |