File: image_enum_editor.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 (316 lines) | stat: -rw-r--r-- 13,554 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#------------------------------------------------------------------------------
# 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: Define the Tkinter implementation of the various image
#              enumeration editors and the image enumeration editor factory.
#
#  Symbols defined: ToolkitEditorFactory
#
#------------------------------------------------------------------------------

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

import sys
import tk

from os               import getcwd
from os.path          import join, dirname, exists
from enum_editor      import ToolkitEditorFactory as EditorFactory
from editor           import Editor
from helper           import bitmap_cache, position_near
from image_control    import ImageControl
from enthought.traits.api import Str, Type, Module, Any

#-------------------------------------------------------------------------------
#  'ToolkitEditorFactory' class:
#-------------------------------------------------------------------------------

class ToolkitEditorFactory ( EditorFactory ):
    
    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------
    
    suffix = Str    # Suffix to add to values to form image names
    path   = Str    # Path to use to locate image files
    klass  = Type   # Class used to derive the path to the image files
    module = Module # Module used to derive the path to the image files
    
    #---------------------------------------------------------------------------
    #  Performs any initialization needed after all constructor traits have 
    #  been set:
    #---------------------------------------------------------------------------
     
    def init ( self ):
        """ Performs any initialization needed after all constructor traits 
            have been set.
        """
        super( ToolkitEditorFactory, self ).init()
        self._update_path()
        
    #---------------------------------------------------------------------------
    #  Handles one of the items defining the path being updated:
    #---------------------------------------------------------------------------
        
    def _update_path ( self ):
        """ Handles one of the items defining the path being updated.
        """
        if self.path != '':
            self._image_path = self.path
        elif self.klass is not None:
            if self.klass.__module__ == '__main__':
                dirs = [ join( dirname( sys.argv[0] ), 'images' ),
                         join( getcwd(), 'images' ) ]
                self._image_path = self.path
                for d in dirs:
                    if exists( d ):
                        self._image_path = d
                        break
            else:
                self._image_path = join( dirname( sys.modules[ 
                    self.klass.__module__ ].__file__ ), 'images' )
        elif self.module is not None:
            self._image_path = join( dirname( self.module.__file__ ), 'images' )
           
    def _path_changed ( self ):            
        self._update_path()
        
    def _klass_changed ( self ):        
        self._update_path()
        
    def _module_changed ( self ):        
        self._update_path()
    
    #---------------------------------------------------------------------------
    #  'Editor' factory methods:
    #---------------------------------------------------------------------------
    
    def simple_editor ( self, ui, object, name, description, parent ):
        return SimpleEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def custom_editor ( self, ui, object, name, description, parent ):
        return CustomEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object, 
                             name        = name, 
                             description = description ) 
    
    def readonly_editor ( self, ui, object, name, description, parent ):
        return ReadonlyEditor( parent,
                               factory     = self, 
                               ui          = ui, 
                               object      = object, 
                               name        = name, 
                               description = description ) 
                                      
#-------------------------------------------------------------------------------
#  'ReadonlyEditor' class:
#-------------------------------------------------------------------------------
                               
