1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
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 http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include "qquickmaterialbusyindicator_p.h" |
38 | |
39 | #include <QtCore/qmath.h> |
40 | #include <QtCore/qeasingcurve.h> |
41 | #include <QtGui/qpainter.h> |
42 | #include <QtQuick/qsgimagenode.h> |
43 | #include <QtQuick/qquickwindow.h> |
44 | #include <QtQuickControls2/private/qquickanimatednode_p.h> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | /* |
49 | Relevant Android code: |
50 | |
51 | - core/res/res/anim/progress_indeterminate_rotation_material.xml contains |
52 | the rotation animation data. |
53 | - core/res/res/anim/progress_indeterminate_material.xml contains the trim |
54 | animation data. |
55 | - core/res/res/interpolator/trim_start_interpolator.xml and |
56 | core/res/res/interpolator/trim_end_interpolator.xml contain the start |
57 | and end trim path interpolators. |
58 | - addCommand() in core/java/android/util/PathParser.java has a list of the |
59 | different path commands available. |
60 | */ |
61 | |
62 | static const int SpanAnimationDuration = 700; |
63 | static const int RotationAnimationDuration = SpanAnimationDuration * 6; |
64 | static const int TargetRotation = 720; |
65 | static const int OneDegree = 16; |
66 | static const qreal MinSweepSpan = 10 * OneDegree; |
67 | static const qreal MaxSweepSpan = 300 * OneDegree; |
68 | |
69 | class QQuickMaterialBusyIndicatorNode : public QQuickAnimatedNode |
70 | { |
71 | public: |
72 | QQuickMaterialBusyIndicatorNode(QQuickMaterialBusyIndicator *item); |
73 | |
74 | void sync(QQuickItem *item) override; |
75 | |
76 | protected: |
77 | void updateCurrentTime(int time) override; |
78 | |
79 | private: |
80 | int m_lastStartAngle = 0; |
81 | int m_lastEndAngle = 0; |
82 | qreal m_width = 0; |
83 | qreal m_height = 0; |
84 | qreal m_devicePixelRatio = 1; |
85 | QColor m_color; |
86 | }; |
87 | |
88 | QQuickMaterialBusyIndicatorNode::QQuickMaterialBusyIndicatorNode(QQuickMaterialBusyIndicator *item) |
89 | : QQuickAnimatedNode(item) |
90 | { |
91 | setLoopCount(Infinite); |
92 | setCurrentTime(item->elapsed()); |
93 | setDuration(RotationAnimationDuration); |
94 | |
95 | QSGImageNode *textureNode = item->window()->createImageNode(); |
96 | textureNode->setOwnsTexture(true); |
97 | appendChildNode(node: textureNode); |
98 | |
99 | // A texture seems to be required here, but we don't have one yet, as we haven't drawn anything, |
100 | // so just use a blank image. |
101 | QImage blankImage(item->width(), item->height(), QImage::Format_ARGB32_Premultiplied); |
102 | blankImage.fill(color: Qt::transparent); |
103 | textureNode->setTexture(item->window()->createTextureFromImage(image: blankImage)); |
104 | } |
105 | |
106 | void QQuickMaterialBusyIndicatorNode::updateCurrentTime(int time) |
107 | { |
108 | const qreal w = m_width; |
109 | const qreal h = m_height; |
110 | const qreal size = qMin(a: w, b: h); |
111 | const qreal dx = (w - size) / 2; |
112 | const qreal dy = (h - size) / 2; |
113 | |
114 | QImage image(size * m_devicePixelRatio, size * m_devicePixelRatio, QImage::Format_ARGB32_Premultiplied); |
115 | image.fill(color: Qt::transparent); |
116 | |
117 | QPainter painter(&image); |
118 | painter.setRenderHint(hint: QPainter::Antialiasing); |
119 | |
120 | QPen pen; |
121 | QSGImageNode *textureNode = static_cast<QSGImageNode *>(firstChild()); |
122 | pen.setColor(m_color); |
123 | pen.setWidth(qCeil(v: size / 12) * m_devicePixelRatio); |
124 | painter.setPen(pen); |
125 | |
126 | const qreal percentageComplete = time / qreal(RotationAnimationDuration); |
127 | const qreal spanPercentageComplete = (time % SpanAnimationDuration) / qreal(SpanAnimationDuration); |
128 | const int iteration = time / SpanAnimationDuration; |
129 | int startAngle = 0; |
130 | int endAngle = 0; |
131 | |
132 | if (iteration % 2 == 0) { |
133 | if (m_lastStartAngle > 360 * OneDegree) |
134 | m_lastStartAngle -= 360 * OneDegree; |
135 | |
136 | // The start angle is only affected by the rotation animation for the "grow" phase. |
137 | startAngle = m_lastStartAngle; |
138 | QEasingCurve angleCurve(QEasingCurve::OutQuad); |
139 | const qreal percentage = angleCurve.valueForProgress(progress: spanPercentageComplete); |
140 | endAngle = m_lastStartAngle + MinSweepSpan + percentage * (MaxSweepSpan - MinSweepSpan); |
141 | m_lastEndAngle = endAngle; |
142 | } else { |
143 | // Both the start angle *and* the span are affected by the "shrink" phase. |
144 | QEasingCurve angleCurve(QEasingCurve::InQuad); |
145 | const qreal percentage = angleCurve.valueForProgress(progress: spanPercentageComplete); |
146 | startAngle = m_lastEndAngle - MaxSweepSpan + percentage * (MaxSweepSpan - MinSweepSpan); |
147 | endAngle = m_lastEndAngle; |
148 | m_lastStartAngle = startAngle; |
149 | } |
150 | |
151 | const int halfPen = pen.width() / 2; |
152 | const QRectF arcBounds = QRectF(halfPen, halfPen, |
153 | m_devicePixelRatio * size - pen.width(), |
154 | m_devicePixelRatio * size - pen.width()); |
155 | // The current angle of the rotation animation. |
156 | const qreal rotation = OneDegree * percentageComplete * -TargetRotation; |
157 | startAngle -= rotation; |
158 | endAngle -= rotation; |
159 | const int angleSpan = endAngle - startAngle; |
160 | painter.drawArc(rect: arcBounds, a: -startAngle, alen: -angleSpan); |
161 | painter.end(); |
162 | |
163 | textureNode->setRect(QRectF(dx, dy, size, size)); |
164 | textureNode->setTexture(window()->createTextureFromImage(image)); |
165 | } |
166 | |
167 | void QQuickMaterialBusyIndicatorNode::sync(QQuickItem *item) |
168 | { |
169 | QQuickMaterialBusyIndicator *indicator = static_cast<QQuickMaterialBusyIndicator *>(item); |
170 | m_color = indicator->color(); |
171 | m_width = indicator->width(); |
172 | m_height = indicator->height(); |
173 | m_devicePixelRatio = indicator->window()->effectiveDevicePixelRatio(); |
174 | } |
175 | |
176 | QQuickMaterialBusyIndicator::QQuickMaterialBusyIndicator(QQuickItem *parent) : |
177 | QQuickItem(parent) |
178 | { |
179 | setFlag(flag: ItemHasContents); |
180 | } |
181 | |
182 | QColor QQuickMaterialBusyIndicator::color() const |
183 | { |
184 | return m_color; |
185 | } |
186 | |
187 | void QQuickMaterialBusyIndicator::setColor(const QColor &color) |
188 | { |
189 | if (m_color == color) |
190 | return; |
191 | |
192 | m_color = color; |
193 | update(); |
194 | } |
195 | |
196 | bool QQuickMaterialBusyIndicator::isRunning() const |
197 | { |
198 | return isVisible(); |
199 | } |
200 | |
201 | void QQuickMaterialBusyIndicator::setRunning(bool running) |
202 | { |
203 | if (running) |
204 | setVisible(true); |
205 | } |
206 | |
207 | int QQuickMaterialBusyIndicator::elapsed() const |
208 | { |
209 | return m_elapsed; |
210 | } |
211 | |
212 | void QQuickMaterialBusyIndicator::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) |
213 | { |
214 | QQuickItem::itemChange(change, data); |
215 | switch (change) { |
216 | case ItemOpacityHasChanged: |
217 | if (qFuzzyIsNull(d: data.realValue)) |
218 | setVisible(false); |
219 | break; |
220 | case ItemVisibleHasChanged: |
221 | update(); |
222 | break; |
223 | default: |
224 | break; |
225 | } |
226 | } |
227 | |
228 | QSGNode *QQuickMaterialBusyIndicator::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) |
229 | { |
230 | QQuickMaterialBusyIndicatorNode *node = static_cast<QQuickMaterialBusyIndicatorNode *>(oldNode); |
231 | if (isRunning() && width() > 0 && height() > 0) { |
232 | if (!node) { |
233 | node = new QQuickMaterialBusyIndicatorNode(this); |
234 | node->start(); |
235 | } |
236 | node->sync(item: this); |
237 | } else { |
238 | m_elapsed = node ? node->currentTime() : 0; |
239 | delete node; |
240 | node = nullptr; |
241 | } |
242 | return node; |
243 | } |
244 | |
245 | QT_END_NAMESPACE |
246 | |