1 from Model import Model
2 from XYPlotSetModel import XYPlotSetModel
3 from utils import Logger
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
16 return len(self._plotSets) == 0
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)
23 self.notifyChange("CurrentPlotSetChange")
25 def getCurrentPlotSet(self):
26 return self._currentPlotSet
28 def getPlotSetContainingCurve(self, curveID):
29 for ps in list(self._plotSets.values()):
30 if curveID in ps._curves:
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()
42 currPs = self.getCurrentPlotSet()
43 if currPs is None or currPs.getID() != ps_id:
44 self.setCurrentPlotSet(ps_id)
45 ps.setCurrentCurve(curveId)
48 def getCurrentCurve(self):
49 ps = self.getCurrentPlotSet()
52 return ps.getCurrentCurve()
54 def clearAllCurrentCurve(self, silent=False):
55 for psID in self._plotSets:
56 self._plotSets[psID].setCurrentCurve(-1)
58 self.notifyChange("CurrentCurveChange")
60 def createXYPlotSet(self, silent=False):
61 cv = XYPlotSetModel(self._controller)
62 self._plotSets[cv.getID()] = cv
63 self._currentPlotSet = cv
65 self.notifyChange("NewPlotSet")
68 def removeXYPlotSet(self, plotSetID):
69 Logger.Debug("====> PlotManager::removeXYPlotSet() %d" % plotSetID)
70 if plotSetID not in 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")
81 self._currentPlotSet = None
82 self.notifyChange("ClearAll")
84 def lockRepaint(self):
85 self._lockRepaint = True
86 self._plotSetsRepaint = set()
88 def isRepaintLocked(self):
89 return self._lockRepaint
91 def registerRepaint(self, ps_id):
92 self._plotSetsRepaint.add(ps_id)
94 def unlockRepaint(self):
95 self._lockRepaint = False
96 for obj in self._plotSetsRepaint:
98 self._plotSetsRepaint = set()