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