class ReadonlyEditor ( Editor ):
        
    #---------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    #---------------------------------------------------------------------------
        
    def init ( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self.control = ImageControl( parent,  
                             bitmap_cache( self.str_value + self.factory.suffix, 
                                           False, self.factory._image_path ) )                                   
        
    #---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #---------------------------------------------------------------------------
        
    def update_editor ( self ):
        """ Updates the editor when the object trait changes external to the 
            editor.
        """
        self.control.Bitmap( bitmap_cache( self.str_value + self.factory.suffix, 
                                           False, self.factory._image_path ) )
                                      
#-------------------------------------------------------------------------------
#  'SimpleEditor' class:
#-------------------------------------------------------------------------------
                               
class SimpleEditor ( ReadonlyEditor ):
        
    #---------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    #---------------------------------------------------------------------------
        
    def init ( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        super( SimpleEditor, self ).init( parent )
        self.control.Selected( True )
        self.control.Handler( self.popup_editor )
        
    #---------------------------------------------------------------------------
    #  Handles the user clicking the ImageControl to display the pop-up dialog:
    #---------------------------------------------------------------------------
        
    def popup_editor ( self, control ):
        """ Handles the user clicking the ImageControl to display the pop-up 
            dialog.
        """
        ImageEnumDialog( self )
                                      
#-------------------------------------------------------------------------------
#  'CustomEditor' class:
#-------------------------------------------------------------------------------
                               
class CustomEditor ( Editor ):
    
    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------
    
    update_handler = Any  # Callback to call when any button clicked
        
    #---------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    #---------------------------------------------------------------------------
        
    def init ( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self._create_image_grid( parent )

    #---------------------------------------------------------------------------
    #  Populates a specified window with a grid of image buttons:
    #---------------------------------------------------------------------------
    
    def _create_image_grid ( self, parent ):
        """ Populates a specified window with a grid of image buttons.
        """
        # Create the panel to hold the ImageControl buttons:
        self.control = panel = wx.Panel( parent, -1 )
        
        # Create the main sizer:
        if self.factory.cols > 1:
           sizer = wx.GridSizer( 0, self.factory.cols, 0, 0 )
        else:
           sizer = wx.BoxSizer( wx.VERTICAL )
        
        # Add the set of all possible choices:
        cur_value = self.str_value
        for value in self.factory._names:
            control = ImageControl( panel, 
                          bitmap_cache( value + self.factory.suffix, False, 
                                        self.factory._image_path ),
                          value == cur_value, 
                          self.update_object )
            control.value = value
            sizer.Add( control, 0, wx.ALL, 2 )
            self.set_tooltip( control )
 
        # Finish setting up the control layout:
        panel.SetAutoLayout( True )
        panel.SetSizer( sizer )
        sizer.Fit( panel )

    #---------------------------------------------------------------------------
    #  Handles the user clicking on an ImageControl to set an object value:
    #---------------------------------------------------------------------------
  
    def update_object ( self, control ):
        """ Handles the user clicking on an ImageControl to set an object value.
        """
        self.value = control.value
        if self.update_handler is not None:
            self.update_handler()
        
    #---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #---------------------------------------------------------------------------
        
    def update_editor ( self ):
        """ Updates the editor when the object trait changes external to the 
            editor.
        """
        value = self.str_value
        for control in self.control.GetChildren():
            control.Selected( value == control.value )

#-------------------------------------------------------------------------------
#  'ImageEnumDialog' class:  
#-------------------------------------------------------------------------------

class ImageEnumDialog ( wx.Frame ):

    #---------------------------------------------------------------------------
    #  Initializes the object:
    #---------------------------------------------------------------------------
 
    def __init__ ( self, editor ):
        """ Initializes the object.
        """
        wx.Frame.__init__( self, editor.control, -1, '',
                           style = wx.SIMPLE_BORDER )
        wx.EVT_ACTIVATE( self, self._on_close_dialog )
        self._closed = False
        
        dlg_editor = CustomEditor( self,
                                   factory        = editor.factory, 
                                   ui             = editor.ui, 
                                   object         = editor.object, 
                                   name           = editor.name, 
                                   description    = editor.description,
                                   update_handler = self._close_dialog )
        
        # Wrap the dialog around the image button panel:
        sizer = wx.BoxSizer( wx.VERTICAL )
        sizer.Add( dlg_editor.control )
        sizer.Fit( self )
 
        # Position the dialog:
        position_near( editor.control, self )
        self.Show()
        
    #---------------------------------------------------------------------------
    #  Closes the dialog:
    #---------------------------------------------------------------------------
 
    def _on_close_dialog ( self, event ):
        """ Closes the dialog.
        """
        if not event.GetActive():
            self._close_dialog()
 
    #---------------------------------------------------------------------------
    #  Closes the dialog:
    #---------------------------------------------------------------------------
 
    def _close_dialog ( self ):
        """ Closes the dialog.
        """
        if not self._closed:
            self._closed = True
            self.Destroy()