CompositionWidget example: use QBasicTimer instead of raw timer IDs

Change-Id: I011645d915193c9ab1e3f001898c88fdd48e64a0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2024-09-05 17:49:06 +03:00
parent 07593900dc
commit 50cdba38ad
2 changed files with 13 additions and 12 deletions

View File

@ -10,7 +10,9 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <qmath.h> #include <qmath.h>
const int animationInterval = 15; // update every 16 ms = ~60FPS using namespace std::chrono_literals;
const auto animationInterval = 15ms; // update every 16 ms = ~60FPS
CompositionWidget::CompositionWidget(QWidget *parent) CompositionWidget::CompositionWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
@ -189,7 +191,7 @@ CompositionRenderer::CompositionRenderer(QWidget *parent)
: ArthurFrame(parent) : ArthurFrame(parent)
{ {
m_animation_enabled = true; m_animation_enabled = true;
m_animationTimer = startTimer(animationInterval); m_animationTimer.start(animationInterval, this);
m_image = QImage(":res/composition/flower.jpg"); m_image = QImage(":res/composition/flower.jpg");
m_image.setAlphaChannel(QImage(":res/composition/flower_alpha.jpg")); m_image.setAlphaChannel(QImage(":res/composition/flower_alpha.jpg"));
m_circle_alpha = 127; m_circle_alpha = 127;
@ -219,11 +221,10 @@ void CompositionRenderer::setAnimationEnabled(bool enabled)
return; return;
m_animation_enabled = enabled; m_animation_enabled = enabled;
if (enabled) { if (enabled) {
Q_ASSERT(!m_animationTimer); Q_ASSERT(!m_animationTimer.isActive());
m_animationTimer = startTimer(animationInterval); m_animationTimer.start(animationInterval, this);
} else { } else {
killTimer(m_animationTimer); m_animationTimer.stop();
m_animationTimer = 0;
} }
} }
@ -326,8 +327,7 @@ void CompositionRenderer::mousePressEvent(QMouseEvent *e)
m_current_object = NoObject; m_current_object = NoObject;
} }
if (m_animation_enabled) { if (m_animation_enabled) {
killTimer(m_animationTimer); m_animationTimer.stop();
m_animationTimer = 0;
} }
} }
@ -342,14 +342,14 @@ void CompositionRenderer::mouseReleaseEvent(QMouseEvent *)
m_current_object = NoObject; m_current_object = NoObject;
if (m_animation_enabled) { if (m_animation_enabled) {
Q_ASSERT(!m_animationTimer); Q_ASSERT(!m_animationTimer.isActive());
m_animationTimer = startTimer(animationInterval); m_animationTimer.start(animationInterval, this);
} }
} }
void CompositionRenderer::timerEvent(QTimerEvent *event) void CompositionRenderer::timerEvent(QTimerEvent *event)
{ {
if (event->timerId() == m_animationTimer) if (event->matches(m_animationTimer))
updateCirclePos(); updateCirclePos();
} }

View File

@ -6,6 +6,7 @@
#include "arthurwidgets.h" #include "arthurwidgets.h"
#include <QBasicTimer>
#include <QPainter> #include <QPainter>
#include <QEvent> #include <QEvent>
@ -137,7 +138,7 @@ private:
ObjectType m_current_object; ObjectType m_current_object;
bool m_animation_enabled; bool m_animation_enabled;
int m_animationTimer; QBasicTimer m_animationTimer;
}; };
#endif // COMPOSITION_H #endif // COMPOSITION_H