~ubuntu-branches/debian/sid/mayavi2/sid

« back to all changes in this revision

Viewing changes to enthought/mayavi/core/ui/engine_rich_view.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-27 04:34:55 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090327043455-vs6ox32daj6ndw33
Tags: 3.2.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""A view of the pipeline with a panel to edit objects.
 
2
 
 
3
"""
 
4
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
 
5
# Copyright (c) 2009, Enthought, Inc.
 
6
# License: BSD Style.
 
7
 
 
8
# Enthought library imports.
 
9
from enthought.traits.ui.api import Item, View, HSplit, InstanceEditor
 
10
from enthought.traits.ui.menu import Action, Separator
 
11
from enthought.pyface.image_resource import ImageResource
 
12
from enthought.pyface.api import GUI
 
13
from enthought.mayavi.core.adder_node import SceneAdderNode
 
14
 
 
15
# Local imports.
 
16
from enthought.mayavi.core.scene import Scene
 
17
from enthought.mayavi.preferences.preference_manager_view import \
 
18
    preference_manager_view
 
19
from enthought.mayavi.core.ui.engine_view import EngineView, \
 
20
            EngineViewHandler
 
21
 
 
22
 
 
23
class EngineRichViewHandler(EngineViewHandler):
 
24
    """ A handler for the EngineRichView object. 
 
25
    """
 
26
 
 
27
    def init_info(self, info):
 
28
        """ Informs the handler what the UIInfo object for a View will be.
 
29
        Overridden here to add a callback on the creation of the view.
 
30
        
 
31
        """
 
32
        super(EngineRichViewHandler, self).init_info(info)
 
33
        info.on_trait_change(self.select_selected, 'initialized')
 
34
        return
 
35
 
 
36
 
 
37
    def select_selected(self, initialized):
 
38
        """ Force the tree editor to select the current engine selection,
 
39
            and eventually collapse other scenes.
 
40
        """
 
41
        # We need to explore the editors to find the one we are
 
42
        # interested in, and to switch its selection to None, and then 
 
43
        # back to what we are interested in.
 
44
        editors = self.info.ui._editors
 
45
        if editors is not None:
 
46
            for editor in editors:
 
47
                if editor.factory is self.info.object.tree_editor:
 
48
                    tree_editor = editor
 
49
                    break
 
50
            else:
 
51
                return
 
52
        else:
 
53
            return
 
54
 
 
55
        # We switch the selection to None, but we avoid
 
56
        # trait callback, to avoid changing the engine's
 
57
        # current_selection.
 
58
        tree_editor.set(selected=None, trait_change_notify=False)
 
59
        current_selection = self.info.object.engine.current_selection
 
60
        GUI.set_trait_later(tree_editor, 'selected', current_selection)
 
61
 
 
62
        # If we are selecting a scene, collapse the others
 
63
        if isinstance(current_selection, Scene) and \
 
64
                                    hasattr(tree_editor._tree, 'Collapse'):
 
65
            # The wx editor can collapse, dunno for the Qt
 
66
            for scene in self.info.object.engine.scenes:
 
67
                if scene is not current_selection:
 
68
                    tree_editor._tree.Collapse(
 
69
                                            tree_editor._get_object_nid(scene))
 
70
                        
 
71
 
 
72
    def _on_dclick(self, object):
 
73
        """ Called when a node in the tree editor is double-clicked.
 
74
        """
 
75
        if isinstance(object, SceneAdderNode):
 
76
            self.info.object._perform_new_scene()
 
77
        else:
 
78
            # In this view, we want the dialogs not to be modals, so that
 
79
            # the EngineRichView window can be closed while leaving
 
80
            # objects dialogs open.
 
81
            object.edit_traits(view=object.dialog_view())
 
82
 
 
83
 
 
84
##############################################################################
 
85
# EngineRichView class.
 
86
##############################################################################
 
87
class EngineRichView(EngineView):
 
88
    """ A view displaying the engine's object tree, alongside with a
 
89
        panel to edit the objects.
 
90
    """
 
91
 
 
92
    ###########################################################################
 
93
    # `HasTraits` interface.
 
94
    ###########################################################################
 
95
    def default_traits_view(self):
 
96
        """The default traits view of the Engine View.
 
97
        """
 
98
        view = View(HSplit(
 
99
                        Item('engine', 
 
100
                            id='mayavi.engine_rich_view.pipeline_view', 
 
101
                            springy=True,
 
102
                            resizable=True,
 
103
                            editor=self.tree_editor, 
 
104
                            dock='tab',
 
105
                            label='Pipeline'), 
 
106
                        Item('engine',
 
107
                            id='mayavi.engine_rich_view.current_selection', 
 
108
                            editor=InstanceEditor(
 
109
                                        view='current_selection_view'),
 
110
                            springy=True,
 
111
                            resizable=True,
 
112
                            style='custom'),
 
113
                    show_labels=False,
 
114
                    ),
 
115
                    id='enthought.mayavi.engine_rich_view',
 
116
                    help=False,
 
117
                    resizable=True,
 
118
                    undo=False,
 
119
                    revert=False,
 
120
                    ok=False,
 
121
                    cancel=False,
 
122
                    title='Mayavi pipeline',
 
123
                    icon=self.icon,
 
124
                    toolbar=self.toolbar,
 
125
                    handler=EngineRichViewHandler)
 
126
        return view
 
127
 
 
128
 
 
129
    def _actions_default(self):
 
130
        """ Append a preferences action to the toolbar: this view of the
 
131
            engine is meant to be a powerful view giving access to
 
132
            all of Mayavi's functionality.
 
133
        """
 
134
        preferences_action = \
 
135
            Action(
 
136
                image=ImageResource('preferences.png',
 
137
                                     search_path=self._image_path),
 
138
                tooltip="Modify Mayavi's preferences",
 
139
                checked=False,
 
140
                defined_when='True',
 
141
                perform=preference_manager_view.dialog_view,
 
142
            )
 
143
 
 
144
        actions = super(EngineRichView, self)._actions_default()
 
145
        actions.extend((Separator(), preferences_action))
 
146
        return actions
 
147
 
 
148
    ###########################################################################
 
149
    # EngineRichView interface.
 
150
    ###########################################################################
 
151
 
 
152
    def scene_editing_view(self, scene):
 
153
        # Selecting an object if good, because it forces the HSplit to 
 
154
        # choose a sensible split ratio
 
155
        for mayavi_scene in self.engine.scenes:
 
156
            sc = mayavi_scene.scene
 
157
            # Support for the `MlabSceneModel` where the `scene_editor`
 
158
            # trait contains the scene.
 
159
            s = getattr(sc, 'scene_editor', sc)
 
160
            if s is scene:
 
161
                self.engine.current_selection = mayavi_scene
 
162
 
 
163
        return self.edit_traits()
 
164
 
 
165
 
 
166
### EOF ######################################################################