Marquee loop can now be paused and resumed.
authorsl
Wed, 04 Jun 2014 18:30:37 +0200
changeset 33cf5eba52cb1d
parent 32 1c2a7f563019
child 34 f2c87f0cfabe
Marquee loop can now be paused and resumed.
Cleaning up our MiniDisplay coneection to render loop API.
MarqueeText.qml
MiniDisplay/minidisplay.cpp
MiniDisplay/minidisplay.h
TestsTab.qml
     1.1 --- a/MarqueeText.qml	Wed Jun 04 08:12:37 2014 +0200
     1.2 +++ b/MarqueeText.qml	Wed Jun 04 18:30:37 2014 +0200
     1.3 @@ -35,10 +35,11 @@
     1.4          //Reset to zero and restart onStopped so that we keep looping
     1.5          //onStopped: {scrollingText.x=0;running=true;}
     1.6  
     1.7 -        property int lengthInPixels:scrollingText.width-separatorText.width-scrollingText.x;
     1.8 +        property int lengthInPixels:(scrollingText.width+separatorText.width)-scrollingText.x;
     1.9          property int durationInMs:(animation.lengthInPixels)*1000/pixelsPerSeconds;
    1.10  
    1.11          NumberAnimation {
    1.12 +            id: animationScrollingTest
    1.13              target: scrollingText;
    1.14              properties: "x"
    1.15              from: scrollingText.x;
    1.16 @@ -68,22 +69,23 @@
    1.17      MouseArea {
    1.18          id:mouseArea
    1.19          anchors.fill: parent
    1.20 -        onClicked: {animation.running=!animation.running;
    1.21 -            /*
    1.22 +        onClicked: {
    1.23 +            //animation.running=!animation.running;
    1.24 +
    1.25              if (!animation.running) {
    1.26                  animation.start();
    1.27                  return;
    1.28              }
    1.29              if (animation.paused)
    1.30              {
    1.31 -                console.log("resume")
    1.32 -                animation.resume;
    1.33 +                //console.log("resume")
    1.34 +                animation.resume();
    1.35              }
    1.36              else
    1.37              {
    1.38 -                console.log("pause")
    1.39 -                animation.pause;
    1.40 -            }*/
    1.41 +                //console.log("pause")
    1.42 +                animation.pause();
    1.43 +            }
    1.44          }
    1.45      }
    1.46  }
     2.1 --- a/MiniDisplay/minidisplay.cpp	Wed Jun 04 08:12:37 2014 +0200
     2.2 +++ b/MiniDisplay/minidisplay.cpp	Wed Jun 04 18:30:37 2014 +0200
     2.3 @@ -5,7 +5,12 @@
     2.4  const int KMaxReadAttempt=100;
     2.5  
     2.6  MiniDisplay::MiniDisplay(QQuickItem *parent):
     2.7 -    QQuickItem(parent),iReadAttempt(0),iBrightness(iDisplay.MaxBrightness())
     2.8 +    QQuickItem(parent),
     2.9 +    iReadAttempt(0),
    2.10 +    iBrightness(iDisplay.MaxBrightness()),
    2.11 +    iAfterAnimatingCallback(NULL),
    2.12 +    iWindow(NULL),
    2.13 +    iFrameCount(0)
    2.14  {
    2.15      // By default, QQuickItem does not draw anything. If you subclass
    2.16      // QQuickItem to create a visual item, you will need to uncomment the
    2.17 @@ -53,6 +58,7 @@
    2.18          }
    2.19      }
    2.20      iDisplay.Close();
    2.21 +    disconnectRenderLoop();
    2.22      emit closed();
    2.23      emit statusChanged();
    2.24  }
    2.25 @@ -201,10 +207,15 @@
    2.26  
    2.27  
    2.28  /**
    2.29 - * @brief MiniDisplay::connectWindow
    2.30 - * @param aWindow
    2.31 +Connect to the render loop of the given window.
    2.32 +This is achieved using the afterAnimating event.
    2.33 +If any JavaScript callback is provided it will be called on every frame.
    2.34 +
    2.35 +@brief MiniDisplay::connectRenderLoop
    2.36 +@param aWindow The window to connect to.
    2.37 +@param aFunction An optional JavaScript callback function
    2.38   */
    2.39 -void MiniDisplay::connectWindow(QQuickWindow* aWindow, QJSValue aFunction)
    2.40 +void MiniDisplay::connectRenderLoop(QQuickWindow* aWindow, QJSValue aFunction)
    2.41  {
    2.42      QObject::connect(aWindow, SIGNAL(afterAnimating()),
    2.43                       this,  SLOT(onAfterAnimating()));
    2.44 @@ -214,11 +225,28 @@
    2.45  }
    2.46  
    2.47  /**
    2.48 - * @brief MiniDisplay::onAfterAnimating
    2.49 - */
    2.50 +
    2.51 +@brief MiniDisplay::disconnectRenderLoop
    2.52 +*/
    2.53 +void MiniDisplay::disconnectRenderLoop()
    2.54 +{
    2.55 +    if (iWindow)
    2.56 +    {
    2.57 +        QObject::disconnect(iWindow, SIGNAL(afterAnimating()),this,  SLOT(onAfterAnimating()));
    2.58 +        iWindow = NULL;
    2.59 +        iAfterAnimatingCallback = NULL;
    2.60 +        iFrameCount=0;
    2.61 +    }
    2.62 +
    2.63 +}
    2.64 +
    2.65 +/**
    2.66 +Once connected to a window render loop this is called on every frame.
    2.67 +
    2.68 +@brief MiniDisplay::onAfterAnimating
    2.69 +*/
    2.70  void MiniDisplay::onAfterAnimating()
    2.71  {
    2.72 -    static int frame=0;
    2.73      //qDebug() << frame << "MiniDisplay::onAfterAnimating";
    2.74      /*
    2.75      if (iAfterAnimatingCallback.isCallable())
    2.76 @@ -235,11 +263,11 @@
    2.77      {
    2.78          if (iAfterAnimatingCallback.isCallable())
    2.79          {
    2.80 -            iAfterAnimatingCallback.call(QJSValueList() << frame);
    2.81 +            iAfterAnimatingCallback.call(QJSValueList() << iFrameCount);
    2.82          }
    2.83      }
    2.84  
    2.85 -    frame++;
    2.86 +    iFrameCount++;
    2.87  }
    2.88  
    2.89  
     3.1 --- a/MiniDisplay/minidisplay.h	Wed Jun 04 08:12:37 2014 +0200
     3.2 +++ b/MiniDisplay/minidisplay.h	Wed Jun 04 18:30:37 2014 +0200
     3.3 @@ -40,7 +40,8 @@
     3.4      Q_INVOKABLE void renderWindow(QQuickWindow* aWindow);
     3.5      Q_INVOKABLE void renderImage(QImage* aImage);
     3.6      //
     3.7 -    Q_INVOKABLE void connectWindow(QQuickWindow* aWindow, QJSValue aFunction);
     3.8 +    Q_INVOKABLE void connectRenderLoop(QQuickWindow* aWindow, QJSValue aFunction);
     3.9 +    Q_INVOKABLE void disconnectRenderLoop();
    3.10      //
    3.11      Q_INVOKABLE void setPixel(int x, int y, bool on);
    3.12  
    3.13 @@ -105,6 +106,7 @@
    3.14      QPoint iFramePosition;
    3.15      QJSValue iAfterAnimatingCallback;
    3.16      QQuickWindow* iWindow;
    3.17 +    int iFrameCount;
    3.18  };
    3.19  
    3.20  #endif // MINIDISPLAY_H
     4.1 --- a/TestsTab.qml	Wed Jun 04 08:12:37 2014 +0200
     4.2 +++ b/TestsTab.qml	Wed Jun 04 18:30:37 2014 +0200
     4.3 @@ -57,8 +57,7 @@
     4.4              pixelsPerSeconds:50
     4.5              anchors.verticalCenter: parent.verticalCenter
     4.6              anchors.horizontalCenter: parent.horizontalCenter
     4.7 -            text: "start ------ abcdefghijklmnopqrtaksdjfkdfjklsdjflksdjfklsjadfkljsad;flasjdlfjasdfjldsdfljf---- end"
     4.8 -            //text: "start ------ end"
     4.9 +            text: "start ---- ABCDEFGHIJKLMNOPQRSTUVWXYZ ---- end"
    4.10              separator: " | "
    4.11          }
    4.12  
    4.13 @@ -116,9 +115,12 @@
    4.14  
    4.15          }
    4.16  
    4.17 -
    4.18 +        //This function is called from C++ afterAnimating.
    4.19 +        //It means it is called in sync with Qt render loop.
    4.20 +        //Qt render loop tries to run at 60 FPS.
    4.21          function doFrame(frameCount)
    4.22          {
    4.23 +            //Skip every second frame otherwise our UI lags.
    4.24              if (frameCount%2!=0)
    4.25              {
    4.26                  return;
    4.27 @@ -181,23 +183,9 @@
    4.28          }
    4.29  
    4.30  
    4.31 -        //! [timer]
    4.32 -        Timer {
    4.33 -            id: timer
    4.34 -            interval: frameWindow.timeoutInterval; running: false; repeat: true
    4.35 -            property bool doFill: true
    4.36 -            onTriggered: {
    4.37 -                //visible = false
    4.38 -                //frameWindow.doFrame();
    4.39 -
    4.40 -                //Signal our timeout
    4.41 -                //frameWindow.timeout();
    4.42 -            }
    4.43 -        }
    4.44 -        //! [timer]
    4.45          Component.onCompleted: {
    4.46              visible = true;
    4.47 -            display.connectWindow(frameWindow,doFrame);
    4.48 +            display.connectRenderLoop(frameWindow,doFrame);
    4.49          }
    4.50  
    4.51      }
    4.52 @@ -273,27 +261,5 @@
    4.53              checked: true
    4.54              onCheckedChanged: {display.frameDifferencing = checked;}
    4.55          }
    4.56 -
    4.57 -        //
    4.58 -        Rectangle {
    4.59 -            border.width: 1
    4.60 -            border.color: "black"
    4.61 -            color: "white"
    4.62 -            anchors.horizontalCenter: parent.horizontalCenter
    4.63 -            width: 250
    4.64 -            height: text.height + 10
    4.65 -            y:100
    4.66 -            MarqueeText {
    4.67 -                id:text
    4.68 -                width: 200
    4.69 -                pixelsPerSeconds:50
    4.70 -                anchors.verticalCenter: parent.verticalCenter
    4.71 -                anchors.horizontalCenter: parent.horizontalCenter
    4.72 -                text: "start ------ abcdefghijklmnopqrtaksdjfkdfjklsdjflksdjfklsjadfkljsad;flasjdlfjasdfjldsdfljf---- end"
    4.73 -                //text: "start ------ end"
    4.74 -                separator: " | "
    4.75 -            }
    4.76 -        }
    4.77 -
    4.78      }
    4.79  }