Skip to content

Commit 6a19f26

Browse files
tinyboxxxUltrawipf
authored andcommitted
Fix language initialization in MainUI
Implemented a new profile initialization without UI and adjusted the initialization order within MainUI. Known issue: After booting, changing the language from any translation to en_US requires a manual restart of the application to take effect.
1 parent e77fd6f commit 6a19f26

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

base_ui.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(self, parent: PyQt6.QtWidgets.QWidget = None, ui_form: str = ""):
4747
self.tech_log = logging.getLogger(ui_form)
4848
if ui_form:
4949
PyQt6.uic.loadUi(helper.res_path(ui_form), self)
50+
# print(f"UI file loaded : {ui_form}")
5051

5152
def init_ui(self):
5253
"""Prototype of init_ui to manage this status in subclass."""

main.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def __init__(self):
7373
"""Init the mainUI : init the UI, all the dlg element, and the main timer."""
7474
PyQt6.QtWidgets.QMainWindow.__init__(self)
7575
base_ui.CommunicationHandler.__init__(self)
76+
77+
self.profile_ui = profile_ui.ProfileUI(main=self) # load profile without UI
78+
self.translator = PyQt6.QtCore.QTranslator(self) # Languages must be created before UI loaded
79+
self.load_language() # load manually
80+
7681
base_ui.WidgetUI.__init__(self, None, "MainWindow.ui")
7782

7883
self.restart_app_flag = False
@@ -97,9 +102,8 @@ def __init__(self):
97102
# Systray
98103
self.systray = SystrayWrapper(self)
99104
# Profile
100-
self.profile_ui = profile_ui.ProfileUI(main=self)
101-
102-
self.make_lang_selector() # Languages must be created as early as possible
105+
self.profile_ui.initialize_ui() # Profile UI
106+
self.make_lang_selector()
103107

104108
self.timer = PyQt6.QtCore.QTimer(self)
105109
self.timer.timeout.connect(self.update_timer) # pylint: disable=no-value-for-parameter
@@ -197,13 +201,23 @@ def setup(self):
197201
nb_device_compat = self.serialchooser.get_ports()
198202
self.serialchooser.auto_connect(nb_device_compat)
199203

200-
# def refresh_widgets(self):
201-
# for w in app.allWidgets(): # Does not actually update the translation
202-
# w.update()
203-
# w.repaint()
204-
# # print(w)
204+
def load_language(self):
205+
"""load language file in profile befor creating UI"""
206+
app.removeTranslator(self.translator)
207+
208+
langid = self.profile_ui.get_global_setting("language",DEFAULTLANG)
209+
# print(f"Loading langid: {langid}")
210+
if langid == DEFAULTLANG:
211+
langfile = ''
212+
else:
213+
langfile = helper.res_path(f"{langid}.qm","translations")
214+
# print(f"Loading language file: {langfile}")
215+
if self.translator.load(langfile):
216+
app.installTranslator(self.translator)
217+
# print(f"Language file loaded: {langfile}")
205218

206219
def change_language(self,enabled):
220+
"""Change language of the UI, this will run too when initializing the UI"""
207221
if(not enabled):
208222
return
209223
langfile,user_language = self.language_action_group.checkedAction().data()
@@ -218,6 +232,7 @@ def change_language(self,enabled):
218232
app.removeTranslator(self.translator)
219233
if self.translator.load(langfile):
220234
app.installTranslator(self.translator)
235+
# print(f"Language file loaded: {langfile}")
221236
self.profile_ui.set_global_setting("language",user_language) #store language
222237

223238
#self.refresh_widgets()
@@ -914,8 +929,8 @@ def process_events():
914929
QueryValueEx as getSubkeyValue,
915930
OpenKey as getKey,
916931
)
917-
918-
if windows_theme_is_light() == 0:
932+
# Check if is not using windows 11 style(windows 11 style is dark mode compatible)
933+
if windows_theme_is_light() == 0 and app.style().objectName() != "windows11":
919934
app.setStyle("Fusion")
920935
app.setPalette(dark_palette.PALETTE_DARK)
921936
window.menubar.setStyleSheet("QMenu::item {color: white; }") # Menu item text ignores palette setting and stays black. Force to white.

profile_ui.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,30 @@ class ProfileUI(base_ui.WidgetUI, base_ui.CommunicationHandler):
3535
profile_selected_event = PyQt6.QtCore.pyqtSignal(str)
3636

3737
def __init__(self, main=None):
38+
"""Initialize profile without UI."""
39+
self.main = main
40+
self.profiles_dlg = None # ProfilesDialog is not initialized yet
41+
self.profile_setup = {}
42+
self.profiles = {}
43+
44+
self._current_class = -1
45+
self._current_command = -1
46+
self._current_instance = -1
47+
self._map_class_running = []
48+
self._running_profile = []
49+
self._profilename_tosave: str = None
50+
51+
self.ui_initialized = False
52+
53+
# 加载配置文件和资料
54+
self.load_profile_settings()
55+
self.load_profiles()
56+
57+
def initialize_ui(self):
3858
"""Init the UI and link the event."""
39-
base_ui.WidgetUI.__init__(self, main, "profile.ui")
59+
base_ui.WidgetUI.__init__(self, self.main, "profile.ui")
4060
base_ui.CommunicationHandler.__init__(self)
41-
self.main = main
61+
4262
self.profiles_dlg = ProfilesDialog(self)
4363
self.profiles_dlg.closeSignal.connect(self.close_profile_manager)
4464

@@ -63,19 +83,7 @@ def __init__(self, main=None):
6383
self.main.systray.refresh_profile_action_status
6484
)
6585

66-
self.profile_setup = {}
67-
self.profiles = {}
68-
69-
self._current_class = -1
70-
self._current_command = -1
71-
self._current_instance = -1
72-
self._map_class_running = []
73-
self._running_profile = []
74-
self._profilename_tosave: str = None
75-
7686
self.setEnabled(False)
77-
self.load_profile_settings()
78-
self.load_profiles()
7987

8088
def save_clicked(self):
8189
"""Save current seeting in Flash and replace the 'Flash profile' settings by the new one."""
@@ -141,7 +149,9 @@ def load_profiles_from_file(self):
141149
self.log("Profile: profiles are not compatible, need to redo them")
142150
else:
143151
self.log("Profile: profiles loaded")
144-
self.refresh_combox_list()
152+
153+
if self.ui_initialized:
154+
self.refresh_combox_list()
145155

146156
def create_or_update_profile_file(self, create: bool = False):
147157
"""Create a profile file if not exist, else update the existing one."""

updater.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get_latest_release(repo : str):
4848
"""Returns the latest release for repo or empty dict on error"""
4949
url = f"https://api.github.com/repos/{repo}/releases/latest"
5050
try:
51-
response = requests.get(url)
51+
response = requests.get(url,timeout=3) # timeout to avoid blocking when board connecting
5252
except requests.ConnectionError:
5353
return {}
5454
if not response:

0 commit comments

Comments
 (0)