File: trait_grid_cell_adapter.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (179 lines) | stat: -rw-r--r-- 6,579 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
# 
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
# 
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" Adapter class to make trait editor controls work inside of a grid. """

# Major package imports
import wx
import wx.grid as wxgrid

wx_28 = (float( wx.__version__[:3] ) >= 2.8)

# Enthought library imports
from enthought.traits.api import false
from enthought.traits.ui.api import default_handler
from enthought.traits.ui.ui import UI

class TraitGridCellAdapter(wxgrid.PyGridCellEditor):
    """ Wrap a trait editor as a GridCellEditor object. """
    
    def __init__(self, trait_editor_factory, obj, name, description,
                 handler = None, context = None):
        """ Build a new TraitGridCellAdapter object. """

        wxgrid.PyGridCellEditor.__init__(self)
        self._factory = trait_editor_factory
        self._editor = None
        self._obj = obj
        self._name = name
        self._description = description
        self._handler = handler
        self._context = context
        return
        
    def Create(self, parent, id, evtHandler):
        """ Called to create the control, which must derive from wxControl. """

        if self._handler is not None:
            handler = self._handler
        else:
            handler = default_handler()

        if self._context is None:
            ui = UI(handler = handler)
        else:
            context = self._context.copy()
            context['table_editor_object'] = context['object']
            context['object'] = self._obj
            ui = UI(handler = handler, context = context)
            
        # make sure the factory knows this is a grid_cell editor
        self._factory.is_grid_cell = True
        self._editor = self._factory.simple_editor(ui,
                                                   self._obj,
                                                   self._name,
                                                   self._description,
                                                   parent)
                                 
        # Tell the editor to actually build the editing widget:
        self._editor.prepare(parent)
        self.SetControl(self._editor.control)

        if evtHandler:
            self._editor.control.PushEventHandler(evtHandler)

        return
    
    def SetSize(self, rect):
        """ Called to position/size the edit control within the cell rectangle.
            If you don't fill the cell (the rect) then be sure to override
            PaintBackground and do something meaningful there.
        """
        #print 'TraitGridCellAdapter.SetSize'
        self._editor.control.SetDimensions(rect.x, rect.y,
                                           rect.width+2, rect.height+2,
                                           wx.SIZE_ALLOW_MINUS_ONE)
        return

    def Show(self, show, attr):
        """ Show or hide the edit control.  You can use the attr (if not None)
            to set colours or fonts for the control.
        """
        #print 'TraitGridCellAdapter.Show'
        if self.IsCreated():
            if wx_28:
                super(TraitGridCellAdapter, self).Show(show, attr)
            else:
                self.base_Show(show, attr)

        return

    def PaintBackground(self, rect, attr):
        """ Draws the part of the cell not occupied by the edit control.  The
            base  class version just fills it with background colour from the
            attribute.  In this class the edit control fills the whole cell so
            don't do anything at all in order to reduce flicker.
        """
        #print 'TraitGridCellAdapter.PaintBackground'
        return self.base_PaintBackground(rect, attr)

    def BeginEdit(self, row, col, grid):
        """ Make sure the control is ready to edit. """

        # print 'TraitGridCellAdapter.BeginEdit'

        # we have to manually set the focus to the control
        #print 'TraitGridCellAdapter.BeginEdit'
        self._editor.update_editor()
        self._editor.control.SetFocus()
        return 

    def EndEdit(self, row, col, grid):
        """ Do anything necessary to complete the editing. """

        #print 'TraitGridCellAdapter.EndEdit'

        # Note that we shouldn't have to do anything here, since the underlying
        # trait editor should take care of setting the value correctly
        #print 'TraitGridCellAdapter.EndEdit'
        return True

    def Reset(self):
        """ Reset the value in the control back to its starting value. """

        # fixme: should we be using the undo history here?
        return

    def IsAcceptedKey(self, evt):
        """ Return True to allow the given key to start editing: the base class
            version only checks that the event has no modifiers.  F2 is special
            and will always start the editor.
        """
        return wxgrid.PyGridCellEditor.base_IsAcceptedKey(self, evt)

    def StartingKey(self, evt):
        """ If the editor is enabled by pressing keys on the grid, this will be
            called to let the editor do something about that first key
            if desired.
        """
        #print 'TraitGridCellAdapter.StartingKey'
        return

    def StartingClick(self):
        """ If the editor is enabled by clicking on the cell, this method
            will be called to allow the editor to simulate the click on the
            control if needed.
        """
        return

    def Destroy(self):
        """ Final cleanup. """
        print 'TraitGridCellAdapter.Destroy'
        self._editor.dispose()
        return

    def Clone(self):
        """ Create a new object which is the copy of this one. """
        #print 'TraitGridCellAdapter.Clone'

        return TraitGridCellAdapter(self._factory, self._obj, self._name,
                                    self._description)

    def dispose(self):
        if self._editor is not None:
            self._editor.dispose()
        return


#### EOF ######################################################################