This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
forked from gilestrolab/pySolo-Video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pvg_panel_one.py
475 lines (373 loc) · 16.8 KB
/
pvg_panel_one.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# pvg_panel_one.py
#
# Copyright 2011 Giorgio Gilestro <[email protected]>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import wx, os
from pvg_common import previewPanel, options
import wx.lib.newevent
ThumbnailClickedEvt, EVT_THUMBNAIL_CLICKED = wx.lib.newevent.NewCommandEvent()
from wx.lib.filebrowsebutton import FileBrowseButton, DirBrowseButton
class thumbnailPanel(previewPanel):
"""
A small preview Panel to be used as thumbnail
"""
def __init__( self, parent, monitor_number, thumbnailSize=(320,240) ):
previewPanel.__init__(self, parent, size=thumbnailSize, keymode=False)
self.number = int(monitor_number)
self.allowEditing = False
self.displayNumber()
self.Bind(wx.EVT_LEFT_UP, self.onLeftClick)
def setThumbnailSize(self, sz):
self.SetSize(sz)
def displayNumber(self):
"""
"""
# font type: wx.DEFAULT, wx.DECORATIVE, wx.ROMAN, wx.SCRIPT, wx.SWISS, wx.MODERN
# slant: wx.NORMAL, wx.SLANT or wx.ITALIC
# weight: wx.NORMAL, wx.LIGHT or wx.BOLD
#font1 = wx.Font(10, wx.SWISS, wx.ITALIC, wx.NORMAL)
# use additional fonts this way ...
pos = int(self.size[0]/2 - 20), int(self.size[1]/2 - 20),
font1 = wx.Font(35, wx.SWISS, wx.NORMAL, wx.NORMAL)
text1 = wx.StaticText( self, wx.ID_ANY, '%s' % (self.number+1), pos)
text1.SetFont(font1)
def onLeftClick(self, evt):
"""
Send signal around that the thumbnail was clicked
"""
event = ThumbnailClickedEvt(self.GetId())
event.id = self.GetId()
event.number = self.number
event.thumbnail = self
self.GetEventHandler().ProcessEvent(event)
class panelGridView(wx.ScrolledWindow):
"""
"""
def __init__(self, parent, gridSize, thumbnailSize=(320,240) ):
"""
"""
wx.ScrolledWindow.__init__(self, parent, wx.ID_ANY, size=(-1,600))
self.SetScrollbars(1, 1, 1, 1)
self.SetScrollRate(10, 10)
self.parent = parent
self.thumbnailSize = thumbnailSize
self.grid_mainSizer = wx.GridSizer(6,3,2,2)
#Populate the thumbnail grid
self.previewPanels = []
for i in range (int(gridSize)):
self.previewPanels.append ( thumbnailPanel(self, monitor_number=i,
thumbnailSize=self.thumbnailSize) )
self.grid_mainSizer.Add(self.previewPanels[i])#, 0, wx.EXPAND|wx.FIXED_MINSIZE, 0)
self.SetSizer(self.grid_mainSizer)
self.Bind(EVT_THUMBNAIL_CLICKED, self.onThumbnailClicked)
def updateMonitors(self, old, now):
diff = old - now
self.gridSize = now
i = diff
j = 0
while i != 0:
if diff < 0:
# Adding monitors to grid
i += 1
self.previewPanels.append ( thumbnailPanel(self, monitor_numer=old+j, thumbnailSize = self.thumbnailSize) )
self.grid_mainSizer.Add(self.previewPanels[old+j])
self.grid_mainSizer.Layout()
j += 1
elif diff > 0:
# Removing monitors from grid
i -= 1
self.grid_mainSizer.Hide(old-1)
self.grid_mainSizer.Remove(old-1)
old -= 1
self.grid_mainSizer.Layout()
def updateThumbs(self, old, new):
self.thumbnailSize = new
for i in range(0, self.gridSize):
self.previewPanels[i].SetThumbnailsize(new)
self.grid_mainSizer.Layout()
def onThumbnailClicked(self, event):
"""
Relay event to sibling panel
"""
wx.PostEvent(self.parent.lowerPanel, event)
event.Skip()
class panelConfigure(wx.Panel):
"""
"""
def __init__(self, parent):
"""
"""
wx.Panel.__init__(self, parent, wx.ID_ANY, size=(-1,300),
style=wx.SUNKEN_BORDER|wx.TAB_TRAVERSAL)
self.parent = parent
self.thumbnail = None
self.mask_file = None
self.source = None
self.sourceType = None
self.track = None
self.trackType = None
lowerSizer = wx.BoxSizer(wx.HORIZONTAL)
#Static box1 (LEFT)
sb_1 = wx.StaticBox(self, -1, "Select Monitor")#, size=(250,-1))
sbSizer_1 = wx.StaticBoxSizer (sb_1, wx.VERTICAL)
n_monitors = options.GetOption("Monitors")
self.MonitorList = ['Monitor %s' % (int(m) + 1) for m in range( n_monitors )]
self.thumbnailNumber = wx.ComboBox(self, -1, size=(-1,-1) , choices=self.MonitorList, style=wx.CB_DROPDOWN | wx.CB_READONLY | wx.CB_SORT)
self.Bind ( wx.EVT_COMBOBOX, self.onChangingMonitor, self.thumbnailNumber)
self.currentSource = wx.TextCtrl (self, -1, "No Source Selected", style=wx.TE_READONLY)
btnSizer_1 = wx.BoxSizer(wx.HORIZONTAL)
self.btnPlay = wx.Button( self, wx.ID_FORWARD, label="Play")
self.btnStop = wx.Button( self, wx.ID_STOP, label="Stop")
self.Bind(wx.EVT_BUTTON, self.onPlay, self.btnPlay)
self.Bind(wx.EVT_BUTTON, self.onStop, self.btnStop)
self.btnPlay.Enable(False); self.btnStop.Enable(False)
self.applyButton = wx.Button( self, wx.ID_APPLY )
self.applyButton.SetToolTip(wx.ToolTip("Apply and Save to file"))
self.Bind(wx.EVT_BUTTON, self.onApplySource, self.applyButton)
btnSizer_1.Add ( self.btnPlay , 0, wx.ALIGN_LEFT|wx.ALL, 5 )
btnSizer_1.Add ( self.btnStop , 0, wx.ALIGN_CENTER|wx.LEFT|wx.TOP|wx.DOWN, 5 )
btnSizer_1.Add ( self.applyButton, 0, wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
sbSizer_1.Add ( self.thumbnailNumber, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
sbSizer_1.Add ( self.currentSource, 0, wx.EXPAND|wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
sbSizer_1.Add ( btnSizer_1, 0, wx.EXPAND|wx.ALIGN_BOTTOM|wx.TOP, 5 )
lowerSizer.Add (sbSizer_1, 0, wx.EXPAND|wx.ALL, 5)
#Static box2 (CENTER)
sb_2 = wx.StaticBox(self, -1, "Select Video input" )
sbSizer_2 = wx.StaticBoxSizer (sb_2, wx.VERTICAL)
grid2 = wx.FlexGridSizer( 0, 2, 0, 0 )
n_cams = options.GetOption("Webcams")
self.WebcamsList = [ 'Webcam %s' % (int(w) +1) for w in range( n_cams ) ]
rb1 = wx.RadioButton(self, -1, 'Camera', style=wx.RB_GROUP)
source1 = wx.ComboBox(self, -1, size=(285,-1) , choices = self.WebcamsList, style=wx.CB_DROPDOWN | wx.CB_READONLY | wx.CB_SORT)
self.Bind(wx.EVT_COMBOBOX, self.sourceCallback, source1)
rb2 = wx.RadioButton(self, -1, 'File' )
source2 = FileBrowseButton(self, -1, labelText='', size=(300,-1), changeCallback = self.sourceCallback)
rb3 = wx.RadioButton(self, -1, 'Folder' )
source3 = DirBrowseButton (self, style=wx.DD_DIR_MUST_EXIST, labelText='', size=(300,-1), changeCallback = self.sourceCallback)
self.controls = []
self.controls.append((rb1, source1))
self.controls.append((rb2, source2))
self.controls.append((rb3, source3))
for radio, source in self.controls:
grid2.Add( radio , 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 2 )
grid2.Add( source , 0, wx.ALIGN_CENTRE|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 2 )
self.Bind(wx.EVT_RADIOBUTTON, self.onChangeSource, radio )
source.Enable(False)
self.controls[0][1].Enable(True)
#grid2.Add(wx.StaticText(self, -1, ""))
sbSizer_2.Add( grid2 )
lowerSizer.Add(sbSizer_2, 0, wx.EXPAND|wx.ALL, 5)
#Static box3 (RIGHT)
sb_3 = wx.StaticBox(self, -1, "Set Tracking Parameters")
sbSizer_3 = wx.StaticBoxSizer (sb_3, wx.VERTICAL)
sbSizer_31 = wx.BoxSizer (wx.HORIZONTAL)
self.activateTracking = wx.CheckBox(self, -1, "Activate Tracking")
self.activateTracking.SetValue(False)
self.activateTracking.Bind ( wx.EVT_CHECKBOX, self.onActivateTracking)
self.isSDMonitor = wx.CheckBox(self, -1, "Sleep Deprivation Monitor")
self.isSDMonitor.SetValue(False)
self.isSDMonitor.Bind ( wx.EVT_CHECKBOX, self.onSDMonitor)
self.isSDMonitor.Enable(False)
sbSizer_31.Add (self.activateTracking, 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
sbSizer_31.Add (self.isSDMonitor, 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
self.pickMaskBrowser = FileBrowseButton(self, -1, labelText='Mask File')
#sbSizer_3.Add ( self.activateTracking , 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
sbSizer_3.Add ( sbSizer_31 , 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
sbSizer_3.Add ( self.pickMaskBrowser , 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP|wx.EXPAND, 5 )
#trackingTypeSizer = wx.Sizer(wx.HORIZONTAL)
self.trackDistanceRadio = wx.RadioButton(self, -1, "Activity as distance traveled", style=wx.RB_GROUP)
self.trackVirtualBM = wx.RadioButton(self, -1, "Activity as midline crossings count")
self.trackPosition = wx.RadioButton(self, -1, "Only position of flies")
sbSizer_3.Add (wx.StaticText ( self, -1, "Calculate fly activity as..."), 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
sbSizer_3.Add (self.trackDistanceRadio, 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 2 )
sbSizer_3.Add (self.trackVirtualBM, 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 2 )
sbSizer_3.Add (self.trackPosition, 0, wx.ALIGN_LEFT|wx.LEFT|wx.RIGHT|wx.TOP, 2 )
lowerSizer.Add(sbSizer_3, -1, wx.EXPAND|wx.ALL, 5)
self.SetSizer(lowerSizer)
self.Bind(EVT_THUMBNAIL_CLICKED, self.onThumbnailClicked)
def __getSource(self):
"""
check which source is ticked and what is the associated value
"""
for (r, s), st in zip(self.controls,range(3)):
if r.GetValue():
source = s.GetValue()
sourceType = st
return source, sourceType
def __getTrackingType(self):
"""
return which tracking we are chosing
['DISTANCE','VBS','XY_COORDS']
"""
if self.trackDistanceRadio.GetValue(): trackType = "DISTANCE"
elif self.trackVirtualBM.GetValue(): trackType = "VBS"
elif self.trackPosition.GetValue(): trackType = "XY_COORDS"
return trackType
def onPlay (self, event=None):
"""
"""
if self.thumbnail:
self.thumbnail.Play()
self.btnStop.Enable(True)
def onStop (self, event=None):
"""
"""
if self.thumbnail and self.thumbnail.isPlaying:
self.thumbnail.Stop()
self.btnStop.Enable(False)
def onThumbnailClicked(self, evt):
"""
Picking thumbnail by clicking on it
"""
self.monitor_number = evt.number + 1
self.thumbnail = evt.thumbnail
self.thumbnailNumber.SetValue(self.MonitorList[self.monitor_number -1 ])
self.updateThumbnail()
def onChangingMonitor(self, evt):
"""
Picking thumbnail by using the dropbox
"""
sel = evt.GetString()
self.monitor_number = self.MonitorList.index(sel) + 1
self.thumbnail = self.parent.scrollThumbnails.previewPanels[self.monitor_number] #this is not very elegant
self.updateThumbnail()
def updateThumbnail(self):
"""
Refreshing thumbnail data
"""
if options.HasMonitor(self.monitor_number):
sourceType, source, track, mask_file, trackType, isSDMonitor = options.GetMonitor(self.monitor_number)
else:
sourceType, source, track, mask_file, trackType, isSDMonitor = [0, '', False, '', 1, False]
if sourceType == 0 and source != '':
source = self.WebcamsList[source]
self.source = self.thumbnail.source = source
self.sourceType = self.thumbnail.sourceType = sourceType
self.thumbnail.track = track
if self.thumbnail.hasMonitor():
self.thumbnail.mon.isSDMonitor = isSDMonitor
#update first static box
active = self.thumbnail.hasMonitor()
self.applyButton.Enable ( active )
self.btnPlay.Enable ( active )
self.btnStop.Enable ( active and self.thumbnail.isPlaying )
text = os.path.split(str(self.source))[1] or "No Source Selected"
self.currentSource.SetValue( text )
#update second static box
for radio, src in self.controls:
src.Enable(False); src.SetValue('')
radio, src = self.controls[self.sourceType]
radio.SetValue(True); src.Enable(True)
src.SetValue(self.source)
#update third static box
self.activateTracking.SetValue(self.thumbnail.track)
self.isSDMonitor.SetValue(isSDMonitor)
self.pickMaskBrowser.SetValue(mask_file or '')
[self.trackDistanceRadio, self.trackVirtualBM, self.trackPosition][trackType].SetValue(True)
def sourceCallback (self, event):
"""
"""
self.applyButton.Enable(True)
def onChangeSource(self, event):
"""
"""
radio_selected = event.GetEventObject()
for radio, source in self.controls:
if radio is radio_selected:
source.Enable(True)
else:
source.Enable(False)
self.applyButton.Enable(True)
def onApplySource(self, event):
"""
"""
source, sourceType = self.__getSource()
track = self.activateTracking.GetValue()
self.mask_file = self.pickMaskBrowser.GetValue()
self.trackType = self.__getTrackingType()
if self.thumbnail:
if sourceType > 0: camera = source
else: camera = self.WebcamsList.index(source)
self.thumbnail.source = camera
self.thumbnail.sourceType = sourceType
#Change the source text
self.currentSource.SetValue( os.path.split(source)[1] )
#Set thumbnail's source
self.thumbnail.setMonitor(camera)
#Enable buttons
self.btnPlay.Enable(True)
self.activateTracking.Enable(True)
self.pickMaskBrowser.Enable(True)
self.saveMonitorConfiguration()
def saveMonitorConfiguration(self):
"""
"""
options.SetMonitor(self.monitor_number,
self.thumbnail.sourceType,
self.thumbnail.source+1,
self.thumbnail.track,
self.mask_file,
self.trackType,
self.thumbnail.mon.isSDMonitor
)
options.Save()
def onActivateTracking(self, event):
"""
"""
if self.thumbnail:
self.thumbnail.track = event.IsChecked()
def onSDMonitor(self, event):
"""
"""
if self.thumbnail:
self.thumbnail.mon.isSDMonitor = event.IsChecked()
class panelOne(wx.Panel):
"""
Panel number One
All the thumbnails
"""
def __init__(self, parent):
wx.Panel.__init__(self, parent)
monitor_number = options.GetOption("Monitors")
self.mon_num = monitor_number
tn_size = options.GetOption("ThumbnailSize")
self.thumb_size = tn_size
self.temp_source = ''
self.source = ''
self.sourceType = -1
self.scrollThumbnails = panelGridView(self, gridSize=monitor_number, thumbnailSize=tn_size)
self.lowerPanel = panelConfigure(self)
self.PanelOneSizer = wx.BoxSizer(wx.VERTICAL)
self.PanelOneSizer.Add(self.scrollThumbnails, 1, wx.EXPAND, 0)
self.PanelOneSizer.Add(self.lowerPanel, 0, wx.EXPAND, 0)
self.SetSizer(self.PanelOneSizer)
def StopPlaying(self):
"""
"""
self.lowerPanel.onStop()
def onRefresh(self):
monitor_number = options.GetOption("Monitors")
if self.mon_num != monitor_number:
self.scrollThumbnails.updateMonitors(self.mon_num, monitor_number)
tn_size = options.GetOption("ThumbnailSize")
if self.thumb_size != tn_size:
self.scrollThumbnails.updateThumbs(self.thumb_size, tn_size)
self.PanelOneSizer.Layout()
self.Layout()