-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
78 lines (64 loc) · 2.62 KB
/
common.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
"""
This file contains functions that are used by multiple windows to prevent duplication.
"""
from PyQt5.Qt import Qt
from PyQt5 import QtWidgets
import globalstuff
def GameIDMismatch():
msgbox = QtWidgets.QMessageBox.question(globalstuff.mainWindow, 'Game ID Mismatch',
"The Game ID in this codelist doesn't match this file's."
"Do you want to continue?")
return msgbox
def CheckChildren(item: QtWidgets.QTreeWidgetItem):
"""
Recursively enables the check on an item's children
"""
for i in range(item.childCount()):
child = item.child(i)
if child.childCount():
CheckChildren(child)
else:
child.setCheckState(0, Qt.Checked)
def CountCheckedCodes(source: QtWidgets.QTreeWidget, userecursive: bool):
"""
Returns a list of the codes currently enabled, based on certain criteria. Matchflag returns 64 if userecursive is
False, 1 if True.
"""
return filter(lambda x: bool(x.checkState(0)), source.findItems('', Qt.MatchContains | Qt.MatchFlag(64 >> 6 * int(not userecursive))))
def SelectItems(source: QtWidgets.QTreeWidget):
"""
Marks items as checked if they are selected, otherwise unchecks them
"""
bucketlist = source.findItems('', Qt.MatchContains | Qt.MatchRecursive)
for item in bucketlist:
if item in source.selectedItems():
item.setCheckState(0, Qt.Checked)
else:
item.setCheckState(0, Qt.Unchecked)
# This for categories which aren't expanded
for item in filter(lambda x: x in source.selectedItems() and x.childCount() and not x.isExpanded(), bucketlist):
CheckChildren(item)
def CleanChildren(item: QtWidgets.QTreeWidgetItem):
"""
The clone function duplicates unchecked children as well, so we're cleaning those off. I'm sorry, little ones.
"""
for i in range(item.childCount()):
child = item.child(i)
if child: # Failsafe
if child.childCount():
CleanChildren(child)
elif child.checkState(0) == Qt.Unchecked:
item.takeChild(i)
def AssembleCode(code: str):
"""
Takes an unformatted string and adds spaces and newlines.
"""
assembledcode = ''
for index, char in enumerate(code):
if not index % 16 and index:
assembledcode = '\n'.join([assembledcode, char.upper()])
elif not index % 8 and index:
assembledcode = ' '.join([assembledcode, char.upper()])
else:
assembledcode = ''.join([assembledcode, char.upper()])
return assembledcode