1
# ------------------------------------------------------------------------------
2
#
3
#  Copyright (c) 2009, Enthought, Inc.
4
#  All rights reserved.
5
#
6
#  This software is provided without warranty under the terms of the BSD
7
#  license included in LICENSE.txt and may be redistributed only
8
#  under the conditions described in the aforementioned license.  The license
9
#  is also available online at http://www.enthought.com/licenses/BSD.txt
10
#
11
#  Thanks for using Enthought open source!
12
#
13
# ------------------------------------------------------------------------------
14

15 8
""" Defines the HTML "editor" for the QT4 user interface toolkit.
16
    HTML editors interpret and display HTML-formatted text, but do not
17
    modify it.
18
"""
19

20

21 8
import webbrowser
22

23 8
from pyface.qt import QtCore, QtGui, QtWebKit
24

25 8
from traits.api import Str
26

27 8
from .editor import Editor
28

29
# -------------------------------------------------------------------------
30
#  'SimpleEditor' class:
31
# -------------------------------------------------------------------------
32

33

34 8
class SimpleEditor(Editor):
35
    """ Simple style editor for HTML.
36
    """
37

38
    # -------------------------------------------------------------------------
39
    #  Trait definitions:
40
    # -------------------------------------------------------------------------
41

42
    #: Is the HTML editor scrollable? This values override the default.
43 8
    scrollable = True
44

45
    #: External objects referenced in the HTML are relative to this URL
46 8
    base_url = Str()
47

48 8
    def init(self, parent):
49
        """ Finishes initializing the editor by creating the underlying toolkit
50
            widget.
51
        """
52 8
        self.control = QtWebKit.QWebView()
53 8
        self.control.setSizePolicy(
54
            QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding
55
        )
56

57 11
        if self.factory.open_externally:
58 0
            page = self.control.page()
59 0
            page.setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateAllLinks)
60 0
            page.linkClicked.connect(self._link_clicked)
61

62 8
        self.base_url = self.factory.base_url
63 8
        self.sync_value(self.factory.base_url_name, "base_url", "from")
64

65 8
    def dispose(self):
66
        """ Disposes of the contents of an editor.
67
        """
68 11
        if self.control is not None and self.factory.open_externally:
69 0
            page = self.control.page()
70 0
            page.linkClicked.disconnect(self._link_clicked)
71 8
        super().dispose()
72

73 8
    def update_editor(self):
74
        """ Updates the editor when the object trait changes external to the
75
            editor.
76
        """
77 8
        text = self.str_value
78 11
        if self.factory.format_text:
79 8
            text = self.factory.parse_text(text)
80 11
        if self.base_url:
81 0
            url = self.base_url
82 11
            if not url.endswith("/"):
83 0
                url += "/"
84 0
            self.control.setHtml(text, QtCore.QUrl.fromLocalFile(url))
85
        else:
86 8
            self.control.setHtml(text)
87

88
    # -- Event Handlers -------------------------------------------------------
89

90 8
    def _base_url_changed(self):
91 0
        self.update_editor()
92

93 8
    def _link_clicked(self, url):
94 0
        webbrowser.open_new(url.toString())

Read our documentation on viewing source code .

Loading