~ubuntu-branches/ubuntu/trusty/python-traitsui/trusty

« back to all changes in this revision

Viewing changes to examples/demo/Advanced/Popup_Dialog_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
Demonstrates using a popup view within another view.
 
6
 
 
7
Try changing the gender of the person from 'Male' to 'Female' using the
 
8
drop-down list. When the person's gender is changed, a pop-up dialog is
 
9
displayed immediately below the gender field providing you with the
 
10
opportunity to cancel the gender change.
 
11
 
 
12
If you click the Cancel button, the person's gender will return to its previous
 
13
value. If you click anywhere else outside of the pop-up dialog, the pop-up
 
14
dialog will simply disappear, leaving the person's new gender value as is.
 
15
 
 
16
The main items of interest in this demo are:
 
17
 - The: kind = 'popup' trait set in the PersonHandler View which marks the view
 
18
   as being a popup view.
 
19
 - The parent = info.gender.control value passed to the edit_traits method
 
20
   when the popup dialog is created in the object_gender_changed method. This
 
21
   value specifies the control that the popup dialog should be positioned near.
 
22
 
 
23
Notes:
 
24
 - Traits UI will automatically position the popup dialog near the specified
 
25
   control in such a way that the pop-up dialog will not overlay the control
 
26
   and will be entirely on the screen (as long as these two conditions do not
 
27
   conflict).
 
28
"""
 
29
 
 
30
#-- Imports --------------------------------------------------------------------
 
31
 
 
32
from traits.api \
 
33
    import HasPrivateTraits, Str, Int, Enum, Instance, Button
 
34
 
 
35
from traitsui.api \
 
36
    import View, HGroup, Item, Handler, UIInfo, spring
 
37
 
 
38
#-- The PersonHandler class ----------------------------------------------------
 
39
 
 
40
class PersonHandler ( Handler ):
 
41
 
 
42
    # The UIInfo object associated with the view:
 
43
    info = Instance( UIInfo )
 
44
 
 
45
    # The cancel button used to revert an unintentional gender change:
 
46
    cancel = Button( 'Cancel' )
 
47
 
 
48
    # The pop-up customization view:
 
49
    view = View(
 
50
        HGroup(
 
51
            spring,
 
52
            Item( 'cancel', show_label = False ),
 
53
        ),
 
54
        kind = 'popup'
 
55
    )
 
56
 
 
57
    # Event handlers:
 
58
    def object_gender_changed ( self, info ):
 
59
        if info.initialized:
 
60
            self.info = info
 
61
            self._ui  = self.edit_traits( parent = info.gender.control )
 
62
 
 
63
    def _cancel_changed ( self ):
 
64
        object = self.info.object
 
65
        object.gender = [ 'Male', 'Female' ][ object.gender == 'Male' ]
 
66
        self._ui.dispose()
 
67
 
 
68
#-- The Person class -----------------------------------------------------------
 
69
 
 
70
class Person ( HasPrivateTraits ):
 
71
 
 
72
    # The person's name, age and gender:
 
73
    name   = Str
 
74
    age    = Int
 
75
    gender = Enum( 'Male', 'Female' )
 
76
 
 
77
    # The traits UI view:
 
78
    traits_view = View(
 
79
        Item( 'name' ),
 
80
        Item( 'age' ),
 
81
        Item( 'gender' ),
 
82
        title   = 'Button Popup Demo',
 
83
        handler = PersonHandler
 
84
    )
 
85
 
 
86
#-- Create and run the demo ----------------------------------------------------
 
87
 
 
88
# Create the demo:
 
89
demo = Person( name = 'Mike Thomas', age  = 32 )
 
90
 
 
91
# Run the demo (if invoked from the command line):
 
92
if __name__ == '__main__':
 
93
    demo.configure_traits()
 
94