File: ui_modal.py

package info (click to toggle)
matplotlib 0.98.1-1%2Blenny4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,624 kB
  • ctags: 22,599
  • sloc: python: 76,915; cpp: 63,459; ansic: 5,353; makefile: 111; sh: 12
file content (230 lines) | stat: -rw-r--r-- 9,429 bytes parent folder | download | duplicates (2)
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
#------------------------------------------------------------------------------
# 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: 12/02/2004
# Description: Create a modal Tkinter user interface for a specified UI object.
#             
# Symbols defined: ui_modal
#
#------------------------------------------------------------------------------

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

import tk

from ui_panel  import panel, show_help
from constants import screen_dy, scrollbar_dx

#-------------------------------------------------------------------------------
#  Creates a non-modal Tkinter user interface for a specified UI object:
#-------------------------------------------------------------------------------

def ui_modal ( ui, parent ):
    ui.control = ModalDialog( ui, parent )
    try:
        ui.prepare_ui()
    except:
        ui.control.Destroy()
        ui.control.ui = None
        ui.control    = None
        ui.result     = False
        raise
    ui.handler.position( ui.info )
    ui.control.ShowModal()
    
#-------------------------------------------------------------------------------
#  'ModalDialog' class:
#-------------------------------------------------------------------------------
    
