From 3a4c4ecb16827419fcb133744a7e97ed860cc669 Mon Sep 17 00:00:00 2001
From: Warwick Allison <warwick.allison@nokia.com>
Date: Tue, 28 Jul 2009 15:58:48 +1000
Subject: Produce frame-rate-perfect videos. While recording video, ignore
 realtime rate and deliver perfect per-frame animation.

---
 tools/qmlviewer/qmlviewer.cpp | 59 +++++++++++++++++++++++++++----------------
 tools/qmlviewer/qmlviewer.h   | 14 +++++-----
 2 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/tools/qmlviewer/qmlviewer.cpp b/tools/qmlviewer/qmlviewer.cpp
index f4eff46..95cd24b 100644
--- a/tools/qmlviewer/qmlviewer.cpp
+++ b/tools/qmlviewer/qmlviewer.cpp
@@ -19,6 +19,8 @@
 #include "qmlpalette.h"
 #include "qml.h"
 #include <private/qperformancelog_p.h>
+#include <QAbstractAnimation>
+#include <private/qabstractanimation_p.h>
 #include "deviceskin.h"
 
 #include <QNetworkDiskCache>
@@ -157,6 +159,14 @@ QmlViewer::QmlViewer(QWidget *parent, Qt::WindowFlags flags)
     layout->addWidget(canvas);
 
     setupProxy();
+
+    connect(&autoStartTimer, SIGNAL(triggered()), this, SLOT(autoStartRecording()));
+    connect(&autoStopTimer, SIGNAL(triggered()), this, SLOT(autoStopRecording()));
+    connect(&recordTimer, SIGNAL(triggered()), this, SLOT(recordFrame()));
+    autoStartTimer.setRunning(false);
+    autoStopTimer.setRunning(false);
+    recordTimer.setRunning(false);
+    recordTimer.setRepeating(true);
 }
 
 QMenuBar *QmlViewer::menuBar() const
@@ -302,7 +312,7 @@ void QmlViewer::takeSnapShot()
 
 void QmlViewer::toggleRecordingWithSelection()
 {
-    if (!recordTimer.isActive()) {
+    if (!recordTimer.isRunning()) {
         QString fileName = QFileDialog::getSaveFileName(this, tr("Save Video File"), "", tr("Common Video files (*.avi *.mpeg *.mov);; GIF Animation (*.gif);; Individual PNG frames (*.png);; All ffmpeg formats (*.*)"));
         if (fileName.isEmpty())
             return;
@@ -319,7 +329,7 @@ void QmlViewer::toggleRecording()
         toggleRecordingWithSelection();
         return;
     }
-    bool recording = !recordTimer.isActive();
+    bool recording = !recordTimer.isRunning();
     recordAction->setText(recording ? tr("&Stop Recording Video\tF2") : tr("&Start Recording Video\tF2"));
     setRecording(recording);
 }
@@ -480,7 +490,7 @@ void QmlViewer::setSkin(const QString& skinDirectory)
 void QmlViewer::setAutoRecord(int from, int to)
 {
     record_autotime = to-from;
-    autoStartTimer.start(from,this);
+    autoStartTimer.setInterval(from);
 }
 
 void QmlViewer::setRecordArgs(const QStringList& a)
@@ -537,12 +547,15 @@ void QmlViewer::keyPressEvent(QKeyEvent *event)
 
 void QmlViewer::setRecording(bool on)
 {
-    if (on == recordTimer.isActive())
+    if (on == recordTimer.isRunning())
         return;
 
+    QUnifiedTimer::instance()->setTimingInterval(on ? record_period:16);
+    QUnifiedTimer::instance()->setConsistentTiming(on);
     if (on) {
         canvas->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
-        recordTimer.start(record_period,this);
+        recordTimer.setInterval(record_period);
+        recordTimer.setRunning(true);
         QString fmt = record_file.right(4).toLower();
         if (fmt != ".png" && fmt != ".gif") {
             // Stream video to ffmpeg
@@ -570,7 +583,7 @@ void QmlViewer::setRecording(bool on)
         }
     } else {
         canvas->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
-        recordTimer.stop();
+        recordTimer.setRunning(false);
         if (frame_stream) {
             qDebug() << "Saving video...";
             frame_stream->close();
@@ -648,7 +661,7 @@ void QmlViewer::setRecording(bool on)
             frames.clear();
         }
     }
-    qDebug() << "Recording: " << (recordTimer.isActive()?"ON":"OFF");
+    qDebug() << "Recording: " << (recordTimer.isRunning()?"ON":"OFF");
 }
 
 void QmlViewer::ffmpegFinished(int code)
@@ -656,23 +669,25 @@ void QmlViewer::ffmpegFinished(int code)
     qDebug() << "ffmpeg returned" << code << frame_stream->readAllStandardError();
 }
 
