Salome HOME
1c8318e49d00e6f65807b0c45356b135d5f96d58
[modules/gui.git] / tools / CurvePlot / src / python / model / PlotManager.py
1 # Copyright (C) 2016-2024  CEA, EDF
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 .Model import Model
21 from .XYPlotSetModel import XYPlotSetModel
22 from .utils import Logger
23
24 class PlotManager(Model):
25   def __init__(self, controller):
26     from collections import OrderedDict
27     Model.__init__(self, controller)
28     self._currentPlotSet = None
29     self._plotSets = OrderedDict()    # key: plotSet ID, value: instance of XYPlotSetModel. We use an OrderedDict so that
30                                       # when removing elemetns, we can easily re-select the last-but-one.
31     self._lockRepaint = False  # if True, repaint routines are blocked.
32     self._plotSetsRepaint = set() # plot waiting for repaint/update while repaint is locked
33
34   def isEmpty(self):
35     return len(self._plotSets) == 0
36
37   def setCurrentPlotSet(self, plotSetID, silent=False):
38     if plotSetID not in self._plotSets and plotSetID != -1:
39       raise ValueError("Invalid plot set ID (%d)!" % plotSetID)
40     self._currentPlotSet = self._plotSets.get(plotSetID, None)
41     if not silent:
42       self.notifyChange("CurrentPlotSetChange")
43
44   def getCurrentPlotSet(self):
45     return self._currentPlotSet
46
47   def getPlotSetContainingCurve(self, curveID):
48     for ps in list(self._plotSets.values()):
49       if curveID in ps._curves:
50         return ps
51     return None
52
53   def setCurrentCurve(self, curveId):
54     ps = self.getPlotSetContainingCurve(curveId)
55     if ps is None and curveId != -1:
56       raise ValueError("Invalid curve ID (%d)!" % curveId)
57     self.clearAllCurrentCurve()
58     if curveId == -1:
59       return -1
60     ps_id = ps.getID()
61     currPs = self.getCurrentPlotSet()
62     if currPs is None or currPs.getID() != ps_id:
63       self.setCurrentPlotSet(ps_id)
64     ps.setCurrentCurve(curveId)
65     return ps_id
66
67   def getCurrentCurve(self):
68     ps = self.getCurrentPlotSet()
69     if ps is None:
70       return None
71     return ps.getCurrentCurve()
72
73   def clearAllCurrentCurve(self, silent=False):
74     for psID in self._plotSets:
75       self._plotSets[psID].setCurrentCurve(-1)
76     if not silent:
77       self.notifyChange("CurrentCurveChange")
78
79   def createXYPlotSet(self, silent=False):
80     cv = XYPlotSetModel(self._controller)
81     self._plotSets[cv.getID()] = cv
82     self._currentPlotSet = cv
83     if not silent:
84       self.notifyChange("NewPlotSet")
85     return cv
86
87   def removeXYPlotSet(self, plotSetID):
88     Logger.Debug("====> PlotManager::removeXYPlotSet() %d" % plotSetID)
89     if plotSetID not in self._plotSets:
90       print(self._plotSets)
91       raise ValueError("Plot set ID (%d) not found for deletion!" % plotSetID)
92     ps = self._plotSets.pop(plotSetID)
93     if self._currentPlotSet is ps:
94       self._currentPlotSet = None
95     self.notifyChange("RemovePlotSet")
96     return ps
97
98   def clearAll(self):
99     self._plotSets = {}
100     self._currentPlotSet = None
101     self.notifyChange("ClearAll")
102
103   def lockRepaint(self):
104     self._lockRepaint = True
105     self._plotSetsRepaint = set()
106
107   def isRepaintLocked(self):
108     return self._lockRepaint
109
110   def registerRepaint(self, ps_id):
111     self._plotSetsRepaint.add(ps_id)
112
113   def unlockRepaint(self):
114     self._lockRepaint = False
115     for obj in self._plotSetsRepaint:
116       obj.notifyChange()
117     self._plotSetsRepaint = set()
118