demos/composition/composition.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
parent 4 3b1da2848fc7
--- a/demos/composition/composition.cpp	Mon Mar 15 12:43:09 2010 +0200
+++ b/demos/composition/composition.cpp	Thu Apr 08 14:19:33 2010 +0300
@@ -48,6 +48,8 @@
 #include <QMouseEvent>
 #include <qmath.h>
 
+const int animationInterval = 15; // update every 16 ms = ~60FPS
+
 CompositionWidget::CompositionWidget(QWidget *parent)
     : QWidget(parent)
 {
@@ -236,6 +238,7 @@
     : ArthurFrame(parent)
 {
     m_animation_enabled = true;
+    m_animationTimer = startTimer(animationInterval);
 #ifdef Q_WS_QWS
     m_image = QPixmap(":res/composition/flower.jpg");
     m_image.setAlphaChannel(QPixmap(":res/composition/flower_alpha.jpg"));
@@ -264,6 +267,20 @@
     return rect;
 }
 
+void CompositionRenderer::setAnimationEnabled(bool enabled)
+{
+    if (m_animation_enabled == enabled)
+        return;
+    m_animation_enabled = enabled;
+    if (enabled) {
+        Q_ASSERT(!m_animationTimer);
+        m_animationTimer = startTimer(animationInterval);
+    } else {
+        killTimer(m_animationTimer);
+        m_animationTimer = 0;
+    }
+}
+
 void CompositionRenderer::updateCirclePos()
 {
     if (m_current_object != NoObject)
@@ -471,10 +488,6 @@
         painter->drawImage(0, 0, m_buffer);
 #endif
     }
-
-    if (m_animation_enabled && m_current_object == NoObject) {
-        updateCirclePos();
-    }
 }
 
 void CompositionRenderer::mousePressEvent(QMouseEvent *e)
@@ -489,6 +502,10 @@
     } else {
         m_current_object = NoObject;
     }
+    if (m_animation_enabled) {
+        killTimer(m_animationTimer);
+        m_animationTimer = 0;
+    }
 }
 
 void CompositionRenderer::mouseMoveEvent(QMouseEvent *e)
@@ -500,7 +517,15 @@
 {
     m_current_object = NoObject;
 
-    if (m_animation_enabled)
+    if (m_animation_enabled) {
+        Q_ASSERT(!m_animationTimer);
+        m_animationTimer = startTimer(animationInterval);
+    }
+}
+
+void CompositionRenderer::timerEvent(QTimerEvent *event)
+{
+    if (event->timerId() == m_animationTimer)
         updateCirclePos();
 }