From 27faa381dc5dd6c5cc41a0826df35b71339d3e7e Mon Sep 17 00:00:00 2001 From: Chris Colbert Date: Mon, 19 Nov 2012 12:13:53 -0500 Subject: [PATCH] Add an initial WebView widget. --- enaml/qt/qt_factories.py | 6 ++++ enaml/qt/qt_web_view.py | 60 +++++++++++++++++++++++++++++++++++++++ enaml/widgets/api.py | 1 + enaml/widgets/web_view.py | 48 +++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 enaml/qt/qt_web_view.py create mode 100644 enaml/widgets/web_view.py diff --git a/enaml/qt/qt_factories.py b/enaml/qt/qt_factories.py index 912875fa..00073426 100644 --- a/enaml/qt/qt_factories.py +++ b/enaml/qt/qt_factories.py @@ -197,6 +197,11 @@ def tool_bar_factory(): return QtToolBar +def web_view_factory(): + from .qt_web_view import QtWebView + return QtWebView + + def window_factory(): from .qt_window import QtWindow return QtWindow @@ -242,6 +247,7 @@ def window_factory(): 'TextEditor': text_editor_factory, 'TimeSelector': time_selector_factory, 'ToolBar': tool_bar_factory, + 'WebView': web_view_factory, 'Window': window_factory, } diff --git a/enaml/qt/qt_web_view.py b/enaml/qt/qt_web_view.py new file mode 100644 index 00000000..c492c963 --- /dev/null +++ b/enaml/qt/qt_web_view.py @@ -0,0 +1,60 @@ +#------------------------------------------------------------------------------ +# Copyright (c) 2012, Enthought, Inc. +# All rights reserved. +#------------------------------------------------------------------------------ +from .qt.QtCore import QUrl +from .qt.QtWebKit import QWebView +from .qt_control import QtControl + + +class QtWebView(QtControl): + """ A Qt implementation of an Enaml WebView. + + """ + def create_widget(self, parent, tree): + """ Create the underlying QWebView control. + + """ + return QWebView(parent) + + def create(self, tree): + """ Create and initialize the underlying control. + + """ + super(QtWebView, self).create(tree) + html = tree['html'] + if html: + self.set_html(html) + else: + self.set_url(tree['url']) + + #-------------------------------------------------------------------------- + # Message Handling + #-------------------------------------------------------------------------- + def on_action_set_url(self, content): + """ Handle the 'set_url' action from the Enaml widget. + + """ + self.set_url(content['url']) + + def on_action_set_html(self, content): + """ Handle the 'set_html' action from the Enaml widget. + + """ + self.set_html(content['html']) + + #-------------------------------------------------------------------------- + # Widget Update Methods + #-------------------------------------------------------------------------- + def set_url(self, url): + """ Set the url for the underlying control. + + """ + self.widget().setUrl(QUrl(url)) + + def set_html(self, html): + """ Set the html source for the underlying control. + + """ + self.widget().setHtml(html, 'c:/') + diff --git a/enaml/widgets/api.py b/enaml/widgets/api.py index f576d2fd..290ce6d5 100644 --- a/enaml/widgets/api.py +++ b/enaml/widgets/api.py @@ -44,5 +44,6 @@ from .text_editor import TextEditor from .time_selector import TimeSelector from .tool_bar import ToolBar +from .web_view import WebView from .window import Window diff --git a/enaml/widgets/web_view.py b/enaml/widgets/web_view.py new file mode 100644 index 00000000..2464eeee --- /dev/null +++ b/enaml/widgets/web_view.py @@ -0,0 +1,48 @@ +#------------------------------------------------------------------------------ +# Copyright (c) 2012, Enthought, Inc. +# All rights reserved. +#------------------------------------------------------------------------------ +from traits.api import Unicode + +from .control import Control + + +class WebView(Control): + """ A widget which displays a web page. + + Unlike the simpler `Html` widget, this widget supports the features + of a full web browser. + + """ + #: The URL to load in the web view. This can be a path to a remote + #: resource or a path to a file on the local filesystem. This value + #: is mutually exclusive of `html`. + url = Unicode + + #: The html to load into the web view. This value is mutually + #: exclusive of `url`. + html = Unicode + + #: A web view expands freely in height and width by default. + hug_width = 'ignore' + hug_height = 'ignore' + + #-------------------------------------------------------------------------- + # Initialization + #-------------------------------------------------------------------------- + def snapshot(self): + """ Create the snapshot for the widget. + + """ + snap = super(WebView, self).snapshot() + snap['url'] = self.url + snap['html'] = self.html + return snap + + def bind(self): + """ Bind the change handlers for the widget. + + """ + super(WebView, self).bind() + self.publish_attributes('url', 'html') +