Salome HOME
Copyright update 2022
[modules/gui.git] / tools / CurvePlot / src / python / views / CurveTabsView.py
1 # Copyright (C) 2016-2022  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 .View import View
21 from .XYView import XYView
22 from .utils import Logger
23
24 class CurveTabsView(View):
25   def __init__(self, controller):
26     View.__init__(self, controller)
27     self._XYViews = {}  # key: SALOME view ID, value: XYView
28       
29   def closeXYView(self, salomeViewID):
30     Logger.Debug("CurveTabsView::closeXYView: %d" % salomeViewID)
31     self._controller.setCurvePlotRequestingClose(True)
32     self._controller._sgPyQt.closeView(salomeViewID)
33     self._controller.setCurvePlotRequestingClose(False)
34     # Destroy the view
35     self._XYViews.pop(salomeViewID)
36     Logger.Debug("CurveTabsView::closeXYView count %d" % len(self._XYViews))
37   
38   def createXYView(self, model):
39     v = XYView(self._controller)
40     self._controller.associate(model, v)
41     return v
42   
43   def onCurrentPlotSetChange(self):
44     """ Avoid a unnecessary call to update() when just switching current plot set! """
45     cps = self._model.getCurrentPlotSet()
46     if not cps is None:
47       mp = self.mapModId2ViewId()
48       salomeViewID = mp[cps.getID()]
49       self._controller._sgPyQt.activateView(salomeViewID)
50   
51   def mapModId2ViewId(self):
52     """ Gives a map from model ID (the model behind the XYView) to view ID
53     """
54     lst = [(v._model.getID(), view_id) for view_id, v in list(self._XYViews.items())]
55     return dict(lst)
56   
57   def update(self):
58     """
59     Updates the list of tabs shown in the GUI
60     """
61     if self._model is None:
62         return
63     
64     # Check list of tabs:
65     set_mod = set(self._model._plotSets.keys())
66     set_view = { v._model.getID() for v in list(self._XYViews.values()) }
67     mp = self.mapModId2ViewId()
68     
69     # Deleted/Added curves:
70     dels = set_view - set_mod
71     added = set_mod - set_view
72     
73     for d in dels:
74       salomeViewID = mp[d]
75       v = self._XYViews[salomeViewID]
76       v.cleanBeforeClose()
77       self.closeXYView(salomeViewID)
78     
79     newViews = []
80     for a in added:
81       newViews.append(self.createXYView(self._model._plotSets[a]))
82     
83     # Now update all tabs individually (this will trigger creation of new ones if not already there):
84     for v in list(self._XYViews.values()) + newViews:
85       # The update on newViews will trigger the SALOME view creation:
86       v.update() 
87     
88     # And complete internal structure for new views
89     # This is not done in 
90     for v in newViews:
91       self._XYViews[v._salomeViewID] = v
92       
93     # Finally activate the proper tab:
94     self.onCurrentPlotSetChange()
95