1
# -*- coding: utf-8 -*-
2
# See the file "LICENSE" for the full license governing this code.
3
import logging
4
logger = logging.getLogger(__name__)
5
logging.basicConfig(level=logging.DEBUG)
6
7
from PySide import QtCore, QtWebKit
8
9
10
class MxSignaler(QtCore.QObject):
11
    """Signal-slot mechanism between WebViews and Python backend.
12
    
13
    This JavaScript object proxy is an alternative for QML,
14
    this works on at least Qt 4.6 and can evaluateJavaScript.
15
    This is slower that it ought to be.
16
17
    QML would provide a better and simpler mechanism, is *faster*,
18
    and the JavaScript console.log would be connected readily to stdout.
19
    What is missing is a way to inject JavaScript into QDeclarativeWebView.
20
    !!! It may exist but is undocumented !!! :]
21
    """
22
    
23
    def __init__(self,webView):
24
        QtCore.QObject.__init__(self)
25
        self.webView = webView
26
    
27
    @QtCore.Slot()
28
    def addObjects(self):
29
        """Injects the signaler into the mainFrame."""
30
        logger.debug("adding JavaScript objects to page")
31
        page = self.webView.page()
32
        logger.debug(page)
33
        page.mainFrame().addToJavaScriptWindowObject("qml", self)
34
35
    # signal from remoteControl webView
36
    buttonPress = QtCore.Signal('QString')
37
    
38
    # slot for remoteControl webView
39
    @QtCore.Slot('QString')
40
    def selectState(self,state):
41
        """Updates the remote control UI by calling "window.selectState(state)" function."""
42
        logger.debug('select '+state)
43
        page = self.webView.page()
44
        page.mainFrame().evaluateJavaScript("window.selectState('%s');" % state)
45
46
    # slot for remoteControl webView
47
    @QtCore.Slot('QString')
48
    def unselectState(self,state):
49
        """Updates the remote control UI by calling "window.selectState(state)" function."""
50
        logger.debug('unselect '+state)
51
        page = self.webView.page()
52
        page.mainFrame().evaluateJavaScript("window.unselectState('%s');" % state)
53
54
    # slot for screen webView
55
    @QtCore.Slot('QString')
56
    def updateScreen(self,url):
57
        """Updates the image url by calling "window.updateScreen(url)" function."""
58
        logger.debug("updating screen with: "+url)
59
        page = self.webView.page()
60
        page.mainFrame().evaluateJavaScript("window.updateScreen('%s');" % url)