-void QmlViewer::timerEvent(QTimerEvent *event)
+void QmlViewer::autoStartRecording()
 {
-    if (event->timerId() == recordTimer.timerId()) {
-        canvas->QWidget::render(&frame);
-        if (frame_stream) {
-            frame_stream->write((char*)frame.bits(),frame.numBytes());
-        } else {
-            frames.append(new QImage(frame));
-        }
-        if (record_autotime && autoTimer.elapsed() >= record_autotime)
-            setRecording(false);
-    } else if (event->timerId() == autoStartTimer.timerId()) {
-        autoTimer.start();
-        autoStartTimer.stop();
-        setRecording(true);
+    setRecording(true);
+    autoStopTimer.setInterval(record_autotime);
+    autoStopTimer.setRunning(true);
+}
+
+void QmlViewer::autoStopRecording()
+{
+    setRecording(true);
+}
+
+void QmlViewer::recordFrame()
+{
+    canvas->QWidget::render(&frame);
+    if (frame_stream) {
+        frame_stream->write((char*)frame.bits(),frame.numBytes());
     } else {
-        QWidget::timerEvent(event);
+        frames.append(new QImage(frame));
     }
 }
 
diff --git a/tools/qmlviewer/qmlviewer.h b/tools/qmlviewer/qmlviewer.h
index d671410..e7177eb 100644
--- a/tools/qmlviewer/qmlviewer.h
+++ b/tools/qmlviewer/qmlviewer.h
@@ -15,7 +15,7 @@
 #define QMLVIEWER_H
 
 #include <QMenuBar>
-#include <QBasicTimer>
+#include <QmlTimer>
 #include <QTime>
 #include <QList>
 
@@ -39,7 +39,7 @@ public:
     void setRecordArgs(const QStringList&);
     int recordPeriod() const { return record_period; }
     void setRecording(bool on);
-    bool isRecording() const { return recordTimer.isActive(); }
+    bool isRecording() const { return recordTimer.isRunning(); }
     void setAutoRecord(int from, int to);
     void setDeviceKeys(bool);
     void setNetworkCacheSize(int size);
@@ -60,13 +60,15 @@ public slots:
 
 protected:
     virtual void keyPressEvent(QKeyEvent *);
-    virtual void timerEvent(QTimerEvent *);
 
     void createMenu(QMenuBar *menu, QMenu *flatmenu);
 
 private slots:
     void setScaleSkin();
     void setScaleView();
+    void autoStartRecording();
+    void autoStopRecording();
+    void recordFrame();
 
 private:
     void setupProxy();
@@ -78,12 +80,12 @@ private:
     QFxView *canvas;
     QmlPalette *palette;
     QmlPalette *disabledPalette;
-    QBasicTimer recordTimer;
+    QmlTimer recordTimer;
     QImage frame;
     QList<QImage*> frames;
     QProcess* frame_stream;
-    QBasicTimer autoStartTimer;
-    QTime autoTimer;
+    QmlTimer autoStartTimer;
+    QmlTimer autoStopTimer;
     QString record_dither;
     QString record_file;
     QStringList record_args;
-- 
cgit v0.12