1 /****************************************************************************
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function getArgumentValue(name, names, values, from)
26 var i = (from == undefined) ? 0 : from;
27 for ( ; i < names.length; ++i) {
38 function MyWebPlugin(formUrl, scriptUrl, parent)
40 QWidget.call(this, parent);
42 this.initialized = false;
43 this.formReply = this.downloadFile(formUrl, this.formDownloaded);
44 if (scriptUrl == undefined)
47 this.scriptReply = this.downloadFile(scriptUrl, this.scriptDownloaded);
50 MyWebPlugin.prototype = new QWidget();
52 MyWebPlugin.prototype.downloadFile = function(url, callback)
54 if (this.accessManager == undefined)
55 this.accessManager = new QNetworkAccessManager();
56 var reply = this.accessManager.get(new QNetworkRequest(url));
57 reply.finished.connect(this, callback);
61 MyWebPlugin.prototype.formDownloaded = function()
63 var loader = new QUiLoader();
64 this.form = loader.load(this.formReply);
65 var layout = new QVBoxLayout(this);
66 layout.addWidget(this.form, 0, Qt.AlignCenter);
70 MyWebPlugin.prototype.scriptDownloaded = function()
72 var stream = new QTextStream(this.scriptReply);
73 this.script = stream.readAll();
77 MyWebPlugin.prototype.initialize = function()
81 if ((this.form == undefined) || (this.script == undefined))
83 var ctor = eval(this.script);
84 if (typeof ctor != "function")
86 this.instance = new ctor(this.form);
87 this.initialized = true;
92 // QWebPluginFactory subclass
95 function MyWebPluginFactory(parent)
97 QWebPluginFactory.call(this, parent);
100 MyWebPluginFactory.prototype = new QWebPluginFactory();
102 MyWebPluginFactory.prototype.create = function(mimeType, url, argumentNames, argumentValues)
104 if (mimeType != "application/x-qtform")
107 var formUrl = getArgumentValue("form", argumentNames, argumentValues);
108 var scriptUrl = getArgumentValue("script", argumentNames, argumentValues);
109 if (formUrl == undefined)
112 return new MyWebPlugin(new QUrl(formUrl), new QUrl(scriptUrl));
120 var view = new QWebView();
121 view.settings().setAttribute(QWebSettings.PluginsEnabled, true);
123 var factory = new MyWebPluginFactory();
124 view.page().setPluginFactory(factory);
126 view.load(new QUrl("WebKitPlugins.html"));
129 QCoreApplication.exec();