File: custom_feature.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (334 lines) | stat: -rw-r--r-- 13,754 bytes parent folder | download
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#-------------------------------------------------------------------------------
#
#  Adds a 'custom' feature to DockWindow which allows views to contribute
#  custom features to their own tab.
#
#  Written by: David C. Morrill
#
#  Date: 07/16/2006
#
#  (c) Copyright 2006 by David C. Morrill
#
#-------------------------------------------------------------------------------

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

from enthought.traits.api \
    import HasTraits, HasPrivateTraits, Property, Instance, Str, true

from enthought.traits.ui.api \
    import View

from enthought.pyface.dock.api \
    import DockWindowFeature

from enthought.pyface.image_resource \
    import ImageResource

#-------------------------------------------------------------------------------
#  Constants:
#-------------------------------------------------------------------------------

# Standard sequence types:
SequenceTypes = ( list, tuple )

#-------------------------------------------------------------------------------
#  'CustomFeature' class:
#-------------------------------------------------------------------------------

class CustomFeature ( HasPrivateTraits ):

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

    # The current image to display on the feature bar:
    image = Instance( ImageResource, allow_none = True )

    # The tooltip to display when the mouse is hovering over the image:
    tooltip = Str

    # Is the feature currently enabled?
    enabled = true

    # Name of the method to invoke on a left click:
    click = Str

    # Name of the method to invoke on a right click:
    right_click = Str

    # Name of the method to invoke when the user starts to drag with the left
    # mouse button:
    drag = Str

    # Name of the method to invoke when the user starts to ctrl-drag with the
    # left mouse button:
    control_drag = Str

    # Name of the method to invoke when the user starts to shift-drag with the
    # left mouse button:
    shift_drag = Str

    # Name of the method to invoke when the user starts to alt-drag with the
    # left mouse button:
    alt_drag = Str

    # Name of the method to invoke when the user starts to drag with the right
    # mouse button:
    right_drag = Str

    # Name of the method to invoke when the user starts to ctrl-drag with the
    # right mouse button:
    control_right_drag = Str

    # Name of the method to invoke when the user starts to shift-drag with the
    # right mouse button:
    shift_right_drag = Str

    # Name of the method to invoke when the user starts to alt-drag with the
    # right mouse button:
    alt_right_drag = Str

    # Name of the method to invoke when the user drops an object:
    drop = Str

    # Name of the method to invoke to see if the user can drop an object:
    can_drop = Str

#-------------------------------------------------------------------------------
#  'ACustomFeature' class:
#-------------------------------------------------------------------------------

class ACustomFeature ( DockWindowFeature ):

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

    # The CustomFeature associated with this feature:
    custom_feature = Instance( CustomFeature )

    # The current image to display on the feature bar:
    image = Property

    # The tooltip to display when the mouse is hovering over the image:
    tooltip = Property

#-- Property Implementations ---------------------------------------------------

    def _get_image ( self ):
        if self.custom_feature.enabled:
            return self.custom_feature.image

        return None

    def _get_tooltip ( self ):
        return self.custom_feature.tooltip

