File: adder_node.py

package info (click to toggle)
mayavi2 4.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,892 kB
  • sloc: python: 49,447; javascript: 32,885; makefile: 129; fortran: 60
file content (397 lines) | stat: -rw-r--r-- 13,361 bytes parent folder | download | duplicates (5)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
Custom nodes for a Tree Editor that provide views for adding various nodes
to the tree.
"""
# Authors: Judah De Paula <judah@enthought.com>
#          Prabhu Ramachandran <prabhu_r@users.sf.net>
# Copyright (c) 2008, Enthought, Inc.
# License: BSD Style.

# Enthought library imports.
from traits.api import (HasTraits, Str, Property, Any, Button,
                                  List, Instance, provides,
                                  ToolbarButton)
from traitsui.api import View, Item, Group,\
        TextEditor, TreeEditor, TreeNode, ListEditor, ITreeNode
from pyface.api import ImageResource
from pyface.resource.api import resource_path

# Local imports.
from .registry import registry

###############################################################################
# AdderNode class
###############################################################################
@provides(ITreeNode)
class AdderNode(TreeNode):
    """ Base class that will display a TreeNode to add items to the tree.
    """

    # String to be shown in the TreeEditor.
    label = Str('Base AdderNode')

    # Default tooltip for this class.
    tooltip = Str('Add an item')

    # The parent object that should be manipulated for adding children.
    object =  Any

    # Duck-typing is necessary since Mayavi assumes nodes always have scenes.
    scene = Property

    # Trait view to show in the Mayavi current object panel.
    view = View(Group(label='AdderNode'))

    def dialog_view(self):
        """ View shown by double-clicking on the node.  Same as in Base().
        """
        view = self.trait_view()
        view.buttons = [ ]
        view.title = self.label
        view.icon = ImageResource('add.ico')
        view.resizable = True
        view.width = 350
        view.height = 650
        return view

    def _get_scene(self):
        """ Trait Property getter for 'scene'.
        """
        object = self.object
        if isinstance(object, AdderNode):
            return None
        if object is not None:
            return object.scene
        else:
            return None

    #------------------------------------------------------------------------
    # The ITreeNode interface needed by the Qt tree_editor
    #------------------------------------------------------------------------

    def get_label(self):
        return self.label

    def get_icon(self, obj, is_expanded=False):
        return self.icon_name

    def get_icon_path(self):
        return resource_path()

    def get_tooltip(self):
        return self.tooltip

    def allows_children(self):
        return False

    def get_children_id(self, node=None):
        return []

    def when_label_changed(self, label_updated, remove):
        return

    def when_column_labels_change(self, listener, remove):
        return

###############################################################################
# SceneAdderNode class
###############################################################################
class SceneAdderNode(AdderNode):
    """ Subclass for adding Scene nodes to a Mayavi Engine node.
    """

    # String to be shown in the TreeEditor.
    label = Str('Add a new scene')

    # The name of the icon
    icon_name = Str('add_scene.png')

    # Button for the View.
    add_scene = Button('Add a new scene',
                      image=ImageResource('add_scene.png'))

    # Trait view to show in the Mayavi current object panel.
    view = View(Group(Item('add_scene', show_label=False, style='custom'),
                      label='Add a scene'))


    def _add_scene_fired(self):
        """ Trait handler for when the add_scene button is clicked.
        """
        self.object.new_scene()


###############################################################################
# DocumentedItem class
###############################################################################
class DocumentedItem(HasTraits):
    """ Container to hold a name and a documentation for an action.
    """

    # Name of the action
    name = Str

    # Button to trigger the action
    add = ToolbarButton('Add', orientation='horizontal',
                    image=ImageResource('add.ico'))

    # Object the action will apply on
    object = Any

    # Two lines documentation for the action
    documentation = Str

    view = View('_',
                Item('add', style='custom', show_label=False),
                Item('documentation', style='readonly',
                    editor=TextEditor(multi_line=True),
                    resizable=True,
                    show_label=False),
                )

    def _add_fired(self):
        """ Trait handler for when the add_source button is clicked in
            one of the sub objects in the list.
        """
        action = getattr(self.object.menu_helper, self.id)
        action()


def documented_item_factory(name='', documentation='',
                id='', object=None):
    """ Factory for creating a DocumentedItem with the right button
        label.
    """
    documentation = documentation.replace('\n', '')
    documentation = documentation.replace('  ', '')

    class MyDocumentedItem(DocumentedItem):
        add = ToolbarButton('%s' % name, orientation='horizontal',
                        image=ImageResource('add.ico'))

    return MyDocumentedItem(
                        name=name,
                        documentation=documentation,
                        id=id,
                        object=object)


###############################################################################
# ListAdderNode class
###############################################################################
class ListAdderNode(AdderNode):
    """ A node for adding object, with a list of objects to add generated
        from the registry.
    """

    # The list of items to display to the user.
    items_list = List(DocumentedItem)

    # A reference to the registry, to generate this list.
    items_list_source = List()

    # Selected item
    selected_item = Instance(DocumentedItem)

    # A reference to self, to allow to build the tree view.
    self = Instance(AdderNode)

    # The icon of the displayed objects
    icon_name = Str('add.ico')

    def _self_default(self):
        return self

    def default_traits_view(self):
        nodes = [TreeNode(node_for=[AdderNode],
                          label='name',
                          copy=False,
                          delete=False,
                          rename=False,
                          children='items_list',
                          ),
                 TreeNode(node_for=[DocumentedItem],
                          label='name',
                          copy=False,
                          delete=False,
                          rename=False,
                          icon_item=self.icon_name,
                          ),
                 ]

        tree_editor = TreeEditor(editable=False,
                                 hide_root=True,
                                 orientation='vertical',
                                 selected='object.selected_item',
                                 nodes=nodes,
                                 on_dclick='object._on_tree_dclick',
                                 )

        view = View(Item('self',
                            show_label=False,
                            editor=tree_editor,
                            resizable=True,
                            springy=True,
                            height=0.5),
                    Item('selected_item', style='custom', show_label=False,
                            height=0.5),
                    resizable=True)
        return view


    def _object_changed(self, value):
        """ Trait handler for when the self.object trait changes.
        """
        result = []
        if value is not None:
            # Don't need 'x', but do need to generate the actions.
            x = value.menu_helper.actions
            for src in self.items_list_source:
                if not self._is_action_suitable(value, src):
                    continue
                name = src.menu_name.replace('&','')
                result.append(
                        documented_item_factory(
                                name=name,
                                documentation=src.help,
                                id=src.id,
                                object=value)
                        )
        self.items_list = result


    def _is_action_suitable(self, object, src):
        """ Check that the action described by src can be applied on the
            given object.
        """
        if  hasattr(object.menu_helper, 'check_%s' % src.id) \
                and getattr(object.menu_helper, 'check_%s' % src.id)():
            return True
        else:
            return False

    def _on_tree_dclick(self, object):
        """ Called when an user double clicks on an item in the tree
            view.
        """
        object._add_fired()


###############################################################################
# SourceAdderNode class
###############################################################################
class SourceAdderNode(ListAdderNode):
    """ Tree node that presents a view to the user to add a scene source.
    """

    # Button for adding a data file, with automatic format checking.
    open_file = ToolbarButton('Load data from file',
                                orientation='horizontal',
                                image=ImageResource('file.png'))

    # A reference to the registry, to generate this list.
    items_list_source = [source for source in registry.sources
                         if len(source.extensions) == 0]

    # The string to display on the icon in the TreeEditor.
    label = 'Add Data Source'

    # The icon of the displayed objects
    icon_name = Str('source.ico')

    # Trait view to show in the Mayavi current object panel.
    def default_traits_view(self):
        return View(Group(Group(Item('open_file', style='custom'),
                      show_labels=False, show_border=False),
                      Item('items_list', style='readonly',
                            editor=ListEditor(style='custom')),
                      show_labels=False,
                      label='Add a data source'))

    def _open_file_fired(self):
        """ Trait handler for when the open_file button is clicked.
        """
        self.object.menu_helper.open_file_action()

    def _is_action_suitable(self, object, src):
        return True


###############################################################################
# ModuleAdderNode class
###############################################################################
class ModuleAdderNode(ListAdderNode):
    """ Tree node that presents a view to the user to add modules.
    """
    # String to be shown in the TreeEditor.
    label = Str('Add a visualization module')

    # The icon of the displayed objects
    icon_name = Str('module.ico')

    # A reference to the registry, to generate this list.
    items_list_source = registry.modules

    def _object_changed(self, value):
        if value is not None:
            value.menu_helper._build_filter_actions()
        ListAdderNode._object_changed(self, value)


###############################################################################
# FilterAdderNode class
###############################################################################
class FilterAdderNode(ListAdderNode):
    """ Tree node that presents a view to the user to add filters.
    """
    # String to be shown in the TreeEditor.
    label = Str('Add a processing filter')

    # The icon of the displayed objects
    icon_name = Str('filter.ico')

    # A reference to the registry, to generate this list.
    items_list_source = registry.filters


###############################################################################
# ModuleFilterAdderNode class
###############################################################################
class ModuleFilterAdderNode(AdderNode):
    """ Tree node that presents a view to the user to add filter and
        modules.
    """

    # The string to display on the icon in the TreeEditor.
    label = 'Add module or filter'

    # An adder node for modules
    modules = Instance(ModuleAdderNode, ())

    # An adder node for filters
    filters = Instance(FilterAdderNode, ())

    def _object_changed(self):
        """ Propagate the object to the sub nodes.
        """
        self.filters.object = self.object
        self.modules.object = self.object

    # Trait view to show in the Mayavi current object panel.
    view = View(
                Group(Item('modules', style='custom', springy=True,
                            resizable=True,
                            height=1.,
                            ),
                    show_labels=False,
                    label='Visualization modules'),
                Group(Item('filters', style='custom', springy=True,
                            resizable=True,
                            height=1.,
                            ),
                    show_labels=False,
                    label='Processing filters'),
                )


### EOF #######################################################################