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
|
#------------------------------------------------------------------------------
# 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: Item
#------------------------------------------------------------------------------
"""Defines the Item class, which is used to represent a single item within
a Traits-based user interface.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
import re
from string \
import find, rfind
from enthought.traits.api \
import Instance, Str, Int, Range, Constant, false, Callable, Delegate
from enthought.traits.trait_base \
import user_name_for
from view_element \
import ViewSubElement
from ui_traits \
import container_delegate
from editor_factory \
import EditorFactory
#-------------------------------------------------------------------------------
# Constants:
#-------------------------------------------------------------------------------
# Pattern of all digits
all_digits = re.compile( r'\d+' )
# Pattern for finding size infomation embedded in an item description
size_pat = re.compile( r"^(.*)<(.*)>(.*)$", re.MULTILINE | re.DOTALL )
# Pattern for finding tooltip infomation embedded in an item description
tooltip_pat = re.compile( r"^(.*)`(.*)`(.*)$", re.MULTILINE | re.DOTALL )
#-------------------------------------------------------------------------------
# Trait definitions:
#-------------------------------------------------------------------------------
# Reference to an EditorFactory
ItemEditor = Instance( EditorFactory, allow_none = True )
# Amount of padding to add around item
Padding = Range( -15, 15, 0, desc = 'amount of padding to add around item' )
#-------------------------------------------------------------------------------
# 'Item' class:
#-------------------------------------------------------------------------------
class Item ( ViewSubElement ):
""" An element in a Traits-based user interface.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# A unique identifier for the item. If not set, it defaults to the value
# of **name**.
id = Str
# User interface label for the item in the GUI. If this attribute is not
# set, the label is the value of **name** with slight modifications:
# underscores are replaced by spaces, and the first letter is capitalized.
# If an item's **name** is not specified, its label is displayed as
# static text, without any editor widget.
label = Str
# Name of the trait the item is editing
name = Str
# Help text describing the purpose of the item. The built-in help handler
# displays this text in a pop-up window if the user clicks the widget's
# label. View-level help displays the help text for all items in a view.
# If this attribute is not set, the built-in help handler generates a
# description based on the trait definition.
help = Str
# The HasTraits object whose trait attribute the item is editing
object = container_delegate
# Presentation style for the item
style = container_delegate
# Docking style for the item
dock = container_delegate
# Image to display on notebook tabs
image = container_delegate
# Category of elements dragged from view
export = container_delegate
# Should a label be displayed for the item?
show_label = Delegate( 'container', 'show_labels' )
# Editor to use for the item
editor = ItemEditor
# Should the item use extra space? If set to True, the widget expands to
# fill any extra space that is available in the display. If set to True
# for more than one item in the same View, any extra space is divided
# between them.
resizable = false
# Should the item use extra space along its Group's layout orientation?
springy = false
# Should the item's label use emphasized text? If the label is not shown,
# this attribute is ignored.
emphasized = false
# Should the item receive focus initially?
has_focus = false
# Pre-condition for including the item in the display. If the expression
# evaluates to False, the item is not defined in the display. Conditions
# for **defined_when** are evaluated only once, when the display is first
# constructed. Use this attribute for conditions based on attributes that
# vary from object to object, but that do not change over time. For example,
# displaying a 'maiden_name' item only for female employees in a company
# database.
defined_when = Str
# Pre-condition for showing the item. If the expression evaluates to False,
# the widget is not visible (and disappears if it was previously visible).
# If the value evaluates to True, the widget becomes visible. All
# **visible_when** conditions are checked each time that any trait value
# is edited in the display. Therefore, you can use **visible_when**
# conditions to hide or show widgets in response to user input.
visible_when = Str
# Pre-condition for enabling the item. If the expression evaluates to False,
# the widget is disabled, that is, it does not accept input. All
# **enabled_when** conditions are checked each time that any trait value
# is edited in the display. Therefore, you can use **enabled_when**
# conditions to enable or disable widgets in response to user input.
enabled_when = Str
# Amount of extra space, in pixels, to add around the item. Values must be
# integers between -15 and 15. Use negative values to subtract from the
# default spacing.
padding = Padding
# Tooltip to display over the item, when the mouse pointer is left idle
# over the widget. Make this text as concise as possible; use the **help**
# attribute to provide more detailed information.
tooltip = Str
# A Callable to use for formatting the contents of the item. This function
# or method is called to create the string representation of the trait value
# to be edited. If the widget does not use a string representation, this
# attribute is ignored.
format_func = Callable
# Python format string to use for formatting the contents of the item.
# The format string is applied to the string representation of the trait
# value before it is displayed in the widget. This attribute is ignored if
# the widget does not use a string representation, or if the
# **format_func** is set.
format_str = Str
# Requested width of the editor (in pixels). The actual displayed width
# is at least the maximum of **width** and the optimal width of the widget
# as calculated by the GUI toolkit. Specify a negative value to ignore the
# toolkit's optimal width. For example, use -50 to force a width of 50
# pixels. The default value of -1 ensures that the toolkit's optimal width
# is used.
width = Int( -1 )
# Requested height of the editor (in pixels). The actual displayed height
# is at least the maximum of **width** and the optimal height of the widget
# as calculated by the GUI toolkit. Specify a negative value to ignore the
# toolkit's optimal height. For example, use -50 to force a height of 50
# pixels. The default value of -1 ensures that the toolkit's optimal height
# is used.
height = Int( -1 )
#---------------------------------------------------------------------------
# Initialize the object:
#---------------------------------------------------------------------------
def __init__ ( self, value = None, **traits ):
""" Initializes the item object.
"""
super( ViewSubElement, self ).__init__( **traits )
if value is None:
return
if not isinstance(value, basestring):
raise TypeError, ("The argument to Item must be a string of the "
"form: {id:}{object.}{name}{[label]}`tooltip`{#^}{$|@|*|~|;style}")
value, empty = self._parse_label( value )
if empty:
self.show_label = False
value = self._parse_style( value )
value = self._parse_size( value )
value = self._parse_tooltip( value )
value = self._option( value, '#', 'resizable', True )
value = self._option( value, '^', 'emphasized', True )
value = self._split( 'id', value, ':', find, 0, 1 )
value = self._split( 'object', value, '.', find, 0, 1 )
if value != '':
self.name = value
#---------------------------------------------------------------------------
# Returns whether or not the object is replacable by an Include object:
#---------------------------------------------------------------------------
def is_includable ( self ):
""" Returns a Boolean indicating whether the object is replaceable by an
Include object.
"""
return (self.id != '')
#---------------------------------------------------------------------------
# Returns whether or not the Item represents a spacer or separator:
#---------------------------------------------------------------------------
def is_spacer ( self ):
""" Returns True if the item represents a spacer or separator.
"""
name = self.name.strip()
return ((name == '') or (name == '_') or
(all_digits.match( name ) is not None))
#---------------------------------------------------------------------------
# Gets the help text associated with the Item in a specified UI:
#---------------------------------------------------------------------------
def get_help ( self, ui ):
""" Gets the help text associated with the Item in a specified UI.
"""
# Return 'None' if the Item is a separator or spacer:
if self.is_spacer():
return None
# Otherwise, it must be a trait Item:
return self.help or None
#---------------------------------------------------------------------------
# Gets the label to use for a specified Item in a specified UI:
#---------------------------------------------------------------------------
def get_label ( self, ui ):
""" Gets the label to use for a specified Item.
"""
# Return 'None' if the Item is a separator or spacer:
if self.is_spacer():
return None
label = self.label
if label != '':
return label
name = self.name
object = ui.context[ self.object ]
trait = object.base_trait( name )
label = user_name_for( name )
tlabel = trait.label
if tlabel is None:
return label
if isinstance(tlabel, basestring):
if tlabel[0:3] == '...':
return label + tlabel[3:]
if tlabel[-3:] == '...':
return tlabel[:-3] + label
if self.label != '':
return self.label
return tlabel
return tlabel( object, name, label )
#---------------------------------------------------------------------------
# Returns an id used to identify the item:
#---------------------------------------------------------------------------
def get_id ( self ):
""" Returns an ID used to identify the item.
"""
if self.id != '':
return self.id
return self.name
#---------------------------------------------------------------------------
# Parses a '<width,height>' value from the string definition:
#---------------------------------------------------------------------------
def _parse_size ( self, value ):
""" Parses a '<width,height>' value from the string definition.
"""
match = size_pat.match( value )
if match is not None:
data = match.group( 2 )
value = match.group( 1 ) + match.group( 3 )
col = data.find( ',' )
if col < 0:
self._set_int( 'width', data )
else:
self._set_int( 'width', data[ : col ] )
self._set_int( 'height', data[ col + 1: ] )
return value
#---------------------------------------------------------------------------
# Parses a '`tooltip`' value from the string definition:
#---------------------------------------------------------------------------
def _parse_tooltip ( self, value ):
""" Parses a *tooltip* value from the string definition.
"""
match = tooltip_pat.match( value )
if match is not None:
self.tooltip = match.group( 2 )
value = match.group( 1 ) + match.group( 3 )
return value
#---------------------------------------------------------------------------
# Sets a specified trait to a specified string converted to an integer:
#---------------------------------------------------------------------------
def _set_int ( self, name, value ):
""" Sets a specified trait to a specified string converted to an
integer.
"""
value = value.strip()
if value != '':
setattr( self, name, int( value ) )
#---------------------------------------------------------------------------
# Returns a 'pretty print' version of the Item:
#---------------------------------------------------------------------------
def __repr__ ( self ):
""" Returns a "pretty print" version of the Item.
"""
return '"%s%s%s%s%s"' % ( self._repr_value( self.id, '', ':' ),
self._repr_value( self.object, '', '.',
'object' ),
self._repr_value( self.name ),
self._repr_value( self.label,'=' ),
self._repr_value( self.style, ';', '',
'simple' ) )
#-------------------------------------------------------------------------------
# 'Label' class:
#-------------------------------------------------------------------------------
class Label ( Item ):
""" An item that is a label.
"""
#---------------------------------------------------------------------------
# Initializes the object:
#---------------------------------------------------------------------------
def __init__ ( self, label ):
super( Label, self ).__init__( label = label )
#-------------------------------------------------------------------------------
# 'Heading' class:
#-------------------------------------------------------------------------------
class Heading ( Label ):
""" An item that is a fancy label.
"""
# Override the 'style' trait to default to the fancy 'custom' style:
style = Constant( 'custom' )
#-------------------------------------------------------------------------------
# 'Spring' class:
#-------------------------------------------------------------------------------
class Spring ( Item ):
""" An item that is a layout "spring".
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Name of the trait the item is editing
name = 'spring'
# Should a label be displayed?
show_label = false
# Editor to use for the item
editor = Instance( 'enthought.traits.ui.api.NullEditor', () )
# Should the item use extra space along its Group's layout orientation?
springy = True
# A pre-defined spring for convenience
spring = Spring()
|