From e7f9b4a71780cb2445e75d50ba74f57df657db66 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Wed, 29 Mar 2023 10:10:10 +0200 Subject: [PATCH] ui: Add language to configuration. --- src/config.py | 11 +++++++ src/pamhyr.py | 22 ++++++++++++-- src/view/ASubWindow.py | 11 +++++++ src/view/ConfigureWindow.py | 13 +++++++- src/view/MainWindow.py | 27 ++++------------- src/view/ui/ConfigureDialog.ui | 55 ++++++++++++++++++++++++++++++---- src/view/ui/MainWindow.ui | 11 ------- 7 files changed, 109 insertions(+), 41 deletions(-) diff --git a/src/config.py b/src/config.py index f0097680..1417e86d 100644 --- a/src/config.py +++ b/src/config.py @@ -30,10 +30,21 @@ class Config(object): self.backup_frequence = "00:05:00" self.backup_max = 10 + # Languages + self.lang = "" + @classmethod def filename(cls): return os.environ["HOME"] + config_dir + config_file + @classmethod + def languages(cls): + return { + "System": "", + "English": "en", + "French": "fr", + } + def save(self): os.makedirs(os.path.dirname(self.filename), exist_ok=True) with open(self.filename, 'wb') as out_file: diff --git a/src/pamhyr.py b/src/pamhyr.py index 3c02c327..1ca646ac 100755 --- a/src/pamhyr.py +++ b/src/pamhyr.py @@ -16,9 +16,25 @@ def main(): app = QApplication(sys.argv) translator = QTranslator() - lang = locale.getdefaultlocale() - if "fr" not in lang[0]: - translator.load(os.path.dirname(__file__) + "/lang/fr.qm") + + lang_file = "" + if conf.lang == "": + # System language + sys_lang = locale.getdefaultlocale() + if "fr" in sys_lang[0]: + lang_file = os.path.dirname(__file__) + "/lang/fr.qm" + elif conf.lang == "fr": + # French + lang_file = os.path.dirname(__file__) + "/lang/fr.qm" + else: + lang_file = "" + # English default language + + if lang_file != "": + ok = translator.load(lang_file) + if not ok: + print("failed") + app.installTranslator(translator) application = ApplicationWindow(conf=conf) diff --git a/src/view/ASubWindow.py b/src/view/ASubWindow.py index 29ef8c81..a2bd7549 100644 --- a/src/view/ASubWindow.py +++ b/src/view/ASubWindow.py @@ -237,6 +237,17 @@ class ASubWindow(QDialog): """ self.find(QComboBox, name).setCurrentText(item) + def get_combobox_text(self, name:str): + """Get current text of combo box + + Args: + name: The combo box component name + + Returns: + Current text + """ + return self.find(QComboBox, name).currentText() + # Custom dialog def file_dialog(self, select_file=True, callback=lambda x: None): diff --git a/src/view/ConfigureWindow.py b/src/view/ConfigureWindow.py index 4962fa20..551bf3fa 100644 --- a/src/view/ConfigureWindow.py +++ b/src/view/ConfigureWindow.py @@ -11,7 +11,8 @@ from PyQt5.QtCore import ( from PyQt5.QtWidgets import ( QDialogButtonBox, QPushButton, QLineEdit, - QFileDialog, QTableView, QAbstractItemView + QFileDialog, QTableView, QAbstractItemView, + QComboBox, ) @@ -90,6 +91,13 @@ class ConfigureWindow(ASubWindow, ListedSubWindow): self.find(QTableView, "tableView_solver").resizeColumnsToContents() self.connect() + # Language + languages = Config.languages() + for lang in languages: + self.combobox_add_item("comboBox_language", lang) + if self.conf.lang == languages[lang]: + self.set_combobox_text("comboBox_language", lang) + def connect(self): buttons = { "pushButton_solver_add": self.add_solver, @@ -129,6 +137,9 @@ class ConfigureWindow(ASubWindow, ListedSubWindow): self.conf.backup_frequence = self.get_time_edit("timeEdit_backup_frequence") self.conf.backup_max = self.get_spin_box("spinBox_backup_max") + # Language + self.conf.lang = Config.languages()[self.get_combobox_text("comboBox_language")] + self.end() def reject(self): diff --git a/src/view/MainWindow.py b/src/view/MainWindow.py index 222415d6..4b4e7754 100644 --- a/src/view/MainWindow.py +++ b/src/view/MainWindow.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os +from queue import Queue from PyQt5 import QtGui from PyQt5.QtCore import ( @@ -18,6 +19,7 @@ from view.ConfigureWindow import ConfigureWindow from view.NewStudyWindow import NewStudyWindow from view.NetworkWindow import NetworkWindow from view.AboutWindow import AboutWindow +from view.ui.MainWindow import Ui_MainWindow from model.Study import Study @@ -78,7 +80,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow): Returns: Nothing """ - self.ui.findChild(QAction, action).setEnabled(enable) + self.findChild(QAction, action).setEnabled(enable) def init_callback(self): """Connect action to callback function @@ -115,30 +117,13 @@ class ApplicationWindow(QMainWindow, ListedSubWindow): "action_toolBar_sections": lambda: self.open_dummy("Tronçons"), "action_toolBar_frictions": lambda: self.open_dummy("Frottements"), "action_toolBar_building": lambda: self.open_dummy("Ouvrages"), - ## Language - "action_english": lambda: self.set_language(""), - "action_french": lambda: self.set_language("fr"), } for action in actions: - self.ui.findChild(QAction, action)\ - .triggered.connect(actions[action]) - - def set_language(self, lang): - if lang != "": - translator = QTranslator() - translator.load(os.path.dirname(__file__) + f"/lang/{lang}.qm") - QApplication.instance().installTranslator(translator) - self.trans = translator - else: - QApplication.instance().removeTranslator(self.trans) - - self.retranslateUi() + self.findChild(QAction, action)\ + .triggered.connect(actions[action]) + # action.triggered.connect(actions[action]) - def retranslateUi(self): - for action in self.menubar.children(): - if isinstance(action, QAction): - action.setText(self.trans("MainWindow", action.getText())) def changeEvent(self, event): if event.type() == QEvent.LanguageChange: diff --git a/src/view/ui/ConfigureDialog.ui b/src/view/ui/ConfigureDialog.ui index 77a43e43..c54b2b45 100644 --- a/src/view/ui/ConfigureDialog.ui +++ b/src/view/ui/ConfigureDialog.ui @@ -107,7 +107,7 @@ </item> </layout> </widget> - <widget class="QWidget" name="tab_mailleur"> + <widget class="QWidget" name="tab_mesh"> <attribute name="title"> <string>Meshing tool</string> </attribute> @@ -116,7 +116,7 @@ <rect> <x>10</x> <y>10</y> - <width>621</width> + <width>611</width> <height>30</height> </rect> </property> @@ -155,7 +155,7 @@ <x>10</x> <y>10</y> <width>621</width> - <height>93</height> + <height>61</height> </rect> </property> <layout class="QHBoxLayout" name="horizontalLayout"> @@ -198,7 +198,7 @@ </layout> </widget> </widget> - <widget class="QWidget" name="tab"> + <widget class="QWidget" name="tab_backup"> <attribute name="title"> <string>Backup</string> </attribute> @@ -208,7 +208,7 @@ <x>10</x> <y>10</y> <width>611</width> - <height>125</height> + <height>121</height> </rect> </property> <layout class="QHBoxLayout" name="horizontalLayout_4"> @@ -313,6 +313,51 @@ </layout> </widget> </widget> + <widget class="QWidget" name="tab_2"> + <attribute name="title"> + <string>Language</string> + </attribute> + <widget class="QLabel" name="label_4"> + <property name="geometry"> + <rect> + <x>11</x> + <y>11</y> + <width>309</width> + <height>16</height> + </rect> + </property> + <property name="font"> + <font> + <italic>true</italic> + </font> + </property> + <property name="text"> + <string>Please restart application after language modification</string> + </property> + </widget> + <widget class="QWidget" name=""> + <property name="geometry"> + <rect> + <x>11</x> + <y>33</y> + <width>191</width> + <height>26</height> + </rect> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_6"> + <item> + <widget class="QLabel" name="label_5"> + <property name="text"> + <string>Language</string> + </property> + </widget> + </item> + <item> + <widget class="QComboBox" name="comboBox_language"/> + </item> + </layout> + </widget> + </widget> </widget> </item> <item> diff --git a/src/view/ui/MainWindow.ui b/src/view/ui/MainWindow.ui index 41ce2ca6..d5a2b900 100644 --- a/src/view/ui/MainWindow.ui +++ b/src/view/ui/MainWindow.ui @@ -201,16 +201,6 @@ <addaction name="action_menu_help_mage"/> <addaction name="action_menu_about"/> </widget> - <widget class="QMenu" name="menu_language"> - <property name="locale"> - <locale language="English" country="Europe"/> - </property> - <property name="title"> - <string>&Language</string> - </property> - <addaction name="action_english"/> - <addaction name="action_french"/> - </widget> <addaction name="menu_File"/> <addaction name="menu_network"/> <addaction name="menu_geometry"/> @@ -218,7 +208,6 @@ <addaction name="menu_run"/> <addaction name="menu_plot"/> <addaction name="menu_cartography"/> - <addaction name="menu_language"/> <addaction name="menu_help"/> </widget> <widget class="QStatusBar" name="statusbar"/> -- GitLab