~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to traitsui/value_tree.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#------------------------------------------------------------------------------
 
2
#
 
3
#  Copyright (c) 2006, Enthought, Inc.
 
4
#  All rights reserved.
 
5
#
 
6
#  This software is provided without warranty under the terms of the BSD
 
7
#  license included in enthought/LICENSE.txt and may be redistributed only
 
8
#  under the conditions described in the aforementioned license.  The license
 
9
#  is also available online at http://www.enthought.com/licenses/BSD.txt
 
10
#
 
11
#  Thanks for using Enthought open source!
 
12
#
 
13
#  Author: David C. Morrill
 
14
#  Date:   01/05/2006
 
15
#
 
16
#------------------------------------------------------------------------------
 
17
 
 
18
""" Defines tree node classes and editors for various types of values.
 
19
"""
 
20
 
 
21
#-------------------------------------------------------------------------------
 
22
#  Imports:
 
23
#-------------------------------------------------------------------------------
 
24
 
 
25
from __future__ import absolute_import
 
26
 
 
27
import inspect
 
28
 
 
29
from types import FunctionType, MethodType
 
30
 
 
31
from traits.api import Any, Bool, HasPrivateTraits, HasTraits, Instance, List, Str
 
32
 
 
33
from .tree_node import ObjectTreeNode, TreeNode, TreeNodeObject
 
34
 
 
35
from .editors.tree_editor import TreeEditor
 
36
 
 
37
#-------------------------------------------------------------------------------
 
38
#  'SingleValueTreeNodeObject' class:
 
39
#-------------------------------------------------------------------------------
 
40
 
 
41
class SingleValueTreeNodeObject ( TreeNodeObject ):
 
42
    """ A tree node for objects of types that have a single value.
 
43
    """
 
44
 
 
45
    #---------------------------------------------------------------------------
 
46
    #  Trait definitions:
 
47
    #---------------------------------------------------------------------------
 
48
 
 
49
    # The parent of this node
 
50
    parent = Instance( TreeNodeObject )
 
51
 
 
52
    # Name of the value
 
53
    name = Str
 
54
 
 
55
    # User-specified override of the default label
 
56
    label = Str
 
57
 
 
58
    # The value itself
 
59
    value = Any
 
60
 
 
61
    # Is the value readonly?
 
62
    readonly = Bool( False )
 
63
 
 
64
    #---------------------------------------------------------------------------
 
65
    #  Returns whether chidren of this object are allowed or not:
 
66
    #---------------------------------------------------------------------------
 
67
 
 
68
    def tno_allows_children ( self, node ):
 
69
        """ Returns whether this object can have children (False for this
 
70
        class).
 
71
        """
 
72
        return False
 
73
 
 
74
    #---------------------------------------------------------------------------
 
75
    #  Returns whether or not the object has children:
 
76
    #---------------------------------------------------------------------------
 
77
 
 
78
    def tno_has_children ( self, node ):
 
79
        """ Returns whether the object has children (False for this class).
 
80
        """
 
81
        return False
 
82
 
 
83
    #---------------------------------------------------------------------------
 
84
    #  Returns whether or not the object's children can be renamed:
 
85
    #---------------------------------------------------------------------------
 
86
 
 
87
    def tno_can_rename ( self, node ):
 
88
        """ Returns whether the object's children can be renamed (False for
 
89
        this class).
 
90
        """
 
91
        return False
 
92
 
 
93
    #---------------------------------------------------------------------------
 
94
    #  Returns whether or not the object's children can be copied:
 
95
    #---------------------------------------------------------------------------
 
96
 
 
97
    def tno_can_copy ( self, node ):
 
98
        """ Returns whether the object's children can be copied (True for this
 
99
        class).
 
100
        """
 
101
        return True
 
102
 
 
103
    #---------------------------------------------------------------------------
 
104
    #  Returns whether or not the object's children can be deleted:
 
105
    #---------------------------------------------------------------------------
 
106
 
 
107
    def tno_can_delete ( self, node ):
 
108
        """ Returns whether the object's children can be deleted (False for
 
109
        this class).
 
110
        """
 
111
        return False
 
112
 
 
113
    #---------------------------------------------------------------------------
 
114
    #  Returns whether or not the object's children can be inserted (or just
 
115
    #  appended):
 
