Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquicksprite.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquicksprite_p.h"
5#include "qquickimagebase_p.h"
6#include <qqml.h>
7#include <QQmlContext>
8#include <QDebug>
9#include <QRandomGenerator>
10
12
173static const int unsetDuration = -2;//-1 means perframe for duration
175 : QQuickStochasticState(parent)
176 , m_generatedCount(0)
177 , m_framesPerRow(0)
178 , m_rowY(0)
179 , m_rowStartX(0)
180 , m_reverse(false)
181 , m_frameHeight(0)
182 , m_frameWidth(0)
183 , m_frames(1)
184 , m_frameX(0)
185 , m_frameY(0)
186 , m_frameRate(unsetDuration)
187 , m_frameRateVariation(0)
188 , m_frameDuration(unsetDuration)
189 , m_frameDurationVariation(0)
190 , m_frameSync(false)
191 , m_devicePixelRatio(1.0)
192{
193}
194
199
200int QQuickSprite::variedDuration() const //Deals with precedence when multiple durations are set
201{
202 if (m_frameSync)
203 return 0;
204
205 if (m_frameRate != unsetDuration) {
206 qreal fpms = (m_frameRate
207 + (m_frameRateVariation * QRandomGenerator::global()->generateDouble() * 2)
208 - m_frameRateVariation) / 1000.0;
209 return qMax(qreal(0.0) , m_frames / fpms);
210 } else if (m_frameDuration != unsetDuration) {
211 int mspf = m_frameDuration
212 + (m_frameDurationVariation * QRandomGenerator::global()->generateDouble() * 2)
213 - m_frameDurationVariation;
214 return qMax(0, m_frames * mspf);
215 } else if (duration() >= 0) {
216 qWarning() << "Sprite::duration is changing meaning to the full animation duration.";
217 qWarning() << "Use Sprite::frameDuration for the old meaning, of per frame duration.";
218 qWarning() << "As an interim measure, duration/durationVariation means the same as frameDuration/frameDurationVariation, and you'll get this warning spewed out everywhere to motivate you.";
219 //Note that the spammyness is due to this being the best location to detect, but also called once each animation loop
220 return QQuickStochasticState::variedDuration() * m_frames;
221 }
222 return 1000; //When nothing set
223}
224
225void QQuickSprite::startImageLoading()
226{
227 m_pix.clear(this);
228 if (!m_source.isEmpty()) {
229 const QQmlContext *context = qmlContext(this);
230 QQmlEngine *e = context ? context->engine() : nullptr;
231 if (!e) { //If not created in QML, you must set the QObject parent to the QML element so this can work
233 e = context ? context->engine() : nullptr;
234 if (!e)
235 qWarning() << "QQuickSprite: Cannot find QQmlEngine - this class is only for use in QML and may not work";
236 }
237 const QUrl resolvedUrl = context ? context->resolvedUrl(m_source) : m_source;
238 QUrl loadUrl = resolvedUrl;
239 QQuickImageBase::resolve2xLocalFile(resolvedUrl, m_devicePixelRatio, &loadUrl,
240 &m_devicePixelRatio);
241
242 m_pix.load(e, loadUrl);
243 }
244}
245
247
248#include "moc_qquicksprite_p.cpp"
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
static void resolve2xLocalFile(const QUrl &url, qreal targetDevicePixelRatio, QUrl *sourceUrl, qreal *sourceDevicePixelRatio)
void load(QQmlEngine *, const QUrl &)
int variedDuration() const override
QQuickSprite(QObject *parent=nullptr)
~QQuickSprite() override
virtual int variedDuration() const
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
Definition qrandom.h:275
\inmodule QtCore
Definition qurl.h:94
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition qurl.cpp:1896
Combined button and popup list for selecting options.
static void * context
#define qWarning
Definition qlogging.h:166
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
static QT_BEGIN_NAMESPACE const int unsetDuration
\qmltype Sprite \instantiates QQuickSprite \inqmlmodule QtQuick
static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer< QQmlContextData > &context)
double qreal
Definition qtypes.h:187