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 <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)
: QWidget(parent)
@ -189,7 +191,7 @@ CompositionRenderer::CompositionRenderer(QWidget *parent)
: ArthurFrame(parent)
{
m_animation_enabled = true;
m_animationTimer = startTimer(animationInterval);
m_animationTimer.start(animationInterval, this);
m_image = QImage(":res/composition/flower.jpg");
m_image.setAlphaChannel(QImage(":res/composition/flower_alpha.jpg"));
m_circle_alpha = 127;
@ -219,11 +221,10 @@ void CompositionRenderer::setAnimationEnabled(bool enabled)
return;
m_animation_enabled = enabled;
if (enabled) {
Q_ASSERT(!m_animationTimer);
m_animationTimer = startTimer(animationInterval);
Q_ASSERT(!m_animationTimer.isActive());
m_animationTimer.start(animationInterval, this);
} else {
killTimer(m_animationTimer);
m_animationTimer = 0;
m_animationTimer.stop();
}
}
@ -326,8 +327,7 @@ void CompositionRenderer::mousePressEvent(QMouseEvent *e)
m_current_object = NoObject;
}
if (m_animation_enabled) {
killTimer(m_animationTimer);
m_animationTimer = 0;
m_animationTimer.stop();
}
}
@ -342,14 +342,14 @@ void CompositionRenderer::mouseReleaseEvent(QMouseEvent *)
m_current_object = NoObject;
if (m_animation_enabled) {
Q_ASSERT(!m_animationTimer);
m_animationTimer = startTimer(animationInterval);
Q_ASSERT(!m_animationTimer.isActive());
m_animationTimer.start(animationInterval, this);
}
}
void CompositionRenderer::timerEvent(QTimerEvent *event)
{
if (event->timerId() == m_animationTimer)
if (event->matches(m_animationTimer))
updateCirclePos();
}

View File

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