~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to traitsui/qt4/editor_factory.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 described in the PyQt GPL exception also apply
 
7
 
 
8
#
 
9
# Author: Riverbank Computing Limited
 
10
#------------------------------------------------------------------------------
 
11
 
 
12
""" Defines the base PyQt classes the various styles of editors used in a
 
13
Traits-based user interface.
 
14
"""
 
15
 
 
16
#-------------------------------------------------------------------------------
 
17
#  Imports:
 
18
#-------------------------------------------------------------------------------
 
19
 
 
20
from pyface.qt import QtCore, QtGui
 
21
 
 
22
from traits.api \
 
23
    import TraitError
 
24
 
 
25
from traitsui.editor_factory \
 
26
    import EditorFactory as BaseEditorFactory
 
27
 
 
28
from editor \
 
29
    import Editor
 
30
 
 
31
#-------------------------------------------------------------------------------
 
32
#  'EditorFactory' class
 
33
#   Deprecated alias for traitsui.editor_factory.EditorFactory
 
34
#-------------------------------------------------------------------------------
 
35
 
 
36
class EditorFactory(BaseEditorFactory):
 
37
    """ Deprecated alias for traitsui.editor_factory.EditorFactory.
 
38
    """
 
39
 
 
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)
 
44
 
 
45
#-------------------------------------------------------------------------------
 
46
#  'SimpleEditor' class:
 
47
#-------------------------------------------------------------------------------
 
48
 
 
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
 
53
    value.
 
54
    """
 
55
    #---------------------------------------------------------------------------
 
56
    #  Finishes initializing the editor by creating the underlying toolkit
 
57
    #  widget:
 
58
    #---------------------------------------------------------------------------
 
59
 
 
60
    def init ( self, parent ):
 
61
        """ Finishes initializing the editor by creating the underlying toolkit
 
62
            widget.
 
63
        """
 
64
        self.control = _SimpleField(self)
 
65
        self.set_tooltip()
 
66
 
 
67
    #---------------------------------------------------------------------------
 
68
    #  Invokes the pop-up editor for an object trait:
 
69
    #
 
70
    #  (Normally overridden in a subclass)
 
71
    #---------------------------------------------------------------------------
 
72
 
 
73
    def popup_editor(self):
 
74
        """ Invokes the pop-up editor for an object trait.
 
75
        """
 
76
        pass
 
77
 
 
78
#-------------------------------------------------------------------------------
 
79
#  'TextEditor' class:
 
80
#-------------------------------------------------------------------------------
 
81
 
 
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.
 
85
    """
 
86
    #---------------------------------------------------------------------------
 
87
    #  Finishes initializing the editor by creating the underlying toolkit
 
88
    #  widget:
 
89
    #---------------------------------------------------------------------------
 
90
 
 
91
    def init ( self, parent ):
 
92
        """ Finishes initializing the editor by creating the underlying toolkit
 
93
            widget.
 
94
        """
 
95
        self.control = QtGui.QLineEdit(self.str_value)
 
96
        QtCore.QObject.connect(self.control,
 
97
                QtCore.SIGNAL('editingFinished()'), self.update_object)
 
98
        self.set_tooltip()
 
99
 
 
100
    #---------------------------------------------------------------------------
 
101
    #  Handles the user changing the contents of the edit control:
 
102
    #---------------------------------------------------------------------------
 
103
 
 
104
    def update_object(self):
 
105
        """ Handles the user changing the contents of the edit control.
 
106
        """
 
107
        try:
 
108
            self.value = unicode(self.control.text())
 
109
        except TraitError, excp:
 
110
            pass
 
111
 
 
112
#-------------------------------------------------------------------------------
 
113
#  'ReadonlyEditor' class:
 
114
#-------------------------------------------------------------------------------
 
115
 
 
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.
 
119
    """
 
120
    #---------------------------------------------------------------------------
 
121
    #  Finishes initializing the editor by creating the underlying toolkit
 
122
    #  widget:
 
123
    #---------------------------------------------------------------------------
 
124
 
 
125
    def init ( self, parent ):
 
126
        """ Finishes initializing the editor by creating the underlying toolkit
 
127
            widget.
 
128
        """
 
129
        self.control = QtGui.QLabel(self.str_value)
 
130
 
 
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)
 
135
 
 
136
        self.set_tooltip()
 
137
 
 
138
    #---------------------------------------------------------------------------
 
139
    #  Updates the editor when the object trait changes external to the editor:
 
140
    #---------------------------------------------------------------------------
 
141
 
 
142
    def update_editor ( self ):
 
143
        """ Updates the editor when the object trait changes externally to the
 
144
            editor.
 
145
        """
 
146
        self.control.setText(self.str_value)
 
147
 
 
148
#-------------------------------------------------------------------------------
 
149
#  '_SimpleField' class:
 
150
#-------------------------------------------------------------------------------
 
151
 
 
152
class _SimpleField(QtGui.QLineEdit):
 
153
 
 
154
    def __init__(self, editor):
 
155
        QtGui.QLineEdit.__init__(self, editor.str_value)
 
156
 
 
157
        self.setReadOnly(True)
 
158
        self._editor = editor
 
159
 
 
160
    def mouseReleaseEvent(self, e):
 
161
        QtGui.QLineEdit.mouseReleaseEvent(self, e)
 
162
 
 
163
        if e.button() == QtCore.Qt.LeftButton:
 
164
            self._editor.popup_editor()