1
# ------------------------------------------------------------------------------
2
# Copyright (c) 2007, Riverbank Computing Limited
3
# All rights reserved.
4
#
5
# This software is provided without warranty under the terms of the BSD license.
6
# However, when used with the GPL version of PyQt the additional terms
7
# described in the PyQt GPL exception also apply
8

9
#
10
# Author: Riverbank Computing Limited
11
# ------------------------------------------------------------------------------
12

13 8
""" Defines the base PyQt classes the various styles of editors used in a
14
Traits-based user interface.
15
"""
16

17

18 8
from pyface.qt import QtCore, QtGui
19

20 8
from traits.api import TraitError
21

22 8
from traitsui.editor_factory import EditorFactory as BaseEditorFactory
23

24 8
from .editor import Editor
25

26

27
# -------------------------------------------------------------------------
28
#  'EditorFactory' class
29
#   Deprecated alias for traitsui.editor_factory.EditorFactory
30
# -------------------------------------------------------------------------
31

32

33 8
class EditorFactory(BaseEditorFactory):
34
    """ Deprecated alias for traitsui.editor_factory.EditorFactory.
35
    """
36

37 8
    def __init__(self, *args, **kwds):
38 0
        super(EditorFactory, self).__init__(*args, **kwds)
39 0
        warnings.warn(
40
            "DEPRECATED: Use traitsui.editor_factory."
41
            ".EditorFactory instead.",
42
            DeprecationWarning,
43
        )
44

45

46 8
class SimpleEditor(Editor):
47
    """ Base class for simple style editors, which displays a text field
48
    containing the text representation of the object trait value. Clicking in
49
    the text field displays an editor-specific dialog box for changing the
50
    value.
51
    """
52

53 8
    def init(self, parent):
54
        """ Finishes initializing the editor by creating the underlying toolkit
55
            widget.
56
        """
57 0
        self.control = _SimpleField(self)
58 0
        self.set_tooltip()
59

60
    # -------------------------------------------------------------------------
61
    #  Invokes the pop-up editor for an object trait:
62
    #
63
    #  (Normally overridden in a subclass)
64
    # -------------------------------------------------------------------------
65

66 8
    def popup_editor(self):
67
        """ Invokes the pop-up editor for an object trait.
68
        """
69 0
        pass
70

71

72 8
class TextEditor(Editor):
73
    """ Base class for text style editors, which displays an editable text
74
    field, containing a text representation of the object trait value.
75
    """
76

77 8
    def init(self, parent):
78
        """ Finishes initializing the editor by creating the underlying toolkit
79
            widget.
80
        """
81 8
        self.control = QtGui.QLineEdit(self.str_value)
82 8
        self.control.editingFinished.connect(self.update_object)
83 8
        self.set_tooltip()
84

85 8
    def dispose(self):
86
        """ Disposes of the contents of an editor.
87
        """
88 11
        if self.control is not None:
89 8
            self.control.editingFinished.disconnect(self.update_object)
90 8
        super(TextEditor, self).dispose()
91

92 8
    def update_object(self):
93
        """ Handles the user changing the contents of the edit control.
94
        """
95 11
        if self.control is None:
96 0
            return
97 0
        try:
98 0
            self.value = str(self.control.text())
99 0
        except TraitError as excp:
100 0
            pass
101

102

103 8
class ReadonlyEditor(Editor):
104
    """ Base class for read-only style editors, which displays a read-only text
105
    field, containing a text representation of the object trait value.
106
    """
107

108
    # -------------------------------------------------------------------------
109
    #  Finishes initializing the editor by creating the underlying toolkit
110
    #  widget:
111
    # -------------------------------------------------------------------------
112

113 8
    text_alignment_map = {
114
        "left": QtCore.Qt.AlignLeft,
115
        "right": QtCore.Qt.AlignRight,
116
        "just": QtCore.Qt.AlignJustify,
117
        "top": QtCore.Qt.AlignLeft,
118
        "bottom": QtCore.Qt.AlignBottom,
119
        "vcenter": QtCore.Qt.AlignVCenter,
120
        "hcenter": QtCore.Qt.AlignHCenter,
121
        "center": QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter,
122
    }
123

124 8
    def init(self, parent):
125
        """ Finishes initializing the editor by creating the underlying toolkit
126
            widget.
127
        """
128 8
        self.control = QtGui.QLabel(self.str_value)
129

130 11
        if self.item.resizable is True or self.item.height != -1.0:
131 0
            self.control.setSizePolicy(
132
                QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding
133
            )
134 0
            self.control.setWordWrap(True)
135

136 8
        alignment = None
137 11
        for item in self.factory.text_alignment.split(","):
138 8
            item_alignment = self.text_alignment_map.get(item, None)
139 11
            if item_alignment:
140 11
                if alignment:
141 0
                    alignment = alignment | item_alignment
142
                else:
143 0
                    alignment = item_alignment
144

145 11
        if alignment:
146 0
            self.control.setAlignment(alignment)
147

148 8
        self.set_tooltip()
149

150 8
    def update_editor(self):
151
        """ Updates the editor when the object trait changes externally to the
152
            editor.
153
        """
154 8
        self.control.setText(self.str_value)
155

156

157 8
class _SimpleField(QtGui.QLineEdit):
158 8
    def __init__(self, editor):
159 0
        QtGui.QLineEdit.__init__(self, editor.str_value)
160

161 0
        self.setReadOnly(True)
162 0
        self._editor = editor
163

164 8
    def mouseReleaseEvent(self, e):
165 0
        QtGui.QLineEdit.mouseReleaseEvent(self, e)
166

167 11
        if e.button() == QtCore.Qt.LeftButton:
168 0
            self._editor.popup_editor()

Read our documentation on viewing source code .

Loading