Salome HOME
c80f96d23a28c8dbb8559f5e46f3faa3f699996f
[modules/gui.git] / tools / CurvePlot / src / python / views / CurveView.py
1 # Copyright (C) 2016-2021  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 .utils import Logger
22
23 class CurveView(View):
24   _PICKER_PRECISION = 20  #pts
25     
26   def __init__(self, controller, parentXYView):
27     View.__init__(self, controller)
28     self._mplAxes = None
29     self._mplLines = None
30     self._isHighlighted = False
31     self._initialLineWidth = None
32     self._parentXYView = parentXYView
33     
34     self._marker = None
35     self._color = None
36     self._lineStyle = None
37     
38   def setMPLAxes(self, axes):
39     self._mplAxes = axes
40         
41   def erase(self):
42     self._mplAxes.lines.remove(self._mplLines[0])
43     self._mplLines = None
44     
45   def draw(self):
46     m = self._model
47     x_idx, y_idx = m.getXAxisIndex(), m.getYAxisIndex()
48     d = self._model.getTable().getData()
49     self._mplLines = self._mplAxes.plot(d[:, x_idx], d[:, y_idx], label=m._title, 
50                                         picker=self._PICKER_PRECISION)
51     self._initialLineWidth = self._mplLines[0].get_linewidth()
52   
53   def onCurveTitleChange(self):
54     if self._mplLines is None:
55       return
56     self._mplLines[0].set_label(self._model._title)
57   
58   def update(self):
59     Logger.Debug("CurveView::udpate")
60     if self._mplLines is None:
61       return
62     lineStyle, marker, color = self.getLineStyle(), self.getMarker(), self.getColor()
63     self.erase()
64     self.draw()
65     # Reset correctly color, marker and highlight state
66     self.setLineStyle(lineStyle)
67     self.setMarker(marker)
68     self.setColor(color)
69     self.toggleHighlight(self._isHighlighted, force=True)
70     
71   def setLineStyle(self, lin_style):
72     lin = self._mplLines[0] 
73     lin.set_linestyle(lin_style)
74     
75   def getLineStyle(self):
76     if self._mplLines is None:
77       return None
78     return self._mplLines[0].get_linestyle()
79     
80   def setMarker(self, marker):
81     lin = self._mplLines[0]
82     lin.set_marker(marker)
83
84   def getMarker(self):
85     if self._mplLines is None:
86       return None
87     return self._mplLines[0].get_marker()
88
89   def toggleHighlight(self, highlight, force=False):
90     lin = self._mplLines[0]
91     if highlight and (force or not self._isHighlighted):
92       lin.set_linewidth(2*self._initialLineWidth)
93       self._isHighlighted = True
94     elif not highlight and (force or self._isHighlighted):
95       lin.set_linewidth(self._initialLineWidth)
96       self._isHighlighted = False
97     else:
98       # Nothing to do, already the correct state
99       return
100     
101   def isHighlighted(self):
102     return self._isHighlighted
103     
104   def setColor(self, rgb_color):
105     lin = self._mplLines[0]
106     lin.set_color(rgb_color)
107     
108   def getColor(self):
109     if self._mplLines is None:
110       return None
111     return self._mplLines[0].get_color()