/usr/lib/python2.7/dist-packages/traitsui/handler.py is in python-traitsui 4.1.0-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | #------------------------------------------------------------------------------
#
# 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
#
#------------------------------------------------------------------------------
""" Defines the Handler class used to manage and control the editing process in
a Traits-based user interface.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from __future__ import absolute_import
from .toolkit import toolkit
from .help import on_help_call
from .view_element import ViewElement
from .helper import user_name_for
from .ui_info import UIInfo
from traits.api import HasPrivateTraits, HasTraits, Instance
#-------------------------------------------------------------------------------
# Closes a DockControl (if allowed by the associated traits UI Handler):
#-------------------------------------------------------------------------------
def close_dock_control ( dock_control ):
""" Closes a DockControl (if allowed by the associated Traits UI Handler).
"""
# Retrieve the traits UI object set when we created the DockControl:
ui = dock_control.data
# Ask the traits UI handler if it is OK to close the window:
if not ui.handler.close( ui.info, True ):
# If not, tell the DockWindow not to close it:
return False
# Otherwise, clean up and close the traits UI:
ui.dispose()
# And tell the DockWindow to remove the DockControl:
return True
#-------------------------------------------------------------------------------
# 'Handler' class:
#-------------------------------------------------------------------------------
class Handler ( HasPrivateTraits ):
""" Provides access to and control over the run-time workings of a
Traits-based user interface.
"""
#---------------------------------------------------------------------------
# Informs the handler what the UIInfo object for a View will be:
#---------------------------------------------------------------------------
def init_info ( self, info ):
""" Informs the handler what the UIInfo object for a View will be.
This method is called before the UI for the View has been
constructed. It is provided so that the handler can save the
reference to the UIInfo object in case it exposes viewable traits
whose values are properties that depend upon items in the context
being edited.
"""
pass
#---------------------------------------------------------------------------
# Initializes the controls of a user interface:
#---------------------------------------------------------------------------
def init ( self, info ):
""" Initializes the controls of a user interface.
Parameters
----------
info : UIInfo object
The UIInfo object associated with the view
Returns
-------
A Boolean, indicating whether the user interface was successfully
initialized. A True value indicates that the UI can be displayed;
a False value indicates that the display operation should be
cancelled. The default implementation returns True without taking
any other action.
Description
-----------
This method is called after all user interface elements have been
created, but before the user interface is displayed. Override this
method to customize the user interface before it is displayed.
"""
return True
#---------------------------------------------------------------------------
# Positions a dialog-based user interface on the display:
#---------------------------------------------------------------------------
def position ( self, info ):
""" Positions a dialog-based user interface on the display.
Parameters
----------
info : UIInfo object
The UIInfo object associated with the window
Returns
-------
Nothing.
Description
-----------
This method is called after the user interface is initialized (by
calling init()), but before the user interface is displayed. Override
this method to position the window on the display device. The default
implementation calls the position() method of the current toolkit.
Usually, you do not need to override this method, because you can
control the window's placement using the **x** and **y** attributes
of the View object.
"""
toolkit().position( info.ui )
#---------------------------------------------------------------------------
# Handles a request to close a dialog-based user interface by the user:
#---------------------------------------------------------------------------
def close ( self, info, is_ok ):
""" Handles the user attempting to close a dialog-based user interface.
Parameters
----------
info : UIInfo object
The UIInfo object associated with the view
is_ok : Boolean
Indicates whether the user confirmed the changes (such as by
clicking **OK**.)
Returns
-------
A Boolean, indicating whether the window should be allowed to close.
Description
-----------
This method is called when the user attempts to close a window, by
clicking an **OK** or **Cancel** button, or clicking a Close control
on the window). It is called before the window is actually destroyed.
Override this method to perform any checks before closing a window.
While Traits UI handles "OK" and "Cancel" events automatically, you
can use the value of the *is_ok* parameter to implement additional
behavior.
"""
return True
#---------------------------------------------------------------------------
# Handles a dialog-based user interface being closed by the user:
#---------------------------------------------------------------------------
def closed ( self, info, is_ok ):
""" Handles a dialog-based user interface being closed by the user.
Parameters
----------
info : UIInfo object
The UIInfo object associated with the view
is_ok : Boolean
Indicates whether the user confirmed the changes (such as by
clicking **OK**.)
Description
-----------
This method is called *after* the window is destroyed. Override this
method to perform any clean-up tasks needed by the application.
"""
return
#---------------------------------------------------------------------------
# Handles the 'Revert' button being clicked:
#---------------------------------------------------------------------------
def revert ( self, info ):
""" Handles the **Revert** button being clicked.
"""
return
#---------------------------------------------------------------------------
# Handles the 'Apply' button being clicked:
#---------------------------------------------------------------------------
def apply ( self, info ):
""" Handles the **Apply** button being clicked.
"""
return
#---------------------------------------------------------------------------
# Shows the help associated with the view:
#---------------------------------------------------------------------------
def show_help ( self, info, control = None ):
""" Shows the help associated with the view.
Parameters
----------
info : UIInfo object
The UIInfo object associated with the view
control : UI control
The control that invokes the help dialog box
Description
-----------
This method is called when the user clicks a **Help** button in a
Traits user interface. The method calls the global help handler, which
might be the default help handler, or might be a custom help handler.
See **traitsui.help** for details about the setting the
global help handler.
"""
if control is None:
control = info.ui.control
on_help_call()( info, control )
#---------------------------------------------------------------------------
# Handles setting a specified object trait's value:
#---------------------------------------------------------------------------
def setattr ( self, info, object, name, value ):
""" Handles the user setting a specified object trait's value.
Parameters
----------
object : object
The object whose attribute is being set
name : string
The name of the attribute being set
value
The value to which the attribute is being set
Description
-----------
This method is called when an editor attempts to set a new value for
a specified object trait attribute. Use this method to control what
happens when a trait editor tries to set an attribute value. For
example, you can use this method to record a history of changes, in
order to implement an "undo" mechanism. No result is returned. The
default implementation simply calls the built-in setattr() function.
If you override this method, make sure that it actually sets the
attribute, either by calling the parent method or by setting the
attribute directly
"""
setattr( object, name, value )
#---------------------------------------------------------------------------
# Gets a specified View object:
#---------------------------------------------------------------------------
def trait_view_for ( self, info, view, object, object_name, trait_name ):
""" Gets a specified View object.
"""
# If a view element was passed instead of a name or None, return it:
if isinstance( view, ViewElement ):
return view
# Generate a series of possible view or method names of the form:
# - 'view'
# trait_view_for_'view'( object )
# - 'class_view'
# trait_view_for_'class_view'( object )
# - 'object_name_view'
# trait_view_for_'object_name_view'( object )
# - 'object_name_class_view'
# trait_view_for_'object_name_class_view'( object )
# where 'class' is the class name of 'object', 'object' is the object
# name, and 'name' is the trait name. It returns the first view
# or method result which is defined on the handler:
klass = object.__class__.__name__
cname = '%s_%s' % ( object_name, trait_name )
aview = ''
if view:
aview = '_' + view
names = [ '%s_%s%s' % ( cname, klass, aview ),
'%s%s' % ( cname, aview ),
'%s%s' % ( klass, aview ) ]
if view:
names.append( view )
for name in names:
result = self.trait_view( name )
if result is not None:
return result
method = getattr( self, 'trait_view_for_%s' % name, None )
if callable( method ):
result = method( info, object )
if result is not None:
return result
# If nothing is defined on the handler, return either the requested
# view on the object itself, or the object's default view:
return object.trait_view( view ) or object.trait_view()
#-- 'DockWindowHandler' interface implementation -------------------------------
#---------------------------------------------------------------------------
# Returns whether or not a specified object can be inserted into the view:
#---------------------------------------------------------------------------
def can_drop ( self, info, object ):
""" Can the specified object be inserted into the view?
"""
from pyface.dock.api import DockControl
if isinstance( object, DockControl ):
return self.can_import( info, object.export )
drop_class = info.ui.view.drop_class
return ((drop_class is not None) and
isinstance( object, drop_class ))
#---------------------------------------------------------------------------
# Returns whether or not a specified external view category can be
# imported:
#---------------------------------------------------------------------------
def can_import ( self, info, category ):
return (category in info.ui.view.imports)
#---------------------------------------------------------------------------
# Returns the DockControl object for a specified object:
#---------------------------------------------------------------------------
def dock_control_for ( self, info, parent, object ):
""" Returns the DockControl object for a specified object.
"""
from pyface.dock.api import IDockable, DockControl
from .dockable_view_element import DockableViewElement
try:
name = object.name
except:
try:
name = object.label
except:
name = ''
if len( name ) == 0:
name = user_name_for( object.__class__.__name__ )
image = None
export = ''
if isinstance( object, DockControl ):
dock_control = object
image = dock_control.image
export = dock_control.export
dockable = dock_control.dockable
close = dockable.dockable_should_close()
if close:
dock_control.close( force = True )
control = dockable.dockable_get_control( parent )
# If DockControl was closed, then reset it to point to the new
# control:
if close:
dock_control.set( control = control,
style = parent.owner.style )
dockable.dockable_init_dockcontrol( dock_control )
return dock_control
elif isinstance( object, IDockable ):
dockable = object
control = dockable.dockable_get_control( parent )
else:
ui = object.get_dockable_ui( parent )
dockable = DockableViewElement( ui = ui )
export = ui.view.export
control = ui.control
dc = DockControl( control = control,
name = name,
export = export,
style = parent.owner.style,
image = image,
closeable = True )
dockable.dockable_init_dockcontrol( dc )
return dc
#---------------------------------------------------------------------------
# Creates a new view of a specified control:
#---------------------------------------------------------------------------
def open_view_for ( self, control, use_mouse = True ):
""" Creates a new view of a specified control.
"""
from pyface.dock.api import DockWindowShell
DockWindowShell( control, use_mouse = use_mouse )
#---------------------------------------------------------------------------
# Handles a DockWindow becoming empty:
#---------------------------------------------------------------------------
def dock_window_empty ( self, dock_window ):
""" Handles a DockWindow becoming empty.
"""
if dock_window.auto_close:
dock_window.control.GetParent.Destroy()
#-- HasTraits overrides: -------------------------------------------------------
#---------------------------------------------------------------------------
# Edits the object's traits: (Overrides HasTraits)
#---------------------------------------------------------------------------
def edit_traits ( self, view = None, parent = None, kind = None,
context = None, handler = None, id = '',
scrollable = None, **args ):
""" Edits the object's traits.
"""
if context is None:
context = self
if handler is None:
handler = self
return self.trait_view( view ).ui( context, parent, kind,
self.trait_view_elements(), handler, id, scrollable, args )
#---------------------------------------------------------------------------
# Configure the object's traits (Overrides HasTraits):
#---------------------------------------------------------------------------
def configure_traits ( self, filename = None, view = None,
kind = None, edit = True,
context = None, handler = None,
id = '', scrollable = None, **args ):
""" Configures the object's traits.
"""
super( HasPrivateTraits, self ).configure_traits(
filename, view, kind, edit, context, handler or self, id,
scrollable, **args )
#-- Private Methods: -----------------------------------------------------------
#---------------------------------------------------------------------------
# Handles an 'Undo' change request:
#---------------------------------------------------------------------------
def _on_undo ( self, info ):
""" Handles an "Undo" change request.
"""
if info.ui.history is not None:
info.ui.history.undo()
#---------------------------------------------------------------------------
# Handles a 'Redo' change request:
#---------------------------------------------------------------------------
def _on_redo ( self, info ):
""" Handles a "Redo" change request.
"""
if info.ui.history is not None:
info.ui.history.redo()
#---------------------------------------------------------------------------
# Handles a 'Revert' all changes request:
#---------------------------------------------------------------------------
def _on_revert ( self, info ):
""" Handles a "Revert all changes" request.
"""
if info.ui.history is not None:
info.ui.history.revert()
self.revert( info )
#---------------------------------------------------------------------------
# Handles a 'Close' request:
#---------------------------------------------------------------------------
def _on_close ( self, info ):
""" Handles a "Close" request.
"""
if (info.ui.owner is not None) and self.close( info, True ):
info.ui.owner.close()
#-------------------------------------------------------------------------------
# Default handler:
#-------------------------------------------------------------------------------
_default_handler = Handler()
def default_handler ( handler = None ):
""" Returns the global default handler.
If *handler* is an instance of Handler, this function sets it as the
global default handler.
"""
global _default_handler
if isinstance( handler, Handler ):
_default_handler = handler
return _default_handler
#-------------------------------------------------------------------------------
# 'Controller' class:
#-------------------------------------------------------------------------------
class Controller ( Handler ):
""" Defines a handler class which provides a view and controller for a
specified model.
This class is used when implementing a standard MVC-based design. The
**model** trait contains most, if not all, of the data being viewed,
and can be referenced in a Controller instance's View definition using
unadorned trait names. (e.g., ``Item('name')``).
"""
#-- Trait Definitions ------------------------------------------------------
# The model this handler defines a view and controller for
model = Instance( HasTraits )
# The Info object associated with the controller
info = Instance( UIInfo )
#-- HasTraits Method Overrides ---------------------------------------------
def __init__ ( self, model = None, **metadata ):
""" Initializes the object and sets the model (if supplied).
"""
super( Controller, self ).__init__( **metadata )
self.model = model
def trait_context ( self ):
""" Returns the default context to use for editing or configuring
traits.
"""
return { 'object': self.model, 'controller': self, 'handler': self }
#-- Handler Method Overrides -----------------------------------------------
#---------------------------------------------------------------------------
# Informs the handler what the UIInfo object for a View will be:
#---------------------------------------------------------------------------
def init_info ( self, info ):
""" Informs the handler what the UIInfo object for a View will be.
"""
self.info = info
#-------------------------------------------------------------------------------
# 'ModelView' class:
#-------------------------------------------------------------------------------
class ModelView ( Controller ):
""" Defines a handler class which provides a view and controller for a
specified model.
This class is useful when creating a variant of the standard MVC-based
design. A subclass of ModelView reformulates a number of traits on
its **model** object as properties on the ModelView subclass itself,
usually in order to convert them into a more user-friendly format. In
this design, the ModelView subclass supplies not only the view and
the controller, but also, in effect, the model (as a set of properties
wrapped around the original model). Because of this, the ModelView
context dictionary specifies the ModelView instance itself as the
special *object* value, and assigns the original model object as the
*model* value. Thus, the traits of the ModelView object can be referenced
in its View definition using unadorned trait names.
"""
#-- HasTraits Method Overrides ---------------------------------------------
def trait_context ( self ):
""" Returns the default context to use for editing or configuring
traits.
"""
return { 'object': self, 'handler': self, 'model': self.model }
#-------------------------------------------------------------------------------
# 'ViewHandler' class:
#-------------------------------------------------------------------------------
class ViewHandler ( Handler ):
pass
|