-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_widgets.py
More file actions
1025 lines (818 loc) · 30.6 KB
/
gui_widgets.py
File metadata and controls
1025 lines (818 loc) · 30.6 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PyQt5 import QtGui, QtCore, uic, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from functools import partial
import os
import re
from contacts_db import Contact, Contact_db
archivedPrefix = "archived_"
class ContactWidget(QWidget):
def __init__(self, time, x, y, targetDB, distance, lat, lon, on_finish=None):
super(ContactWidget, self).__init__()
self.aisCode = 0
self.aisTag = ""
self.activitySelects = []
self.directionSelects = []
self.content = []
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.topLayout = QVBoxLayout()
self.botLayout = QVBoxLayout()
self.layout.addLayout(self.topLayout)
self.layout.addLayout(self.botLayout)
self.label = QLabel("New Contact")
self.topLayout.addWidget(self.label)
self.distLbl = QLabel("Distance: " + str(int(distance)) + "m")
self.topLayout.addWidget(self.distLbl)
self.coordsLbl = QLabel("{:.6f}, {:.6f}".format(lat, lon))
self.topLayout.addWidget(self.coordsLbl)
aisBtn = QPushButton("AIS")
aisBtn.clicked.connect(self.ais)
self.topLayout.addWidget(aisBtn)
self.content.append(aisBtn)
susBtn = QPushButton("Suspect AIS")
susBtn.clicked.connect(self.suspect)
self.topLayout.addWidget(susBtn)
self.content.append(susBtn)
nonAisBtn = QPushButton("Non-AIS")
nonAisBtn.clicked.connect(self.non_ais)
self.topLayout.addWidget(nonAisBtn)
self.content.append(nonAisBtn)
nonVesselBtn = QPushButton("Marine Mammal")
nonVesselBtn.clicked.connect(self.nonvessel)
self.topLayout.addWidget(nonVesselBtn)
self.content.append(nonVesselBtn)
cancelBtn = QPushButton("Cancel")
cancelBtn.clicked.connect(self.deleteLater)
self.topLayout.addWidget(cancelBtn)
self.time = time
self.x = x
self.y = y
self.db = targetDB
self.distance = distance
self.lat = lat
self.lon = lon
self.on_finish = on_finish
self.tags = ""
self.vesselType = ""
self.tagsField = QLineEdit()
self.confirmBtn = QPushButton("Confirm")
self.aisLbl = QLabel("Waiting for ais...")
self.repeatBox = QCheckBox("Repeat")
self.repeatBox.setToolTip("Skip counting this contact when generating most reports")
def populateLowerContent(self):
self.botLayout.addWidget(self.tagsField)
self.botLayout.addWidget(self.repeatBox)
self.botLayout.addWidget(self.confirmBtn)
self.confirmBtn.clicked.connect(self.save_contact)
def ais(self, event, fromStart=True):
self.aisCode = 2
self.clear_content()
if fromStart:
self.topLayout.addWidget(self.aisLbl)
self.populateLowerContent()
self.types = ["Cargo", "Cruise ship", "EcoTourism", "Ferry", "Fishing", "Pleasure craft",
"Sailing vessel", "Tanker", "Tug", "Misc."]
# self.types = ["Cargo", "Container Ship", "Tanker", "Naval Vessel", "Ferry", "Fishing",
# "Ecotourism", "Research/Coastguard", "Tug", "Sailboat", "Pleasurecraft", "Charter fishing",
# "Misc."]
for type in self.types:
btn = QPushButton(type)
self.topLayout.addWidget(btn)
self.content.append(btn)
btn.clicked.connect(partial(self.activity_select, type, True))
def link_ais(self, mmsi, isA):
# Prevents us from assigning ais to a non-ais or mammal contact
if self.aisCode > 0:
self.aisCode = mmsi
if isA:
self.aisTag = "A"
else:
self.aisTag = "B"
self.aisLbl.setText(str(mmsi) + " (" + self.aisTag + ")")
def suspect(self, event, fromStart=True):
self.aisCode = 1
self.clear_content()
self.types = ["Cargo", "Cruise ship", "EcoTourism", "Ferry", "Fishing", "Pleasure craft",
"Sailing vessel", "Tanker", "Tug", "Misc."]
# self.types = ["Cargo", "Container Ship", "Tanker", "Naval Vessel", "Ferry", "Fishing",
# "Ecotourism", "Research/Coastguard", "Tug", "Sailboat", "Pleasurecraft", "Charter fishing",
# "Misc."]
for type in self.types:
btn = QPushButton(type)
self.topLayout.addWidget(btn)
self.content.append(btn)
btn.clicked.connect(partial(self.activity_select, type, True))
if fromStart:
self.populateLowerContent()
def non_ais(self, event, fromStart=True):
self.aisCode = 0
self.clear_content()
self.types = ["Motorboat", "Sailboat", "Comercial fishing", "Sport fishing", "Ecotourism",
"Tug", "Kayak", "UnID vessel", "Misc."]
for type in self.types:
btn = QPushButton(type)
self.topLayout.addWidget(btn)
self.content.append(btn)
btn.clicked.connect(partial(self.activity_select, type, True))
if fromStart:
self.populateLowerContent()
def nonvessel(self, event, fromStart=True):
self.aisCode = -1
self.clear_content()
self.types = ["Killer Whale", "Humpback Whale", "Harbour Porpoise",
"Dall's Porpoise", "Sealion", "Misc."]
for type in self.types:
btn = QPushButton(type)
self.topLayout.addWidget(btn)
self.content.append(btn)
btn.clicked.connect(partial(self.activity_select, type, False))
if fromStart:
self.populateLowerContent()
def activity_select(self, vesselType, isVessel):
self.vesselType = vesselType
self.clear_content()
if isVessel:
activityTypes = ["Trolling", "Jigging", "UnID fishing", "Transiting",
"Stationary non-fishing", "Marine mammal viewing"]
else:
activityTypes = ["Resting", "Travelling", "Foraging", "Socializing", "Unknown", "Misc"]
typeLabel = QLabel(vesselType)
self.topLayout.addWidget(typeLabel)
self.content.append(typeLabel)
self.activityRadioGroup = QButtonGroup()
for activity in activityTypes:
btn = QRadioButton(activity)
self.activityRadioGroup.addButton(btn)
self.content.append(btn)
self.topLayout.addWidget(btn)
self.activitySelects.append(btn)
divider = QHLine()
self.content.append(divider)
self.topLayout.addWidget(divider)
directionLabel = QLabel("Direction of travel:")
self.topLayout.addWidget(directionLabel)
self.content.append(directionLabel)
self.directionRadioGroup = QButtonGroup()
for direction in ["North", "South"]:
btn = QRadioButton(direction)
self.directionRadioGroup.addButton(btn)
self.content.append(btn)
self.topLayout.addWidget(btn)
self.directionSelects.append(btn)
def save_contact(self):
# If declared as ais and no ais added yet, don't do anything:
if self.aisCode == 2:
return
# Add positional data, category, and activity as tags
tags = "dist=" + str(self.distance)
tags += ",lat=" + str(self.lat) + ",lon=" + str(self.lon)
tags += ",type=" + self.vesselType
for btn in self.activitySelects:
if btn.isChecked():
tags += ",activity="
tags += btn.text()
for btn in self.directionSelects:
if btn.isChecked():
tags += ",direction="
tags += btn.text()
if self.repeatBox.isChecked():
tags += ",repeat=True"
# Add any other tags
tags += ","
tags += self.tagsField.text()
# check if AIS A or B (defaults to B)
c = Contact(self.time, self.x, self.y, self.aisCode, self.aisTag, tags)
self.db.add_contact(c)
if self.on_finish:
self.on_finish()
self.deleteLater()
def clear_content(self):
for item in self.content:
item.deleteLater()
self.content = []
self.activitySelects = []
# TODO: improve this interface. Maybe allow for linking to AIS?
class ContactEditWidget(QWidget):
def __init__(self, contact, contact_db, on_finish=None):
super(ContactEditWidget, self).__init__()
self.db = contact_db
self.contact = contact
self.on_finish = on_finish
self.layout = QVBoxLayout()
self.setLayout(self.layout)
# Add ais information if applicable
self.aisTag = QLabel(str(contact.ais) + " (" + contact.aisClass + ")")
if contact.ais > 0:
self.layout.addWidget(self.aisTag)
self.infoLabel = QLabel("Edit Contact tags:")
self.layout.addWidget(self.infoLabel)
self.tagsLabel = QTextEdit()
self.tagsLabel.setText(contact.tags)
self.layout.addWidget(self.tagsLabel)
self.confirmBtn = QPushButton("Save changes")
self.confirmBtn.clicked.connect(self.confirm_change)
self.layout.addWidget(self.confirmBtn)
self.cancelBtn = QPushButton("Cancel")
self.cancelBtn.clicked.connect(self.cancel_change)
self.layout.addWidget(self.cancelBtn)
self.delBtn = QPushButton("Delete contact")
self.delBtn.clicked.connect(self.delete_contact)
self.layout.addWidget(self.delBtn)
def confirm_change(self):
self.contact.tags = self.tagsLabel.text()
self.db.update_contact(self.contact)
if self.on_finish:
self.on_finish()
self.deleteLater()
def cancel_change(self):
if self.on_finish:
self.on_finish()
self.deleteLater()
def delete_contact(self):
self.db.remove_contact(self.contact)
if self.on_finish:
self.on_finish()
self.deleteLater()
class AisWidget(QWidget):
def __init__(self, link_func, update_func=None):
super(AisWidget, self).__init__()
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.aisLayouts = []
self.boxes = []
self.btns = []
self.mmsis = []
self.ignoreList = []
self.link_func = link_func
self.update_func = update_func
def change_mmsis(self, mmsis):
mmsis = list(map(str, mmsis))
# Remove old tracks
for box in self.boxes:
box.deleteLater()
for btn in self.btns:
btn.deleteLater()
for l in self.aisLayouts:
l.deleteLater()
self.boxes = []
self.btns = []
self.aisLayouts = []
self.mmsis = mmsis
# Add new tracks
for mmsi in mmsis:
box = QCheckBox(mmsi)
btn = QPushButton("Link")
btn.setFixedWidth(50)
lay = QHBoxLayout()
if mmsi not in self.ignoreList:
box.setChecked(True)
else:
box.setChecked(False)
lay.addWidget(box)
self.boxes.append(box)
box.stateChanged.connect(self.on_change)
btn.clicked.connect(partial(self.link_func, int(mmsi)))
lay.addWidget(btn)
self.btns.append(btn)
self.aisLayouts.append(lay)
self.layout.addLayout(lay)
# Update ignore list
ignoreList = []
for mmsi in self.ignoreList:
if mmsi in self.mmsis:
ignoreList.append(mmsi)
self.ignoreList = ignoreList
def on_change(self):
# update ignore list
self.ignoreList = []
for box in self.boxes:
if not box.isChecked():
self.ignoreList.append(str(box.text()))
self.update_func()
def get_selected_mmsis(self):
retList = []
for mmsi in self.mmsis:
if mmsi not in self.ignoreList:
retList.append(int(mmsi))
return retList
class CaliEditWidget(QWidget):
def __init__(self, time, update_func=None, reset_func=None):
super(CaliEditWidget, self).__init__()
self.layout = QGridLayout()
self.setLayout(self.layout)
self.update_func = update_func
self.reset_funct = reset_func
self.time = time
self.pointLbls = []
self.resetbtns = []
for i in range(2):
self.pointLbls.append(QLabel('( , )'))
self.layout.addWidget(self.pointLbls[i], 0, i)
self.resetbtns.append(QPushButton("reset\npoint " + str(i + 1)))
self.resetbtns[i].setEnabled(False)
self.layout.addWidget(self.resetbtns[i], 1, i)
self.resetbtns[i].clicked.connect(partial(self.reset_point, i))
def set_point(self, x, y, initializing=False):
for i, lbl in enumerate(self.pointLbls):
if str(lbl.text()) == '( , )':
lbl.setText("(" + str(x) + "," + str(y) + ")")
self.resetbtns[i].setEnabled(True)
if (not initializing) and self.check_for_completed():
self.save_point()
if self.reset_funct:
self.reset_funct()
return
def reset_point(self, i):
self.pointLbls[i].setText('( , )')
self.resetbtns[i].setEnabled(False)
if self.reset_funct:
self.reset_funct()
def check_for_completed(self):
for lbl in self.pointLbls:
if not re.match(r'\((\d+),(\d+)\)', str(lbl.text())):
return False
return True
def save_point(self):
print('saving calibration data to memory')
points = []
for lbl in self.pointLbls:
m = re.match(r'\((\d+),(\d+)\)', str(lbl.text()))
points.extend([int(m.group(1)), int(m.group(2))])
self.update_func(points[0], points[1], points[2], points[3], self.time)
# returns ((x1, y1), (x2, y2))
def get_points(self):
points = []
for lbl in self.pointLbls:
m = re.match(r'\((\d+),(\d+)\)', str(lbl.text()))
if m:
points.append((int(m.group(1)), int(m.group(2))))
return points
class GenerateReportGui(QWidget):
def __init__(self, targetFunc, caliDB, defaultReportName="report"):
super(GenerateReportGui, self).__init__()
self.targFunc = targetFunc
self.caliDB = caliDB
# Set primary layout to window
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
self.setGeometry(400, 250, 1000, 600)
# Create and position calendar widgets
self.calLayout = QHBoxLayout()
self.mainLayout.addLayout(self.calLayout)
self.cal = QCalendarWidget()
self.cal2 = QCalendarWidget()
self.calLayout.addWidget(self.cal)
self.calLayout.addWidget(self.cal2)
# Add place to change the file name
self.repNameLayout = QHBoxLayout()
self.mainLayout.addLayout(self.repNameLayout)
self.repNameLbl = QLabel("Report name:")
self.repNameLayout.addWidget(self.repNameLbl)
self.repNameEdit = QLineEdit()
self.repNameEdit.setText(defaultReportName)
self.repNameEdit.textChanged.connect(self.verify_path)
self.repNameLayout.addWidget(self.repNameEdit)
self.cancelBtn = QPushButton("Cancel")
self.cancelBtn.clicked.connect(self.deleteLater)
self.mainLayout.addWidget(self.cancelBtn)
self.confirmBtn = QPushButton("Generate Report")
self.confirmBtn.clicked.connect(self.call_targ_func)
self.mainLayout.addWidget(self.confirmBtn)
self.show()
self.verify_path()
# checks if the filename chosen already
def verify_path(self):
fileName = str(self.repNameEdit.text()) + '.csv'
if os.path.exists(fileName):
self.confirmBtn.setText("File already exists")
self.confirmBtn.setEnabled(False)
else:
self.confirmBtn.setText("Generate Report")
self.confirmBtn.setEnabled(True)
def call_targ_func(self):
start = list(self.cal.selectedDate().getDate())
end = list(self.cal2.selectedDate().getDate())
self.targFunc(start, end, self.caliDB, str(self.repNameEdit.text()) + '.csv')
self.deleteLater()
# ---------------------------
class DatabaseSelector(QWidget):
def __init__(self, type, setVariableFunc):
super(DatabaseSelector, self).__init__()
self.setVariableFunc = setVariableFunc
self.content = []
typeText = 'calibration' if type == 'cali' else 'contacts'
self.folderPrefix = 'calibration_db_' if type == 'cali' else 'contacts_db_'
self.type = type
self.setWindowTitle('Select {} database'.format(typeText))
# Set primary layout to window
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
# self.setGeometry(450, 300, 400, 500)
# Set up scroll area
self.scrollArea = QScrollArea()
self.scrollArea.setWidgetResizable(True)
self.scrollWidget = QWidget()
self.scrollLayout = QVBoxLayout()
self.scrollWidget.setLayout(self.scrollLayout)
self.scrollArea.setWidget(self.scrollWidget)
self.mainLayout.addWidget(self.scrollArea)
self.populate_scroll_area()
# Add general buttons at the bottom
hLayout = QHBoxLayout()
self.mainLayout.addLayout(hLayout)
newBtn = QPushButton("Create new database")
newBtn.clicked.connect(self.on_edit)
hLayout.addWidget(newBtn)
hSpacer = QSpacerItem(1, 1, QSizePolicy.Expanding, QSizePolicy.Minimum)
hLayout.addSpacerItem(hSpacer)
recoverArchivedBtn = QPushButton("Recover archived databases")
recoverArchivedBtn.clicked.connect(self.on_recover_archived)
hLayout.addWidget(recoverArchivedBtn)
self.show()
def populate_scroll_area(self):
# Start by removing all old stuff here
for item in self.content:
item.deleteLater()
self.content = []
# Find all folders that look like the correct type of database
folderList = []
for folder in os.listdir(os.getcwd()):
if os.path.isdir(folder) and re.match(self.folderPrefix, folder):
folderList.append(folder)
# Add each to scroll area
for folder in folderList:
name = folder[len(self.folderPrefix):]
try:
with open(os.path.join(folder, "info.txt"), 'r') as infoFile:
description = infoFile.read()
# truncate the file if someone writes a ludicrous description
if len(description) > 500:
description = description[0:500] + '...'
hLayout = QHBoxLayout()
self.scrollLayout.addLayout(hLayout)
nameLbl = QLabel(name)
hLayout.addWidget(nameLbl)
selectBtn = QPushButton("Select")
selectBtn.clicked.connect(partial(self.on_select, folder))
hLayout.addWidget(selectBtn)
editBtn = QPushButton("Edit")
editBtn.clicked.connect(partial(self.on_edit, folder))
hLayout.addWidget(editBtn)
archiveBtn = QPushButton("Archive")
archiveBtn.clicked.connect(partial(self.on_archive, folder))
hLayout.addWidget(archiveBtn)
selectBtn.setMaximumWidth(80)
selectBtn.setMinimumWidth(80)
editBtn.setMaximumWidth(80)
editBtn.setMinimumWidth(80)
archiveBtn.setMaximumWidth(80)
archiveBtn.setMinimumWidth(80)
descriptionLbl = QLabel(description)
descriptionLbl.setWordWrap(True)
self.scrollLayout.addWidget(descriptionLbl)
self.content.extend([nameLbl, descriptionLbl, selectBtn, editBtn, archiveBtn])
except:
print("Warning: could not read folder " + folder + ". Skipping")
continue
def on_select(self, foldername):
if foldername:
self.setVariableFunc(foldername)
self.deleteLater()
def on_edit(self, foldername=None):
creator = Cali_DB_constructor if self.type == 'cali' else Contacts_DB_constructor
self.databaseCreateEdit = creator(self.populate_scroll_area, foldername)
def on_archive(self, foldername):
global archivedPrefix
msgbox = QMessageBox(QMessageBox.Question, "Confirm archive",
"Are you sure you want to archive {}?".format(foldername[len(self.folderPrefix):]))
msgbox.addButton(QMessageBox.Yes)
msgbox.addButton(QMessageBox.Cancel)
reply = msgbox.exec()
if reply != QMessageBox.Yes:
return
os.rename(foldername, archivedPrefix + foldername)
self.populate_scroll_area()
def on_recover_archived(self):
self.recoverer = Database_recoverer(self.type, self.populate_scroll_area)
class Database_recoverer(QWidget):
def __init__(self, type, onDeleteCallback):
super(Database_recoverer, self).__init__()
self.folderPrefix = 'calibration_db_' if type == 'cali' else 'contacts_db_'
self.content = []
self.onDeleteCallback = onDeleteCallback
typeText = 'calibration' if type == 'cali' else 'contacts'
self.setWindowTitle('Restore archived {} databases'.format(typeText))
# Set primary layout to window
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
# Set up scroll area
self.scrollArea = QScrollArea()
self.scrollArea.setWidgetResizable(True)
self.scrollWidget = QWidget()
self.scrollLayout = QVBoxLayout()
self.scrollWidget.setLayout(self.scrollLayout)
self.scrollArea.setWidget(self.scrollWidget)
self.mainLayout.addWidget(self.scrollArea)
self.populate_scroll_area()
# Add close button at the bottom
hLayout = QHBoxLayout()
self.mainLayout.addLayout(hLayout)
hSpacer = QSpacerItem(1, 1, QSizePolicy.Expanding, QSizePolicy.Minimum)
hLayout.addSpacerItem(hSpacer)
recoverArchivedBtn = QPushButton("Done")
recoverArchivedBtn.clicked.connect(self.close_window)
hLayout.addWidget(recoverArchivedBtn)
self.show()
def populate_scroll_area(self):
global archivedPrefix
# Start by removing all old stuff here
for item in self.content:
item.deleteLater()
self.content = []
# Find all folders that look like the correct type of database
folderList = []
for folder in os.listdir(os.getcwd()):
if os.path.isdir(folder) and re.match(archivedPrefix + self.folderPrefix, folder):
folderList.append(folder)
# Add each to scroll area
for folder in folderList:
name = folder[len(archivedPrefix + self.folderPrefix):]
try:
with open(os.path.join(folder, "info.txt"), 'r') as infoFile:
description = infoFile.read()
# truncate the file if someone writes a ludicrous description
if len(description) > 500:
description = description[0:500] + '...'
hLayout = QHBoxLayout()
self.scrollLayout.addLayout(hLayout)
nameLbl = QLabel(name)
hLayout.addWidget(nameLbl)
recoverBtn = QPushButton("Recover")
recoverBtn.clicked.connect(partial(self.on_recover_archived, folder))
hLayout.addWidget(recoverBtn)
recoverBtn.setMaximumWidth(80)
recoverBtn.setMinimumWidth(80)
descriptionLbl = QLabel(description)
descriptionLbl.setWordWrap(True)
self.scrollLayout.addWidget(descriptionLbl)
self.content.extend([nameLbl, descriptionLbl, recoverBtn])
except:
print("Warning: could not read folder " + folder + ". Skipping")
continue
def on_recover_archived(self, foldername):
global archivedPrefix
os.rename(foldername, foldername[len(archivedPrefix):])
self.populate_scroll_area()
def close_window(self):
self.onDeleteCallback()
self.deleteLater()
class Cali_DB_constructor(QWidget):
def __init__(self, onDeleteCallback, foldername=None):
super(Cali_DB_constructor, self).__init__()
self.prefix = "calibration_db_"
self.origName = None if not foldername else foldername[len(self.prefix):] # Also used for new/edit differentiation
self.onDeleteCallback = onDeleteCallback
self.setWindowTitle('{} calibration database:'.format("New" if not self.origName else "Edit"))
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
# Stuff for database name
self.nameLayout = QHBoxLayout()
self.nameLayout.addWidget(QLabel("Name:"))
self.mainLayout.addLayout(self.nameLayout)
self.nameEdit = QLineEdit()
self.nameLayout.addWidget(self.nameEdit)
# stuff for database description
self.infoLayout = QHBoxLayout()
self.infoLayout.addWidget(QLabel("Short description:"))
self.mainLayout.addLayout(self.infoLayout)
self.infoEdit = QTextEdit()
self.infoLayout.addWidget(self.infoEdit)
# AIS folder location
self.aisLayout = QHBoxLayout()
self.aisLayout.addWidget(QLabel("AIS folder location:"))
self.mainLayout.addLayout(self.aisLayout)
self.aisEdit = QLineEdit()
self.aisLayout.addWidget(self.aisEdit)
self.aisBrowseBtn = QPushButton("Browse...")
self.aisBrowseBtn.clicked.connect(partial(self.folder_browse_popup, self.aisEdit))
self.aisBrowseBtn.setMaximumWidth(80)
self.aisLayout.addWidget(self.aisBrowseBtn)
# Reference points and related
self.mainLayout.addWidget(QHLine())
self.gridLayout = QGridLayout()
self.mainLayout.addLayout(self.gridLayout)
self.gridLayout.addWidget(QLabel("Latitude (degrees)"), 0, 1)
self.gridLayout.addWidget(QLabel("Longitude (degrees)"), 0, 2)
self.gridLayout.addWidget(QLabel("Elevation (meters)"), 0, 3)
self.gridLayout.addWidget(QLabel("Camera:"), 1, 0)
self.gridLayout.addWidget(QLabel("Reference point 1:"), 2, 0)
self.gridLayout.addWidget(QLabel("Reference point 2:"), 3, 0)
self.latEdits = []
self.longEdits = []
self.heightEdits = []
for i in range(1, 4):
latEdit = QLineEdit()
latEdit.setValidator(QtGui.QDoubleValidator())
self.gridLayout.addWidget(latEdit, i, 1)
self.latEdits.append(latEdit)
longEdit = QLineEdit()
longEdit.setValidator(QtGui.QDoubleValidator())
self.gridLayout.addWidget(longEdit, i, 2)
self.longEdits.append(longEdit)
heightEdit = QLineEdit()
heightEdit.setValidator(QtGui.QDoubleValidator())
self.gridLayout.addWidget(heightEdit, i, 3)
self.heightEdits.append(heightEdit)
# AIS limits
self.mainLayout.addWidget(QHLine())
self.mainLayout.addWidget(QLabel("Bounding box on camera field of view:"))
self.boundingBoxLayout = QGridLayout()
self.mainLayout.addLayout(self.boundingBoxLayout)
self.boundingBoxLayout.addWidget(QLabel("Minimum"), 0, 1)
self.boundingBoxLayout.addWidget(QLabel("Maximum"), 0, 2)
self.boundingBoxLayout.addWidget(QLabel("Latitude:"), 1, 0)
self.boundingBoxLayout.addWidget(QLabel("Longitude:"), 2, 0)
self.latEditL = QLineEdit()
self.latEditL.setValidator(QtGui.QDoubleValidator())
self.boundingBoxLayout.addWidget(self.latEditL, 1, 1)
self.latEditH = QLineEdit()
self.latEditH.setValidator(QtGui.QDoubleValidator())
self.boundingBoxLayout.addWidget(self.latEditH, 1, 2)
self.longEditL = QLineEdit()
self.longEditL.setValidator(QtGui.QDoubleValidator())
self.boundingBoxLayout.addWidget(self.longEditL, 2, 1)
self.longEditH = QLineEdit()
self.longEditH.setValidator(QtGui.QDoubleValidator())
self.boundingBoxLayout.addWidget(self.longEditH, 2, 2)
# General controls at bottom
self.mainLayout.addWidget(QHLine())
self.buttonsLayout = QHBoxLayout()
self.mainLayout.addLayout(self.buttonsLayout)
hSpacer = QSpacerItem(1, 1, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.buttonsLayout.addSpacerItem(hSpacer)
self.cancelBtn = QPushButton("Cancel")
self.cancelBtn.clicked.connect(self.close_window)
self.buttonsLayout.addWidget(self.cancelBtn)
self.saveBtn = QPushButton("Save")
self.saveBtn.clicked.connect(self.save_db)
self.buttonsLayout.addWidget(self.saveBtn)
if foldername and os.path.exists(foldername):
self.load_db()
self.show()
def load_db(self):
self.nameEdit.setText(self.origName)
with open(os.path.join(self.prefix + self.origName, "info.txt"), 'r') as infile:
self.infoEdit.setText(infile.read())
metaDict = {
'camlat': self.latEdits[0],
'camlon': self.longEdits[0],
'camheight': self.heightEdits[0],
'ref1lat': self.latEdits[1],
'ref1lon': self.longEdits[1],
'ref1height': self.heightEdits[1],
'ref2lat': self.latEdits[2],
'ref2lon': self.longEdits[2],
'ref2height': self.heightEdits[2],
'latH': self.latEditH,
'latL': self.latEditL,
'lonH': self.longEditH,
'lonL': self.longEditL,
'aisFolder': self.aisEdit
}
with open(os.path.join(self.prefix + self.origName, "meta.txt"), 'r') as infile:
for line in infile.readlines():
splt = line.split(":")
if len(splt) == 2:
try:
metaDict[splt[0]].setText(splt[1].strip())
except:
print("unidentified line in cali db edit load")
print(line)
def save_db(self):
name = self.nameEdit.text()
description = self.infoEdit.toPlainText()
if len(name) == 0:
self.show_warning_box("Unable to save: name is required")
return
if len(description) == 0:
self.show_warning_box("Unable to save: description is required")
return
for box in (self.latEdits[0], self.latEdits[1], self.latEdits[2], self.longEdits[0], self.longEdits[1],
self.longEdits[2], self.heightEdits[0], self.heightEdits[1], self.heightEdits[2]):
if len(box.text()) < 1:
self.show_warning_box("Unable to save: required reference or camera data missing")
return
if not self.origName:
os.mkdir(self.prefix + name)
else:
if not self.origName == name:
os.rename(self.prefix + self.origName, self.prefix + name)
with open(os.path.join(self.prefix + name, "info.txt"), 'w+') as infoFile:
infoFile.write(description)
with open(os.path.join(self.prefix + name, "meta.txt"), 'w+') as metaFile:
metaFile.write("camlat:{}\n".format(self.latEdits[0].text()))
metaFile.write("camlon:{}\n".format(self.longEdits[0].text()))
metaFile.write("camheight:{}\n\n".format(self.heightEdits[0].text()))
metaFile.write("ref1lat:{}\n".format(self.latEdits[1].text()))
metaFile.write("ref1lon:{}\n".format(self.longEdits[1].text()))
metaFile.write("ref1height:{}\n\n".format(self.heightEdits[1].text()))
metaFile.write("ref2lat:{}\n".format(self.latEdits[2].text()))
metaFile.write("ref2lon:{}\n".format(self.longEdits[2].text()))
metaFile.write("ref2height:{}\n\n".format(self.heightEdits[2].text()))
# only write these if the necessary data is present:
if len(self.latEditH.text()) > 0:
metaFile.write("latH:{}\n".format(self.latEditH.text()))
if len(self.latEditL.text()) > 0:
metaFile.write("latL:{}\n".format(self.latEditL.text()))
if len(self.longEditH.text()) > 0:
metaFile.write("lonH:{}\n".format(self.longEditH.text()))
if len(self.longEditL.text()) > 0:
metaFile.write("lonL:{}\n\n".format(self.longEditL.text()))
metaFile.write("aisFolder:{}".format(self.aisEdit.text()))
self.close_window()
def close_window(self):
self.onDeleteCallback()
self.deleteLater()
# lineEdit will have its text set to wherever the user selects
# Borrowed from SonicTrail (copywrite Gregory O'Hagan)
def folder_browse_popup(self, lineEdit):
folder = QFileDialog.getExistingDirectory(self, "Select Directory")
if folder:
lineEdit.setText(str(folder))
@staticmethod
def show_warning_box(message):
msg = QMessageBox()
msg.setText(message)
msg.exec_()
class Contacts_DB_constructor(QWidget):
def __init__(self, onDeleteCallback, foldername=None):
super(Contacts_DB_constructor, self).__init__()
self.prefix = "contacts_db_"
self.origName = None if not foldername else foldername[len(self.prefix):] # Also used for new/edit differentiation
self.onDeleteCallback = onDeleteCallback
self.setWindowTitle('{} contacts database:'.format("New" if not self.origName else "Edit"))
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
# Stuff for database name
self.nameLayout = QHBoxLayout()
self.nameLayout.addWidget(QLabel("Name:"))
self.mainLayout.addLayout(self.nameLayout)
self.nameEdit = QLineEdit()
self.nameLayout.addWidget(self.nameEdit)
# stuff for database description
self.infoLayout = QHBoxLayout()
self.infoLayout.addWidget(QLabel("Short description:"))
self.mainLayout.addLayout(self.infoLayout)
self.infoEdit = QTextEdit()
self.infoLayout.addWidget(self.infoEdit)
# General controls at bottom
self.buttonsLayout = QHBoxLayout()
self.mainLayout.addLayout(self.buttonsLayout)
hSpacer = QSpacerItem(1, 1, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.buttonsLayout.addSpacerItem(hSpacer)
self.cancelBtn = QPushButton("Cancel")
self.cancelBtn.clicked.connect(self.close_window)
self.buttonsLayout.addWidget(self.cancelBtn)
self.saveBtn = QPushButton("Save")
self.saveBtn.clicked.connect(self.save_db)
self.buttonsLayout.addWidget(self.saveBtn)
if self.origName:
self.load_db()
self.show()
def load_db(self):
self.nameEdit.setText(self.origName)
with open(os.path.join(self.prefix + self.origName, "info.txt"), 'r') as infile:
self.infoEdit.setText(infile.read())
def save_db(self):
name = self.nameEdit.text()
description = self.infoEdit.toPlainText()
if len(name) == 0:
self.show_warning_box("Unable to save: missing name")
return
if len(description) == 0:
self.show_warning_box("Unable to save: missing description")
return
if not self.origName:
os.mkdir(self.prefix + name)
else:
if not self.origName == name:
os.rename(self.prefix + self.origName, self.prefix + name)