1
#------------------------------------------------------------------------------
2
# Copyright (c) 2007, Riverbank Computing Limited
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 described in the PyQt GPL exception also apply
9
# Author: Riverbank Computing Limited
10
#------------------------------------------------------------------------------
12
""" Defines the base PyQt classes the various styles of editors used in a
13
Traits-based user interface.
16
#-------------------------------------------------------------------------------
18
#-------------------------------------------------------------------------------
20
from pyface.qt import QtCore, QtGui
25
from traitsui.editor_factory \
26
import EditorFactory as BaseEditorFactory
31
#-------------------------------------------------------------------------------
32
# 'EditorFactory' class
33
# Deprecated alias for traitsui.editor_factory.EditorFactory
34
#-------------------------------------------------------------------------------
36
class EditorFactory(BaseEditorFactory):
37
""" Deprecated alias for traitsui.editor_factory.EditorFactory.
40
def __init__(self, *args, **kwds):
41
super(EditorFactory, self).__init__(*args, **kwds)
42
warnings.warn("DEPRECATED: Use traitsui.editor_factory."
43
".EditorFactory instead.", DeprecationWarning)
45
#-------------------------------------------------------------------------------
46
# 'SimpleEditor' class:
47
#-------------------------------------------------------------------------------
49
class SimpleEditor ( Editor ):
50
""" Base class for simple style editors, which displays a text field
51
containing the text representation of the object trait value. Clicking in
52
the text field displays an editor-specific dialog box for changing the
55
#---------------------------------------------------------------------------
56
# Finishes initializing the editor by creating the underlying toolkit
58
#---------------------------------------------------------------------------
60
def init ( self, parent ):
61
""" Finishes initializing the editor by creating the underlying toolkit
64
self.control = _SimpleField(self)
67
#---------------------------------------------------------------------------
68
# Invokes the pop-up editor for an object trait:
70
# (Normally overridden in a subclass)
71
#---------------------------------------------------------------------------
73
def popup_editor(self):
74
""" Invokes the pop-up editor for an object trait.
78
#-------------------------------------------------------------------------------
80
#-------------------------------------------------------------------------------
82
class TextEditor ( Editor ):
83
""" Base class for text style editors, which displays an editable text
84
field, containing a text representation of the object trait value.
86
#---------------------------------------------------------------------------
87
# Finishes initializing the editor by creating the underlying toolkit
89
#---------------------------------------------------------------------------
91
def init ( self, parent ):
92
""" Finishes initializing the editor by creating the underlying toolkit
95
self.control = QtGui.QLineEdit(self.str_value)
96
QtCore.QObject.connect(self.control,
97
QtCore.SIGNAL('editingFinished()'), self.update_object)
100
#---------------------------------------------------------------------------
101
# Handles the user changing the contents of the edit control:
102
#---------------------------------------------------------------------------
104
def update_object(self):
105
""" Handles the user changing the contents of the edit control.
108
self.value = unicode(self.control.text())
109
except TraitError, excp:
112
#-------------------------------------------------------------------------------
113
# 'ReadonlyEditor' class:
114
#-------------------------------------------------------------------------------
116
class ReadonlyEditor ( Editor ):
117
""" Base class for read-only style editors, which displays a read-only text
118
field, containing a text representation of the object trait value.
120
#---------------------------------------------------------------------------
121
# Finishes initializing the editor by creating the underlying toolkit
123
#---------------------------------------------------------------------------
125
def init ( self, parent ):
126
""" Finishes initializing the editor by creating the underlying toolkit
129
self.control = QtGui.QLabel(self.str_value)
131
if self.item.resizable is True or self.item.height != -1.0:
132
self.control.setSizePolicy(QtGui.QSizePolicy.Expanding,
133
QtGui.QSizePolicy.Expanding)
134
self.control.setWordWrap(True)
138
#---------------------------------------------------------------------------
139
# Updates the editor when the object trait changes external to the editor:
140
#---------------------------------------------------------------------------
142
def update_editor ( self ):
143
""" Updates the editor when the object trait changes externally to the
146
self.control.setText(self.str_value)
148
#-------------------------------------------------------------------------------
149
# '_SimpleField' class:
150
#-------------------------------------------------------------------------------
152
class _SimpleField(QtGui.QLineEdit):
154
def __init__(self, editor):
155
QtGui.QLineEdit.__init__(self, editor.str_value)
157
self.setReadOnly(True)
158
self._editor = editor
160
def mouseReleaseEvent(self, e):
161
QtGui.QLineEdit.mouseReleaseEvent(self, e)
163
if e.button() == QtCore.Qt.LeftButton:
164
self._editor.popup_editor()