Created
February 10, 2012 09:33
-
-
Save schuster-rainer/1788031 to your computer and use it in GitHub Desktop.
QML viewer using PySide to display a qml or html embedding a qml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sys | |
from PySide import QtCore, QtGui, QtDeclarative | |
import argparse | |
from PySide.QtWebKit import QWebPluginFactory, QWebView, QWebSettings | |
class PluginFactory(QWebPluginFactory): | |
def plugins(self): | |
plugins = [] | |
qmlmime = self.MimeType() | |
qmlmime.name = 'QmlFile' | |
qmlmime.fileExtensions = ['.qml'] | |
qmlplugin = self.Plugin() | |
qmlplugin.name = 'QmlPlugin' | |
qmlplugin.mimeTypes = [qmlmime] | |
print "adding QmlPlugin" | |
plugins.append(qmlplugin) | |
return plugins | |
def create(self, mimeType, url, argumentNames, argumentValues): | |
print mimeType, url | |
if mimeType != 'application/x-qml': | |
return None | |
for name, value in zip(argumentNames, argumentValues): | |
if name == 'width': | |
width = int(value) | |
elif name == 'height': | |
height = int(value) | |
view = QtDeclarative.QDeclarativeView() | |
view.resize(width, height) | |
print url | |
view.setSource(url) | |
return view | |
def html_view(args, mainModule): | |
view = QWebView() | |
fac = PluginFactory() | |
view.page().setPluginFactory(fac) | |
QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True) | |
view.load(QtCore.QUrl(args.src)) | |
return view | |
#return app.exec_() | |
def parse_arguments(): | |
parser = argparse.ArgumentParser(description='Creates a QDeclarativeView set its source. It imports the corresponding python module and calls the init(view) function', | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('src', | |
default='./main.qml', | |
help='path to qml file') | |
parser.add_argument('--module', | |
help='set the module with a function init(view)') | |
parser.add_argument('--width', | |
type=int, default='800', | |
help='width of the window') | |
parser.add_argument('--height', | |
type=int, default='640', | |
help='height of the window') | |
parser.add_argument('--title', | |
default='MainView', | |
help='title for the window') | |
parser.add_argument('--fullscreen', action='store_true', | |
help='show view in fullscreen') | |
parser.add_argument('--printpath', action='store_true', | |
help='print sys.path') | |
parser.add_argument('--icon', help='use the icon as window icon') | |
parser.add_argument('--resize', dest='resizeMode', | |
default='1', | |
type=int, | |
help='''0 = The view resizes with the root item in the QML; 1 = The view will automatically resize the root item to the size of the view''') | |
return parser.parse_args() | |
def qml_view(args, mainModule): | |
# Renders 'view.qml' | |
view = QtDeclarative.QDeclarativeView() | |
if mainModule and hasattr(mainModule, "initContext"): | |
print "callining initContext" | |
mainModule.initContext(view) | |
view.setSource(QtCore.QUrl.fromLocalFile(args.src)) | |
resizeMode = QtDeclarative.QDeclarativeView.ResizeMode(args.resizeMode) | |
view.setResizeMode(resizeMode) | |
def apply_arguments(view, args, mainModule): | |
if args.title: | |
view.setWindowTitle(args.title) | |
if args.width and args.height: | |
#view.setAttribute("width", str(args.width)) | |
view.resize(args.width, args.height) | |
if args.icon: | |
view.setWindowIcon(QtGui.QIcon(args.icon)) | |
if args.width and args.height: | |
#view.setAttribute("width", str(args.width)) | |
view.resize(args.width, args.height) | |
if args.module and mainModule: | |
if hasattr(mainModule, "init"): | |
mainModule.init(view) | |
if args.fullscreen: | |
view.showFullScreen() | |
def main(): | |
args = parse_arguments() | |
from pprint import pprint | |
if args.printpath: | |
pprint(sys.path) | |
import re | |
#html = re.search( r'(\w*).(html|xml)', args.src) | |
qml = re.search( r'(\w*).qml', args.src) | |
mainModule = None | |
if args.module: | |
try: | |
mainModule = __import__( args.module, globals(), locals()) | |
except Exception as e: | |
print e | |
view = None | |
if qml: | |
app = QtGui.QApplication(sys.argv) | |
view = qml_view(args, mainModule) | |
else: | |
app = QtGui.QApplication([]) | |
view = QWebView() | |
fac = PluginFactory() | |
view.page().setPluginFactory(fac) | |
QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True) | |
view.load(QtCore.QUrl(args.src)) | |
#view = html_view(args, mainModule) | |
view.show() | |
apply_arguments(view, args, mainModule) | |
# Run the main Qt loop | |
return app.exec_() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment