| 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 "qquickdefaultprogressbar_p.h" |
| 38 | |
| 39 | #include <QtCore/qeasingcurve.h> |
| 40 | #include <QtQuick/private/qquickitem_p.h> |
| 41 | #include <QtQuick/private/qsgadaptationlayer_p.h> |
| 42 | #include <QtQuickControls2/private/qquickanimatednode_p.h> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | static const int Blocks = 4; |
| 47 | static const int BlockWidth = 16; |
| 48 | static const int BlockRestingSpacing = 4; |
| 49 | static const int BlockMovingSpacing = 48; |
| 50 | static const int BlockSpan = Blocks * (BlockWidth + BlockRestingSpacing) - BlockRestingSpacing; |
| 51 | static const int TotalDuration = 4000; |
| 52 | static const int SecondPhaseStart = TotalDuration * 0.4; |
| 53 | static const int ThirdPhaseStart = TotalDuration * 0.6; |
| 54 | |
| 55 | static inline qreal blockStartX(int blockIndex) |
| 56 | { |
| 57 | return ((blockIndex + 1) * -BlockWidth) - (blockIndex * BlockMovingSpacing); |
| 58 | } |
| 59 | |
| 60 | static inline qreal blockRestX(int blockIndex, qreal availableWidth) |
| 61 | { |
| 62 | const qreal spanRightEdgePos = availableWidth / 2 + BlockSpan / 2.0; |
| 63 | return spanRightEdgePos - (blockIndex + 1) * BlockWidth - (blockIndex * BlockRestingSpacing); |
| 64 | } |
| 65 | |
| 66 | static inline qreal blockEndX(int blockIndex, qreal availableWidth) |
| 67 | { |
| 68 | return availableWidth - blockStartX(blockIndex: Blocks - 1 - blockIndex) - BlockWidth; |
| 69 | } |
| 70 | |
| 71 | class QQuickDefaultProgressBarNode : public QQuickAnimatedNode |
| 72 | { |
| 73 | public: |
| 74 | QQuickDefaultProgressBarNode(QQuickDefaultProgressBar *item); |
| 75 | |
| 76 | void updateCurrentTime(int time) override; |
| 77 | void sync(QQuickItem *item) override; |
| 78 | |
| 79 | private: |
| 80 | bool m_indeterminate = false; |
| 81 | qreal m_pixelsPerSecond = 0; |
| 82 | }; |
| 83 | |
| 84 | QQuickDefaultProgressBarNode::QQuickDefaultProgressBarNode(QQuickDefaultProgressBar *item) |
| 85 | : QQuickAnimatedNode(item), |
| 86 | m_pixelsPerSecond(item->width()) |
| 87 | { |
| 88 | setLoopCount(Infinite); |
| 89 | setDuration(TotalDuration); |
| 90 | } |
| 91 | |
| 92 | void QQuickDefaultProgressBarNode::updateCurrentTime(int time) |
| 93 | { |
| 94 | QSGTransformNode *transformNode = static_cast<QSGTransformNode*>(firstChild()); |
| 95 | for (int i = 0; i < Blocks; ++i) { |
| 96 | Q_ASSERT(transformNode->type() == QSGNode::TransformNodeType); |
| 97 | |
| 98 | QMatrix4x4 m; |
| 99 | const qreal restX = blockRestX(blockIndex: i, availableWidth: m_pixelsPerSecond); |
| 100 | const qreal timeInSeconds = time / 1000.0; |
| 101 | |
| 102 | if (time < SecondPhaseStart) { |
| 103 | // Move into the resting position for the first phase. |
| 104 | QEasingCurve easingCurve(QEasingCurve::InQuad); |
| 105 | const qreal easedCompletion = easingCurve.valueForProgress(progress: time / qreal(SecondPhaseStart)); |
| 106 | const qreal distance = m_pixelsPerSecond * (easedCompletion * (SecondPhaseStart / 1000.0)); |
| 107 | const qreal position = blockStartX(blockIndex: i) + distance; |
| 108 | const qreal destination = restX; |
| 109 | m.translate(x: qMin(a: position, b: destination), y: 0); |
| 110 | } else if (time < ThirdPhaseStart) { |
| 111 | // Stay in the same position for the second phase. |
| 112 | m.translate(x: restX, y: 0); |
| 113 | } else { |
| 114 | // Move out of view for the third phase. |
| 115 | const int thirdPhaseSubKickoff = (BlockMovingSpacing / m_pixelsPerSecond) * 1000; |
| 116 | const int subphase = (time - ThirdPhaseStart) / thirdPhaseSubKickoff; |
| 117 | // If we're not at this subphase yet, don't try to animate movement, |
| 118 | // because it will be incorrect. |
| 119 | if (subphase < i) |
| 120 | return; |
| 121 | |
| 122 | const qreal timeSinceSecondPhase = timeInSeconds - (ThirdPhaseStart / 1000.0); |
| 123 | // We only want to start keeping track of time once our subphase has started, |
| 124 | // otherwise we move too much because we account for time that has already elapsed. |
| 125 | // For example, if we were 60 milliseconds into the third subphase: |
| 126 | // |
| 127 | // 0 ..... 1 ..... 2 ... |
| 128 | // 100 100 60 |
| 129 | // |
| 130 | // i == 0, timeSinceOurKickoff == 260 |
| 131 | // i == 1, timeSinceOurKickoff == 160 |
| 132 | // i == 2, timeSinceOurKickoff == 60 |
| 133 | const qreal timeSinceOurKickoff = timeSinceSecondPhase - (thirdPhaseSubKickoff / 1000.0 * i); |
| 134 | const qreal position = restX + (m_pixelsPerSecond * (timeSinceOurKickoff)); |
| 135 | const qreal destination = blockEndX(blockIndex: i, availableWidth: m_pixelsPerSecond); |
| 136 | m.translate(x: qMin(a: position, b: destination), y: 0); |
| 137 | } |
| 138 | |
| 139 | transformNode->setMatrix(m); |
| 140 | |
| 141 | transformNode = static_cast<QSGTransformNode*>(transformNode->nextSibling()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void QQuickDefaultProgressBarNode::sync(QQuickItem *item) |
| 146 | { |
| 147 | QQuickDefaultProgressBar *bar = static_cast<QQuickDefaultProgressBar *>(item); |
| 148 | if (m_indeterminate != bar->isIndeterminate()) { |
| 149 | m_indeterminate = bar->isIndeterminate(); |
| 150 | if (m_indeterminate) |
| 151 | start(); |
| 152 | else |
| 153 | stop(); |
| 154 | } |
| 155 | m_pixelsPerSecond = item->width(); |
| 156 | |
| 157 | QQuickItemPrivate *d = QQuickItemPrivate::get(item); |
| 158 | |
| 159 | QMatrix4x4 m; |
| 160 | m.translate(x: 0, y: (item->height() - item->implicitHeight()) / 2); |
| 161 | setMatrix(m); |
| 162 | |
| 163 | if (m_indeterminate) { |
| 164 | if (childCount() != Blocks) { |
| 165 | // This was previously a regular progress bar; remove the old nodes. |
| 166 | removeAllChildNodes(); |
| 167 | } |
| 168 | |
| 169 | QSGTransformNode *transformNode = static_cast<QSGTransformNode*>(firstChild()); |
| 170 | for (int i = 0; i < Blocks; ++i) { |
| 171 | if (!transformNode) { |
| 172 | transformNode = new QSGTransformNode; |
| 173 | appendChildNode(node: transformNode); |
| 174 | } |
| 175 | |
| 176 | QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode*>(transformNode->firstChild()); |
| 177 | if (!rectNode) { |
| 178 | rectNode = d->sceneGraphContext()->createInternalRectangleNode(); |
| 179 | rectNode->setColor(bar->color()); |
| 180 | transformNode->appendChildNode(node: rectNode); |
| 181 | } |
| 182 | |
| 183 | QMatrix4x4 m; |
| 184 | m.translate(x: blockStartX(blockIndex: i), y: 0); |
| 185 | transformNode->setMatrix(m); |
| 186 | |
| 187 | rectNode->setRect(QRectF(QPointF(0, 0), QSizeF(BlockWidth, item->implicitHeight()))); |
| 188 | rectNode->update(); |
| 189 | |
| 190 | transformNode = static_cast<QSGTransformNode *>(transformNode->nextSibling()); |
| 191 | } |
| 192 | } else { |
| 193 | if (childCount() > 1) { |
| 194 | // This was previously an indeterminate progress bar; remove the old nodes. |
| 195 | removeAllChildNodes(); |
| 196 | } |
| 197 | |
| 198 | QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(firstChild()); |
| 199 | if (!rectNode) { |
| 200 | rectNode = d->sceneGraphContext()->createInternalRectangleNode(); |
| 201 | rectNode->setColor(bar->color()); |
| 202 | appendChildNode(node: rectNode); |
| 203 | } |
| 204 | |
| 205 | rectNode->setRect(QRectF(QPointF(0, 0), QSizeF(bar->progress() * item->width(), item->implicitHeight()))); |
| 206 | rectNode->update(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | QQuickDefaultProgressBar::QQuickDefaultProgressBar(QQuickItem *parent) : |
| 211 | QQuickItem(parent) |
| 212 | { |
| 213 | setFlag(flag: ItemHasContents); |
| 214 | } |
| 215 | |
| 216 | qreal QQuickDefaultProgressBar::progress() const |
| 217 | { |
| 218 | return m_progress; |
| 219 | } |
| 220 | |
| 221 | void QQuickDefaultProgressBar::setProgress(qreal progress) |
| 222 | { |
| 223 | if (progress == m_progress) |
| 224 | return; |
| 225 | |
| 226 | m_progress = progress; |
| 227 | update(); |
| 228 | } |
| 229 | |
| 230 | bool QQuickDefaultProgressBar::isIndeterminate() const |
| 231 | { |
| 232 | return m_indeterminate; |
| 233 | } |
| 234 | |
| 235 | void QQuickDefaultProgressBar::setIndeterminate(bool indeterminate) |
| 236 | { |
| 237 | if (indeterminate == m_indeterminate) |
| 238 | return; |
| 239 | |
| 240 | m_indeterminate = indeterminate; |
| 241 | setClip(m_indeterminate); |
| 242 | update(); |
| 243 | } |
| 244 | |
| 245 | QColor QQuickDefaultProgressBar::color() const |
| 246 | { |
| 247 | return m_color; |
| 248 | } |
| 249 | |
| 250 | void QQuickDefaultProgressBar::setColor(const QColor &color) |
| 251 | { |
| 252 | if (color == m_color) |
| 253 | return; |
| 254 | |
| 255 | m_color = color; |
| 256 | update(); |
| 257 | } |
| 258 | |
| 259 | void QQuickDefaultProgressBar::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) |
| 260 | { |
| 261 | QQuickItem::itemChange(change, data); |
| 262 | if (change == ItemVisibleHasChanged) |
| 263 | update(); |
| 264 | } |
| 265 | |
| 266 | QSGNode *QQuickDefaultProgressBar::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *) |
| 267 | { |
| 268 | QQuickDefaultProgressBarNode *node = static_cast<QQuickDefaultProgressBarNode *>(oldNode); |
| 269 | if (isVisible() && width() > 0 && height() > 0) { |
| 270 | if (!node) |
| 271 | node = new QQuickDefaultProgressBarNode(this); |
| 272 | node->sync(item: this); |
| 273 | } else { |
| 274 | delete node; |
| 275 | node = nullptr; |
| 276 | } |
| 277 | return node; |
| 278 | } |
| 279 | |
| 280 | QT_END_NAMESPACE |
| 281 | |