forked from rickshory/DataMgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stations.py
347 lines (282 loc) · 13.8 KB
/
stations.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import wx, sqlite3, datetime
import os, sys, re, cPickle
import scidb
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
# ----------------------------------------------------------------------
# customized for drag/drop from list of Stations
# DragStationList
class DragStationList(wx.ListCtrl, ListCtrlAutoWidthMixin):
def __init__(self, *arg, **kw):
wx.ListCtrl.__init__(self, *arg, **kw)
ListCtrlAutoWidthMixin.__init__(self) # rightmost column will fill the rest of the list
self.Bind(wx.EVT_LIST_BEGIN_DRAG, self._startDrag)
def _startDrag(self, e):
""" Put together a data object for drag-and-drop _from_ this list. """
l = ['Station']
# mostly need the ItemData, which is the record ID in the Stations table
idx = self.GetFirstSelected()
l.append(self.GetItemData(idx))
# as a convenience, pass the list row text
l.append(self.GetItemText(idx))
# and the ChannelSegment list column to put it in
l.append(0)
# Pickle the object
itemdata = cPickle.dumps(l, 1)
# create our own data format and use it in a
# custom data object
ldata = wx.CustomDataObject("RecIDandTable")
ldata.SetData(itemdata)
# Now make a data object for the item list.
data = wx.DataObjectComposite()
data.Add(ldata)
# Create drop source and begin drag-and-drop.
dropSource = wx.DropSource(self)
dropSource.SetData(data)
res = dropSource.DoDragDrop(flags=wx.Drag_DefaultMove)
# If move, we could remove the item from this list.
if res == wx.DragMove:
pass # disable removing, we only want to assign its info to the other list
# ----------------------------------------------------------------------
# customized for drag/drop from list of Series
# DragSeriesList
class DragSeriesList(wx.ListCtrl, ListCtrlAutoWidthMixin):
def __init__(self, *arg, **kw):
wx.ListCtrl.__init__(self, *arg, **kw)
ListCtrlAutoWidthMixin.__init__(self) # rightmost column will fill the rest of the list
self.Bind(wx.EVT_LIST_BEGIN_DRAG, self._startDrag)
def _startDrag(self, e):
""" Put together a data object for drag-and-drop _from_ this list. """
l = ['Series']
# mostly need the ItemData, which is the record ID in the DataSeries table
idx = self.GetFirstSelected()
l.append(self.GetItemData(idx))
# as a convenience, pass the list row text
l.append(self.GetItemText(idx))
# and the ChannelSegment list column to put it in
l.append(1)
# Pickle the object
itemdata = cPickle.dumps(l, 1)
# create our own data format and use it in a
# custom data object
ldata = wx.CustomDataObject("RecIDandTable")
ldata.SetData(itemdata)
# Now make a data object for the item list.
data = wx.DataObjectComposite()
data.Add(ldata)
# Create drop source and begin drag-and-drop.
dropSource = wx.DropSource(self)
dropSource.SetData(data)
res = dropSource.DoDragDrop(flags=wx.Drag_DefaultMove)
# If move, we could remove the item from this list.
if res == wx.DragMove:
pass # disable removing, we only want to assign its info to the other list
# customized for drag/drop from list of ChannelSegments
# drag-TO is implemented, drag-FROM is disabled
# DragChannelSegmentList
class DragChannelSegmentList(wx.ListCtrl, ListCtrlAutoWidthMixin):
def __init__(self, *arg, **kw):
wx.ListCtrl.__init__(self, *arg, **kw)
ListCtrlAutoWidthMixin.__init__(self)
self.setResizeColumn(3) # Channel column will take up any extra spaces
dt = ListChannelSegmentDrop(self)
self.SetDropTarget(dt)
def _insert(self, x, y, lObj):
""" Target the drop to given x, y coordinates --- used with drag-and-drop. """
# Find insertion point.
index, flags = self.HitTest((x, y))
print "index from HitTest", index
print "flag from HitTest", flags
if index != wx.NOT_FOUND: # clicked on an item
ChanSegID = self.GetItemData(index)
stSQL = "UPDATE ChannelSegments SET " + lObj[0] + "ID = ? WHERE ID = ?;"
# if lObj[0] == 'Station':
# stSQL = "UPDATE ChannelSegments SET StationID = ? WHERE ID = ?;"
# if lObj[0] == 'Series':
# stSQL = "UPDATE ChannelSegments SET SeriesID = ? WHERE ID = ?;"
scidb.curD.execute(stSQL, (lObj[1], ChanSegID))
# on the next load, this change will show in the ChannelSegments list
# but for now just patch in the string
self.SetStringItem(index, lObj[3], lObj[2])
# maybe refresh entire list, but following formats are not quite right
# self.parent.fillChannelSegmentsList()
# framePanel.fillChannelSegmentsList()
# customized for drop to a list of ChannelSegments
# -----
# ListChannelSegmentDrop
class ListChannelSegmentDrop(wx.PyDropTarget):
""" Drop target for Channel Segments list.
Allows dropping Station/Series onto ChannelSegment to assign those handles
"""
def __init__(self, source):
""" Arguments:
- source: source listctrl.
"""
wx.PyDropTarget.__init__(self)
self.dv = source
# specify the type of data we will accept
self.data = wx.CustomDataObject("RecIDandTable")
self.SetDataObject(self.data)
# Called when OnDrop returns True. We need to get the data and
# do something with it.
def OnData(self, x, y, d):
# copy the data from the drag source to our data object
if self.GetData():
# convert it back to a list and give it to the viewer
ldata = self.data.GetData()
l = cPickle.loads(ldata)
self.dv._insert(x, y, l)
# what is returned signals the source what to do
# with the original data (move, copy, etc.) In this
# case we just return the suggested value given to us.
return d
class SetupStationsPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.InitUI()
def InitUI(self):
# improve this layout using wx.SplitterWindow instead
sizerWholeFrame = wx.GridBagSizer(5, 5)
hdr = wx.StaticText(self, label="Drag Stations and Series to assign them to Channel Segments")
sizerWholeFrame.Add(hdr, pos=(0, 0), span=(1, 2), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
hLine = wx.StaticLine(self)
sizerWholeFrame.Add(hLine, pos=(1, 0), span=(1, 3),
flag=wx.EXPAND|wx.BOTTOM, border=1)
sizerSta = wx.GridBagSizer(1, 1)
hdrStation = wx.StaticText(self, label="Stations:")
sizerSta.Add(hdrStation, pos=(0, 0), span=(1, 1),
flag=wx.ALIGN_LEFT|wx.TOP|wx.LEFT, border=1)
btnAddStation = wx.Button(self, label="New", size=(32, 20))
btnAddStation.Bind(wx.EVT_BUTTON, lambda evt, str=btnAddStation.GetLabel(): self.onClick_BtnAddStation(evt, str))
sizerSta.Add(btnAddStation, pos=(0, 1), flag=wx.ALIGN_LEFT|wx.LEFT, border=10)
self.lstStations = DragStationList(self, style=wx.LC_REPORT|wx.LC_NO_HEADER|wx.LC_SINGLE_SEL)
self.lstStations.InsertColumn(0, "Station")
self.fillStationsList()
sizerSta.Add(self.lstStations, pos=(1, 0), span=(2, 2), flag=wx.EXPAND)
sizerSta.AddGrowableRow(1)
sizerSta.AddGrowableCol(1)
sizerWholeFrame.Add(sizerSta, pos=(2, 0), span=(1, 1),
flag=wx.EXPAND)
sizerSer = wx.GridBagSizer(1, 1)
hdrSeries = wx.StaticText(self, label="Series:")
sizerSer.Add(hdrSeries, pos=(0, 0), span=(1, 1),
flag=wx.ALIGN_LEFT|wx.TOP|wx.LEFT, border=1)
btnAddSeries = wx.Button(self, label="New", size=(32, 20))
btnAddSeries.Bind(wx.EVT_BUTTON, lambda evt, str=btnAddSeries.GetLabel(): self.onClick_BtnAddSeries(evt, str))
sizerSer.Add(btnAddSeries, pos=(0, 1), flag=wx.ALIGN_LEFT|wx.LEFT, border=10)
self.lstSeries = DragSeriesList(self, style=wx.LC_REPORT|wx.LC_NO_HEADER|wx.LC_SINGLE_SEL)
self.lstSeries.InsertColumn(0, "Series")
self.fillSeriesList()
sizerSer.Add(self.lstSeries, pos=(1, 0), span=(2, 2), flag=wx.EXPAND)
sizerSer.AddGrowableRow(1)
sizerSer.AddGrowableCol(1)
sizerWholeFrame.Add(sizerSer, pos=(2, 1), span=(1, 1),
flag=wx.EXPAND)
sizerChanSegs = wx.GridBagSizer(1, 1)
hdrChanSegs = wx.StaticText(self, label="Channel Segments:")
sizerChanSegs.Add(hdrChanSegs, pos=(0, 0), span=(1, 1), flag=wx.ALIGN_LEFT|wx.TOP, border=5)
self.lstChanSegs = DragChannelSegmentList(self, style=wx.LC_REPORT|wx.LC_VRULES|wx.LC_SINGLE_SEL)
self.lstChanSegs.InsertColumn(0, "Station")
self.lstChanSegs.InsertColumn(1, "Series")
self.lstChanSegs.InsertColumn(2, "Channel Segment (Col, Logger, Sensor, Type, Units, HrOffset)")
self.lstChanSegs.InsertColumn(3, "Start")
self.lstChanSegs.InsertColumn(4, "End")
self.fillChannelSegmentsList()
sizerChanSegs.Add(self.lstChanSegs, pos=(1, 0), span=(1, 2), flag=wx.EXPAND)
sizerChanSegs.AddGrowableRow(1)
sizerChanSegs.AddGrowableCol(1)
sizerWholeFrame.Add(sizerChanSegs, pos=(3, 0), span=(1, 3),
flag=wx.EXPAND)
sizerWholeFrame.AddGrowableCol(0)
sizerWholeFrame.AddGrowableCol(1)
sizerWholeFrame.AddGrowableRow(3)
self.SetSizerAndFit(sizerWholeFrame)
def fillStationsList(self):
self.lstStations.DeleteAllItems()
scidb.curD.execute("SELECT ID, StationName FROM Stations;")
recs = scidb.curD.fetchall()
for rec in recs:
idx = self.lstStations.InsertStringItem(sys.maxint, rec["StationName"])
self.lstStations.SetItemData(idx, rec["ID"])
def fillSeriesList(self):
self.lstSeries.DeleteAllItems()
scidb.curD.execute("SELECT ID, DataSeriesDescription FROM DataSeries;")
recs = scidb.curD.fetchall()
for rec in recs:
idx = self.lstSeries.InsertStringItem(sys.maxint, rec["DataSeriesDescription"])
self.lstSeries.SetItemData(idx, rec["ID"])
def fillChannelSegmentsList(self):
self.lstChanSegs.DeleteAllItems()
stSQL = """
SELECT ChannelSegments.ID,
COALESCE(Stations.StationName, '(none)') AS Station,
COALESCE( DataSeries.DataSeriesDescription, '(none)') AS Series,
DataChannels.Column || ', ' || Loggers.LoggerSerialNumber || ', ' ||
Sensors.SensorSerialNumber || ', ' || DataTypes.TypeText || ', ' ||
DataUnits.UnitsText || ', ' || DataChannels.UTC_Offset AS Channel,
COALESCE(ChannelSegments.SegmentBegin, '(open)') AS Begin,
COALESCE(ChannelSegments.SegmentEnd, '(open)') AS End
FROM ((((((ChannelSegments
LEFT JOIN Stations ON ChannelSegments.StationID = Stations.ID)
LEFT JOIN DataSeries ON ChannelSegments.SeriesID = DataSeries.ID)
LEFT JOIN DataChannels ON ChannelSegments.ChannelID = DataChannels.ID)
LEFT JOIN Loggers ON DataChannels.LoggerID = Loggers.ID)
LEFT JOIN Sensors ON DataChannels.SensorID = Sensors.ID)
LEFT JOIN DataTypes ON DataChannels.DataTypeID = DataTypes.ID)
LEFT JOIN DataUnits ON DataChannels.DataUnitsID = DataUnits.ID;
"""
scidb.curD.execute(stSQL)
recs = scidb.curD.fetchall()
for rec in recs:
idx = self.lstChanSegs.InsertStringItem(sys.maxint, rec["Station"])
self.lstChanSegs.SetItemData(idx, rec["ID"])
self.lstChanSegs.SetStringItem(idx, 1, rec["Series"])
self.lstChanSegs.SetStringItem(idx, 2, rec["Channel"])
self.lstChanSegs.SetStringItem(idx, 3, rec["Begin"])
self.lstChanSegs.SetStringItem(idx, 4, rec["End"])
def onButton(self, event, strLabel):
""""""
print ' You clicked the button labeled "%s"' % strLabel
def onClick_BtnAddStation(self, event, strLabel):
"""
"""
dlg = wx.TextEntryDialog(None, "Name of Station you are adding to the database:", "New Station", " ")
answer = dlg.ShowModal()
if answer == wx.ID_OK:
stNewStation = dlg.GetValue()
stNewStation = " ".join(stNewStation.split())
recID = scidb.assureItemIsInTableField(stNewStation, "Stations", "StationName")
self.fillStationsList()
else:
stNewStation = ''
dlg.Destroy()
def onClick_BtnAddSeries(self, event, strLabel):
"""
"""
dlg = wx.TextEntryDialog(None, "Name of Data Series you are adding to the database:", "New Data Series", " ")
answer = dlg.ShowModal()
if answer == wx.ID_OK:
stNewSeries = dlg.GetValue()
stNewSeries = " ".join(stNewSeries.split())
recID = scidb.assureItemIsInTableField(stNewSeries, "DataSeries", "DataSeriesDescription")
self.fillSeriesList()
else:
stNewSeries = ''
dlg.Destroy()
# def onClick_BtnNotWorkingYet(self, event, strLabel):
# wx.MessageBox('"Hello" is not implemented yet', 'Info',
# wx.OK | wx.ICON_INFORMATION)
class SetupStationsFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.InitUI()
self.SetSize((750, 600))
# self.Centre()
self.Show(True)
def InitUI(self):
framePanel = SetupStationsPanel(self, wx.ID_ANY)
def main():
app = wx.App(redirect=False)
SetupStationsFrame(None, wx.ID_ANY, 'Assign Stations and Series')
app.MainLoop()
if __name__ == '__main__':
main()