1 # Copyright (C) 2016-2022 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from .Model import Model
22 from .utils import toUnicodeWithWarning, Logger
24 class TableModel(Model):
25 def __init__(self, controller):
26 Model.__init__(self, controller)
27 self._columnTitles = {}
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:
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)
47 def setData(self, data):
48 data = self.__checkAndFormatData(data)
50 self.notifyChange("DataChange")
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")
61 # Void data but keeping same number of cols:
62 self._data = np.zeros((0, sh[1]))
63 self.notifyChange("DataChange")
66 if self._data is not None:
67 return self._data.shape
71 def setTitle(self, ti):
72 ti = toUnicodeWithWarning(ti, "TableModel::setTitle()")
74 self.notifyChange("TitleChange")
79 def addColumn(self, lst):
83 raise ValueError("Invalid number of rows in added column! (is %d, should be %d)" % (len(lst), sh[0]))
86 self._data = np.zeros((sh[0],sh[1]+1))
87 self._data[:,:-1] = tmp
91 self._data = np.zeros((len(lst), 1), dtype=np.float64)
93 self._data[:, idx] = lst
94 self.notifyChange("DataChange")
96 def setColumnTitle(self, index, txt):
97 self._columnTitles[index] = txt
98 self.notifyChange("ColumnTitleChange")
100 def getColumnTitle(self, index):
101 return self._columnTitles.get(index, "")
103 def removeValue(self, nrow, ncol):
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")
111 return self._data.__str__()