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

« back to all changes in this revision

Viewing changes to examples/demo/Advanced/List_editor_notebook_selection_demo.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
#  Copyright (c) 2007, Enthought, Inc.
 
2
#  License: BSD Style.
 
3
 
 
4
"""
 
5
This shows how the currently active notebook tab of a ListEditor can be
 
6
controlled using the ListEditor's 'selected' trait.
 
7
 
 
8
Note the interaction between the spinner control (for the 'index' trait) and
 
9
the currently selected notebook tab. Try changing the spinner value, then try
 
10
clicking on various notebook tabs.
 
11
 
 
12
Also note that rearranging the notebook tabs (using drag and drop) does not
 
13
affect the correspondence between the index value and its associated notebook
 
14
tab. The correspondence is determined by the contents of the 'people' trait,
 
15
and not by the physical layout of the notebook tabs.
 
16
 
 
17
Finally, note that the ListEditor will automatically scroll the tabs to make
 
18
the selected tab completely visible.
 
19
"""
 
20
 
 
21
#-- Imports --------------------------------------------------------------------
 
22
 
 
23
from traits.api \
 
24
    import HasStrictTraits, Str, Int, Regex, List, Instance, Range
 
25
 
 
26
from traitsui.api \
 
27
    import View, VGroup, Item, ListEditor
 
28
 
 
29
#-- Person Class ---------------------------------------------------------------
 
30
 
 
31
class Person ( HasStrictTraits ):
 
32
 
 
33
    # Trait definitions:
 
34
    name  = Str
 
35
    age   = Int
 
36
    phone = Regex( value = '000-0000', regex = '\d\d\d[-]\d\d\d\d' )
 
37
 
 
38
    # Traits view definition:
 
39
    traits_view = View( 'name', 'age', 'phone',
 
40
                        width   = 0.18,
 
41
                        buttons = [ 'OK', 'Cancel' ] )
 
42
 
 
43
#-- Sample Data ----------------------------------------------------------------
 
44
 
 
45
people = [
 
46
   Person( name = 'Dave Chomsky',        age = 39, phone = '555-1212' ),
 
47
   Person( name = 'Mike Wakowski',       age = 28, phone = '555-3526' ),
 
48
   Person( name = 'Joe Higginbotham',    age = 34, phone = '555-6943' ),
 
49
   Person( name = 'Tom Derringer',       age = 22, phone = '555-7586' ),
 
50
   Person( name = 'Dick Van Der Hooten', age = 63, phone = '555-3895' ),
 
51
   Person( name = 'Harry McCallum',      age = 46, phone = '555-3285' ),
 
52
   Person( name = 'Sally Johnson',       age = 43, phone = '555-8797' ),
 
53
   Person( name = 'Fields Timberlawn',   age = 31, phone = '555-3547' )
 
54
]
 
55
 
 
56
#-- ListEditorNotebookSelectionDemo Class --------------------------------------
 
57
 
 
58
class ListEditorNotebookSelectionDemo ( HasStrictTraits ):
 
59
 
 
60
    #-- Trait Definitions ------------------------------------------------------
 
61
 
 
62
    # List of people:
 
63
    people = List( Person )
 
64
 
 
65
    # The currently selected person:
 
66
    selected = Instance( Person )
 
67
 
 
68
    # The index of the currently selected person:
 
69
    index = Range( 0, 7, mode = 'spinner' )
 
70
 
 
71
    #-- Traits View Definitions ------------------------------------------------
 
72
 
 
73
    traits_view = View(
 
74
        Item( 'index' ),
 
75
        '_',
 
76
        VGroup(
 
77
            Item( 'people@',
 
78
                  id         = 'notebook',
 
79
                  show_label = False,
 
80
                  editor     = ListEditor( use_notebook = True,
 
81
                                           deletable    = False,
 
82
                                           selected     = 'selected',
 
83
                                           export       = 'DockWindowShell',
 
84
                                           page_name    = '.name' )
 
85
            )
 
86
        ),
 
87
        id   = 'traitsui.demo.Traits UI Demo.Advanced.'
 
88
               'List_editor_notebook_selection_demo',
 
89
        dock = 'horizontal' )
 
90
 
 
91
    #-- Trait Event Handlers ---------------------------------------------------
 
92
 
 
93
    def _selected_changed ( self, selected ):
 
94
        self.index = self.people.index( selected )
 
95
 
 
96
    def _index_changed ( self, index ):
 
97
        self.selected = self.people[ index ]
 
98
 
 
99
#-- Set Up The Demo ------------------------------------------------------------
 
100
 
 
101
demo = ListEditorNotebookSelectionDemo( people = people )
 
102
 
 
103
if __name__ == "__main__":
 
104
    demo.configure_traits()
 
105