116
    #---------------------------------------------------------------------------
 
117
 
 
118
    def tno_can_insert ( self, node ):
 
119
        """ Returns whether the object's children can be inserted (False,
 
120
        meaning children are appended, for this class).
 
121
        """
 
122
        return False
 
123
 
 
124
    #---------------------------------------------------------------------------
 
125
    #  Returns the icon for a specified object:
 
126
    #---------------------------------------------------------------------------
 
127
 
 
128
    def tno_get_icon ( self, node, is_expanded ):
 
129
        """ Returns the icon for a specified object.
 
130
        """
 
131
        return ('@icons:%s_node' % self.__class__.__name__[ : -4 ].lower())
 
132
 
 
133
    #---------------------------------------------------------------------------
 
134
    #  Sets the label for a specified node:
 
135
    #---------------------------------------------------------------------------
 
136
 
 
137
    def tno_set_label ( self, node, label ):
 
138
        """ Sets the label for a specified object.
 
139
        """
 
140
        if label == '?':
 
141
            label = ''
 
142
        self.label = label
 
143
 
 
144
    #---------------------------------------------------------------------------
 
145
    #  Gets the label to display for a specified object:
 
146
    #---------------------------------------------------------------------------
 
147
 
 
148
    def tno_get_label ( self, node ):
 
149
        """ Gets the label to display for a specified object.
 
150
        """
 
151
        if self.label != '':
 
152
            return self.label
 
153
 
 
154
        if self.name == '':
 
155
            return self.format_value( self.value )
 
156
 
 
157
        return '%s: %s' % ( self.name, self.format_value( self.value ) )
 
158
 
 
159
    #---------------------------------------------------------------------------
 
160
    #  Returns the formatted version of the value:
 
161
    #---------------------------------------------------------------------------
 
162
 
 
163
    def format_value ( self, value ):
 
164
        """ Returns the formatted version of the value.
 
165
        """
 
166
        return repr( value )
 
167
 
 
168
    #---------------------------------------------------------------------------
 
169
    #  Returns the correct node type for a specified value:
 
170
    #---------------------------------------------------------------------------
 
171
 
 
172
    def node_for ( self, name, value ):
 
173
        """ Returns the correct node type for a specified value.
 
174
        """
 
175
        for type, node in basic_types():
 
176
            if isinstance( value, type ):
 
177
                break
 
178
        else:
 
179
            node = OtherNode
 
180
            if inspect.isclass( value ):
 
181
                node = ClassNode
 
182
 
 
183
            elif hasattr( value, '__class__' ):
 
184
                node = ObjectNode
 
185
 
 
186
        return node( parent   = self,
 
187
                     name     = name,
 
188
                     value    = value,
 
189
                     readonly = self.readonly )
 
190
 
 
191
#-------------------------------------------------------------------------------
 
192
#  'MultiValueTreeNodeObject' class:
 
193
#-------------------------------------------------------------------------------
 
194
 
 
195
class MultiValueTreeNodeObject ( SingleValueTreeNodeObject ):
 
196
    """ A tree node for objects of types that have multiple values.
 
197
    """
 
198
 
 
199
    #---------------------------------------------------------------------------
 
200
    #  Returns whether chidren of this object are allowed or not:
 
201
    #---------------------------------------------------------------------------
 
202
 
 
203
    def tno_allows_children ( self, node ):
 
204
        """ Returns whether this object can have children (True for this class).
 
205
        """
 
206
        return True
 
207
 
 
208
    #---------------------------------------------------------------------------
 
209
    #  Returns whether or not the object has children:
 
210
    #---------------------------------------------------------------------------
 
211
 
 
212
    def tno_has_children ( self, node ):
 
213
        """ Returns whether the object has children (True for this class).
 
214
        """
 
215
        return True
 
216
 
 
217
#-------------------------------------------------------------------------------
 
218
#  'StringNode' class:
 
219
#-------------------------------------------------------------------------------
 
220
 
 
221
class StringNode ( SingleValueTreeNodeObject ):
 
222
    """ A tree node for strings.
 
223
    """
 
224
 
 
225
    #---------------------------------------------------------------------------
 
226
    #  Returns the formatted version of the value:
 
227
    #---------------------------------------------------------------------------
 
228
 
 
229
    def format_value ( self, value ):
 