#-- Overrides of DockWindowFeature Methods -------------------------------------

    #---------------------------------------------------------------------------
    #  Handles the user left clicking on the feature image:
    #---------------------------------------------------------------------------

    def click ( self ):
        """ Handles the user left clicking on the feature image.
        """
        self.dynamic_call( 'click' )

    #---------------------------------------------------------------------------
    #  Handles the user right clicking on the feature image:
    #---------------------------------------------------------------------------

    def right_click ( self ):
        """ Handles the user right clicking on the feature image.
        """
        self.dynamic_call( 'right_click' )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the left mouse button:
    #---------------------------------------------------------------------------

    def drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the left mouse button.
        """
        return self.dynamic_call( 'drag', None )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the left mouse button while holding down the 'Ctrl' key:
    #---------------------------------------------------------------------------

    def control_drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the left mouse button while holding down the 'Ctrl' key:
        """
        return self.dynamic_call( 'control_drag', None )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the left mouse button while holding down the 'Shift' key:
    #---------------------------------------------------------------------------

    def shift_drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the left mouse button while holding down the 'Shift' key.
        """
        return self.dynamic_call( 'shift_drag', None )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the left mouse button while holding down the 'Alt' key:
    #---------------------------------------------------------------------------

    def alt_drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the left mouse button while holding down the 'Alt' key:
        """
        return self.dynamic_call( 'alt_drag', None )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the right mouse button:
    #---------------------------------------------------------------------------

    def right_drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the right mouse button.
        """
        return self.dynamic_call( 'right_drag', None )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the right mouse button while holding down the 'Ctrl' key:
    #---------------------------------------------------------------------------

    def control_right_drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the right mouse button while holding down the 'Ctrl' key:
        """
        return self.dynamic_call( 'control_right_drag', None )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the right mouse button while holding down the 'Shift' key:
    #---------------------------------------------------------------------------

    def shift_right_drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the right mouse button while holding down the 'Shift'
            key.
        """
        return self.dynamic_call( 'shift_right_drag', None )

    #---------------------------------------------------------------------------
    #  Returns the object to be dragged when the user drags the feature image
    #  with the right mouse button while holding down the 'Alt' key:
    #---------------------------------------------------------------------------

    def alt_right_drag ( self ):
        """ Returns the object to be dragged when the user drags the feature
            image with the right mouse button while holding down the 'Alt' key:
        """
        return self.dynamic_call( 'alt_right_drag', None )

    #---------------------------------------------------------------------------
    #  Handles the user dropping a specified object on the feature image:
    #---------------------------------------------------------------------------

    def drop ( self, object ):
        """ Handles the user dropping a specified object on the feature image.
        """
        return self.dynamic_call( 'drop', args = ( object, ) )

    #---------------------------------------------------------------------------
    #  Returns whether a specified object can be dropped on the feature image:
    #---------------------------------------------------------------------------

    def can_drop ( self, object ):
        """ Returns whether a specified object can be dropped on the feature
            image.
        """
        return self.dynamic_call( 'can_drop', False, args = ( object, ) )

    #---------------------------------------------------------------------------
    #  Performs any clean-up needed when the feature is being removed:
    #---------------------------------------------------------------------------

    def dispose ( self ):
        """ Performs any clean-up needed when the feature is being removed.
        """
        self.custom_feature.on_trait_change( self._custom_feature_updated,
                                             remove = True )

#-- ACustomFeature Methods ------------------------------------------------------

    #---------------------------------------------------------------------------
    #  Performs a method invocation using the associated CustomFeature:
    #---------------------------------------------------------------------------

    def dynamic_call ( self, method, default = None, args = () ):
        """ Performs a method invocation using the associated CustomFeature.
        """
        method = getattr( self.custom_feature, method )
        if method == '':
            return default

        return getattr( self.dock_control.object, method )( *args )

    #---------------------------------------------------------------------------
    #  Handles the 'custom_feature' trait being changed:
    #---------------------------------------------------------------------------

    def _custom_feature_changed ( self, custom_feature ):
        """ Handles the 'custom_feature' trait being changed.
        """
        custom_feature.on_trait_change( self._custom_feature_updated )

    #---------------------------------------------------------------------------
    #  Handles any trait on the associated dynamic feature being changed:
    #---------------------------------------------------------------------------

    def _custom_feature_updated ( self, object, name, old, new ):
        """ Handles any trait on the associated dynamic feature being changed.
        """
        self._bitmap = None
        self.refresh()

#-- Overidable Class Methods ---------------------------------------------------

    #---------------------------------------------------------------------------
    #  Returns a feature object for use with the specified DockControl (or None
    #  if the feature does not apply to the DockControl object):
    #---------------------------------------------------------------------------

    def feature_for ( cls, dock_control ):
        """ Returns a feature object for use with the specified DockControl (or
            None if the feature does not apply to the DockControl object).
        """
        from enthought.pyface.dock.features.api import is_not_none

        object = dock_control.object
        if isinstance( object, HasTraits ):
            result = []
            for name in object.trait_names( custom_feature = is_not_none ):
                custom_feature = getattr( object, name, None )
                if isinstance( custom_feature, CustomFeature ):
                    result.append( cls( dock_control   = dock_control,
                                        custom_feature = custom_feature ) )
                elif isinstance( custom_feature, SequenceTypes ):
                    for feature in custom_feature:
                        if isinstance( feature, CustomFeature ):
                            result.append( cls( dock_control   = dock_control,
                                                custom_feature = feature ) )

            return result

        return None

    feature_for = classmethod( feature_for )