-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwidget_package.py
473 lines (395 loc) · 21.4 KB
/
widget_package.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
import arduboy.arduhex
import arduboy.fxcart
import arduboy.patch
import arduboy.image
import arduboy.common
from arduboy.constants import *
import gui_common
import widgets_common
import widget_slot
import constants
import gui_utils
import utils
import debug_actions
from typing import List
from PyQt6.QtWidgets import QVBoxLayout, QWidget, QPushButton, QGroupBox, QHBoxLayout, QFileDialog, QComboBox
from PyQt6.QtWidgets import QLineEdit, QLabel, QScrollArea, QTableWidget, QListWidget, QTableWidgetItem, QTextBrowser
from PyQt6.QtWidgets import QListWidgetItem, QTextEdit, QStackedWidget
from PyQt6.QtCore import Qt
FIELDS_SPACING = 3
LICENSE_HELP_URL = "https://choosealicense.com/"
class BinaryWidget(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
layout.setSpacing(FIELDS_SPACING)
# ------------- TOP ROW (IMAGE/BASIC DATA) -----------------
toprow_container = QWidget()
toprow_layout = QHBoxLayout()
toprow_layout.setContentsMargins(0,0,0,0)
toprow_container.setLayout(toprow_layout)
self.image_select = widget_slot.TitleImageWidget()
self.image_select.setToolTip("Cart image; users will see this when browsing games on their Arduboy!")
toprow_layout.addWidget(self.image_select)
toprow_layout.setStretchFactor(self.image_select, 0)
basicdata_container = QWidget()
basicdata_layout = QVBoxLayout()
basicdata_layout.setContentsMargins(0,0,0,0)
basicdata_container.setLayout(basicdata_layout)
self.title_edit = QLineEdit()
self.title_edit.setPlaceholderText("Title (optional)")
basicdata_layout.addWidget(self.title_edit)
self.device_select = QComboBox()
for d in arduboy.arduhex.ALLOWED_DEVICES:
self.device_select.addItem(d)
self.device_select.setToolTip("The device this binary is for.\n-Arduboy binaries can be used on any official Arduboy, but should not include FX data.\n-ArduboyFX and ArduboyMini are for FX-enabled titles that are compiled for those specific devices")
basicdata_layout.addWidget(self.device_select)
toprow_layout.addWidget(basicdata_container)
toprow_layout.setStretchFactor(basicdata_container, 1)
layout.addWidget(toprow_container)
# -------------- DATA CONTROLS ----------------
def mkdata(basetext, field, default_func, opentitle, filetypes, reader, setextra = None, lengthcalc = None):
container = QWidget()
container_layout = QHBoxLayout()
container_layout.setContentsMargins(0,0,0,0)
button = QPushButton()
container_layout.addWidget(button)
container_layout.setStretchFactor(button, 1)
deletebutton = QPushButton("❌")
container_layout.addWidget(deletebutton)
container_layout.setStretchFactor(deletebutton, 0)
lengthcalc = lengthcalc or (lambda: len(getattr(self, field)))
def refresh():
length = lengthcalc()
button.setText(basetext + (f" - {length} bytes" if length else " - None"))
if length == 0:
deletebutton.setDisabled(True)
deletebutton.setStyleSheet(f"color: {gui_common.SUBDUEDCOLOR}")
else:
deletebutton.setDisabled(False)
deletebutton.setStyleSheet(f"color: {gui_common.ERRORCOLOR}")
def setdata():
file_path, _ = QFileDialog.getOpenFileName(self, opentitle, "", filetypes)
if file_path:
setattr(self, field, reader(file_path))
if setextra:
setextra()
refresh()
button.clicked.connect(setdata)
gui_common.set_emoji_font(deletebutton)
def deletedata():
setattr(self, field, default_func())
refresh()
deletebutton.clicked.connect(deletedata)
container.setLayout(container_layout)
layout.addWidget(container)
deletedata() # Might as well set a bunch of stuff
return refresh
def read_text(fp):
with open(fp, "r") as f:
return f.read()
def read_binary(fp):
with open(fp, "rb") as f:
return bytearray(f.read())
def setdata_extra():
save_bytes = arduboy.fxcart.embedded_save_size(self.data_raw)
if save_bytes:
# Ask if the user wants to create a save out of this
if gui_utils.yes_no("Split save section out",
"The data provided appears to have a save section at the end. This is normal when using the development binary. Do you want to strip the save and add it properly to the slot (recommended)?",
self):
self.save_raw = self.data_raw[-save_bytes:]
self.data_raw = self.data_raw[:-save_bytes]
self.refresh_fxsavetext()
self.refresh_fxdatatext()
def hexlength():
return len(arduboy.common.hex_to_bin(self.hex_raw))
self.refresh_hextext = mkdata(".hex program", "hex_raw", lambda: "", "Open .hex file", constants.HEX_FILEFILTER, read_text, lengthcalc=hexlength)
self.refresh_fxdatatext = mkdata("FX data", "data_raw", lambda: bytearray(), "Open FX data file", constants.BIN_FILEFILTER, read_binary, setextra = setdata_extra)
self.refresh_fxsavetext = mkdata("FX save", "save_raw", lambda: bytearray(), "Open FX save file", constants.BIN_FILEFILTER, read_binary)
# -------------- FINAL COMPOSE ---------------
self.setLayout(layout)
def get_binary(self) -> arduboy.arduhex.ArduboyBinary:
return arduboy.arduhex.ArduboyBinary(
self.device_select.currentText(),
self.title_edit.text(),
self.hex_raw,
self.data_raw,
self.save_raw,
arduboy.image.bin_to_pilimage(self.image_select.image_bytes) if self.image_select.image_bytes else None
)
def fill(self, binary: arduboy.arduhex.ArduboyBinary):
self.title_edit.setText(binary.title)
self.device_select.setCurrentText(binary.device)
if binary.cartImage:
self.image_select.set_image_pil(binary.cartImage)
self.hex_raw = binary.hex_raw
self.data_raw = binary.data_raw
self.save_raw = binary.save_raw
self.refresh_hextext()
self.refresh_fxdatatext()
self.refresh_fxsavetext()
class PackageEditor(QWidget):
def __init__(self):
super().__init__()
editor_layout = QHBoxLayout()
# Editor panes
self.info_group = QGroupBox("Package Info")
self.binary_group = QGroupBox("Package Binaries")
# ------------ INFO PANE ------------------
info_layout = QVBoxLayout()
info_layout.setSpacing(FIELDS_SPACING)
self.info_group.setLayout(info_layout)
# Need a widget that can switch between views
self.stacked_editor = QStackedWidget()
# Need a widget that represents all the fields that will get closed when the license editor is open.
self.license_replace = QWidget()
license_replace_layout = QVBoxLayout()
license_replace_layout.setContentsMargins(0,0,0,0)
license_replace_layout.setSpacing(FIELDS_SPACING)
self.license_replace.setLayout(license_replace_layout)
# Need the license editor widget
self.license_edit = QTextEdit()
self.license_edit.setPlaceholderText("Enter full license text here.\n\nIf choosing a license online, simply copy + paste the full license text here. You may need to edit some fields for your name and the date.")
# Add them both to the stack
self.stacked_editor.addWidget(self.license_replace)
self.stacked_editor.addWidget(self.license_edit)
info_layout.addWidget(self.stacked_editor)
info_layout.setStretchFactor(self.stacked_editor, 1)
# All these junk fields are exactly the same
def mkedit(placeholder, tooltip):
edit = QLineEdit()
edit.setPlaceholderText(placeholder)
if tooltip:
edit.setToolTip(tooltip)
license_replace_layout.addWidget(edit)
license_replace_layout.setStretchFactor(edit, 0)
return edit
# All the junk fields
self.title_edit = mkedit("Title", "Title of your program. This is used as part of a key to uniquely identify your program, try not to change it! (Required)")
self.version_edit = mkedit("Version", "Version of your program; you should increment it every update! (Required)")
self.author_edit = mkedit("Author", "Singular name or entity. This is used as part of a key to uniquely identify your program, try not to change it! (Required)")
self.info_edit = mkedit("Description", "A short description of your program")
self.genre_edit = mkedit("Genre", "How you would categorize your program (consider the existing cart categories, such as Action/Adventure/etc)")
self.url_edit = mkedit("URL", "A website for your program or team (not required)")
self.sourceurl_edit = mkedit("Source URL", "The link to the source code for this program (github?)")
self.email_edit = mkedit("Email", "A point of contact for support (not required)")
# Some fields not included just yet
# The contributors section
contributors_header_container = QWidget()
contributors_header_layout = QHBoxLayout()
contributors_header_layout.setContentsMargins(0,0,0,0)
contributors_header_container.setLayout(contributors_header_layout)
contributors_label = QLabel("Contributors:")
contributors_header_layout.addWidget(contributors_label)
contributors_header_layout.setStretchFactor(contributors_label, 1)
self.contributors_add = QPushButton("Add")
self.contributors_add.setStyleSheet("font-size: 10px")
self.contributors_add.clicked.connect(self.add_contributor)
contributors_header_layout.addWidget(self.contributors_add)
contributors_header_layout.setStretchFactor(self.contributors_add, 0)
self.contributors_remove = QPushButton("Remove")
self.contributors_remove.setStyleSheet("font-size: 10px")
self.contributors_remove.clicked.connect(self.remove_contributor)
contributors_header_layout.addWidget(self.contributors_remove)
contributors_header_layout.setStretchFactor(self.contributors_remove, 0)
license_replace_layout.addWidget(contributors_header_container)
license_replace_layout.setStretchFactor(contributors_label, 0)
self.contributors_table = QTableWidget()
self.contributors_table.setColumnCount(3)
self.contributors_table.setHorizontalHeaderLabels(["Name ", "Roles ", "Urls (comma separated)"])
self.contributors_table.resizeColumnsToContents()
self.contributors_table.horizontalHeader().setStretchLastSection(True)
license_replace_layout.addWidget(self.contributors_table)
license_replace_layout.setStretchFactor(self.contributors_table, 1)
license_row = QWidget()
license_layout = QHBoxLayout()
license_layout.setContentsMargins(0,0,0,0)
license_row.setLayout(license_layout)
self.license_edit_button = QPushButton("Edit License")
self.license_edit_button.clicked.connect(self.open_license)
license_layout.addWidget(self.license_edit_button)
self.license_help = widgets_common.ClickableLink("Help me choose", LICENSE_HELP_URL)
self.license_help.setFixedHeight(self.license_edit_button.sizeHint().height())
self.license_help.setFixedWidth(125)
license_layout.addWidget(self.license_help)
info_layout.addWidget(license_row)
info_layout.setStretchFactor(license_row, 0)
# ------------ Binary PANE ------------------
binary_layout = QVBoxLayout()
self.binary_group.setLayout(binary_layout)
controls_container = QWidget()
controls_layout = QHBoxLayout()
controls_layout.setContentsMargins(0,0,0,0)
controls_container.setLayout(controls_layout)
self.binary_add = QPushButton("Add")
self.binary_add.clicked.connect(self.add_binary)
controls_layout.addWidget(self.binary_add)
self.binary_remove = QPushButton("Remove")
self.binary_remove.clicked.connect(self.remove_binary)
controls_layout.addWidget(self.binary_remove)
binary_layout.addWidget(controls_container)
binary_layout.setStretchFactor(self.binary_add, 0)
self.binary_list = QListWidget()
binary_layout.addWidget(self.binary_list)
binary_layout.setStretchFactor(self.binary_list, 1)
# -------------- FINAL COMPOSE ---------------
editor_layout.addWidget(self.info_group)
editor_layout.addWidget(self.binary_group)
editor_layout.setStretchFactor(self.info_group, 1)
editor_layout.setStretchFactor(self.binary_group, 1)
self.setLayout(editor_layout)
def add_contributor(self, contributor: arduboy.arduhex.ArduboyContributor = None):
rowPosition = self.contributors_table.rowCount()
self.contributors_table.insertRow(rowPosition)
if contributor:
fields = [contributor.name, ", ".join(contributor.roles), ", ".join(contributor.urls)]
else:
fields = ["","",""]
for i, field in enumerate(fields):
self.contributors_table.setItem(rowPosition , i, QTableWidgetItem(field))
def remove_contributor(self):
selected_items = self.contributors_table.selectedItems()
for item in selected_items:
self.contributors_table.removeRow(item.row())
def add_binary(self, binary: arduboy.arduhex.ArduboyBinary = None):
item = QListWidgetItem()
widget = BinaryWidget()
if binary:
widget.fill(binary)
# item.setFlags(item.flags() | 2) # Add the ItemIsEditable flag to enable reordering
item.setSizeHint(widget.sizeHint())
# IDK what the right order for all this is...
self.binary_list.addItem(item)
self.binary_list.setItemWidget(item, widget)
self.binary_list.setCurrentItem(item)
def remove_binary(self):
selected_items = self.binary_list.selectedItems()
# selected_count = len(selected_items)
for item in selected_items:
row = self.binary_list.row(item)
self.binary_list.takeItem(row)
def fill(self, package: arduboy.arduhex.ArduboyParsed):
self.title_edit.setText(package.title)
self.version_edit.setText(package.version)
self.author_edit.setText(package.author)
self.info_edit.setText(package.description)
self.genre_edit.setText(package.genre)
self.url_edit.setText(package.url)
self.sourceurl_edit.setText(package.sourceUrl)
self.email_edit.setText(package.email)
self.license_edit.setPlainText(package.license)
for c in package.contributors:
self.add_contributor(c)
for b in package.binaries:
self.add_binary(b)
def get_contributors(self) -> List[arduboy.arduhex.ArduboyContributor]:
result = []
for row in range(self.contributors_table.rowCount()):
contributor = arduboy.arduhex.ArduboyContributor(self.contributors_table.item(row, 0).text())
def columnsplit(col):
raw = self.contributors_table.item(row, col).text()
return [x.strip() for x in raw.split(",")] if raw else []
contributor.roles = columnsplit(1)
contributor.urls = columnsplit(2)
result.append(contributor)
return result
def get_binaries(self) -> List[arduboy.arduhex.ArduboyBinary]:
result = []
for x in range(self.binary_list.count()):
widget = self.binary_list.itemWidget(self.binary_list.item(x)) #.get_slot_data() for x in range(self.list_widget.count())]
result.append(widget.get_binary())
return result
def create_package(self):
package = arduboy.arduhex.empty_parsed_arduboy()
package.title = self.title_edit.text()
package.version = self.version_edit.text()
package.author = self.author_edit.text()
package.description = self.info_edit.text()
package.genre = self.genre_edit.text()
package.url = self.url_edit.text()
package.sourceUrl = self.sourceurl_edit.text()
package.email = self.email_edit.text()
package.license = self.license_edit.toPlainText()
if not package.title:
raise Exception("Title is required!")
if not package.version:
raise Exception("Version is required! Just put 1.0 if you're unsure!")
if not package.author:
raise Exception("Author is required!")
package.contributors = self.get_contributors()
package.binaries = self.get_binaries()
if len(package.binaries) == 0:
raise Exception("No binaries; there must always be at least one program!")
for b in package.binaries:
if b.fx_enabled() and b.device == arduboy.arduhex.DEVICE_ARDUBOY:
raise Exception(f"Binary '{b.title}' can't be marked for device '{b.device}', it is FX enabled!")
if not b.hex_raw or len(b.hex_raw) == 0:
raise Exception("You MUST provide the main .hex file for every binary!")
return package
def open_license(self):
open_tab = self.stacked_editor.currentIndex()
if open_tab == 0:
self.stacked_editor.setCurrentIndex(1)
self.license_edit_button.setText("Close License")
elif open_tab == 1:
self.stacked_editor.setCurrentIndex(0)
self.license_edit_button.setText("Edit License")
class PackageWidget(QWidget):
def __init__(self):
super().__init__()
full_layout = QVBoxLayout()
self.setLayout(full_layout)
self.package_editor = None
self.reset_editor()
self.prep_editor_layout()
# Controls for saving/loading/etc packages
package_controls = QWidget() # QGroupBox("Package Controls")
package_controls_layout = QHBoxLayout()
package_controls.setLayout(package_controls_layout)
load_package_button = QPushButton("Load")
load_package_button.clicked.connect(self.do_load_package)
package_controls_layout.addWidget(load_package_button)
save_package_button = QPushButton("Save")
save_package_button.clicked.connect(self.do_save_package)
package_controls_layout.addWidget(save_package_button)
clear_package_button = QPushButton("Reset")
clear_package_button.clicked.connect(self.do_reset_package)
package_controls_layout.addWidget(clear_package_button)
full_layout.addWidget(package_controls)
full_layout.setStretchFactor(package_controls, 0)
def prep_editor_layout(self):
self.package_editor.layout().setContentsMargins(0, 0, 0, 0)
self.layout().setStretchFactor(self.package_editor, 1)
def reset_editor(self, arduparsed: arduboy.arduhex.ArduboyParsed = None):
new_editor = PackageEditor()
if self.package_editor:
self.layout().replaceWidget(self.package_editor, new_editor)
self.package_editor.setParent(None) # Removes it from the interface
else:
self.layout().addWidget(new_editor)
if arduparsed:
new_editor.fill(arduparsed)
else:
new_editor.add_binary() # Just a convenience maybe? Could be an inconvenience...
self.package_editor = new_editor
self.prep_editor_layout()
def do_reset_package(self):
# Must confirm
if gui_utils.yes_no("Confirm reset package", "Are you sure you want to reset the package?", self):
# We do something really stupid
self.reset_editor()
debug_actions.global_debug.add_action_str(f"Reset arduboy package editor")
def do_load_package(self):
file_path, _ = QFileDialog.getOpenFileName(self, "Open Arduboy File", "", constants.ARDUHEX_FILEFILTER)
if file_path:
parsed = arduboy.arduhex.read_any(file_path)
self.reset_editor(parsed)
debug_actions.global_debug.add_action_str(f"Loaded arduboy package into editor: {file_path}")
def do_save_package(self):
package = self.package_editor.create_package()
filepath, _ = QFileDialog.getSaveFileName(self, "Save slot as .arduboy", utils.get_arduhex_backup_filename(package), constants.ARDUBOY_FILEFILTER)
if filepath:
# The slot is special and can have additional fields. Might as well get them now
arduboy.arduhex.write_arduboy(package, filepath)
debug_actions.global_debug.add_action_str(f"Wrote arduboy file for: {package.title} to {filepath}")