230
        """ Returns the formatted version of the value.
 
231
        """
 
232
        n = len( value )
 
233
        if len( value ) > 80:
 
234
            value = '%s...%s' % ( value[ :42 ], value[ -35: ] )
 
235
 
 
236
        return '%s [%d]' % ( repr( value ), n )
 
237
 
 
238
#-------------------------------------------------------------------------------
 
239
#  'NoneNode' class:
 
240
#-------------------------------------------------------------------------------
 
241
 
 
242
class NoneNode ( SingleValueTreeNodeObject ):
 
243
    """ A tree node for None values.
 
244
    """
 
245
    pass
 
246
 
 
247
#-------------------------------------------------------------------------------
 
248
#  'BoolNode' class:
 
249
#-------------------------------------------------------------------------------
 
250
 
 
251
class BoolNode ( SingleValueTreeNodeObject ):
 
252
    """ A tree node for Boolean values.
 
253
    """
 
254
    pass
 
255
 
 
256
#-------------------------------------------------------------------------------
 
257
#  'IntNode' class:
 
258
#-------------------------------------------------------------------------------
 
259
 
 
260
class IntNode ( SingleValueTreeNodeObject ):
 
261
    """ A tree node for integer values.
 
262
    """
 
263
    pass
 
264
 
 
265
#-------------------------------------------------------------------------------
 
266
#  'FloatNode' class:
 
267
#-------------------------------------------------------------------------------
 
268
 
 
269
class FloatNode ( SingleValueTreeNodeObject ):
 
270
    """ A tree node for floating point values.
 
271
    """
 
272
    pass
 
273
 
 
274
#-------------------------------------------------------------------------------
 
275
#  'ComplexNode' class:
 
276
#-------------------------------------------------------------------------------
 
277
 
 
278
class ComplexNode ( SingleValueTreeNodeObject ):
 
279
    """ A tree node for complex number values.
 
280
    """
 
281
    pass
 
282
 
 
283
#-------------------------------------------------------------------------------
 
284
#  'OtherNode' class:
 
285
#-------------------------------------------------------------------------------
 
286
 
 
287
class OtherNode ( SingleValueTreeNodeObject ):
 
288
    """ A tree node for single-value types for which there is not another
 
289
    node type.
 
290
    """
 
291
    pass
 
292
 
 
293
#-------------------------------------------------------------------------------
 
294
#  'TupleNode' class:
 
295
#-------------------------------------------------------------------------------
 
296
 
 
297
class TupleNode ( MultiValueTreeNodeObject ):
 
298
    """ A tree node for tuples.
 
299
    """
 
300
    #---------------------------------------------------------------------------
 
301
    #  Returns the formatted version of the value:
 
302
    #---------------------------------------------------------------------------
 
303
 
 
304
    def format_value ( self, value ):
 
305
        """ Returns the formatted version of the value.
 
306
        """
 
307
        return 'Tuple(%d)' % len( value )
 
308
 
 
309
    #---------------------------------------------------------------------------
 
310
    #  Returns whether or not the object has children:
 
311
    #---------------------------------------------------------------------------
 
312
 
 
313
    def tno_has_children ( self, node ):
 
314
        """ Returns whether the object has children, based on the length of
 
315
            the tuple.
 
316
        """
 
317
        return (len( self.value ) > 0)
 
318
 
 
319
    #---------------------------------------------------------------------------
 
320
    #  Gets the object's children:
 
321
    #---------------------------------------------------------------------------
 
322
 
 
323
    def tno_get_children ( self, node ):
 
324
        """ Gets the object's children.
 
325
        """
 
326
        node_for = self.node_for
 
327
        value    = self.value
 
328
        if len( value ) > 500:
 
329
            return ([ node_for( '[%d]' % i, x )
 
330
                      for i, x in enumerate( value[ : 250 ] ) ] +
 
331
                    [ StringNode( value = '...', readonly = True ) ] +
 
332
                    [ node_for( '[%d]' % i, x )
 
333
                      for i, x in enumerate( value[ -250: ] ) ])
 
334
 
 
335
        return [ node_for( '[%d]' % i, x ) for i, x in enumerate( value ) ]
 
336
 
 
337
#-------------------------------------------------------------------------------
 
338
#  'ListNode' class:
 
339
#-------------------------------------------------------------------------------
 
