-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode_Families.py
435 lines (315 loc) · 14.1 KB
/
code_Families.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
# import the GUI forms that we create with Qt Creator
import form_Families
import code_Filter
import code_Lists
import code_Individual
# import basic Python libraries
import random
from copy import deepcopy
from math import floor
# 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,
QColor,
QFont,
QPen
)
from PyQt5.QtCore import (
Qt,
pyqtSignal,
QTimer
)
from PyQt5.QtWidgets import (
QApplication,
QTableWidgetItem,
QHeaderView,
QMdiSubWindow,
QGraphicsScene,
QGraphicsEllipseItem,
QMenu
)
class Families(QMdiSubWindow, form_Families.Ui_frmFamilies):
# create "resized" as a signal that the window can emit
# we respond to this signal with the form's resizeMe method below
resized = pyqtSignal()
class MyEllipse(QGraphicsEllipseItem):
family = ""
parent= ""
def mouseDoubleClickEvent(self, event):
# Do your stuff here.
self.parent.CreateFamilyList(self.family)
def contextMenuEvent(self, event):
menu = QMenu()
menu.setStyleSheet("color:black; background-color: white;")
actionSetFilterToFamily = menu.addAction("Set filter to " + self.family)
action = menu.exec(event.screenPos())
if action == actionSetFilterToFamily:
self.parent.setFamilyFilter(self.family)
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.lstFamilies.currentRowChanged.connect(self.FillSpecies)
self.lstFamilies.itemDoubleClicked.connect(self.ClickedLstFamilies)
self.lstSpecies.itemDoubleClicked.connect(self.ClickedLstSpecies)
self.tblPieChartLegend.doubleClicked.connect(self.clickedPieChartLegend)
self.lstFamilies.addAction(self.actionSetFilterToFamily)
self.actionSetFilterToFamily.triggered.connect(self.lstFamiliesSetFilterToFamily)
self.lstSpecies.addAction(self.actionSetFilterToSpecies)
self.actionSetFilterToSpecies.triggered.connect(self.lstSpeciesSetFilterToSpecies)
self.tblPieChartLegend.addAction(self.actionSetFamilyFilterToPieChartLegendFamily)
self.actionSetFamilyFilterToPieChartLegendFamily.triggered.connect(self.tblPieChartLegendSetFilterToFamily)
self.lstFamilies.setSpacing(2)
self.lstSpecies.setSpacing(2)
self.tabFamilies.setCurrentIndex(1)
self.filter = code_Filter.Filter()
self.filteredSpeciesList = []
self.filteredSpeciesListWithFamilies = []
self.familiesList = []
def lstFamiliesSetFilterToFamily(self):
family = self.lstFamilies.currentItem().text()
self.setFamilyFilter(family)
def tblPieChartLegendSetFilterToFamily(self):
currentRow = self.tblPieChartLegend.currentRow()
family = self.tblPieChartLegend.item(currentRow, 1).text()
self.setFamilyFilter(family)
def lstSpeciesSetFilterToSpecies(self):
commonName = self.lstSpecies.currentItem().text()
self.mdiParent.setSpeciesFilter(commonName)
def setFamilyFilter(self, family):
self.mdiParent.setFamilyFilter(family)
def clickedPieChartLegend(self):
currentRow = self.tblPieChartLegend.currentRow()
family = self.tblPieChartLegend.item(currentRow, 1).text()
self.CreateFamilyList(family)
def ClickedLstSpecies(self):
species = self.lstSpecies.currentItem().text()
self.CreateIndividual(species)
def ClickedLstFamilies(self):
family = self.lstFamilies.currentItem().text()
self.CreateFamilyList(family)
def CreateFamilyList(self, family):
tempFilter = deepcopy(self.filter)
tempFilter.setFamily(family)
sub = code_Lists.Lists()
sub.mdiParent = self.mdiParent
sub.FillSpecies(tempFilter)
self.parent().parent().addSubWindow(sub)
self.mdiParent.PositionChildWindow(sub, self)
sub.show()
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 FillSpecies(self):
self.lstSpecies.clear()
if self.lstFamilies.currentIndex() is not None:
selectedFamily = self.lstFamilies.currentItem().text()
familySpecies = []
# check if we've already added each species' family to list
if len(self.filteredSpeciesListWithFamilies) == 0:
# need to add species' families to list
for s in self.filteredSpeciesList:
thisFamily = self.mdiParent.db.GetFamilyName(s)
self.filteredSpeciesListWithFamilies.append([s, thisFamily])
for sf in self.filteredSpeciesListWithFamilies:
if sf[1]== selectedFamily:
familySpecies.append(sf[0])
self.lstSpecies.addItems(familySpecies)
self.lstSpecies.setSpacing(2)
count = self.mdiParent.db.CountSpecies(familySpecies)
self.lblSpecies.setText("Species for selected family: " + str(count))
def html(self):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
html = """
<!DOCTYPE html>
<html>
<head>
</head>
<style>
* {
font-size: 75%;
font-family: "Times New Roman", Times, serif;
}
table, th, td {
border-collapse: collapse;
}
th, td {
padding: 5px;
}
th {
text-align: left;
}
</style>
<body>
"""
html = html + (
"<H1>" +
self.lblLocation.text() +
"</H1>"
)
html = html + (
"<H2>" +
self.lblDateRange.text() +
"</H2>"
)
html = html + (
"<H2>" +
self.lblDetails.text() +
"</H2>"
)
html=html + (
"<font size='2'>"
)
# add family names and the species under them
for r in range(self.lstFamilies.count()):
html = html + (
"<h3>" +
self.lstFamilies.item(r).text() +
"</h3>"
)
self.filter.setFamily(self.lstFamilies.item(r).text())
for s in self.mdiParent.db.GetSpecies(self.filter):
html = html + (
s +
"<br>"
)
html = html + (
"<font size>" +
"</body>" +
"</html>"
)
QApplication.restoreOverrideCursor()
return(html)
def FillFamilies(self, filter):
self.filter = deepcopy(filter)
self.familiesList = self.mdiParent.db.GetFamilies(self.filter)
self.filteredSpeciesList = self.mdiParent.db.GetSpecies(self.filter)
cleanedFilteredSpeciesList = []
for s in self.filteredSpeciesList:
if "sp." not in s and "/" not in s and " x " not in s:
cleanedFilteredSpeciesList.append(s)
self.filteredSpeciesList = cleanedFilteredSpeciesList
self.lblFamilies.setText("Families: " + str(len(self.familiesList )))
self.lstFamilies.addItems(self.familiesList )
if len(self.familiesList ) > 0:
self.lstFamilies.setCurrentRow(0)
self.FillSpecies()
self.lstFamilies.setSpacing(2)
else:
# no families were found matching filter, so report failure back to MainWindow
return(False)
self.mdiParent.SetChildDetailsLabels(self, self.filter)
self.setWindowTitle("Families: "+ self.lblLocation.text() + ": " + self.lblDateRange.text())
QApplication.processEvents()
# report success back to MainWindow
return(True)
def FillPieChart(self):
self.tblPieChartLegend.clear()
QApplication.processEvents()
self.tblPieChartLegend.setColumnCount(3)
self.tblPieChartLegend.setRowCount(len(self.familiesList))
self.tblPieChartLegend.horizontalHeader().setVisible(False)
header = self.tblPieChartLegend.horizontalHeader()
header.setSectionResizeMode(1, QHeaderView.Stretch)
self.tblPieChartLegend.setShowGrid(False)
colors = []
familiesCount = []
set_angle = 0
R = 0
for f in self.familiesList:
familiesCount.append(sum(x.count(str(f)) for x in self.filteredSpeciesListWithFamilies))
total = sum(familiesCount)
for fl in range(len(self.familiesList)):
number = []
# randomly create three color values (rgb)
for rgb in range(3):
number.append(random.randrange(0, 255))
colors.append(QColor(number[0],number[1],number[2]))
scene = QGraphicsScene()
for family in familiesCount:
# create the angle of each wedge according to its perecent of 360
angle = round(family/total*16*360)
# set size of circle and create wedge
ellipse = self.MyEllipse(0, 0, 100, 100)
# set the tooltip for the ellipse wedge
ellipse.setToolTip(self.familiesList[R] + ": " + str(family))
# set center of circle, like an axle
ellipse.setPos(0,0)
# rotate through the wedge
ellipse.setStartAngle(set_angle)
ellipse.setSpanAngle(angle)
# assign color
ellipse.setBrush(colors[R])
# turn off pen so we won't have ellipse borders
pen = QPen()
pen.setWidthF(.1)
ellipse.setPen(pen)
# create set_angle for next time around
set_angle = angle + set_angle
# add the actual wedge to the scene object
scene.addItem(ellipse)
# save meta data to ellipse for future use (e.g., clicks)
ellipse.family = self.familiesList[R]
ellipse.parent = self
# add entry to legend table and set proper color
colorItem = QTableWidgetItem()
colorItem.setBackground(QColor(colors[R]))
familyNameItem = QTableWidgetItem()
familyNameItem.setData(Qt.DisplayRole, self.familiesList[R])
familyCountItem = QTableWidgetItem()
familyCountItem.setData(Qt.DisplayRole, family)
self.tblPieChartLegend.setItem(R, 0, colorItem)
self.tblPieChartLegend.setItem(R, 1, familyNameItem)
self.tblPieChartLegend.setItem(R, 2, familyCountItem)
R = R + 1
self.gvPieChart.setScene(scene)
self.fitPieChart()
return(True)
def fitPieChart(self):
self.gvPieChart.setSceneRect(0, 0, 100, 100)
self.gvPieChart.fitInView(self.gvPieChart.sceneRect(), Qt.KeepAspectRatio)
def resizeEvent(self, event):
#routine to handle events on objects, like clicks, lost focus, gained focus, 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.fitPieChart()
def scaleMe(self):
scaleFactor = self.mdiParent.scaleFactor
windowWidth = 780 * scaleFactor
windowHeight = 500 * 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
self.lblLocation.setFont(QFont("Helvetica", floor(fontSize * 1.4 )))
self.lblLocation.setStyleSheet("QLabel { font: bold }");
self.lblDateRange.setFont(QFont("Helvetica", floor(fontSize * 1.2 )))
self.lblDateRange.setStyleSheet("QLabel { font: bold }");
self.lblDetails.setFont(QFont("Helvetica", floor(fontSize * 1.2 )))
self.lblDetails.setStyleSheet("QLabel { font: bold }");
metrics = self.tblPieChartLegend.fontMetrics()
textHeight = metrics.boundingRect("A").height()
textWidth = metrics.boundingRect("Rank").width()
for t in [self.tblPieChartLegend]:
header = t.horizontalHeader()
header.resizeSection(0, floor(.75 * textWidth))
header.resizeSection(2, floor(.75 * textWidth))
for r in range(t.rowCount()):
t.setRowHeight(r, textHeight * 1.1)