Skip to content
This repository was archived by the owner on May 24, 2021. It is now read-only.

Commit 27faa38

Browse files
committed
Add an initial WebView widget.
1 parent 3ea3077 commit 27faa38

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

enaml/qt/qt_factories.py

+6
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ def tool_bar_factory():
197197
return QtToolBar
198198

199199

200+
def web_view_factory():
201+
from .qt_web_view import QtWebView
202+
return QtWebView
203+
204+
200205
def window_factory():
201206
from .qt_window import QtWindow
202207
return QtWindow
@@ -242,6 +247,7 @@ def window_factory():
242247
'TextEditor': text_editor_factory,
243248
'TimeSelector': time_selector_factory,
244249
'ToolBar': tool_bar_factory,
250+
'WebView': web_view_factory,
245251
'Window': window_factory,
246252
}
247253

enaml/qt/qt_web_view.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright (c) 2012, Enthought, Inc.
3+
# All rights reserved.
4+
#------------------------------------------------------------------------------
5+
from .qt.QtCore import QUrl
6+
from .qt.QtWebKit import QWebView
7+
from .qt_control import QtControl
8+
9+
10+
class QtWebView(QtControl):
11+
""" A Qt implementation of an Enaml WebView.
12+
13+
"""
14+
def create_widget(self, parent, tree):
15+
""" Create the underlying QWebView control.
16+
17+
"""
18+
return QWebView(parent)
19+
20+
def create(self, tree):
21+
""" Create and initialize the underlying control.
22+
23+
"""
24+
super(QtWebView, self).create(tree)
25+
html = tree['html']
26+
if html:
27+
self.set_html(html)
28+
else:
29+
self.set_url(tree['url'])
30+
31+
#--------------------------------------------------------------------------
32+
# Message Handling
33+
#--------------------------------------------------------------------------
34+
def on_action_set_url(self, content):
35+
""" Handle the 'set_url' action from the Enaml widget.
36+
37+
"""
38+
self.set_url(content['url'])
39+
40+
def on_action_set_html(self, content):
41+
""" Handle the 'set_html' action from the Enaml widget.
42+
43+
"""
44+
self.set_html(content['html'])
45+
46+
#--------------------------------------------------------------------------
47+
# Widget Update Methods
48+
#--------------------------------------------------------------------------
49+
def set_url(self, url):
50+
""" Set the url for the underlying control.
51+
52+
"""
53+
self.widget().setUrl(QUrl(url))
54+
55+
def set_html(self, html):
56+
""" Set the html source for the underlying control.
57+
58+
"""
59+
self.widget().setHtml(html, 'c:/')
60+

enaml/widgets/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@
4444
from .text_editor import TextEditor
4545
from .time_selector import TimeSelector
4646
from .tool_bar import ToolBar
47+
from .web_view import WebView
4748
from .window import Window
4849

enaml/widgets/web_view.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright (c) 2012, Enthought, Inc.
3+
# All rights reserved.
4+
#------------------------------------------------------------------------------
5+
from traits.api import Unicode
6+
7+
from .control import Control
8+
9+
10+
class WebView(Control):
11+
""" A widget which displays a web page.
12+
13+
Unlike the simpler `Html` widget, this widget supports the features
14+
of a full web browser.
15+
16+
"""
17+
#: The URL to load in the web view. This can be a path to a remote
18+
#: resource or a path to a file on the local filesystem. This value
19+
#: is mutually exclusive of `html`.
20+
url = Unicode
21+
22+
#: The html to load into the web view. This value is mutually
23+
#: exclusive of `url`.
24+
html = Unicode
25+
26+
#: A web view expands freely in height and width by default.
27+
hug_width = 'ignore'
28+
hug_height = 'ignore'
29+
30+
#--------------------------------------------------------------------------
31+
# Initialization
32+
#--------------------------------------------------------------------------
33+
def snapshot(self):
34+
""" Create the snapshot for the widget.
35+
36+
"""
37+
snap = super(WebView, self).snapshot()
38+
snap['url'] = self.url
39+
snap['html'] = self.html
40+
return snap
41+
42+
def bind(self):
43+
""" Bind the change handlers for the widget.
44+
45+
"""
46+
super(WebView, self).bind()
47+
self.publish_attributes('url', 'html')
48+

0 commit comments

Comments
 (0)