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

Commit

Permalink
Add an initial WebView widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Nov 19, 2012
1 parent 3ea3077 commit 27faa38
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
6 changes: 6 additions & 0 deletions enaml/qt/qt_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}

60 changes: 60 additions & 0 deletions enaml/qt/qt_web_view.py
Original file line number Diff line number Diff line change
@@ -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:/')

1 change: 1 addition & 0 deletions enaml/widgets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

48 changes: 48 additions & 0 deletions enaml/widgets/web_view.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 27faa38

Please sign in to comment.