340
 
 
341
class ListNode ( TupleNode ):
 
342
    """ A tree node for lists.
 
343
    """
 
344
 
 
345
    #---------------------------------------------------------------------------
 
346
    #  Returns the formatted version of the value:
 
347
    #---------------------------------------------------------------------------
 
348
 
 
349
    def format_value ( self, value ):
 
350
        """ Returns the formatted version of the value.
 
351
        """
 
352
        return 'List(%d)' % len( value )
 
353
 
 
354
    #---------------------------------------------------------------------------
 
355
    #  Returns whether or not the object's children can be deleted:
 
356
    #---------------------------------------------------------------------------
 
357
 
 
358
    def tno_can_delete ( self, node ):
 
359
        """ Returns whether the object's children can be deleted.
 
360
        """
 
361
        return (not self.readonly)
 
362
 
 
363
    #---------------------------------------------------------------------------
 
364
    #  Returns whether or not the object's children can be inserted (or just
 
365
    #  appended):
 
366
    #---------------------------------------------------------------------------
 
367
 
 
368
    def tno_can_insert ( self, node ):
 
369
        """ Returns whether the object's children can be inserted (vs.
 
370
        appended).
 
371
        """
 
372
        return (not self.readonly)
 
373
 
 
374
#-------------------------------------------------------------------------------
 
375
#  'SetNode' class:
 
376
#-------------------------------------------------------------------------------
 
377
 
 
378
class SetNode ( ListNode ):
 
379
    """ A tree node for sets.
 
380
    """
 
381
 
 
382
    #---------------------------------------------------------------------------
 
383
    #  Returns the formatted version of the value:
 
384
    #---------------------------------------------------------------------------
 
385
 
 
386
    def format_value ( self, value ):
 
387
        """ Returns the formatted version of the value.
 
388
        """
 
389
        return 'Set(%d)' % len( value )
 
390
 
 
391
#-------------------------------------------------------------------------------
 
392
#  'ArrayNode' class:
 
393
#-------------------------------------------------------------------------------
 
394
 
 
395
class ArrayNode ( TupleNode ):
 
396
    """ A tree node for arrays.
 
397
    """
 
398
 
 
399
    #---------------------------------------------------------------------------
 
400
    #  Returns the formatted version of the value:
 
401
    #---------------------------------------------------------------------------
 
402
 
 
403
    def format_value ( self, value ):
 
404
        """ Returns the formatted version of the value.
 
405
        """
 
406
        return 'Array(%s)' % ','.join( [ str( n ) for n in value.shape ] )
 
407
 
 
408
#-------------------------------------------------------------------------------
 
409
#  'DictNode' class:
 
410
#-------------------------------------------------------------------------------
 
411
 
 
412
class DictNode ( TupleNode ):
 
413
    """ A tree node for dictionaries.
 
414
    """
 
415
 
 
416
    #---------------------------------------------------------------------------
 
417
    #  Returns the formatted version of the value:
 
418
    #---------------------------------------------------------------------------
 
419
 
 
420
    def format_value ( self, value ):
 
421
        """ Returns the formatted version of the value.
 
422
        """
 
423
        return 'Dict(%d)' % len( value )
 
424
 
 
425
    #---------------------------------------------------------------------------
 
426
    #  Gets the object's children:
 
427
    #---------------------------------------------------------------------------
 
428
 
 
429
    def tno_get_children ( self, node ):
 
430
        """ Gets the object's children.
 
431
        """
 
432
        node_for = self.node_for
 
433
        items    = [ ( repr( k ), v ) for k, v in self.value.items() ]
 
434
        items.sort( lambda l, r: cmp( l[0], r[0] ) )
 
435
        if len( items ) > 500:
 
436
            return ([ node_for( '[%s]' % k, v ) for k, v in items[: 250 ] ] +
 
437
                    [ StringNode( value = '...', readonly = True ) ]        +
 
438
                    [ node_for( '[%s]' % k, v ) for k, v in items[ -250: ] ])
 
439
 
 
440
        return [ node_for( '[%s]' % k, v ) for k, v in items ]
 
441
 
 
442
    #---------------------------------------------------------------------------
 
443
    #  Returns whether or not the object's children can be deleted:
 
444
    #---------------------------------------------------------------------------
 
445
 
 
446
    def tno_can_delete ( self, node ):
 
