Newer
Older
# -*- coding: utf-8 -*-
"""
/***************************************************************************

Benjamin Jakimow
committed
EO Time Series Viewer
-------------------
begin : 2015-08-20
git sha : $Format:%H$
copyright : (C) 2017 by HU-Berlin
email : benjamin.jakimow@geo.hu-berlin.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
# noinspection PyPep8Naming
import sys, os
from qgis.core import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
from eotimeseriesviewer.timeseries import TimeSeries, SensorInstrument
from eotimeseriesviewer.utils import loadUI
class SensorDockUI(QgsDockWidget, loadUI('sensordock.ui')):
def __init__(self, parent=None):
super(SensorDockUI, self).__init__(parent)
self.setupUi(self)
self.TS = None
def setTimeSeries(self, timeSeries):
from eotimeseriesviewer.timeseries import TimeSeries
from eotimeseriesviewer.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
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
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':
elif columnName == 'id':
value = sensor.id()
elif columnName == 'wl':
value = 'undefined'
else:
value = ','.join([str(w) for w in sensor.wl])
if sensor.wlu is not None:
value += '[{}]'.format(sensor.wlu)
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
class SensorListModel(QAbstractListModel):
def __init__(self, TS, parent=None, *args):
super(SensorListModel, self).__init__()
assert isinstance(TS, TimeSeries)
self.TS = TS
self.TS.sigSensorAdded.connect(self.insertSensor)
self.TS.sigSensorRemoved.connect(self.removeSensor)
self.mSensors = []
self.sortColumnIndex = 0
self.sortOrder = Qt.AscendingOrder
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
self.insertSensor(s)
def insertSensor(self, sensor, i=None):
assert isinstance(sensor, SensorInstrument)
if i is None:
i = len(self.mSensors)
self.beginInsertRows(QModelIndex(), i, i)
self.mSensors.insert(i, sensor)
self.endInsertRows()
def removeSensor(self, sensor):
assert isinstance(sensor, SensorInstrument)
if sensor in self.mSensors:
i = self.mSensors.index(sensor)
self.beginRemoveRows(QModelIndex(), i, i)
self.mSensors.remove(sensor)
self.endRemoveRows()
def sort(self, col, order):
if self.rowCount() == 0:
return
self.layoutAboutToBeChanged.emit()
r = order != Qt.AscendingOrder
self.mSensors.sort(key = lambda s:s.name(), reverse=r)
self.layoutChanged.emit()
def rowCount(self, parent = QModelIndex()):
return len(self.mSensors)
def removeRows(self, row, count , parent=QModelIndex()):
self.beginRemoveRows(parent, row, row+count-1)
toRemove = self.mSensors[row:row + count]
for tsd in toRemove:
self.mSensors.remove(tsd)
self.endRemoveRows()
def sensor2idx(self, sensor):
assert isinstance(sensor, SensorInstrument)
return self.createIndex(self.mSensors.index(sensor), 0)
def idx2sensor(self, index):
assert isinstance(index, QModelIndex)
if index.isValid():
return self.mSensors[index.row()]
return None
def data(self, index, role = Qt.DisplayRole):
if role is None or not index.isValid():
return None
value = None
sensor = self.idx2sensor(index)
assert isinstance(sensor, SensorInstrument)
if role == Qt.DisplayRole:
value = sensor.name()
elif role == Qt.UserRole:
value = sensor
return value
def flags(self, index):
if index.isValid():
flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
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