Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys, os
from qgis.core import *
from PyQt4.QtCore import *
from timeseriesviewer import *
import numpy as np
import pyqtgraph as pg
from timeseriesviewer.ui.widgets import *
from timeseriesviewer.timeseries import TimeSeries, TimeSeriesDatum, SensorInstrument
from timeseriesviewer.ui.docks import load, TsvDockWidgetBase
class SensorDockUI(TsvDockWidgetBase, load('sensordock.ui')):
def __init__(self, parent=None):
super(SensorDockUI, self).__init__(parent)
self.setupUi(self)
self.TS = None
def connectTimeSeries(self, timeSeries):
from timeseriesviewer.timeseries import TimeSeries
from timeseriesviewer.sensorvisualization import SensorTableModel
assert isinstance(timeSeries, TimeSeries)
self.TS = timeSeries
model = SensorTableModel(self.TS)
self.sensorView.setModel(model)
self.sensorView.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
s = ""
class SensorTableModel(QAbstractTableModel):
columnames = ['name', 'nb', 'n images','wl','id']
def __init__(self, TS, parent=None, *args):
super(SensorTableModel, self).__init__()
assert isinstance(TS, TimeSeries)
self.TS = TS
self.TS.sigSensorAdded.connect(self.addSensor)
self.TS.sigSensorRemoved.connect(self.removeSensor)
self.items = []
self.sortColumnIndex = 0
self.sortOrder = Qt.AscendingOrder
for s in self.TS.Sensors:
self.addSensor(s)
def addSensor(self, sensor):
assert isinstance(sensor, SensorInstrument)
self.items.append(sensor)
self.sort(self.sortColumnIndex, self.sortOrder)
def removeSensor(self, sensor):
assert isinstance(sensor, SensorInstrument)
if sensor in self.items:
self.items.remove(sensor)
def sort(self, col, order):
if self.rowCount() == 0:
return
self.layoutAboutToBeChanged.emit()
colName = self.columnames[col]
r = order != Qt.AscendingOrder
if colName == 'name':
self.items.sort(key = lambda s:s.name(), reverse=r)
elif colName == 'nb':
self.items.sort(key=lambda s: s.nb, reverse=r)
self.layoutChanged.emit()
def rowCount(self, parent = QModelIndex()):
return len(self.items)
def removeRows(self, row, count , parent=QModelIndex()):
self.beginRemoveRows(parent, row, row+count-1)
toRemove = self.items[row:row+count]
for tsd in toRemove:
self.items.remove(tsd)
self.endRemoveRows()
def getIndexFromSensor(self, sensor):
return self.createIndex(self.items.index(sensor),0)
def getSensorFromIndex(self, index):
if index.isValid():
return self.items[index.row()]
return None
def columnCount(self, parent = QModelIndex()):
return len(self.columnames)
def data(self, index, role = Qt.DisplayRole):
if role is None or not index.isValid():
return None
value = None
columnName = self.columnames[index.column()]
sensor = self.getSensorFromIndex(index)
assert isinstance(sensor, SensorInstrument)
if role == Qt.DisplayRole:
if columnName == 'name':
value = sensor.name()
elif columnName == 'nb':
value = str(sensor.nb)
elif columnName == 'n images':
value = str(len(self.TS.getTSDs(sensorOfInterest=sensor)))
elif columnName == 'id':
value = sensor.id()
elif columnName == 'wl':
if sensor.wavelengths.ndim == 0:
value = 'undefined'
else:
value = ','.join([str(w) for w in sensor.wavelengths]) +sensor.wavelengthUnits
elif role == Qt.CheckStateRole:
if columnName == 'name':
value = None
elif role == Qt.UserRole:
value = sensor
return value
def setData(self, index, value, role=None):
if role is None or not index.isValid():
return None
columnName = self.columnames[index.column()]
sensor = self.getSensorFromIndex(index)
assert isinstance(sensor, SensorInstrument)
if role == Qt.EditRole and columnName == 'name':
if len(value) == 0: #do not accept empty strings
return False
sensor.setName(str(value))
return True
return False
def flags(self, index):
if index.isValid():
columnName = self.columnames[index.column()]
flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
if columnName in ['name']: #allow check state
flags = flags | Qt.ItemIsUserCheckable | Qt.ItemIsEditable
return flags
#return item.qt_flags(index.column())
return None
def headerData(self, col, orientation, role):
if Qt is None:
return None
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.columnames[col]
elif orientation == Qt.Vertical and role == Qt.DisplayRole:
return col
return None