class ModalDialog ( wx.Dialog ):
    
    #---------------------------------------------------------------------------
    #  Initializes the object:
    #---------------------------------------------------------------------------
    
    def __init__ ( self, ui, parent ):
        wx.Dialog.__init__( self, parent, -1, ui.view.title )
        wx.EVT_CLOSE( self, self._on_cancel )
        wx.EVT_CHAR(  self, self._on_key )
        
        self.ui = ui
        
        # Create the 'context' copies we will need while editing:
        context     = ui.context
        ui._context = context
        ui.context  = self._copy_context( context )
        ui._revert  = self._copy_context( context )
        
        # Create the actual trait sheet panel and imbed it in a scrollable 
        # window:
        sizer       = wx.BoxSizer( wx.VERTICAL )
        sw          = wx.ScrolledWindow( self )
        trait_sheet = panel( ui, sw )
        sizer.Add( trait_sheet, 1, wx.EXPAND | wx.ALL, 4 )
        tsdx, tsdy = trait_sheet.GetSizeTuple()
        tsdx += 8
        tsdy += 8
        
        max_dy = (2 * screen_dy) / 3
        sw.SetAutoLayout( True )
        sw.SetSizer( sizer )
        sw.SetSize( wx.Size( tsdx + ((tsdy > max_dy) * scrollbar_dx), 
                             min( tsdy, max_dy ) ) )
        sw.SetScrollRate( 16, 16 )
        
        sw_sizer = wx.BoxSizer( wx.VERTICAL )
        sw_sizer.Add( sw, 1, wx.EXPAND )
        
        # Create the necessary special function buttons:
        sw_sizer.Add( wx.StaticLine( self, -1 ), 0, wx.EXPAND )
        b_sizer = wx.BoxSizer( wx.HORIZONTAL )
        if ui.view.apply:
            self.apply = self._add_button( 'Apply', self._on_apply, b_sizer, 
                                           False )
            ui.on_trait_change( self._on_applyable, 'modified', 
                                dispatch = 'ui' )
            if ui.view.revert:
                self.revert = self._add_button( 'Revert', self._on_revert, 
                                                b_sizer, False )
        self._add_button( 'OK', self._on_ok, b_sizer )
        self._add_button( 'Cancel', self._on_cancel, b_sizer )
        if ui.view.help:
            self._add_button( 'Help', self._on_help, b_sizer )
        sw_sizer.Add( b_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5 )
         
        # Lay all of the dialog contents out:            
        sw_sizer.Fit( self )
        self.SetSizer( sw_sizer )
        self.SetAutoLayout( True )
        
    #---------------------------------------------------------------------------
    #  Creates a copy of a 'context' dictionary:
    #---------------------------------------------------------------------------
        
    def _copy_context ( self, context ):
        """ Creates a copy of a 'context' dictionary.
        """
        result = {}
        for name, value in context.items():
            result[ name ] = value.clone_traits()
        return result
        
    #---------------------------------------------------------------------------
    #  Applies the traits in the 'from' context to the 'to' context:
    #---------------------------------------------------------------------------
        
    def _apply_context ( self, from_context, to_context ):
        """ Applies the traits in the 'from' context to the 'to' context.
        """
        for name, value in from_context.items():
            to_context[ name ].copy_traits( value )

    #---------------------------------------------------------------------------
    #  Closes the window and saves changes (if allowed by the handler):
    #---------------------------------------------------------------------------
 
    def _on_ok ( self, event = None ):
        """ Closes the window and saves changes (if allowed by the handler).
        """
        if self.ui.handler.close( self.ui.info, True ):
            self._apply_context( self.ui.context, self.ui._context )
            self._close_page( wx.ID_OK )

    #---------------------------------------------------------------------------
    #  Closes the window and discards changes (if allowed by the handler):
    #---------------------------------------------------------------------------
 
    def _on_cancel ( self, event = None ):
        """ Closes the window and discards changes (if allowed by the handler).
        """
        if self.ui.handler.close( self.ui.info, False ):
            self._apply_context( self.ui._revert, self.ui._context )
            self._close_page( wx.ID_CANCEL )
    
    #---------------------------------------------------------------------------
    #  Handles the 'Help' button being clicked:
    #---------------------------------------------------------------------------
           
    def _on_help ( self, event ):
        """ Handles the 'Help' button being clicked.
        """
        show_help( self.ui, event.GetEventObject() )

    #---------------------------------------------------------------------------
    #  Closes the dialog window:
    #---------------------------------------------------------------------------
            
    def _close_page ( self, rc ):            
        """ Closes the dialog window.
        """
        self.EndModal( rc )
        self.ui.control = None
        self.ui.result  = (rc == wx.ID_OK)
        self.ui         = None
        self.Destroy()
 
    #---------------------------------------------------------------------------
    #  Handles the user hitting the 'Esc'ape key:
    #---------------------------------------------------------------------------
 
    def _on_key ( self, event ):
        """ Handles the user hitting the 'Esc'ape key.
        """
        if event.GetKeyCode() == 0x1B:
           self._on_cancel( event )
   
    #---------------------------------------------------------------------------
    #  Handles an 'Apply' all changes request:
    #---------------------------------------------------------------------------
           
    def _on_apply ( self, event ):
        """ Handles an 'Apply' changes request.
        """
        self._apply_context( self.ui.context, self.ui._context )
        self.revert.Enable( True )
        self.ui.modified = False
   
    #---------------------------------------------------------------------------
    #  Handles a 'Revert' all changes request:
    #---------------------------------------------------------------------------
           
    def _on_revert ( self, event ):
        """ Handles a 'Revert' changes request.
        """
        self._apply_context( self.ui._revert, self.ui._context )
        self._apply_context( self.ui._revert, self.ui.context )
        self.revert.Enable( False )
        self.ui.modified = False
            
    #---------------------------------------------------------------------------
    #  Handles the user interface 'modified' state changing:
    #---------------------------------------------------------------------------
            
    def _on_applyable ( self, state ):
        """ Handles the user interface 'modified' state changing.
        """
        self.apply.Enable( state )

    #---------------------------------------------------------------------------
    #  Creates a new dialog button:
    #---------------------------------------------------------------------------

    def _add_button ( self, label, action, sizer, enabled = True ):
        """ Creates a new dialog button.
        """
        button = wx.Button( self, -1, label )
        wx.EVT_BUTTON( self, button.GetId(), action )
        sizer.Add( button, 0, wx.LEFT, 5 )
        button.Enable( enabled )
        return button