-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode_Location.py
556 lines (435 loc) · 18 KB
/
code_Location.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
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
# import the GUI forms that we create with Qt Creator
import form_Location
# import classes from other project files
import code_Filter
import code_Lists
import code_MapHtml
import code_Individual
import code_Stylesheet
from collections import defaultdict
# import the Qt components we'll use
# do this so later we won't have to clutter our code with references to parent Qt classes
from PyQt5.QtGui import (
QCursor,
QFont
)
from PyQt5.QtCore import (
Qt,
pyqtSignal,
QIODevice,
QByteArray,
QBuffer,
)
from PyQt5.QtWidgets import (
QApplication,
QTableWidgetItem,
QHeaderView,
QMdiSubWindow
)
from math import (
floor
)
from PyQt5.QtWebEngineWidgets import (
QWebEngineView,
)
from base64 import (
b64encode
)
import code_Stylesheet
class Location(QMdiSubWindow, form_Location.Ui_frmLocation):
resized = pyqtSignal()
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.setAttribute(Qt.WA_DeleteOnClose,True)
self.mdiParent = ""
self.resized.connect(self.resizeMe)
self.tblDates.currentItemChanged.connect(self.FillSpeciesForDate)
self.lstSpecies.doubleClicked.connect(self.ClickedLstSpecies)
self.tblDates.doubleClicked.connect(self.ClickedTblDates)
self.tblSpecies.doubleClicked.connect(self.ClickedTblSpecies)
self.tblDates.setShowGrid(False)
self.horizontalLayout_2.setContentsMargins(5, 5, 5, 5)
self.horizontalLayout_2.setSpacing(4)
self.webMap = QWebEngineView(self.tabMap)
self.webMap.setObjectName("webMap")
self.horizontalLayout_2.addWidget(self.webMap)
self.tabLocation.setCurrentIndex(0)
def ClickedLstSpecies(self):
species = self.lstSpecies.currentItem().text()
self.CreateIndividual(species)
def ClickedTblDates(self):
thisDate = self.tblDates.item(self.tblDates.currentRow(), 1).text()
thisLocation = self.lblLocation.text()
tempFilter = code_Filter.Filter()
tempFilter.setLocationType("Location")
tempFilter.setLocationName(thisLocation)
tempFilter.setStartDate(thisDate)
tempFilter.setEndDate(thisDate)
self.CreateSpeciesList(tempFilter)
def ClickedTblSpecies(self):
selectedRow = self.tblSpecies.currentRow()
selectedColumn = self.tblSpecies.currentColumn()
if selectedColumn > 1:
thisDate = self.tblSpecies.item(selectedRow, selectedColumn).text()
thisLocation = self.lblLocation.text()
tempFilter = code_Filter.Filter()
tempFilter.setLocationType("Location")
tempFilter.setLocationName(thisLocation)
tempFilter.setStartDate(thisDate)
tempFilter.setEndDate(thisDate)
self.CreateSpeciesList(tempFilter)
else:
thisSpecies = self.tblSpecies.item(selectedRow, 1).text()
self.CreateIndividual(thisSpecies)
def CreateIndividual(self, species):
sub = code_Individual.Individual()
sub.mdiParent = self.mdiParent
sub.FillIndividual(species)
self.parent().parent().addSubWindow(sub)
self.mdiParent.PositionChildWindow(sub, self)
sub.show()
sub.resizeMe()
def CreateSpeciesList(self, filter):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
sub = code_Lists.Lists()
sub.mdiParent = self.mdiParent
sub.FillSpecies(filter)
self.parent().parent().addSubWindow(sub)
self.mdiParent.PositionChildWindow( sub, self)
sub.show()
QApplication.restoreOverrideCursor()
def FillLocation(self, location):
self.location = location
thisLocationDates= []
filter = code_Filter.Filter()
filter.setLocationType("Location")
filter.setLocationName(location)
thisLocationDates = self.mdiParent.db.GetDates(filter)
thisLocationDates.sort()
self.tblDates.setColumnCount(4)
self.tblDates.setRowCount(len(thisLocationDates))
self.tblDates.horizontalHeader().setVisible(True)
self.tblDates.setHorizontalHeaderLabels(['Rank', 'Date', 'Species', 'Checklists'])
header = self.tblDates.horizontalHeader()
header.setSectionResizeMode(2, QHeaderView.Stretch)
self.lblLocation.setText(location)
self.lblFirstVisited.setText("First visited: " + thisLocationDates[0])
self.lblMostRecentlyVisited.setText("Most recently visited: " + thisLocationDates[len(thisLocationDates)-1])
dateArray = []
for d in thisLocationDates:
if d != "":
dSpecies = set()
checklistCount = set()
for sighting in self.mdiParent.db.locationDict[location]:
if d == sighting["date"]:
dSpecies.add(sighting["commonName"])
checklistCount.add(sighting["checklistID"])
dateArray.append([len(dSpecies), d, len(checklistCount)])
dateArray.sort(reverse=True)
rank = 0
lastDateTotal = 0
R = 0
for date in dateArray:
rankItem = QTableWidgetItem()
if date[0] != lastDateTotal:
rank = R + 1
rankItem.setData(Qt.DisplayRole, rank)
dateItem = QTableWidgetItem()
dateItem.setText(date[1])
totalSpeciesItem = QTableWidgetItem()
totalSpeciesItem.setData(Qt.DisplayRole, date[0])
totalChecklistsItem = QTableWidgetItem()
totalChecklistsItem.setData(Qt.DisplayRole, date[2])
self.tblDates.setItem(R, 0, rankItem)
self.tblDates.setItem(R, 1, dateItem)
self.tblDates.setItem(R, 2, totalSpeciesItem)
self.tblDates.setItem(R, 3, totalChecklistsItem)
lastDateTotal = date[0]
R = R + 1
self.tblDates.selectRow(0)
if self.tblDates.rowCount() == 1:
self.lblDatesSeen.setText("Date (1)")
else:
self.lblDatesSeen.setText("Dates (" + str(self.tblDates.rowCount()) + ")")
# display all dates for the selected location
self.tblDates.setSortingEnabled(True)
self.tblDates.sortItems(0,0)
self.tblDates.setCurrentCell(0, 1)
self.coordinates = self.mdiParent.db.GetLocationCoordinates(location)
# display the species in the species for date list
self.FillSpeciesForDate()
# display the main all-species list
self.FillSpecies()
self.scaleMe()
self.resizeMe()
def FillMap(self):
coordinatesDict = defaultdict()
coordinatesDict[self.location] = self.coordinates
mapWidth = self.width() - 40
mapHeight= self.height() - 170
thisMap = code_MapHtml.MapHtml()
thisMap.mapHeight = mapHeight
thisMap.mapWidth = mapWidth
thisMap.coordinatesDict = coordinatesDict
html = thisMap.html()
self.webMap.setHtml(html)
def FillSpecies(self):
location = self.lblLocation.text()
tempFilter = code_Filter.Filter()
tempFilter.setLocationType("Location")
tempFilter.setLocationName(location)
# get species data from db
thisWindowList = self.mdiParent.db.GetSpeciesWithData(tempFilter)
# set up tblSpecies column headers and widths
self.tblSpecies.setColumnCount(6)
self.tblSpecies.setRowCount(len(thisWindowList)+1)
self.tblSpecies.horizontalHeader().setVisible(True)
self.tblSpecies.setHorizontalHeaderLabels(['Tax', 'Species', 'First', 'Last', 'Chlists', '% of Chlists'])
header = self.tblSpecies.horizontalHeader()
header.setSectionResizeMode(1, QHeaderView.Stretch)
self.tblSpecies.setShowGrid(False)
font = QFont()
font.setBold(True)
count = 0
nonSpeciesTaxaCount = 0
# add species and dates to table row by row
R = 0
for species in thisWindowList:
taxItem = QTableWidgetItem()
taxItem.setData(Qt.DisplayRole, R)
speciesItem = QTableWidgetItem()
speciesItem.setText(species[0])
firstItem = QTableWidgetItem()
firstItem.setData(Qt.DisplayRole, species[1])
lastItem = QTableWidgetItem()
lastItem.setData(Qt.DisplayRole, species[2])
checklistsItem = QTableWidgetItem()
checklistsItem.setData(Qt.DisplayRole, species[5])
percentageItem = QTableWidgetItem()
percentageItem.setData(Qt.DisplayRole, species[6])
self.tblSpecies.setItem(R, 0, taxItem)
self.tblSpecies.setItem(R, 1, speciesItem)
self.tblSpecies.setItem(R, 2, firstItem)
self.tblSpecies.setItem(R, 3, lastItem)
self.tblSpecies.setItem(R, 4, checklistsItem)
self.tblSpecies.setItem(R, 5, percentageItem)
self.tblSpecies.item(R, 1).setFont(font)
# set the species to gray if it's not a true species
if " x " in species[0] or "sp." in species[0] or "/" in species[0]:
self.tblSpecies.item(R, 1).setForeground(Qt.gray)
nonSpeciesTaxaCount += 1
else:
self.tblSpecies.item(R, 1).setForeground(code_Stylesheet.speciesColor)
count += 1
R += 1
labelText = "Species: " + str(count)
if nonSpeciesTaxaCount > 0:
labelText = labelText + " + " + str(nonSpeciesTaxaCount) + " taxa"
self.lblSpecies.setText(labelText)
def SetDate(self, date):
if self.tblDates.rowCount() > 0:
for d in range(self.tblDates.rowCount()):
if self.tblDates.item(d, 1).text() == date:
self.tblDates.setCurrentCell(d, 1)
self.FillSpeciesForDate()
self.tabLocation.setCurrentIndex(1)
break
def FillSpeciesForDate(self):
self.lstSpecies.clear()
location = self.lblLocation.text()
if self.tblDates.item(self.tblDates.currentRow(), 1) is not None:
date = self.tblDates.item(self.tblDates.currentRow(), 1).text()
tempFilter = code_Filter.Filter()
tempFilter.setStartDate(date)
tempFilter.setEndDate(date)
tempFilter.setLocationName(location)
tempFilter.setLocationType("Location")
species = self.mdiParent.db.GetSpecies(tempFilter)
font = QFont()
font.setBold(True)
count = 0
nonSpeciesTaxaCount = 0
R = 0
for s in species:
self.lstSpecies.addItem(s)
self.lstSpecies.item(R).setFont(font)
# set the species to gray if it's not a true species
if " x " in s or "sp." in s or "/" in s:
self.lstSpecies.item(R).setForeground(Qt.gray)
nonSpeciesTaxaCount += 1
else:
self.lstSpecies.item(R).setForeground(code_Stylesheet.speciesColor)
count += 1
R += 1
labelText = "Species: " + str(count)
if nonSpeciesTaxaCount > 0:
labelText = labelText + " + " + str(nonSpeciesTaxaCount) + " taxa"
self.lstSpecies.setCurrentRow(0)
self.lstSpecies.setSpacing(2)
self.lblSpeciesSeen.setText(labelText)
def html(self):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
# create start to basic html format
html = """
<!DOCTYPE html>
<html>
<head>
</head>
<style>
* {
font-size: 75%;
font-family: "Times New Roman", Times, serif;
}
th {
text-align: left;
}
</style>
<body>
"""
# add title information
html = html + (
"<H1>" +
self.lblLocation.text() +
"</H1>"
)
html = html + (
"<H3>" +
self.lblFirstVisited.text() +
"</H3>"
)
html = html + (
"<H3>" +
self.lblMostRecentlyVisited.text() +
"</H3>"
)
# grab the map image from the map tap
# process it into a byte array and encode it
# so we can insert it inline into the html
myPixmap = self.webMap.grab()
myPixmap = myPixmap.scaledToWidth(600, Qt.SmoothTransformation)
myByteArray = QByteArray()
myBuffer = QBuffer(myByteArray)
myBuffer.open(QIODevice.WriteOnly)
myPixmap.save(myBuffer, "PNG")
encodedImage = b64encode(myByteArray)
html = html + ("""
<img src="data:image/png;base64,
""")
html = html + str(encodedImage)[1:]
html = html + ("""
" />
""")
html = html + (
"<H4>" +
"Species"
"</H4>"
)
html=html + (
"<font size='2'>" +
"<p>"
)
# loopthrough the species listed in tblSpecies
for r in range(self.tblSpecies.rowCount()):
html= html + (
self.tblSpecies.item(r, 1).text()
+ "<br>"
)
html= html + (
"<H4>" +
"Dates" +
"</H4>"
)
# create filter set to our current location
filter = code_Filter.Filter()
filter.setLocationType = "Location"
filter.setLocationName = self.lblLocation.text()
# for each date in tblDates, find the species and display them in a table
for r in range(self.tblDates.rowCount()):
html= html + (
"<b>" +
self.tblDates.item(r, 1).text() +
"</b>"
)
filter.setStartDate(self.tblDates.item(r, 1).text())
filter.setEndDate(self.tblDates.item(r, 1).text())
species = self.mdiParent.db.GetSpecies(filter)
html = html + (
"<br>"
"<table width='100%'>"
"<tr>"
)
# set up counter R to start a new row after listing each 3 species
R = 1
for s in species:
html = html + (
"<td>" +
s +
"</td>"
)
if R == 3:
html = html + (
"</tr>"
"<tr>"
)
R = 0
R = R + 1
html= html + (
"<br>" +
"<br>" +
"<br>" +
"</table>"
)
html = html + (
"<font size>" +
"</body>" +
"</html>"
)
QApplication.restoreOverrideCursor()
return(html)
def resizeEvent(self, event):
#routine to handle events on objects, like clicks, lost focus, gained forcus, etc.
self.resized.emit()
return super(self.__class__, self).resizeEvent(event)
def resizeMe(self):
windowWidth = self.frameGeometry().width()
windowHeight = self.frameGeometry().height()
self.scrollArea.setGeometry(5, 27, windowWidth -10 , windowHeight-35)
self.FillMap()
def scaleMe(self):
scaleFactor = self.mdiParent.scaleFactor
windowWidth = 800 * scaleFactor
windowHeight = 600 * scaleFactor
self.resize(windowWidth, windowHeight)
fontSize = self.mdiParent.fontSize
scaleFactor = self.mdiParent.scaleFactor
#scale the font for all widgets in window
for w in self.children():
try:
w.setFont(QFont("Helvetica", fontSize))
except:
pass
baseFont = QFont(QFont("Helvetica", fontSize))
locationFont = QFont(QFont("Helvetica", floor(fontSize * 1.4)))
locationFont.setBold(True)
self.lblLocation.setFont(locationFont)
self.lblFirstVisited.setFont(baseFont)
self.lblMostRecentlyVisited.setFont(baseFont)
header = self.tblSpecies.horizontalHeader()
metrics = self.tblSpecies.fontMetrics()
dateTextWidth = metrics.boundingRect("2222-22-22").width()
dateTextHeight = metrics.boundingRect("2222-22-22").height()
taxText = str(self.tblSpecies.rowCount())
taxTextWidth = metrics.boundingRect(taxText).width()
header.resizeSection(0, floor(1.7 * taxTextWidth))
header.resizeSection(2, floor(1.3 * dateTextWidth))
header.resizeSection(3, floor(1.3 * dateTextWidth))
for R in range(self.tblSpecies.rowCount()):
self.tblSpecies.setRowHeight(R, dateTextHeight)
header = self.tblDates.horizontalHeader()
textWidth = metrics.boundingRect("Rank").width()
header.resizeSection(0, floor(1.5 * textWidth))
header.resizeSection(1, floor(1.5 * dateTextWidth))
for R in range(self.tblDates.rowCount()):
self.tblDates.setRowHeight(R, dateTextHeight)
self.FillMap()