-
Notifications
You must be signed in to change notification settings - Fork 5
/
gui_handlers.py
249 lines (215 loc) · 10.4 KB
/
gui_handlers.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
import os
import sys
import pathlib
import textwrap
from pathlib import Path
from zipfile import ZipFile, ZIP_DEFLATED
import wx
import wx.adv
from loguetools import version
from loguetools import dump, common
from loguetools.explode import explode
from loguetools.translate import translate
from loguetools_gui_autogenerated import MainFrame
def print_sep():
print("__________________________________________________________________________\n")
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
class MyFileDropTarget(wx.FileDropTarget):
"""Drag and drop file loading
https://stackoverflow.com/a/31393351
"""
def __init__(self, parent):
wx.FileDropTarget.__init__(self)
self.parent = parent
def OnDropFiles(self, x, y, filenames):
filepath = filenames[0]
if Path(filepath).suffix in common.all_suffixes:
# validated extension
self.parent.loadfile(filepath)
return True
class MyFrame(MainFrame):
def __init__(self, parent):
super().__init__(parent)
# Application icon
self.SetIcon(wx.Icon(resource_path(u"icons/tools.ico")))
# Direct stdout to TextCtrlOut
sys.stdout = self.m_textCtrlOut
# Populate toolbar with tool buttons and connect them up; do this here because we need to wrap the icon paths
self.tool_load = self.toolbar.AddLabelTool(
wx.ID_OPEN, u"tool",
wx.Bitmap(resource_path(u"icons/file_manager.ico"), wx.BITMAP_TYPE_ANY),
wx.NullBitmap, wx.ITEM_NORMAL, u"Open Pack",
u"Open Pack - Open a Patch library", None)
self.tool_translate = self.toolbar.AddLabelTool(
wx.ID_FILE1, u"tolib",
wx.Bitmap(resource_path(u"icons/compile_warning.ico"), wx.BITMAP_TYPE_ANY),
wx.NullBitmap, wx.ITEM_NORMAL, u"Translate to Lib",
u"Translate Pack - Convert to minilogue xd and save as lib", None)
self.tool_translate_to_presets = self.toolbar.AddLabelTool(
wx.ID_FILE2, u"topresets",
wx.Bitmap(resource_path(u"icons/compile_error.ico"), wx.BITMAP_TYPE_ANY),
wx.NullBitmap, wx.ITEM_NORMAL, u"Translate to Preset Pack",
u"Translate Pack - Convert to minilogue xd and save as preset pack", None)
self.tool_explode = self.toolbar.AddLabelTool(
wx.ID_SAVE, u"tool",
wx.Bitmap(resource_path(u"icons/document_tree.ico"), wx.BITMAP_TYPE_ANY),
wx.NullBitmap, wx.ITEM_NORMAL, u"Explode",
u"Explode - Save all programs/patches to separate files", None)
self.tool_dump = self.toolbar.AddLabelTool(
wx.ID_VIEW_DETAILS, u"tool",
wx.Bitmap(resource_path(u"icons/box_open.ico"), wx.BITMAP_TYPE_ANY),
wx.NullBitmap, wx.ITEM_NORMAL, u"Dump",
u"Dump - Display program/patch content", None)
self.tool_exit = self.toolbar.AddLabelTool(
wx.ID_EXIT, u"tool",
wx.Bitmap(resource_path(u"icons/door_in.ico"), wx.BITMAP_TYPE_ANY),
wx.NullBitmap, wx.ITEM_NORMAL, u"Exit", u"Exit", None)
self.toolbar.Realize()
# Connect Events
self.Bind(wx.EVT_TOOL, self.OnLoadFile, id=self.tool_load.GetId())
self.Bind(wx.EVT_TOOL, self.OnTranslate, id=self.tool_translate.GetId())
self.Bind(wx.EVT_TOOL, self.OnTranslateToPresets, id=self.tool_translate_to_presets.GetId())
self.Bind(wx.EVT_TOOL, self.OnExplode, id=self.tool_explode.GetId())
self.Bind(wx.EVT_TOOL, self.OnDump, id=self.tool_dump.GetId())
self.Bind(wx.EVT_TOOL, self.OnExit, id=self.tool_exit.GetId())
self.toolbar.EnableTool(wx.ID_FILE1, False) # translate to lib
self.toolbar.EnableTool(wx.ID_FILE2, False) # translate to preset pack
self.toolbar.EnableTool(wx.ID_SAVE, False) # explode
self.toolbar.EnableTool(wx.ID_VIEW_DETAILS, False) # dump
self.logue_type = None
file_drop_target = MyFileDropTarget(self)
self.listCtrl.SetDropTarget(file_drop_target)
def OnLoadFile(self, event):
"""Load/Save/etc buttons (in controls area)
"""
# otherwise ask the user what new file to open
with wx.FileDialog(
self, "Choose a file",
wildcard=\
"patches|"
"*.mnlgxdpreset;*.mnlgxdlib;*.mnlgxdprog;"
"*.mnlgpreset;*.mnlglib;*.mnlgprog;"
"*.prlgpreset;*.prlglib;*.prlgprog;"
"*.molgpreset;*.molglib;*.molgprog;"
"*.kklib;*.kkprog;"
"|minilogue (.mnlgpreset;.mnlglib;.mnlgprog)|"
"*.mnlgpreset;*.mnlglib;*.mnlgprog;"
"|minilogue xd (.mnlgxdpreset;.mnlgxdlib;.mnlgxdprog)|"
"*.mnlgxdpreset;*.mnlgxdlib;*.mnlgxdprog;"
"|prologue (.prlgpreset;.prlglib;.prlgprog)|"
"*.prlgpreset;*.prlglib;*.prlgprog"
"|monologue (.molgpreset;.molglib;.molgprog)|"
"*.molgpreset;*.molglib;*.molgprog"
"|KingKORG (.kklib;.kkprog)|"
"*.kklib;*.kkprog"
"",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# Proceed loading the file chosen by the user
pathname = fileDialog.GetPath()
self.loadfile(pathname)
def loadfile(self, pathname):
"""File loader called when file dialog is used or file is dragged and dropped.
Args:
pathname (str or Path object): Path to file
"""
try:
with open(pathname, 'r') as fileobj:
self.LoadData(fileobj)
self.file = fileobj.name
self.toolbar.EnableTool(wx.ID_FILE1, False)
self.toolbar.EnableTool(wx.ID_FILE2, False)
self.toolbar.EnableTool(wx.ID_SAVE, False)
self.toolbar.EnableTool(wx.ID_VIEW_DETAILS, False)
self.logue_type, self.is_a_collection = \
common.file_type(pathlib.Path(self.file).suffix)
if self.is_a_collection:
self.toolbar.EnableTool(wx.ID_SAVE, True)
if self.logue_type in {"prologue", "og"}:
self.toolbar.EnableTool(wx.ID_FILE1, True)
self.toolbar.EnableTool(wx.ID_FILE2, True)
if self.logue_type == "xd":
suffix = pathlib.Path(self.file).suffix
assert suffix in {".mnlgxdpreset", ".mnlgxdlib", ".mnlgxdprog"}
if suffix == ".mnlgxdlib":
self.toolbar.EnableTool(wx.ID_FILE2, True)
else:
self.toolbar.EnableTool(wx.ID_FILE1, True)
self.statusBar.SetStatusText(f"Loaded {self.file}")
except IOError:
wx.LogError("Cannot open file '%s'." % pathname)
def LoadData(self, fileobj):
self.zipobj = ZipFile(fileobj.name, "r", compression=ZIP_DEFLATED, compresslevel=9)
self.proglist = common.zipread_progbins(self.zipobj)
lc = self.listCtrl
lc.ClearAll()
lc.AppendColumn("id")
lc.AppendColumn("name")
lc.AppendColumn("md5:4")
lc.SetColumnWidth(0, 40)
lc.SetColumnWidth(1, 200)
lc.SetColumnWidth(2, 50)
for i, p in enumerate(self.proglist):
patchdata = self.zipobj.read(p)
flavour, hash = common.patch_ident(patchdata)
if not common.is_init_patch(flavour, hash):
prgname = common.program_name(patchdata, flavour)
lc.Append([i+1, prgname, hash[:4]])
def OnPatchSelected(self, event):
if self.logue_type in {"og", "xd"}:
self.toolbar.EnableTool(wx.ID_VIEW_DETAILS, True)
def OnPatchDeselected(self, event):
self.toolbar.EnableTool(wx.ID_VIEW_DETAILS, False)
def OnDump(self, event):
item = self.listCtrl.GetFocusedItem()
prgname = self.listCtrl.GetItemText(item, col=1)
print(f"{prgname}")
patchdata = self.zipobj.read(self.proglist[item])
print(common.flavour_to_product[common.patch_ident(patchdata)[0]] + " patch")
dump.print_patch(patchdata)
print_sep()
def OnTranslate(self, event):
unskip_init = self.m_checkBox_inits.GetValue()
skip_factory = self.m_checkBox_factory.GetValue()
translate(self.file, None, None, None, unskip_init, skip_factory, False)
def OnTranslateToPresets(self, event):
unskip_init = self.m_checkBox_inits.GetValue()
skip_factory = self.m_checkBox_factory.GetValue()
translate(self.file, None, None, None, unskip_init, skip_factory, True)
def OnExplode(self, event):
prepend_id = self.m_checkBox_id.GetValue()
append_md5_4 = self.m_checkBox_md5.GetValue()
append_version = self.m_checkBox_version.GetValue()
unskip_init = self.m_checkBox_inits.GetValue()
skip_factory = self.m_checkBox_factory.GetValue()
explode(self.file, None, None, prepend_id, append_md5_4, append_version, unskip_init, skip_factory)
def OnExit(self, event):
"""Close the frame, terminating the application."""
self.Close(True)
def OnAbout(self, event):
aboutInfo = wx.adv.AboutDialogInfo()
aboutInfo.SetName("loguetools")
aboutInfo.SetVersion(version.__version__)
aboutInfo.SetDescription(
textwrap.dedent("""
Tools for Korg minilogue, prologue
and minilogue xd program banks
Author: Gary Ruben
""")
)
aboutInfo.SetCopyright("3-clause BSD licence")
aboutInfo.SetWebSite("https://github.com/gazzar/loguetools")
wx.adv.AboutBox(aboutInfo)
def OnMouseoverId(self, event):
self.m_checkBox_id.SetToolTipString("Prepend id to filename")
def OnMouseoverMd5(self, event):
self.m_checkBox_md5.SetToolTipString("Append md5 checksum to filename")
def OnMouseoverVer(self, event):
self.m_checkBox_version.SetToolTipString("Append loguetools version to filename")
def OnMouseoverInits(self, event):
self.m_checkBox_inits.SetToolTipString("Include patches named Init Program")