Salome HOME
Updated copyright comment
[modules/gui.git] / tools / CurvePlot / src / python / model / TableModel.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 import numpy as np
21 from .Model import Model
22 from .utils import toUnicodeWithWarning, Logger
23
24 class TableModel(Model):
25   def __init__(self, controller):
26     Model.__init__(self, controller)
27     self._columnTitles = {}
28     self._data = None
29     self._title = ""
30   
31   def getData(self):
32     return self._data
33   
34   def __checkAndFormatData(self, data):
35     if isinstance(data, np.ndarray):
36       if len(data.shape) == 1:
37         data = np.resize(data, (data.shape[0], 1))
38       elif len(data.shape) == 2:
39         pass
40       else:
41         raise ValueError("Invalid shape! Must be a vector or a rank-2 tensor (i.e. a matrix)!")
42     elif isinstance(data, list):
43       data = np.array((len(data), 1), dtype=np.float64)
44       data[:] = data
45     return data 
46   
47   def setData(self, data):
48     data = self.__checkAndFormatData(data)
49     self._data = data
50     self.notifyChange("DataChange")
51   
52   def extend(self, data):
53     data = self.__checkAndFormatData(data)
54     if data.shape[1] != self._data.shape[1]:
55       raise ValueError("Invalid shape! Must have the same number of columns than already existing data!")
56     self._data = np.vstack([self._data, data])
57     self.notifyChange("DataChange")
58
59   def clear(self):
60     sh = self.getShape()
61     # Void data but keeping same number of cols:
62     self._data = np.zeros((0, sh[1]))
63     self.notifyChange("DataChange")
64   
65   def getShape(self):
66     if self._data is not None:
67       return self._data.shape
68     else:
69       return (0,0)
70   
71   def setTitle(self, ti):
72     ti = toUnicodeWithWarning(ti, "TableModel::setTitle()")
73     self._title = ti
74     self.notifyChange("TitleChange")
75   
76   def getTitle(self):
77     return self._title
78   
79   def addColumn(self, lst):
80     sh = self.getShape()
81     if sh != (0,0):
82       if len(lst) != sh[0]:
83         raise ValueError("Invalid number of rows in added column! (is %d, should be %d)" % (len(lst), sh[0]))
84       # Add a column
85       tmp = self._data
86       self._data = np.zeros((sh[0],sh[1]+1))
87       self._data[:,:-1] = tmp
88       idx = -1
89     else:
90       # First assignation
91       self._data = np.zeros((len(lst), 1), dtype=np.float64)
92       idx = 0
93     self._data[:, idx] = lst
94     self.notifyChange("DataChange") 
95   
96   def setColumnTitle(self, index, txt):
97     self._columnTitles[index] = txt
98     self.notifyChange("ColumnTitleChange") 
99   
100   def getColumnTitle(self, index):
101     return self._columnTitles.get(index, "")
102   
103   def removeValue(self, nrow, ncol):
104     sh = self.getShape()
105     if nrow >= sh[0] or ncol >= sh[1]:
106       raise ValueError("Specified row and column (%d, %d) invalid with current data size (%d, %d)" % (nrow, ncol, sh[0], sh[1]))
107     self._data[nrow, ncol] = np.NaN
108     self.notifyChange("DataChange") 
109     
110   def __str__(self):
111     return self._data.__str__()
112