forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_controls.py
More file actions
327 lines (273 loc) · 14 KB
/
custom_controls.py
File metadata and controls
327 lines (273 loc) · 14 KB
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
#!/usr/bin/env python
# This file is part of PixelFlasher https://github.com/badabing2005/PixelFlasher
#
# Copyright (C) 2025 Badabing2005
# SPDX-FileCopyrightText: 2025 Badabing2005
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Also add information on how to contact you by electronic and paper mail.
#
# If your software can interact with users remotely through a computer network,
# you should also make sure that it provides a way for users to get its source.
# For example, if your program is a web application, its interface could
# display a "Source" link that leads users to an archive of the code. There are
# many ways you could offer source, and different solutions will be better for
# different programs; see section 13 for the specific requirements.
#
# You should also get your employer (if you work as a programmer) or school, if
# any, to sign a "copyright disclaimer" for the program, if necessary. For more
# information on this, and how to apply and follow the GNU AGPL, see
# <https://www.gnu.org/licenses/>.
import wx
import wx.lib.buttons as buttons
import os
import json
import traceback
import webbrowser
import contextlib
from datetime import datetime
from runtime import get_device_images_history_file_path, detect_encoding, puml, get_phone
from i18n import _
# ============================================================================
# Class FilePickerComboBox
# ============================================================================
class FilePickerComboBox(wx.Panel):
def __init__(self, parent, dialog_title=_("Select a file"), wildcard="All files (*.*)|*.*"):
super(FilePickerComboBox, self).__init__(parent)
self.history_file = get_device_images_history_file_path()
self.dialog_title = dialog_title
self.wildcard = wildcard
self.history = []
self.combo_box = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_READONLY)
self.browse_button = wx.Button(self, wx.ID_ANY, _('Browse'))
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.combo_box, 1, wx.EXPAND)
sizer.Add(self.browse_button, 0, wx.EXPAND)
self.SetSizer(sizer)
self.browse_button.Bind(wx.EVT_BUTTON, self.on_browse)
self.combo_box.Bind(wx.EVT_MOUSEWHEEL, self.on_mousewheel)
if os.path.exists(self.history_file):
try:
encoding = detect_encoding(self.history_file)
with open(self.history_file, 'r', encoding=encoding, errors="replace") as f:
self.history = json.load(f)
self.combo_box.SetItems(self.history)
except Exception as e:
print(f"\n❌ {datetime.now():%Y-%m-%d %H:%M:%S} ERROR: encountered an exception during device_images_history_file loading.")
print(f"Exception: {e}")
print("Deleting the device_images_history_file to recover ...")
os.remove(self.history_file)
def SetButtonLabel(self, label):
self.browse_button.SetLabel(label)
def SetButtonWidth(self, width):
current_size = self.browse_button.GetSize()
self.browse_button.SetMinSize(wx.Size(width, current_size.GetHeight()))
self.browse_button.SetSize(wx.Size(width, current_size.GetHeight()))
def GetButtonWidth(self):
return self.browse_button.GetSize().GetWidth()
def GetPickerCtrl(self):
# Return the browse button to make the interface compatible with wx picker controls.
return self.browse_button
def on_browse(self, event):
file_dialog = wx.FileDialog(self, self.dialog_title, wildcard=self.wildcard)
if file_dialog.ShowModal() == wx.ID_OK:
file_path = file_dialog.GetPath()
if file_path not in self.history:
self.history.insert(0, file_path)
self.combo_box.Insert(file_path, 0)
if len(self.history) > 16:
self.history.pop()
if self.combo_box.Count > 16:
self.combo_box.Delete(self.combo_box.Count - 1)
self.combo_box.SetValue(file_path)
wx.PostEvent(self.combo_box, wx.CommandEvent(wx.EVT_COMBOBOX.typeId, self.combo_box.GetId()))
def SetPath(self, path):
if path and path != '' and path not in self.history:
self.history.insert(0, path)
self.combo_box.Insert(path, 0)
if len(self.history) > 16:
self.history.pop()
if self.combo_box.Count > 16:
self.combo_box.Delete(self.combo_box.Count - 1)
with open(self.history_file, 'w', encoding='utf-8') as f:
json.dump(self.history, f)
self.combo_box.SetValue(path)
def on_combo_box_change(self, event):
path = event.GetString()
if path == '':
path = self.combo_box.GetValue()
if not os.path.exists(path):
self.history.remove(path)
self.combo_box.Delete(self.combo_box.FindString(path))
if path in self.history:
self.history.remove(path)
self.history.insert(0, path)
self.combo_box.Delete(self.combo_box.FindString(path))
self.combo_box.Insert(path, 0)
self.combo_box.SetValue(path)
with open(self.history_file, 'w', encoding='utf-8') as f:
json.dump(self.history, f)
def Bind(self, event, handler):
if event == wx.EVT_FILEPICKER_CHANGED:
self.handler = handler
self.combo_box.Bind(wx.EVT_COMBOBOX, self._on_combo_box_change)
def _on_combo_box_change(self, event):
self.handler(event)
self.on_combo_box_change(event)
def GetPath(self):
return self.combo_box.GetStringSelection()
def SetToolTip(self, tooltip_text):
self.combo_box.SetToolTip(tooltip_text)
def on_mousewheel(self, event):
# Stop the event propagation to disable mouse wheel scrolling
event.StopPropagation()
# ============================================================================
# Class NoScrollComboBox
# ============================================================================
class NoScrollComboBox(wx.ComboBox):
def __init__(self, *args, **kwargs):
super(NoScrollComboBox, self).__init__(*args, **kwargs)
self.Bind(wx.EVT_MOUSEWHEEL, self.on_mousewheel)
def on_mousewheel(self, event):
# Stop the event propagation to disable mouse wheel scrolling
event.StopPropagation()
# ============================================================================
# Class NoScrollChoice
# ============================================================================
class NoScrollChoice(wx.Choice):
def __init__(self, *args, **kwargs):
super(NoScrollChoice, self).__init__(*args, **kwargs)
self.Bind(wx.EVT_MOUSEWHEEL, self.on_mousewheel)
def on_mousewheel(self, event):
# Stop the event propagation to disable mouse wheel scrolling
event.StopPropagation()
# ============================================================================
# Class DropDownLink
# ============================================================================
class DropDownLink(wx.BitmapButton):
def __init__(self, parent, id=wx.ID_ANY, bitmap=wx.NullBitmap, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.BU_AUTODRAW):
super().__init__(parent, id, bitmap, pos, size, style)
self.Bind(wx.EVT_BUTTON, self.OnButtonClick)
self.popup_menu = wx.Menu()
def OnButtonClick(self, event):
self.PopupMenu(self.popup_menu)
def AddLink(self, label, url, icon=None):
item = self.popup_menu.Append(wx.ID_ANY, label)
if icon:
item.SetBitmap(icon)
self.Bind(wx.EVT_MENU, lambda event, url=url: self.OnLinkSelected(event, url), item)
def OnLinkSelected(self, event, url):
# Handle the selected link here
print(f"Selected link: {url}")
open_device_image_download_link(url)
# ============================================================================
# Class DropDownButton
# ============================================================================
class DropDownButton(buttons.GenBitmapTextButton):
# def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
# super().__init__(parent, id, wx.NullBitmap, label, pos, size, style)
def __init__(self, parent, id, bitmap, label, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0):
super().__init__(parent, id, bitmap, label, pos, size, style)
self.Bind(wx.EVT_BUTTON, self.OnButtonClick)
self.popup_menu = wx.Menu()
def SetBitmap(self, bitmap):
if bitmap.IsOk():
self.SetBitmapLabel(bitmap)
else:
print("Invalid bitmap")
def OnButtonClick(self, event):
self.PopupMenu(self.popup_menu)
def AddFunction(self, label, function, icon_bitmap=None, enabled=True):
item = self.popup_menu.Append(wx.ID_ANY, label)
item.Enable(enabled)
if icon_bitmap:
item.SetBitmap(icon_bitmap)
self.Bind(wx.EVT_MENU, lambda event, function=function: self.OnFunctionSelected(event, function), item)
return item
def OnFunctionSelected(self, event, function):
# Call the selected function here
function()
# ============================================================================
# Class ResizableButtonDirPickerCtrl
# ============================================================================
class ResizableButtonDirPickerCtrl(wx.DirPickerCtrl):
# Custom DirPickerCtrl with ability to easily resize the browse button.
def __init__(self, parent, id=wx.ID_ANY, path="", message="Select a folder",
style=wx.DIRP_USE_TEXTCTRL|wx.DIRP_DIR_MUST_EXIST,
button_label=None, button_width=None):
super().__init__(parent, id, path, message, style=style)
# Set button label if provided
if button_label is not None:
self.SetButtonLabel(button_label)
# Set button width if provided
if button_width is not None:
self.SetButtonWidth(button_width)
def SetButtonLabel(self, label):
if self.GetPickerCtrl():
self.GetPickerCtrl().SetLabel(label)
def SetButtonWidth(self, width):
if self.GetPickerCtrl():
current_size = self.GetPickerCtrl().GetSize()
self.GetPickerCtrl().SetMinSize(wx.Size(width, current_size.GetHeight()))
self.GetPickerCtrl().SetSize(wx.Size(width, current_size.GetHeight()))
def GetButtonWidth(self):
if self.GetPickerCtrl():
return self.GetPickerCtrl().GetSize().GetWidth()
return 0
# ============================================================================
# Class ResizableButtonFilePickerCtrl
# ============================================================================
class ResizableButtonFilePickerCtrl(wx.FilePickerCtrl):
# Custom FilePickerCtrl with ability to easily resize the browse button.
def __init__(self, parent, id=wx.ID_ANY, path="", message="Select a file",
wildcard="All files (*.*)|*.*", style=wx.FLP_USE_TEXTCTRL,
button_label=None, button_width=None):
super().__init__(parent, id, path, message, wildcard, style=style)
# Set button label if provided
if button_label is not None:
self.SetButtonLabel(button_label)
# Set button width if provided
if button_width is not None:
self.SetButtonWidth(button_width)
def SetButtonLabel(self, label):
if self.GetPickerCtrl():
self.GetPickerCtrl().SetLabel(label)
def SetButtonWidth(self, width):
if self.GetPickerCtrl():
current_size = self.GetPickerCtrl().GetSize()
self.GetPickerCtrl().SetMinSize(wx.Size(width, current_size.GetHeight()))
self.GetPickerCtrl().SetSize(wx.Size(width, current_size.GetHeight()))
def GetButtonWidth(self):
if self.GetPickerCtrl():
return self.GetPickerCtrl().GetSize().GetWidth()
return 0
# ============================================================================
# Function _open_device_image_download_link
# ============================================================================
def open_device_image_download_link(url):
try:
with contextlib.suppress(Exception):
device = get_phone()
if device:
hardware = device.hardware
else:
hardware = ''
print(f"Launching browser for Google image download URL: {url}#{hardware}")
webbrowser.open_new(f"{url}#{hardware}")
puml(f":Open Link;\nnote right\n=== {hardware} Firmware Link\n[[{url}#{hardware}]]\nend note\n", True)
except Exception as e:
print(f"\n❌ {datetime.now():%Y-%m-%d %H:%M:%S} ERROR: Encountered an error while opening firmware link")
traceback.print_exc()