forked from badabing2005/PixelFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_selector.py
More file actions
168 lines (139 loc) · 7.17 KB
/
device_selector.py
File metadata and controls
168 lines (139 loc) · 7.17 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
#!/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
from i18n import _
# ============================================================================
# Class DeviceSelectorDialog
# ============================================================================
class DeviceSelectorDialog(wx.Dialog):
# ============================================================================
# Function __init__
# ============================================================================
def __init__(self, parent, devices, title=_("Select Device"), message=_("Select a device:")):
super().__init__(parent, title=title, style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
self.devices = devices
self.selected_device = None
self._create_ui(message)
self._bind_events()
self._size_and_center()
# ============================================================================
# Function _create_ui
# ============================================================================
def _create_ui(self, message):
main_sizer = wx.BoxSizer(wx.VERTICAL)
# Message
message_label = wx.StaticText(self, label=message)
main_sizer.Add(message_label, 0, wx.ALL | wx.EXPAND, 10)
# Device list
self.device_list = wx.ListCtrl(self, style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
self.device_list.AppendColumn("Device", width=200)
self.device_list.AppendColumn("Filename", width=500)
# Populate the list
for i, device in enumerate(self.devices):
index = self.device_list.InsertItem(i, device.get('device', 'Unknown'))
self.device_list.SetItem(index, 1, device.get('zip_filename', 'Unknown'))
self.device_list.SetItemData(index, i)
# Select first item by default
if self.devices:
self.device_list.Select(0)
main_sizer.Add(self.device_list, 1, wx.ALL | wx.EXPAND, 10)
# Buttons
button_sizer = wx.StdDialogButtonSizer()
ok_button = wx.Button(self, wx.ID_OK, _("OK"))
ok_button.SetDefault()
button_sizer.AddButton(ok_button)
cancel_button = wx.Button(self, wx.ID_CANCEL, _("Cancel"))
button_sizer.AddButton(cancel_button)
button_sizer.Realize()
main_sizer.Add(button_sizer, 0, wx.ALL | wx.ALIGN_RIGHT, 10)
self.SetSizer(main_sizer)
# ============================================================================
# Function _bind_events
# ============================================================================
def _bind_events(self):
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.on_item_activated)
self.Bind(wx.EVT_BUTTON, self.on_ok, id=wx.ID_OK)
# ============================================================================
# Function _size_and_center
# ============================================================================
def _size_and_center(self):
self.SetSize((750, 850))
self.CenterOnParent()
# ============================================================================
# Function on_item_activated
# ============================================================================
def on_item_activated(self, event):
# Handle double-click on list item
self.EndModal(wx.ID_OK)
# ============================================================================
# Function on_ok
# ============================================================================
def on_ok(self, event):
selection = self.device_list.GetFirstSelected()
if selection != -1:
device_index = self.device_list.GetItemData(selection)
self.selected_device = self.devices[device_index]
self.EndModal(wx.ID_OK)
else:
wx.MessageBox(_("Please select a device."), _("No Selection"), wx.OK | wx.ICON_WARNING, self)
# ============================================================================
# Function get_selected_device
# ============================================================================
def get_selected_device(self):
return self.selected_device
# ============================================================================
# Function show_device_selector
# ============================================================================
def show_device_selector(parent, devices, title=_("Select Device"), message=_("Select a device:")):
"""
Show device selector dialog and return selected device.
Args:
parent: Parent window
devices: List of device dictionaries with 'device', 'zip_filename', 'url' keys
title: Dialog title
message: Message to display above the list
Returns:
Selected device dictionary or None if cancelled
"""
if not devices:
wx.MessageBox(_("No devices available."), _("Error"), wx.OK | wx.ICON_ERROR, parent)
return None
dialog = DeviceSelectorDialog(parent, devices, title, message)
try:
if dialog.ShowModal() == wx.ID_OK:
return dialog.get_selected_device()
else:
return None
finally:
dialog.Destroy()