447
        """ Returns whether the object's children can be deleted.
 
448
        """
 
449
        return (not self.readonly)
 
450
 
 
451
#-------------------------------------------------------------------------------
 
452
#  'FunctionNode' class:
 
453
#-------------------------------------------------------------------------------
 
454
 
 
455
class FunctionNode ( SingleValueTreeNodeObject ):
 
456
    """ A tree node for functions
 
457
    """
 
458
 
 
459
    #---------------------------------------------------------------------------
 
460
    #  Returns the formatted version of the value:
 
461
    #---------------------------------------------------------------------------
 
462
 
 
463
    def format_value ( self, value ):
 
464
        """ Returns the formatted version of the value.
 
465
        """
 
466
        return 'Function %s()' % ( value.func_code.co_name )
 
467
 
 
468
#---------------------------------------------------------------------------
 
469
#  'MethodNode' class:
 
470
#---------------------------------------------------------------------------
 
471
 
 
472
class MethodNode ( MultiValueTreeNodeObject ):
 
473
 
 
474
    #---------------------------------------------------------------------------
 
475
    #  Returns the formatted version of the value:
 
476
    #---------------------------------------------------------------------------
 
477
 
 
478
    def format_value ( self, value ):
 
479
        """ Returns the formatted version of the value.
 
480
        """
 
481
        type = 'B'
 
482
        if value.im_self is None:
 
483
            type = 'Unb'
 
484
 
 
485
        return '%sound method %s.%s()' % (
 
486
                   type,
 
487
                   value.im_class.__name__,
 
488
                   value.im_func.func_code.co_name )
 
489
 
 
490
    #---------------------------------------------------------------------------
 
491
    #  Returns whether or not the object has children:
 
492
    #---------------------------------------------------------------------------
 
493
 
 
494
    def tno_has_children ( self, node ):
 
495
        """ Returns whether the object has children.
 
496
        """
 
497
        return (self.value.im_func != None)
 
498
 
 
499
    #---------------------------------------------------------------------------
 
500
    #  Gets the object's children:
 
501
    #---------------------------------------------------------------------------
 
502
 
 
503
    def tno_get_children ( self, node ):
 
504
        """ Gets the object's children.
 
505
        """
 
506
        return [ self.node_for( 'Object', self.value.im_self ) ]
 
507
 
 
508
#-------------------------------------------------------------------------------
 
509
#  'ObjectNode' class:
 
510
#-------------------------------------------------------------------------------
 
511
 
 
512
class ObjectNode ( MultiValueTreeNodeObject ):
 
513
    """ A tree node for objects.
 
514
    """
 
515
 
 
516
    #---------------------------------------------------------------------------
 
517
    #  Returns the formatted version of the value:
 
518
    #---------------------------------------------------------------------------
 
519
 
 
520
    def format_value ( self, value ):
 
521
        """ Returns the formatted version of the value.
 
522
        """
 
523
        try:
 
524
            klass = value.__class__.__name__
 
525
        except:
 
526
            klass = '???'
 
527
        return '%s(0x%08X)' % ( klass, id( value ) )
 
528
 
 
529
    #---------------------------------------------------------------------------
 
530
    #  Returns whether or not the object has children:
 
531
    #---------------------------------------------------------------------------
 
532
 
 
533
    def tno_has_children ( self, node ):
 
534
        """ Returns whether the object has children.
 
535
        """
 
536
        try:
 
537
            return (len( self.value.__dict__ ) > 0)
 
538
        except:
 
539
            return False
 
540
 
 
541
    #---------------------------------------------------------------------------
 
542
    #  Gets the object's children:
 
543
    #---------------------------------------------------------------------------
 
544
 
 
545
    def tno_get_children ( self, node ):
 
546
        """ Gets the object's children.
 
547
        """
 
548
        items = [ ( k, v ) for k, v in self.value.__dict__.items() ]
 
549
        items.sort( lambda l, r: cmp( l[0], r[0] ) )
 
550
        return [ self.node_for( '.' + k, v ) for k, v in items ]
 
551
 
 
552
#-------------------------------------------------------------------------------
 
553
#  'ClassNode' class:
 
554
#-------------------------------------------------------------------------------
 
555
 
 
556
class ClassNode ( ObjectNode ):
 
557
    """ A tree node for classes.
 
558
    """
 
