File: radio_group.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (94 lines) | stat: -rw-r--r-- 3,517 bytes parent folder | download | duplicates (3)
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
#-------------------------------------------------------------------------------
#
#  Define support classes for implementing 'radio button' style components.
#
#  Written by: David C. Morrill
#
#  Date: 10/01/2003
#
#  (c) Copyright 2003 by Enthought, Inc.
#
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

from traits.api import HasPrivateTraits, Trait

#-------------------------------------------------------------------------------
#  Trait definitions:
#-------------------------------------------------------------------------------

# ARadioGroup = Instance( 'RadioGroup' )

#-------------------------------------------------------------------------------
#  'RadioStyle' class:
#-------------------------------------------------------------------------------

class RadioStyle ( HasPrivateTraits ):

    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

#    radio_group = ARadioGroup

    #---------------------------------------------------------------------------
    #  Handle the group the radio style component belongs to being changed:
    #---------------------------------------------------------------------------

    def _group_changed ( self, old, new ):
        if old is not None:
            old.remove( self )
        if new is not None:
            new.add( self )

#-------------------------------------------------------------------------------
#  'RadioGroup' class:
#-------------------------------------------------------------------------------

class RadioGroup ( HasPrivateTraits ):

    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

    # selection = Instance( RadioStyle )
    selection = Trait(None, RadioStyle)

    #---------------------------------------------------------------------------
    #  Handle elements being added to the group:
    #---------------------------------------------------------------------------

    def add ( self, *components ):
        for component in components:
            component.radio_group = self
            if component.selected and (self.selection is not component):
                if self.selection is not None:
                    component.selected = False
                else:
                    self.selection = component

    #---------------------------------------------------------------------------
    #  Handle components being removed from the group:
    #---------------------------------------------------------------------------

    def remove ( self, *components ):
        for component in components:
            if component is self.selection:
                self.selection is None
                break

    #---------------------------------------------------------------------------
    #  Handle the selection being changed:
    #---------------------------------------------------------------------------

    def _selection_changed ( self, old, new ):
        if old is not None:
            old.selected = False


radio_group_trait = Trait(None, RadioGroup)

RadioStyle.add_class_trait('radio_group', radio_group_trait)