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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
#------------------------------------------------------------------------------
# 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: David C. Morrill
# Date: 10/07/2004
#
# Symbols defined: Editor
#------------------------------------------------------------------------------
""" Defines the abstract Editor class, which represents an editing control for
an object trait in a Traits-based user interface.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from enthought.traits.api \
import Trait, HasPrivateTraits, ReadOnly, Any, Property, Undefined, true, \
false, TraitError, Str, Instance
from editor_factory \
import EditorFactory
from undo \
import UndoItem
from item \
import Item
#-------------------------------------------------------------------------------
# Trait definitions:
#-------------------------------------------------------------------------------
# Reference to an EditorFactory object
factory_trait = Trait( EditorFactory )
#-------------------------------------------------------------------------------
# 'Editor' abstract base class:
#-------------------------------------------------------------------------------
class Editor ( HasPrivateTraits ):
""" Represents an editing control for an object trait in a Traits-based
user interface.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The UI (user interface) this editor is part of
ui = ReadOnly
# The object this editor is editing
object = ReadOnly
# The name of the trait this editor is editing
name = ReadOnly
# Original value of object.name
old_value = ReadOnly
# Text description of the object trait being edited
description = ReadOnly
# The Item object used to create this editor
item = Instance( Item, (), allow_none = False )
# Context name of object editor is editing
object_name = Str( 'object' )
# The GUI widget defined by this editor
control = Any
# The GUI label (if any) defined by this editor
label_control = Any
# Is the underlying GUI widget enabled?
enabled = true
# Is the underlying GUI widget visible?
visible = true
# Is the underlying GUI widget scrollable?
scrollable = false
# The EditorFactory used to create this editor:
factory = factory_trait
# Is the editor updating the object.name value?
updating = false
# Current value for object.name
value = Property
# Current value of object trait as a string
str_value = Property
#---------------------------------------------------------------------------
# Initializes the object:
#---------------------------------------------------------------------------
def __init__ ( self, parent, **traits ):
""" Initializes the editor object.
"""
HasPrivateTraits.__init__( self, **traits )
try:
self.old_value = getattr( self.object, self.name )
except AttributeError:
# Getting the attribute will fail for 'Event' traits:
self.old_value = Undefined
#---------------------------------------------------------------------------
# Finishes editor set-up:
#---------------------------------------------------------------------------
def prepare ( self, parent ):
""" Finishes setting up the editor.
"""
self.object.on_trait_change( self._update_editor, self.name,
dispatch = 'ui' )
self.init( parent )
self.update_editor()
#---------------------------------------------------------------------------
# Finishes initializing the editor by creating the underlying toolkit
# widget:
#---------------------------------------------------------------------------
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
raise NotImplementedError
#---------------------------------------------------------------------------
# Disposes of the contents of an editor:
#---------------------------------------------------------------------------
def dispose ( self ):
""" Disposes of the contents of an editor.
"""
self.object.on_trait_change( self._update_editor, self.name,
remove = True )
if self._user_from is not None:
for name, value in self._user_from.items():
user_object, user_name, editor_name, key, is_list = value
self.on_trait_change( self._editor_trait_modified,
editor_name, remove = True )
if is_list:
self.on_trait_change( self._editor_list_modified,
editor_name + '_items', remove = True )
if self._user_to is not None:
for name, value in self._user_to.items():
user_object, user_name, editor_name, key, is_list = value
user_object.on_trait_change( self._user_trait_modified,
user_name, remove = True )
if is_list:
user_object.on_trait_change( self._user_list_modified,
user_name + '_items', remove = True )
#---------------------------------------------------------------------------
# Gets/Sets the associated object trait's value:
#---------------------------------------------------------------------------
def _get_value ( self ):
return getattr( self.object, self.name )
def _set_value ( self, value ):
self.ui.do_undoable( self.__set_value, value )
def __set_value ( self, value ):
self._no_update = True
try:
try:
handler = self.ui.handler
obj_name = self.object_name
method = (getattr( handler, '%s_%s_setattr' % ( obj_name,
self.name ), None ) or
getattr( handler, '%s_setattr' % obj_name, None ) or
getattr( handler, 'setattr' ))
method( self.ui.info, self.object, self.name, value )
except TraitError, excp:
self.error( excp )
raise
finally:
self._no_update = False
#---------------------------------------------------------------------------
# Returns the text representation of a specified object trait value:
#---------------------------------------------------------------------------
def string_value ( self, value ):
""" Returns the text representation of a specified object trait value.
If the **format_func** attribute is set on the editor factory, then
this method calls that function to do the formatting. If the
**format_str** attribute is set on the editor factory, then this
method uses that string for formatting. If neither attribute is
set, then this method just calls the built-in str() function.
"""
factory = self.factory
if factory.format_func is not None:
return factory.format_func( value )
if factory.format_str != '':
return factory.format_str % value
return str( value )
#---------------------------------------------------------------------------
# Returns the text representation of the object trait:
#---------------------------------------------------------------------------
def _get_str_value ( self ):
""" Returns the text representation of the object trait.
"""
return self.string_value( getattr( self.object, self.name ) )
#---------------------------------------------------------------------------
# Returns the text representation of a specified value:
#---------------------------------------------------------------------------
def _str ( self, value ):
""" Returns the text representation of a specified value.
"""
return str( value )
#---------------------------------------------------------------------------
# Handles an error that occurs while setting the object's trait value:
#
# (Should normally be overridden in a subclass)
#---------------------------------------------------------------------------
def error ( self, excp ):
""" Handles an error that occurs while setting the object's trait value.
"""
pass
#---------------------------------------------------------------------------
# Performs updates when the object trait changes:
#---------------------------------------------------------------------------
def _update_editor ( self, object, name, old_value, new_value ):
""" Performs updates when the object trait changes.
"""
# If the editor has gone away for some reason, disconnect and exit:
if self.control is None:
object.on_trait_change( self._update_editor, name, remove = True )
return
# Log the change that was made (as long as it is not for an event):
if object.base_trait( name ).type != 'event':
self.log_change( self.get_undo_item, object, name,
old_value, new_value )
# If the change was not caused by the editor itself:
if not self._no_update:
# Update the editor control to reflect the current object state:
self.update_editor()
#---------------------------------------------------------------------------
# Logs a change made in the editor:
#---------------------------------------------------------------------------
def log_change ( self, undo_factory, *undo_args ):
""" Logs a change made in the editor.
"""
# Indicate that the contents of the user interface have been changed:
ui = self.ui
ui.modified = True
# Create an undo history entry if we are maintaining a history:
undoable = ui._undoable
if undoable >= 0:
history = ui.history
if history is not None:
item = undo_factory( *undo_args )
if item is not None:
if undoable == history.now:
# Create a new undo transaction:
history.add( item )
else:
# Extend the most recent undo transaction:
history.extend( item )
#---------------------------------------------------------------------------
# Updates the editor when the object trait changes external to the editor:
#
# (Should normally be overridden in a subclass)
#---------------------------------------------------------------------------
def update_editor ( self ):
""" Updates the editor when the object trait changes externally to the
editor.
"""
pass
#---------------------------------------------------------------------------
# Creates an undo history entry:
#
# (Can be overridden in a subclass for special value types)
#---------------------------------------------------------------------------
def get_undo_item ( self, object, name, old_value, new_value ):
""" Creates an undo history entry.
"""
return UndoItem( object = object,
name = name,
old_value = old_value,
new_value = new_value )
#---------------------------------------------------------------------------
# Sets/Unsets synchronization between an editor trait and a user object
# trait:
#---------------------------------------------------------------------------
def sync_value ( self, user_name, editor_name,
mode = 'both', is_list = False, remove = False ):
""" Sets or unsets synchronization between an editor trait and a user
object trait.
"""
if user_name != '':
if self._no_trait_update is None:
self._no_trait_update = {}
object_name = 'object'
col = user_name.find( '.' )
if col >= 0:
object_name = user_name[ : col ]
user_name = user_name[ col + 1: ]
user_object = self.ui.context[ object_name ]
value = ( user_object, user_name, editor_name,
'%s:%s' % ( user_name, editor_name ), is_list )
if mode in ( 'from', 'both' ):
user_object.on_trait_change( self._user_trait_modified,
user_name, dispatch = 'ui' )
if is_list:
user_object.on_trait_change( self._user_list_modified,
user_name + '_items', dispatch = 'ui' )
if self._user_to is None:
self._user_to = {}
self._user_to[ user_name ] = value
if mode == 'from':
setattr( self, editor_name,
getattr( user_object, user_name ) )
if mode in ( 'to', 'both' ):
self.on_trait_change( self._editor_trait_modified, editor_name,
dispatch = 'ui' )
if is_list:
self.on_trait_change( self._editor_list_modified,
editor_name + '_items', dispatch = 'ui' )
if self._user_from is None:
self._user_from = {}
self._user_from[ editor_name ] = value
if mode == 'to':
setattr( user_object, user_name,
getattr( self, editor_name ) )
def _user_trait_modified ( self, object, name, old, new ):
user_object, user_name, editor_name, key, is_list = \
self._user_to[ name ]
if key not in self._no_trait_update:
self._no_trait_update[ key ] = None
try:
setattr( self, editor_name, new )
except:
pass
del self._no_trait_update[ key ]
def _user_list_modified ( self, object, name, old, event ):
user_object, user_name, editor_name, key, is_list = \
self._user_to[ name[:-6] ]
if key not in self._no_trait_update:
self._no_trait_update[ key ] = None
n = event.index
try:
getattr( self, editor_name )[ n: n + len( event.removed ) ] = \
event.added
except:
pass
del self._no_trait_update[ key ]
def _editor_trait_modified ( self, object, name, old, new ):
user_object, user_name, editor_name, key, is_list = \
self._user_from[ name ]
if key not in self._no_trait_update:
self._no_trait_update[ key ] = None
try:
setattr( user_object, user_name, new )
except:
pass
del self._no_trait_update[ key ]
def _editor_list_modified ( self, object, name, old, event ):
user_object, user_name, editor_name, key, is_list = \
self._user_from[ name[:-6] ]
if key not in self._no_trait_update:
self._no_trait_update[ key ] = None
n = event.index
try:
getattr( user_object, user_name )[
n: n + len( event.removed )] = event.added
except:
pass
del self._no_trait_update[ key ]
#-- UI preference save/restore interface ---------------------------------------
#---------------------------------------------------------------------------
# Restores any saved user preference information associated with the
# editor:
#---------------------------------------------------------------------------
def restore_prefs ( self, prefs ):
""" Restores any saved user preference information associated with the
editor.
"""
pass
#---------------------------------------------------------------------------
# Returns any user preference information associated with the editor:
#---------------------------------------------------------------------------
def save_prefs ( self ):
""" Returns any user preference information associated with the editor.
"""
return None
#-- End UI preference save/restore interface -----------------------------------
|