559
 
 
560
    #---------------------------------------------------------------------------
 
561
    #  Returns the formatted version of the value:
 
562
    #---------------------------------------------------------------------------
 
563
 
 
564
    def format_value ( self, value ):
 
565
        """ Returns the formatted version of the value.
 
566
        """
 
567
        return value.__name__
 
568
 
 
569
#-------------------------------------------------------------------------------
 
570
#  'TraitsNode' class:
 
571
#-------------------------------------------------------------------------------
 
572
 
 
573
class TraitsNode ( ObjectNode ):
 
574
    """ A tree node for traits.
 
575
    """
 
576
 
 
577
    #---------------------------------------------------------------------------
 
578
    #  Returns whether or not the object has children:
 
579
    #---------------------------------------------------------------------------
 
580
 
 
581
    def tno_has_children ( self, node ):
 
582
        """ Returns whether the object has children.
 
583
        """
 
584
        return (len( self._get_names() ) > 0)
 
585
 
 
586
    #---------------------------------------------------------------------------
 
587
    #  Gets the object's children:
 
588
    #---------------------------------------------------------------------------
 
589
 
 
590
    def tno_get_children ( self, node ):
 
591
        """ Gets the object's children.
 
592
        """
 
593
        names = self._get_names()
 
594
        names.sort()
 
595
        value    = self.value
 
596
        node_for = self.node_for
 
597
        nodes    = []
 
598
        for name in names:
 
599
            try:
 
600
                item_value = getattr( value, name, '<unknown>' )
 
601
            except Exception, excp:
 
602
                item_value = '<%s>' % excp
 
603
            nodes.append( node_for( '.' + name, item_value ) )
 
604
 
 
605
        return nodes
 
606
 
 
607
    #---------------------------------------------------------------------------
 
608
    #  Gets the names of all defined traits/attributes:
 
609
    #---------------------------------------------------------------------------
 
610
 
 
611
    def _get_names ( self ):
 
612
        """ Gets the names of all defined traits or attributes.
 
613
        """
 
614
        value = self.value
 
615
        names = {}
 
616
        for name in value.trait_names( type = lambda x: x != 'event' ):
 
617
            names[ name ] = None
 
618
        for name in value.__dict__.keys():
 
619
            names[ name ] = None
 
620
        return names.keys()
 
621
 
 
622
    #---------------------------------------------------------------------------
 
623
    #  Sets up/Tears down a listener for 'children replaced' on a specified
 
624
    #  object:
 
625
    #---------------------------------------------------------------------------
 
626
 
 
627
    def tno_when_children_replaced ( self, node, listener, remove ):
 
628
        """ Sets up or removes a listener for children being replaced on a
 
629
        specified object.
 
630
        """
 
631
        self._listener = listener
 
632
        self.value.on_trait_change( self._children_replaced, remove = remove,
 
633
                                    dispatch = 'ui' )
 
634
 
 
635
    def _children_replaced ( self ):
 
636
        self._listener( self )
 
637
 
 
638
    #---------------------------------------------------------------------------
 
639
    #  Sets up/Tears down a listener for 'children changed' on a specified
 
640
    #  object:
 
641
    #---------------------------------------------------------------------------
 
642
 
 
643
    def tno_when_children_changed ( self, node, listener, remove ):
 
644
        """ Sets up or removes a listener for children being changed on a
 
645
        specified object.
 
646
        """
 
647
        pass
 
648
 
 
649
#-------------------------------------------------------------------------------
 
650
#  'RootNode' class:
 
651
#-------------------------------------------------------------------------------
 
652
 
 
653
class RootNode ( MultiValueTreeNodeObject ):
 
654
    """ A root node.
 
655
    """
 
656
 
 
657
    #---------------------------------------------------------------------------
 
658
    #  Returns the formatted version of the value:
 
659
    #---------------------------------------------------------------------------
 
660
 
 
661
    def format_value ( self, value ):
 
662
        """ Returns the formatted version of the value.
 
663
        """
 
664
        return ''
 
665
 
 
666
    #---------------------------------------------------------------------------
 
667
    #  Gets the object's children:
 
668
    #---------------------------------------------------------------------------
 
669
 
 
670
    def tno_get_children ( self, node ):
 
671
        """ Gets the object's children.
 
672
        """
 
