summaryrefslogtreecommitdiff
path: root/silx/gui/plot3d/SceneWindow.py
blob: 5121a17bb9e04f4d8a8b41ffd63b2a1b01cb8e70 (plain)
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""This module provides a QMainWindow with a 3D SceneWidget and toolbars.
"""

from __future__ import absolute_import

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "29/11/2017"


from ...gui import qt, icons

from .actions.mode import InteractiveModeAction
from .SceneWidget import SceneWidget
from .tools import OutputToolBar, InteractiveModeToolBar, ViewpointToolBar
from .tools.GroupPropertiesWidget import GroupPropertiesWidget

from .ParamTreeView import ParamTreeView

# Imported here for convenience
from . import items  # noqa


__all__ = ['items', 'SceneWidget', 'SceneWindow']


class _PanPlaneAction(InteractiveModeAction):
    """QAction to set plane pan interaction on a Plot3DWidget

    :param parent: See :class:`QAction`
    :param ~silx.gui.plot3d.Plot3DWidget.Plot3DWidget plot3d:
        Plot3DWidget the action is associated with
    """
    def __init__(self, parent, plot3d=None):
        super(_PanPlaneAction, self).__init__(
            parent, 'panSelectedPlane', plot3d)
        self.setIcon(icons.getQIcon('3d-plane-pan'))
        self.setText('Pan plane')
        self.setCheckable(True)
        self.setToolTip(
            'Pan selected plane. Press <b>Ctrl</b> to rotate the scene.')

    def _planeChanged(self, event):
        """Handle plane updates"""
        if event in (items.ItemChangedType.VISIBLE,
                     items.ItemChangedType.POSITION):
            plane = self.sender()

            isPlaneInteractive = \
                plane._getPlane().plane.isPlane and plane.isVisible()

            if isPlaneInteractive != self.isEnabled():
                self.setEnabled(isPlaneInteractive)
                mode = 'panSelectedPlane' if isPlaneInteractive else 'rotate'
                self.getPlot3DWidget().setInteractiveMode(mode)

    def _selectionChanged(self, current, previous):
        """Handle selected object change"""
        if isinstance(previous, items.PlaneMixIn):
            previous.sigItemChanged.disconnect(self._planeChanged)

        if isinstance(current, items.PlaneMixIn):
            current.sigItemChanged.connect(self._planeChanged)
            self.setEnabled(True)
            self.getPlot3DWidget().setInteractiveMode('panSelectedPlane')
        else:
            self.setEnabled(False)

    def setPlot3DWidget(self, widget):
        previous = self.getPlot3DWidget()
        if isinstance(previous, SceneWidget):
            previous.selection().sigCurrentChanged.disconnect(
                self._selectionChanged)
            self._selectionChanged(
                None, previous.selection().getCurrentItem())

        super(_PanPlaneAction, self).setPlot3DWidget(widget)

        if isinstance(widget, SceneWidget):
            self._selectionChanged(widget.selection().getCurrentItem(), None)
            widget.selection().sigCurrentChanged.connect(
                self._selectionChanged)


class SceneWindow(qt.QMainWindow):
    """OpenGL 3D scene widget with toolbars."""

    def __init__(self, parent=None):
        super(SceneWindow, self).__init__(parent)
        if parent is not None:
            # behave as a widget
            self.setWindowFlags(qt.Qt.Widget)

        self._sceneWidget = SceneWidget()
        self.setCentralWidget(self._sceneWidget)

        self._interactiveModeToolBar = InteractiveModeToolBar(parent=self)
        panPlaneAction = _PanPlaneAction(self, plot3d=self._sceneWidget)
        self._interactiveModeToolBar.addAction(panPlaneAction)

        self._viewpointToolBar = ViewpointToolBar(parent=self)
        self._outputToolBar = OutputToolBar(parent=self)

        for toolbar in (self._interactiveModeToolBar,
                        self._viewpointToolBar,
                        self._outputToolBar):
            toolbar.setPlot3DWidget(self._sceneWidget)
            self.addToolBar(toolbar)
            self.addActions(toolbar.actions())

        self._paramTreeView = ParamTreeView()
        self._paramTreeView.setModel(self._sceneWidget.model())

        selectionModel = self._paramTreeView.selectionModel()
        self._sceneWidget.selection()._setSyncSelectionModel(
            selectionModel)

        paramDock = qt.QDockWidget()
        paramDock.setWindowTitle('Object parameters')
        paramDock.setWidget(self._paramTreeView)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, paramDock)

        self._sceneGroupResetWidget = GroupPropertiesWidget()
        self._sceneGroupResetWidget.setGroup(
            self._sceneWidget.getSceneGroup())

        resetDock = qt.QDockWidget()
        resetDock.setWindowTitle('Global parameters')
        resetDock.setWidget(self._sceneGroupResetWidget)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, resetDock)
        self.tabifyDockWidget(paramDock, resetDock)

        paramDock.raise_()

    def getSceneWidget(self):
        """Returns the SceneWidget of this window.

        :rtype: ~silx.gui.plot3d.SceneWidget.SceneWidget
        """
        return self._sceneWidget

    def getParamTreeView(self):
        """Returns the :class:`ParamTreeView` of this window.

        :rtype: ParamTreeView
        """
        return self._paramTreeView

    def getInteractiveModeToolBar(self):
        """Returns the interactive mode toolbar.

        :rtype: InteractiveModeToolBar
        """
        return self._interactiveModeToolBar

    def getViewpointToolBar(self):
        """Returns the viewpoint toolbar.

        :rtype: ViewpointToolBar
        """
        return self._viewpointToolBar

    def getOutputToolBar(self):
        """Returns the output toolbar.

        :rtype: OutputToolBar
        """
        return self._outputToolBar