Salome HOME
updated copyright message
[modules/gui.git] / tools / CurvePlot / src / python / views / CurveBrowserView.py
1 # Copyright (C) 2016-2023  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 from pyqtside import QtWidgets
21 from pyqtside.QtWidgets import QMenu
22 from pyqtside.QtCore import Qt
23
24 from .View import View
25 from .CurveTreeDockWidget import CurveTreeDockWidget
26 from .utils import Logger
27
28 class CurveBrowserView( View, CurveTreeDockWidget) :
29
30     def __init__( self, controller) :
31         """ Constructor """
32         View.__init__( self, controller)
33         CurveTreeDockWidget.__init__(self)
34         self._noUpdate = False
35         
36         treeWidget = self.getTreeWidget()
37         treeWidget.itemSelectionChanged.connect(self.onItemSelectionChanged)
38         treeWidget.itemDoubleClicked.connect(self.onItemDoubleCliked)
39         treeWidget.setContextMenuPolicy(Qt.CustomContextMenu)
40         treeWidget.customContextMenuRequested.connect(self.openMenu)
41             
42     def update(self):
43         """ Update this view due to model change """
44         if self._model == None or self._noUpdate:
45             return
46           
47         # widget clear and repopulation will trigger onItemSelectionChanged(),
48         # which in turns triggers update() (via setCurrentCurve()). So avoid recursion:
49         self._noUpdate = True
50         
51         treeWidget = self.getTreeWidget()
52         treeWidget.clear()
53         
54         plotSets = self._model._plotSets
55             
56         # The second (hidden) column in the tree bares the ID of the object and its nature (plotset or curve)
57         for p in list(plotSets.values()):
58           item = QtWidgets.QTreeWidgetItem([str(p.getTitle()), str(p.getID()) + '_set'])
59           treeWidget.addTopLevelItem(item)
60           for c in list(p._curves.values()):
61             chld = QtWidgets.QTreeWidgetItem([str(c.getTitle()), str(c.getID()) + '_crv'])
62             item.addChild(chld)
63           
64         treeWidget.expandAll()
65         
66         # Finally select the proper item in the tree:
67         cps = self._model.getCurrentPlotSet()
68         if not cps is None:
69           ccv = cps.getCurrentCurve()
70           if ccv is None:
71             key = str(cps.getID()) + '_set'
72           else:
73             key = str(ccv.getID()) + '_crv'
74           listItems = treeWidget.findItems(key, Qt.MatchExactly | Qt.MatchRecursive,1)
75           if len(listItems) > 0:
76             treeWidget.setCurrentItem(listItems[0])
77             
78         self._noUpdate = False
79     
80     def onItemSelectionChanged(self):
81         """
82         Change the current selected XYplot/curve        
83         """
84         if self._noUpdate:
85           return
86         
87         # setCurrentCurve() and setCurrentPlotSet() will trigger update(),
88         # which in turns triggers onItemSelectionChanged(). So avoid recursion:
89         self._noUpdate = True
90         
91         pm = self._controller._plotManager
92         treeWidget = self.getTreeWidget()
93         it = treeWidget.currentItem()
94         if not it is None:
95           objStr = str(it.text(1))   # no unicode here!
96           objID = int(objStr[0:-4])
97           objTyp = objStr[-3:]
98           if objTyp == 'crv':
99             # Find correct plot set:
100             cps = pm.getPlotSetContainingCurve(objID)
101             if not cps is None:
102               cps.setCurrentCurve(objID)
103               pm.setCurrentPlotSet(cps.getID())
104           elif objTyp == 'set':
105             pm.clearAllCurrentCurve()
106             pm.setCurrentPlotSet(objID)
107           else:
108             raise Exception("Internal error - should not happen")
109         else:
110           ps = pm.getCurrentPlotSet()
111           if not ps is None:
112             ps.setCurrentCurve(-1)
113           pm.setCurrentPlotSet(-1)
114           
115         self._noUpdate = False
116     
117     def onItemDoubleCliked(self):
118         Logger.Debug("item doubled clicked")
119     
120     def openMenu(self, position):
121         menu = self._controller._browserContextualMenu
122         treeWidget = self.getTreeWidget()
123         menu.exec_(treeWidget.mapToGlobal(position))