673
        return [ self.node_for( '', self.value ) ]
 
674
 
 
675
#-------------------------------------------------------------------------------
 
676
#  Define the mapping of object types to nodes:
 
677
#-------------------------------------------------------------------------------
 
678
 
 
679
_basic_types = None
 
680
 
 
681
def basic_types ( ):
 
682
    global _basic_types
 
683
 
 
684
    if _basic_types is None:
 
685
        # Create the mapping of object types to nodes:
 
686
        _basic_types = [
 
687
            ( type( None ), NoneNode ),
 
688
            ( str,          StringNode ),
 
689
            ( unicode,      StringNode ),
 
690
            ( bool,         BoolNode ),
 
691
            ( int,          IntNode ),
 
692
            ( float,        FloatNode ),
 
693
            ( complex,      ComplexNode ),
 
694
            ( tuple,        TupleNode ),
 
695
            ( list,         ListNode ),
 
696
            ( set,          SetNode ),
 
697
            ( dict,         DictNode ),
 
698
            ( FunctionType, FunctionNode ),
 
699
            ( MethodType,   MethodNode ),
 
700
            ( HasTraits,    TraitsNode )
 
701
        ]
 
702
 
 
703
        try:
 
704
            from numpy import array
 
705
 
 
706
            _basic_types.append( ( type( array( [ 1 ] ) ), ArrayNode ) )
 
707
        except ImportError:
 
708
            pass
 
709
 
 
710
    return _basic_types
 
711
 
 
712
#-------------------------------------------------------------------------------
 
713
#  '_ValueTree' class:
 
714
#-------------------------------------------------------------------------------
 
715
 
 
716
class _ValueTree ( HasPrivateTraits ):
 
717
 
 
718
    #---------------------------------------------------------------------------
 
719
    #  Trait definitions:
 
720
    #---------------------------------------------------------------------------
 
721
 
 
722
    # List of arbitrary Python values contained in the tree:
 
723
    values = List( SingleValueTreeNodeObject )
 
724
 
 
725
#-------------------------------------------------------------------------------
 
726
#  Defines the value tree editor(s):
 
727
#-------------------------------------------------------------------------------
 
728
 
 
729
# Nodes in a value tree:
 
730
value_tree_nodes = [
 
731
    ObjectTreeNode(
 
732
        node_for = [ NoneNode, StringNode, BoolNode, IntNode, FloatNode,
 
733
                     ComplexNode, OtherNode, TupleNode, ListNode, ArrayNode,
 
734
                     DictNode, SetNode, FunctionNode, MethodNode, ObjectNode,
 
735
                     TraitsNode, RootNode, ClassNode ] )
 
736
]
 
737
 
 
738
# Editor for a value tree:
 
739
value_tree_editor = TreeEditor(
 
740
    auto_open = 3,
 
741
    hide_root = True,
 
742
    editable  = False,
 
743
    nodes     = value_tree_nodes
 
744
)
 
745
 
 
746
# Editor for a value tree with a root:
 
747
value_tree_editor_with_root = TreeEditor(
 
748
    auto_open = 3,
 
749
    editable  = False,
 
750
    nodes     = [
 
751
        ObjectTreeNode(
 
752
            node_for = [ NoneNode, StringNode, BoolNode, IntNode, FloatNode,
 
753
                         ComplexNode, OtherNode, TupleNode, ListNode, ArrayNode,
 
754
                         DictNode, SetNode, FunctionNode, MethodNode,
 
755
                         ObjectNode, TraitsNode, RootNode, ClassNode ]
 
756
        ),
 
757
        TreeNode( node_for = [ _ValueTree ],
 
758
                  auto_open  = True,
 
759
                  children   = 'values',
 
760
                  move       = [ SingleValueTreeNodeObject ],
 
761
                  copy       = False,
 
762
                  label      = '=Values',
 
763
                  icon_group = 'traits_node',
 
764
                  icon_open  = 'traits_node' )
 
765
    ]
 
766
)
 
767
 
 
768
#-------------------------------------------------------------------------------
 
769
#  Defines a 'ValueTree' trait:
 
770
#-------------------------------------------------------------------------------
 
771
 
 
772
# Trait for a value tree:
 
773
ValueTree = Instance( _ValueTree, (), editor = value_tree_editor_with_root )
 
774