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