MiniDisplay/minidisplay.cpp
changeset 33 cf5eba52cb1d
parent 32 1c2a7f563019
     1.1 --- a/MiniDisplay/minidisplay.cpp	Wed Jun 04 08:12:37 2014 +0200
     1.2 +++ b/MiniDisplay/minidisplay.cpp	Wed Jun 04 18:30:37 2014 +0200
     1.3 @@ -5,7 +5,12 @@
     1.4  const int KMaxReadAttempt=100;
     1.5  
     1.6  MiniDisplay::MiniDisplay(QQuickItem *parent):
     1.7 -    QQuickItem(parent),iReadAttempt(0),iBrightness(iDisplay.MaxBrightness())
     1.8 +    QQuickItem(parent),
     1.9 +    iReadAttempt(0),
    1.10 +    iBrightness(iDisplay.MaxBrightness()),
    1.11 +    iAfterAnimatingCallback(NULL),
    1.12 +    iWindow(NULL),
    1.13 +    iFrameCount(0)
    1.14  {
    1.15      // By default, QQuickItem does not draw anything. If you subclass
    1.16      // QQuickItem to create a visual item, you will need to uncomment the
    1.17 @@ -53,6 +58,7 @@
    1.18          }
    1.19      }
    1.20      iDisplay.Close();
    1.21 +    disconnectRenderLoop();
    1.22      emit closed();
    1.23      emit statusChanged();
    1.24  }
    1.25 @@ -201,10 +207,15 @@
    1.26  
    1.27  
    1.28  /**
    1.29 - * @brief MiniDisplay::connectWindow
    1.30 - * @param aWindow
    1.31 +Connect to the render loop of the given window.
    1.32 +This is achieved using the afterAnimating event.
    1.33 +If any JavaScript callback is provided it will be called on every frame.
    1.34 +
    1.35 +@brief MiniDisplay::connectRenderLoop
    1.36 +@param aWindow The window to connect to.
    1.37 +@param aFunction An optional JavaScript callback function
    1.38   */
    1.39 -void MiniDisplay::connectWindow(QQuickWindow* aWindow, QJSValue aFunction)
    1.40 +void MiniDisplay::connectRenderLoop(QQuickWindow* aWindow, QJSValue aFunction)
    1.41  {
    1.42      QObject::connect(aWindow, SIGNAL(afterAnimating()),
    1.43                       this,  SLOT(onAfterAnimating()));
    1.44 @@ -214,11 +225,28 @@
    1.45  }
    1.46  
    1.47  /**
    1.48 - * @brief MiniDisplay::onAfterAnimating
    1.49 - */
    1.50 +
    1.51 +@brief MiniDisplay::disconnectRenderLoop
    1.52 +*/
    1.53 +void MiniDisplay::disconnectRenderLoop()
    1.54 +{
    1.55 +    if (iWindow)
    1.56 +    {
    1.57 +        QObject::disconnect(iWindow, SIGNAL(afterAnimating()),this,  SLOT(onAfterAnimating()));
    1.58 +        iWindow = NULL;
    1.59 +        iAfterAnimatingCallback = NULL;
    1.60 +        iFrameCount=0;
    1.61 +    }
    1.62 +
    1.63 +}
    1.64 +
    1.65 +/**
    1.66 +Once connected to a window render loop this is called on every frame.
    1.67 +
    1.68 +@brief MiniDisplay::onAfterAnimating
    1.69 +*/
    1.70  void MiniDisplay::onAfterAnimating()
    1.71  {
    1.72 -    static int frame=0;
    1.73      //qDebug() << frame << "MiniDisplay::onAfterAnimating";
    1.74      /*
    1.75      if (iAfterAnimatingCallback.isCallable())
    1.76 @@ -235,11 +263,11 @@
    1.77      {
    1.78          if (iAfterAnimatingCallback.isCallable())
    1.79          {
    1.80 -            iAfterAnimatingCallback.call(QJSValueList() << frame);
    1.81 +            iAfterAnimatingCallback.call(QJSValueList() << iFrameCount);
    1.82          }
    1.83      }
    1.84  
    1.85 -    frame++;
    1.86 +    iFrameCount++;
    1.87  }
    1.88  
    1.89