-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBossPathWidget.py
242 lines (176 loc) · 7.36 KB
/
BossPathWidget.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
from PyQt5 import QtCore, QtWidgets, QtGui
Qt = QtCore.Qt
class BossPathEditorWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=parent)
self.archiveContents = []
self.fileLoaded = False
self.currentLoadedFile = ''
self.selectedFile = ''
self.layout = QtWidgets.QVBoxLayout(self)
# Create Widgets
self.fileSelector = QtWidgets.QComboBox()
self.scrollArea = QtWidgets.QScrollArea()
self.BossPathEntries = BossPathEntryContainer()
# Setup Scroll Area
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setWidget(self.BossPathEntries)
# Default Widgets to disabled
self.fileSelector.setDisabled(True)
self.scrollArea.setDisabled(True)
# Setup Signals
self.fileSelector.currentIndexChanged.connect(self.fileIndexChanged)
# add widgets to layout
topLayout = QtWidgets.QHBoxLayout()
topLayout.addWidget(self.fileSelector, 1, Qt.AlignVCenter)
self.layout.addLayout(topLayout)
self.layout.addWidget(self.scrollArea)
def loadData(self, archiveContents):
self.archiveContents = archiveContents
QtCore.QObject.blockSignals(self.fileSelector, True)
# Add elements to file selector drop-down
for file in self.archiveContents:
if not str(file.name).find('worldIn'):
self.fileSelector.addItem(str(file.name)[7:-4])
QtCore.QObject.blockSignals(self.fileSelector, False)
# Enable the Ui
self.scrollArea.setDisabled(False)
self.fileSelector.setDisabled(False)
# load initial file
self.fileIndexChanged()
def closeData(self):
self.archiveContents = []
self.fileLoaded = False
self.currentLoadedFile = ''
self.selectedFile = ''
self.BossPathEntries.reset()
self.fileSelector.setDisabled(True)
self.scrollArea.setDisabled(True)
self.fileSelector.clear()
def fileIndexChanged(self):
# store the currently selected file's name
self.selectedFile = self.fileSelector.currentText() + '.csv'
# check if a file is already open
if self.fileLoaded:
# if a file is already open, store the changes made and close the file
self.storeChanges()
self.BossPathEntries.reset()
self.loadSelectedFile()
else:
self.loadSelectedFile()
def loadSelectedFile(self):
files = []
# load the data for the file the user selected
for file in self.archiveContents:
if file.name == 'worldIn' + self.selectedFile:
files.append(file)
elif file.name == 'toCastle' + self.selectedFile:
files.append(file)
self.BossPathEntries.populate(files)
self.fileLoaded = True
self.currentLoadedFile = self.selectedFile
def storeChanges(self):
data = self.BossPathEntries.bossPathToArray()
i = 0
for entry in data:
if str(entry) == 'worldIn':
for file in self.archiveContents:
if str(file.name) == 'worldIn' + self.currentLoadedFile:
d = str(data[i + 1])
d = d.encode('shiftjis')
file.data = d
elif str(entry) == 'toCastle':
for file in self.archiveContents:
if str(file.name) == 'toCastle' + self.currentLoadedFile:
d = str(data[i + 1])
d = d.encode('shiftjis')
file.data = d
i += 1
def getArchiveContents(self):
self.storeChanges()
return self.archiveContents
class BossPathEntryContainer(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=parent)
self.layout = QtWidgets.QHBoxLayout(self)
self.worldInEntries = []
self.toCastleEntries = []
def populate(self, files):
for file in files:
if not str(file.name).find('worldIn'):
data = file.data
data = data.decode('shiftjis')
worldIn = BossPathEntry(data, "World Into")
self.layout.addWidget(worldIn, 0, Qt.AlignTop)
self.worldInEntries.append(worldIn)
if not str(file.name).find('toCastle'):
data = file.data
data = data.decode('shiftjis')
toCastle = BossPathEntry(data, "From Tower")
self.layout.addWidget(toCastle, 0, Qt.AlignTop)
self.toCastleEntries.append(toCastle)
if not self.toCastleEntries:
self.layout.insertStretch(1, 0)
def bossPathToArray(self):
temp = []
if self.worldInEntries:
for worldIn in self.worldInEntries:
temp.append('worldIn')
temp.append(worldIn.valuesToString())
if self.toCastleEntries:
for toCastle in self.toCastleEntries:
temp.append('toCastle')
temp.append(toCastle.valuesToString())
return temp
def reset(self):
self.clearLayout(self.layout)
self.worldInEntries = []
self.toCastleEntries = []
def clearLayout(self, layout):
while layout.count():
child = layout.takeAt(0)
if child.widget() is not None:
child.widget().deleteLater()
elif child.layout() is not None:
self.clearLayout(child.layout())
class BossPathEntry(QtWidgets.QFrame, QtWidgets.QWidget):
def __init__(self, data, name, parent=None):
QtWidgets.QFrame.__init__(self, parent=parent)
QtWidgets.QWidget.__init__(self, parent=parent)
self.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.layout = QtWidgets.QVBoxLayout(self)
headerLayout = QtWidgets.QHBoxLayout()
self.entries = []
nameLabel = QtWidgets.QLabel(name)
headerLayout.addWidget(nameLabel)
addEntryBtn = QtWidgets.QPushButton('Insert Node')
addEntryBtn.setIcon(QtGui.QIcon('RouteEditData/icons/plus.png'))
addEntryBtn.pressed.connect(self.addNewEntry)
removeEntryBtn = QtWidgets.QPushButton('Remove Node')
removeEntryBtn.setIcon(QtGui.QIcon('RouteEditData/icons/minus.png'))
removeEntryBtn.pressed.connect(self.removeEntry)
# buttonLayout = QtWidgets.QVBoxLayout()
headerLayout.addWidget(addEntryBtn)
headerLayout.addWidget(removeEntryBtn)
self.layout.addLayout(headerLayout)
data = str(data).split(',')
for i in data:
lineEdit = QtWidgets.QLineEdit(i)
self.entries.append(lineEdit)
self.layout.addWidget(lineEdit)
def valuesToString(self):
temp = []
for lineEdit in self.entries:
temp.append(lineEdit.text())
output = ','.join(temp)
output.encode('shiftjis')
return output
def addNewEntry(self):
lineEdit = QtWidgets.QLineEdit()
self.entries.append(lineEdit)
self.layout.addWidget(lineEdit)
def removeEntry(self):
if len(self.entries) >= 2:
if self.entries[-1] is not None:
self.entries[-1].deleteLater()
self.entries = self.entries[:-1]