|
| 1 | +import wx |
| 2 | +import wx.adv |
| 3 | +import wx.grid |
| 4 | +import wx.dataview |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | +from .editor import EditorFrame |
| 8 | + |
| 9 | +APP_VERSION = "v0.0.1β" |
| 10 | + |
| 11 | + |
| 12 | +class AnonymizerApp(wx.App): |
| 13 | + def __init__(self, filenames=None): |
| 14 | + super().__init__(False) |
| 15 | + |
| 16 | + self.app_frame = AppFrame(None, "AnonymizEDF", filenames) |
| 17 | + |
| 18 | + |
| 19 | +class AppFrame(wx.Frame): |
| 20 | + def __init__(self, parent, title, filenames=None): |
| 21 | + super().__init__(parent, title=title) |
| 22 | + self.CreateStatusBar() |
| 23 | + |
| 24 | + self._configure_menu() |
| 25 | + self._configure_drop_target() |
| 26 | + |
| 27 | + if filenames: |
| 28 | + for fn in filenames: |
| 29 | + self.open_editor(fn) |
| 30 | + else: |
| 31 | + self.Centre() |
| 32 | + self.Show() |
| 33 | + |
| 34 | + def _configure_menu(self): |
| 35 | + menu = wx.MenuBar() |
| 36 | + |
| 37 | + # File menu |
| 38 | + file_menu = wx.Menu() |
| 39 | + |
| 40 | + about_item = file_menu.Append( |
| 41 | + wx.ID_ABOUT, "&About", "Information about this program" |
| 42 | + ) |
| 43 | + self.Bind(wx.EVT_MENU, self.on_menu_about, about_item) |
| 44 | + quit_item = file_menu.Append(wx.ID_EXIT, "&Quit", "Terminate the program") |
| 45 | + self.Bind(wx.EVT_MENU, self.on_menu_quit, quit_item) |
| 46 | + open_item = file_menu.Append(wx.ID_OPEN, "&Open…", "Open an EDF file") |
| 47 | + self.Bind(wx.EVT_MENU, self.on_file_open, open_item) |
| 48 | + |
| 49 | + menu.Append(file_menu, "&File") |
| 50 | + |
| 51 | + self.SetMenuBar(menu) |
| 52 | + |
| 53 | + def _configure_drop_target(self): |
| 54 | + drop_panel = wx.Panel(self, style=wx.BORDER_THEME) |
| 55 | + |
| 56 | + label = wx.StaticText(drop_panel, wx.ID_ANY, label="Drop an EDF file here") |
| 57 | + open_btn = wx.Button(drop_panel, wx.ID_ANY, label="Open…") |
| 58 | + open_btn.Bind(wx.EVT_BUTTON, self.on_file_open) |
| 59 | + sizer_h = wx.BoxSizer(wx.HORIZONTAL) |
| 60 | + sizer_v = wx.BoxSizer(wx.VERTICAL) |
| 61 | + sizer_v.Add(label, 1, wx.CENTER) |
| 62 | + sizer_v.Add(open_btn, 1, wx.CENTER) |
| 63 | + sizer_h.Add(sizer_v, 1, wx.CENTER) |
| 64 | + drop_panel.SetSizer(sizer_h) |
| 65 | + |
| 66 | + target = FileDropTarget(self.on_files_drop) |
| 67 | + drop_panel.SetDropTarget(target) |
| 68 | + |
| 69 | + box = wx.BoxSizer(wx.VERTICAL) |
| 70 | + box.Add(drop_panel, 1, wx.EXPAND | wx.ALL, border=20) |
| 71 | + |
| 72 | + self.SetSizer(box) |
| 73 | + self.Layout() |
| 74 | + |
| 75 | + def on_menu_about(self, event): |
| 76 | + dialog = wx.MessageDialog( |
| 77 | + self, |
| 78 | + f"An Anonymizer for EDF files\n(Version {APP_VERSION})", |
| 79 | + "AnonymizEDF", |
| 80 | + wx.CLOSE, |
| 81 | + ) |
| 82 | + dialog.ShowModal() |
| 83 | + dialog.Destroy() |
| 84 | + |
| 85 | + def on_menu_quit(self, event): |
| 86 | + self.Close(True) |
| 87 | + |
| 88 | + def on_files_drop(self, filenames): |
| 89 | + for filename in filenames: |
| 90 | + self.open_editor(filename) |
| 91 | + |
| 92 | + def on_file_open(self, event): |
| 93 | + dialog = wx.FileDialog( |
| 94 | + self, wildcard="EDF files (*.edf)|*.edf", style=wx.FD_OPEN |
| 95 | + ) |
| 96 | + if dialog.ShowModal() == wx.ID_OK: |
| 97 | + filename = dialog.GetPath() |
| 98 | + self.open_editor(filename) |
| 99 | + |
| 100 | + dialog.Destroy() |
| 101 | + |
| 102 | + def open_editor(self, filename): |
| 103 | + editor = EditorFrame(self, Path(filename)) |
| 104 | + editor.Show() |
| 105 | + self.Hide() |
| 106 | + |
| 107 | + def on_child_closed(self): |
| 108 | + children = [c for c in self.GetChildren() if isinstance(c, wx.Frame)] |
| 109 | + if len(children) <= 1: |
| 110 | + self.Show() |
| 111 | + |
| 112 | + |
| 113 | +class FileDropTarget(wx.FileDropTarget): |
| 114 | + def __init__(self, callback): |
| 115 | + super().__init__() |
| 116 | + self.callback = callback |
| 117 | + |
| 118 | + def OnDropFiles(self, x, y, filenames): |
| 119 | + self.callback(filenames) |
| 120 | + return True |
0 commit comments