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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in 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!
""" Adapter class to make trait editor controls work inside of a grid. """
import wx
from wx.grid import GridCellEditor
from wx import SIZE_ALLOW_MINUS_ONE
def get_control(control):
if isinstance(control, wx.Control):
return control
for control in control.GetChildren():
result = get_control(control)
if result is not None:
return result
return None
class TraitGridCellAdapter(GridCellEditor):
""" Wrap a trait editor as a GridCellEditor object. """
def __init__(
self,
trait_editor_factory,
obj,
name,
description,
handler=None,
context=None,
style="simple",
width=-1.0,
height=-1.0,
):
""" Build a new TraitGridCellAdapter object. """
super().__init__()
self._factory = trait_editor_factory
self._style = style
self._width = width
self._height = height
self._editor = None
self._obj = obj
self._name = name
self._description = description
self._handler = handler
self._context = context
def Create(self, parent, id, evtHandler):
""" Called to create the control, which must derive from wxControl. """
from traitsui.api import UI, default_handler
# If the editor has already been created, ignore the request:
if hasattr(self, "_control"):
return
handler = self._handler
if handler is None:
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)
# Link the editor's undo history in to the main ui undo history if the
# UI object is available:
factory = self._factory
if factory._ui is not None:
ui.history = factory._ui.history
# make sure the factory knows this is a grid_cell editor
factory.is_grid_cell = True
factory_method = getattr(factory, self._style + "_editor")
self._editor = factory_method(
ui, self._obj, self._name, self._description, parent
)
# Tell the editor to actually build the editing widget:
self._editor.prepare(parent)
# Find the control to use as the editor:
self._control = control = self._editor.control
# Calculate and save the required editor height:
grid, row, col = getattr(self, "_grid_info", (None, None, None))
width, height = control.GetBestSize()
self_height = self._height
if self_height > 1.0:
height = int(self_height)
elif (self_height >= 0.0) and (grid is not None):
height = int(self_height * grid.GetSize().Get()[1])
self_width = self._width
if self_width > 1.0:
width = int(self_width)
elif (self_width >= 0.0) and (grid is not None):
width = int(self_width * grid.GetSize().Get()[0])
self._edit_width, self._edit_height = width, height
# Set up the first control found within the cell editor as the cell
# editor control:
control = get_control(control)
if control is not None:
self.SetControl(control)
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.
"""
changed = False
edit_width, edit_height = rect.width, rect.height
grid, row, col = getattr(self, "_grid_info", (None, None, None))
if (grid is not None) and self._editor.scrollable:
edit_width, cur_width = self._edit_width, grid.GetColSize(col)
restore_width = getattr(grid, "_restore_width", None)
if restore_width is not None:
cur_width = restore_width
if (edit_width > cur_width) or (restore_width is not None):
edit_width = max(edit_width, cur_width)
grid._restore_width = cur_width
grid.SetColSize(col, edit_width + 1 + (col == 0))
changed = True
else:
edit_width = cur_width
edit_height, cur_height = self._edit_height, grid.GetRowSize(row)
restore_height = getattr(grid, "_restore_height", None)
if restore_height is not None:
cur_height = restore_height
if (edit_height > cur_height) or (restore_height is not None):
edit_height = max(edit_height, cur_height)
grid._restore_height = cur_height
grid.SetRowSize(row, edit_height + 1 + (row == 0))
changed = True
else:
edit_height = cur_height
if changed:
grid.ForceRefresh()
self._control.SetSize(
rect.x + 1,
rect.y + 1,
edit_width,
edit_height,
SIZE_ALLOW_MINUS_ONE,
)
if changed:
grid.MakeCellVisible(
grid.GetGridCursorRow(), grid.GetGridCursorCol()
)
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.
"""
if self.IsCreated():
super().Show(show, attr)
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.
"""
def BeginEdit(self, row, col, grid):
""" Make sure the control is ready to edit. """
# We have to manually set the focus to the control
self._editor.update_editor()
control = self._control
control.Show(True)
control.SetFocus()
if isinstance(control, wx.TextCtrl):
control.SetSelection(-1, -1)
def EndEdit(self, row, col, grid):
""" Do anything necessary to complete the editing. """
self._control.Show(False)
changed = False
grid, row, col = self._grid_info
if grid._no_reset_col:
grid._no_reset_col = False
else:
width = getattr(grid, "_restore_width", None)
if width is not None:
del grid._restore_width
grid.SetColSize(col, width)
changed = True
if grid._no_reset_row:
grid._no_reset_row = False
else:
height = getattr(grid, "_restore_height", None)
if height is not None:
del grid._restore_height
grid.SetRowSize(row, height)
changed = True
if changed:
grid.ForceRefresh()
def Reset(self):
""" Reset the value in the control back to its starting value. """
# fixme: should we be using the undo history here?
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.
"""
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.
"""
def Destroy(self):
""" Final cleanup. """
self._editor.dispose()
def Clone(self):
""" Create a new object which is the copy of this one. """
return TraitGridCellAdapter(
self._factory,
self._obj,
self._name,
self._description,
style=self._style,
)
def dispose(self):
if self._editor is not None:
self._editor.dispose()
|