Skip to content

Commit

Permalink
CompositionWidget example: use QBasicTimer instead of raw timer IDs
Browse files Browse the repository at this point in the history
Change-Id: I011645d915193c9ab1e3f001898c88fdd48e64a0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
ahmadsamir committed Oct 3, 2024
1 parent 0759390 commit 50cdba3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
22 changes: 11 additions & 11 deletions examples/widgets/painting/composition/composition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/widgets/painting/composition/composition.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "arthurwidgets.h"

#include <QBasicTimer>
#include <QPainter>
#include <QEvent>

Expand Down Expand Up @@ -137,7 +138,7 @@ public slots:

ObjectType m_current_object;
bool m_animation_enabled;
int m_animationTimer;
QBasicTimer m_animationTimer;
};

#endif // COMPOSITION_H

0 comments on commit 50cdba3

Please sign in to comment.