summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2023-11-16 20:30:38 +0100
committerDavid Faure <david.faure@kdab.com>2023-11-18 14:01:12 +0100
commit33f3425a1f2aa9f14b2a443374eadccce669eb9b (patch)
tree524e1b5aa24e6d7296aa7d70e849568c6f4e2c51 /src
parent20f18685c469f9a98165c8cb2e388acb237dcabb (diff)
QSvgWidget: only start the animation timer when visible
Thanks to Gammaray, I noticed that a hidden (and rarely shown) QSvgWidget whose purpose is to show a "searching..." animation was triggering a 33ms QTimer for ever in the application. The slot simply calls update() which doesn't do anything on a hidden widget, but waking up every 33ms is still wasteful -- and noisy in gammaray's Events module. [ChangeLog] Add animationEnabled property to QSvgRenderer, and use it from QSvgWidget to stop animations when hidden. Change-Id: I4ec63254b49a513688f8274cee49a1251b89a6cd Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/svg/qsvgrenderer.cpp37
-rw-r--r--src/svg/qsvgrenderer.h3
-rw-r--r--src/svgwidgets/qsvgwidget.cpp22
-rw-r--r--src/svgwidgets/qsvgwidget.h3
4 files changed, 61 insertions, 4 deletions
diff --git a/src/svg/qsvgrenderer.cpp b/src/svg/qsvgrenderer.cpp
index 31024cf..56c0b77 100644
--- a/src/svg/qsvgrenderer.cpp
+++ b/src/svg/qsvgrenderer.cpp
@@ -81,9 +81,9 @@ public:
delete render;
}
- void startStopTimer()
+ void startOrStopTimer()
{
- if (render && render->animated() && fps > 0) {
+ if (animationEnabled && render && render->animated() && fps > 0) {
ensureTimerCreated();
timer->start(1000 / fps);
} else if (timer) {
@@ -106,6 +106,7 @@ public:
QTimer *timer;
int fps;
QSvg::FeatureSet featureSet;
+ bool animationEnabled = true;
};
/*!
@@ -219,6 +220,34 @@ bool QSvgRenderer::animated() const
}
/*!
+ \property QSvgRenderer::animationEnabled
+ \brief whether the animation should run, if the SVG is animated
+
+ Setting the property to false stops the animation timer.
+ Setting the property to false starts the animation timer,
+ provided that the SVG contains animated elements.
+
+ If the SVG is not animated, the property will have no effect.
+ Otherwise, the property defaults to true.
+
+ \sa animated()
+
+ \since 6.7
+*/
+bool QSvgRenderer::isAnimationEnabled() const
+{
+ Q_D(const QSvgRenderer);
+ return d->animationEnabled;
+}
+
+void QSvgRenderer::setAnimationEnabled(bool enable)
+{
+ Q_D(QSvgRenderer);
+ d->animationEnabled = enable;
+ d->startOrStopTimer();
+}
+
+/*!
\property QSvgRenderer::framesPerSecond
\brief the number of frames per second to be shown
@@ -240,7 +269,7 @@ void QSvgRenderer::setFramesPerSecond(int num)
return;
}
d->fps = num;
- d->startStopTimer();
+ d->startOrStopTimer();
}
/*!
@@ -363,7 +392,7 @@ static bool loadDocument(QSvgRenderer *const q,
delete d->render;
d->render = nullptr;
}
- d->startStopTimer();
+ d->startOrStopTimer();
//force first update
QSvgRendererPrivate::callRepaintNeeded(q);
diff --git a/src/svg/qsvgrenderer.h b/src/svg/qsvgrenderer.h
index 4bc2b25..7e898a4 100644
--- a/src/svg/qsvgrenderer.h
+++ b/src/svg/qsvgrenderer.h
@@ -29,6 +29,7 @@ class Q_SVG_EXPORT QSvgRenderer : public QObject
Q_PROPERTY(int currentFrame READ currentFrame WRITE setCurrentFrame)
Q_PROPERTY(Qt::AspectRatioMode aspectRatioMode READ aspectRatioMode WRITE setAspectRatioMode)
Q_PROPERTY(QSvg::FeatureSet featureSet READ featureSet WRITE setFeatureSet)
+ Q_PROPERTY(bool animationEnabled READ isAnimationEnabled WRITE setAnimationEnabled)
public:
QSvgRenderer(QObject *parent = nullptr);
QSvgRenderer(const QString &filename, QObject *parent = nullptr);
@@ -57,6 +58,8 @@ public:
int currentFrame() const;
void setCurrentFrame(int);
int animationDuration() const;//in seconds
+ bool isAnimationEnabled() const;
+ void setAnimationEnabled(bool enable);
QRectF boundsOnElement(const QString &id) const;
bool elementExists(const QString &id) const;
diff --git a/src/svgwidgets/qsvgwidget.cpp b/src/svgwidgets/qsvgwidget.cpp
index 19b3a44..fc8d0a4 100644
--- a/src/svgwidgets/qsvgwidget.cpp
+++ b/src/svgwidgets/qsvgwidget.cpp
@@ -147,12 +147,32 @@ void QSvgWidget::paintEvent(QPaintEvent *)
}
/*!
+ \reimp
+*/
+void QSvgWidget::showEvent(QShowEvent *)
+{
+ Q_D(QSvgWidget);
+ d->renderer->setAnimationEnabled(true);
+}
+
+/*!
+ \reimp
+*/
+void QSvgWidget::hideEvent(QHideEvent *)
+{
+ Q_D(QSvgWidget);
+ d->renderer->setAnimationEnabled(false);
+}
+
+/*!
Loads the contents of the specified SVG \a file and updates the widget.
*/
void QSvgWidget::load(const QString &file)
{
Q_D(const QSvgWidget);
d->renderer->load(file);
+ if (!isVisible())
+ d->renderer->setAnimationEnabled(false);
}
/*!
@@ -162,6 +182,8 @@ void QSvgWidget::load(const QByteArray &contents)
{
Q_D(const QSvgWidget);
d->renderer->load(contents);
+ if (!isVisible())
+ d->renderer->setAnimationEnabled(false);
}
QT_END_NAMESPACE
diff --git a/src/svgwidgets/qsvgwidget.h b/src/svgwidgets/qsvgwidget.h
index a6e4bbf..cb17969 100644
--- a/src/svgwidgets/qsvgwidget.h
+++ b/src/svgwidgets/qsvgwidget.h
@@ -34,6 +34,9 @@ public Q_SLOTS:
void load(const QByteArray &contents);
protected:
void paintEvent(QPaintEvent *event) override;
+ void showEvent(QShowEvent *event) override;
+ void hideEvent(QHideEvent *event) override;
+
private:
Q_DISABLE_COPY(QSvgWidget)
Q_DECLARE_PRIVATE(QSvgWidget)