summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNo'am Rosenthal <noam.rosenthal@nokia.com>2011-08-09 15:47:21 -0700
committerNo'am Rosenthal <noam.rosenthal@nokia.com>2011-08-09 15:47:21 -0700
commitde80f36191c4c2d538af722083d8460d8603a16b (patch)
treee6a91a91042b26c1845717797b39da397705350d /examples
parent78abcf07d8fa6a66478354533719b418995999b3 (diff)
downloadqtwebchannel-de80f36191c4c2d538af722083d8460d8603a16b.tar.gz
QObject example
Diffstat (limited to 'examples')
-rw-r--r--examples/qtobject/qml/qtobject/index.html85
-rw-r--r--examples/qtobject/qml/qtobject/main.qml8
-rw-r--r--examples/qtobject/qml/qtobject/qtobject.js107
3 files changed, 120 insertions, 80 deletions
diff --git a/examples/qtobject/qml/qtobject/index.html b/examples/qtobject/qml/qtobject/index.html
index a86d3c2..b177522 100644
--- a/examples/qtobject/qml/qtobject/index.html
+++ b/examples/qtobject/qml/qtobject/index.html
@@ -2,88 +2,21 @@
<head>
<script>
document.write('<script src="' + (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/]+)/.exec(location.search)[1]) + '/webchannel.js/createWebChannel"><' + '/script>');
- function output(x)
+ window.output = function(x)
{
- document.querySelector("#out").innerHTML += x + "<br/>";
+ document.querySelector("#out").innerHTML += x + "\n";
}
</script>
+ <script src="qtobject.js"></script>
<script>
- function createObjectProxy(webChannel, objectName, data)
- {
- var object = {_qtSignals: []};
- var methodsAndSignals = [];
- for (var i = 0; i < data.methods.length; ++i)
- methodsAndSignals.push(data.methods[i]);
- for (var i = 0; i < data.signals.length; ++i)
- methodsAndSignals.push(data.signals[i]);
- methodsAndSignals.forEach(function(method) {
- object[method] = function(args, callback) {
- webChannel.exec(JSON.stringify({type: "Qt.invokeMethod", object: objectName, method: method, args: args}), function(response) {
- (callback)(JSON.parse(response));
- });
- };
- });
- data.signals.forEach(function(signal) {
- object[signal].connect = function() {
- webChannel.exec(JSON.stringify({type: "Qt.connectToSignal", signal: signal}));
- var func, target;
- for (var i = 0; i < arguments; ++i) {
- if (typeof arguments[i] == "function" || typeof arguments[i] == "string")
- func = arguments[i];
- else if (typeof arguments[i] == "object")
- target = arguments[i];
- }
- if (typeof func == "string") {
- if (!target)
- return;
- func = target[func];
- }
- if (typeof func != "function")
- return;
- object["_qtSignals"].push({name: signal, target: target, func: func});
- };
- });
-
- data.properties.forEach(function(prop) {
- object.__defineSetter__(prop, function(value) {
- webChannel.exec(JSON.stringify({type: "Qt.setProperty", object: objectName, property: prop, value: value }));
- });
- object.__defineGetter__(prop, function() {
- return (function(callback) {
- webChannel.exec(JSON.stringify({type: "Qt.getProperty", object: objectName, property: prop}), function(response) {
- callback(JSON.parse(response));
- });
- });
- });
- });
- return object;
- }
- window.onload = function() {
- createWebChannel(function(webChannel) {
- webChannel.subscribe(
- "Qt.signal",
- function(payload) {
- alert(1);
- output(payload);
- }
- );
- webChannel.subscribe(
- "Qt.addObject",
- function(payload) {
- var addObjectData = JSON.parse(payload);
- alert(addObjectData.name);
- window[addObjectData.name] = createObjectProxy(webChannel, addObjectData.name, addObjectData.data);
- }
- );
- webChannel.exec(JSON.stringify({type:"Qt.getObjects"}));
- });
- };
</script>
</head>
<body>
- <a href="javascript:testObject.debugMe('Debugging!')">invoke method</a>
- <a href="javascript:testObject.prop1(function(value) { output(value); })">Get property</a>
- <a href="javascript:testObject.prop1 = 'Different property'; testObject.prop1(function(value) { output(value); })">Set property</a>
- <div id="out"></div>
+ <a href="#" onclick="testObject.debugMe('Debugging!')">invoke method</a>
+ <a href="#" onclick="testObject.prop1(function(value) { output(value); })">Get property</a>
+ <a href="#" onclick="testObject.prop1 = 'Different property'; testObject.prop1(function(value) { output(value); })">Set property</a>
+ <a href="#" onclick="testObject.timeout.connect(function() { output('timeout'); }); testObject.startTimer(1000);">Timer</a>
+ <br/>
+ <textarea id="out" style="height:80%; width: 80%"></textarea>
</body>
</html>
diff --git a/examples/qtobject/qml/qtobject/main.qml b/examples/qtobject/qml/qtobject/main.qml
index 603df6a..c18d693 100644
--- a/examples/qtobject/qml/qtobject/main.qml
+++ b/examples/qtobject/qml/qtobject/main.qml
@@ -65,7 +65,7 @@ Rectangle {
} else if (payload.type == "Qt.connectToSignal") {
object[payload.signal].connect(
function(a,b,c,d,e,f,g,h,i,j) {
- broadcast("Qt.signal", JSON.stringify({object: payload.object, signal: payload.signal, arguments: [a,b,c,d,e,f,g,h,i,j]}));
+ broadcast("Qt.signal", JSON.stringify({object: payload.object, signal: payload.signal, args: [a,b,c,d,e,f,g,h,i,j]}));
});
} else if (payload.type == "Qt.getProperty") {
ret = object[payload.property];
@@ -75,8 +75,8 @@ Rectangle {
var objectNames = publisher.objectNames;
for (var i = 0; i < objectNames.length; ++i) {
var name = objectNames[i];
- var object = publisher.namedObject(name);
- addObject(name, object);
+ var o = publisher.namedObject(name);
+ addObject(name, o);
}
}
@@ -87,7 +87,7 @@ Rectangle {
{
publisher.addObject(name, object);
var metaData = { name: name, data: publisher.classInfoForObject(object) };
- broadcast("Qt.addObject", JSON.stringify(metaData));
+ broadcast("Qt.addToWindowObject", JSON.stringify(metaData));
}
onBaseUrlChanged: addObject("testObject", testObject)
diff --git a/examples/qtobject/qml/qtobject/qtobject.js b/examples/qtobject/qml/qtobject/qtobject.js
new file mode 100644
index 0000000..0545737
--- /dev/null
+++ b/examples/qtobject/qml/qtobject/qtobject.js
@@ -0,0 +1,107 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QWebChannel module on Qt labs.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+window.onload = function() {
+ createWebChannel(function(webChannel) {
+ var allSignals = {};
+ webChannel.subscribe(
+ "Qt.signal",
+ function(payload) {
+ var signalData = JSON.parse(payload);
+ var object = allSignals[signalData.object];
+ var conns = (object ? object[signalData.signal] : []) || [];
+ var a = payload.args;
+ conns.forEach(function(callback) {
+ callback.call(a);
+ });
+ }
+ );
+ webChannel.subscribe(
+ "Qt.addToWindowObject",
+ function(payload) {
+ var addObjectData = JSON.parse(payload);
+ var objectSignals = {};
+ var methodsAndSignals = [];
+ var object = {};
+ var objectName = addObjectData.name;
+ var data = addObjectData.data;
+ for (var i = 0; i < data.methods.length; ++i)
+ methodsAndSignals.push(data.methods[i]);
+ for (i = 0; i < data.signals.length; ++i)
+ methodsAndSignals.push(data.signals[i]);
+
+ methodsAndSignals.forEach(function(method) {
+ object[method] = function(args, callback) {
+ webChannel.exec(JSON.stringify({type: "Qt.invokeMethod", object: objectName, method: method, args: args}), function(response) {
+ if (response.length)
+ (callback)(JSON.parse(response));
+ });
+ };
+ });
+
+ data.signals.forEach(function(signal) {
+ object[signal].connect = function(callback) {
+ objectSignals[signal] = objectSignals[signal] || [];
+ webChannel.exec(JSON.stringify({type: "Qt.connectToSignal", object: objectName, signal: signal}));
+ objectSignals[signal].push(callback);
+ };
+ });
+ allSignals[addObjectData.name] = objectSignals;
+
+ data.properties.forEach(function(prop) {
+ object.__defineSetter__(prop, function(value) {
+ webChannel.exec(JSON.stringify({type: "Qt.setProperty", object: objectName, property: prop, value: value }));
+ });
+ object.__defineGetter__(prop, function() {
+ return (function(callback) {
+ webChannel.exec(JSON.stringify({type: "Qt.getProperty", object: objectName, property: prop}), function(response) {
+ callback(JSON.parse(response));
+ });
+ });
+ });
+ });
+
+ window[addObjectData.name] = object;
+ }
+ );
+ webChannel.exec(JSON.stringify({type:"Qt.getObjects"}));
+ });
+};