ui: Add language to configuration.

mesh
Pierre-Antoine Rouby 2023-03-29 10:10:10 +02:00
parent 495f8898ac
commit e7f9b4a717
7 changed files with 109 additions and 41 deletions

View File

@ -30,10 +30,21 @@ class Config(object):
self.backup_frequence = "00:05:00" self.backup_frequence = "00:05:00"
self.backup_max = 10 self.backup_max = 10
# Languages
self.lang = ""
@classmethod @classmethod
def filename(cls): def filename(cls):
return os.environ["HOME"] + config_dir + config_file return os.environ["HOME"] + config_dir + config_file
@classmethod
def languages(cls):
return {
"System": "",
"English": "en",
"French": "fr",
}
def save(self): def save(self):
os.makedirs(os.path.dirname(self.filename), exist_ok=True) os.makedirs(os.path.dirname(self.filename), exist_ok=True)
with open(self.filename, 'wb') as out_file: with open(self.filename, 'wb') as out_file:

View File

@ -16,9 +16,25 @@ def main():
app = QApplication(sys.argv) app = QApplication(sys.argv)
translator = QTranslator() translator = QTranslator()
lang = locale.getdefaultlocale()
if "fr" not in lang[0]: lang_file = ""
translator.load(os.path.dirname(__file__) + "/lang/fr.qm") 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) app.installTranslator(translator)
application = ApplicationWindow(conf=conf) application = ApplicationWindow(conf=conf)

View File

@ -237,6 +237,17 @@ class ASubWindow(QDialog):
""" """
self.find(QComboBox, name).setCurrentText(item) 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 # Custom dialog
def file_dialog(self, select_file=True, callback=lambda x: None): def file_dialog(self, select_file=True, callback=lambda x: None):

View File

@ -11,7 +11,8 @@ from PyQt5.QtCore import (
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QDialogButtonBox, QPushButton, QLineEdit, 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.find(QTableView, "tableView_solver").resizeColumnsToContents()
self.connect() 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): def connect(self):
buttons = { buttons = {
"pushButton_solver_add": self.add_solver, "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_frequence = self.get_time_edit("timeEdit_backup_frequence")
self.conf.backup_max = self.get_spin_box("spinBox_backup_max") 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() self.end()
def reject(self): def reject(self):

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
from queue import Queue
from PyQt5 import QtGui from PyQt5 import QtGui
from PyQt5.QtCore import ( from PyQt5.QtCore import (
@ -18,6 +19,7 @@ from view.ConfigureWindow import ConfigureWindow
from view.NewStudyWindow import NewStudyWindow from view.NewStudyWindow import NewStudyWindow
from view.NetworkWindow import NetworkWindow from view.NetworkWindow import NetworkWindow
from view.AboutWindow import AboutWindow from view.AboutWindow import AboutWindow
from view.ui.MainWindow import Ui_MainWindow
from model.Study import Study from model.Study import Study
@ -78,7 +80,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow):
Returns: Returns:
Nothing Nothing
""" """
self.ui.findChild(QAction, action).setEnabled(enable) self.findChild(QAction, action).setEnabled(enable)
def init_callback(self): def init_callback(self):
"""Connect action to callback function """Connect action to callback function
@ -115,30 +117,13 @@ class ApplicationWindow(QMainWindow, ListedSubWindow):
"action_toolBar_sections": lambda: self.open_dummy("Tronçons"), "action_toolBar_sections": lambda: self.open_dummy("Tronçons"),
"action_toolBar_frictions": lambda: self.open_dummy("Frottements"), "action_toolBar_frictions": lambda: self.open_dummy("Frottements"),
"action_toolBar_building": lambda: self.open_dummy("Ouvrages"), "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: for action in actions:
self.ui.findChild(QAction, action)\ self.findChild(QAction, action)\
.triggered.connect(actions[action]) .triggered.connect(actions[action])
# 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()
def retranslateUi(self):
for action in self.menubar.children():
if isinstance(action, QAction):
action.setText(self.trans("MainWindow", action.getText()))
def changeEvent(self, event): def changeEvent(self, event):
if event.type() == QEvent.LanguageChange: if event.type() == QEvent.LanguageChange:

View File

@ -107,7 +107,7 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tab_mailleur"> <widget class="QWidget" name="tab_mesh">
<attribute name="title"> <attribute name="title">
<string>Meshing tool</string> <string>Meshing tool</string>
</attribute> </attribute>
@ -116,7 +116,7 @@
<rect> <rect>
<x>10</x> <x>10</x>
<y>10</y> <y>10</y>
<width>621</width> <width>611</width>
<height>30</height> <height>30</height>
</rect> </rect>
</property> </property>
@ -155,7 +155,7 @@
<x>10</x> <x>10</x>
<y>10</y> <y>10</y>
<width>621</width> <width>621</width>
<height>93</height> <height>61</height>
</rect> </rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
@ -198,7 +198,7 @@
</layout> </layout>
</widget> </widget>
</widget> </widget>
<widget class="QWidget" name="tab"> <widget class="QWidget" name="tab_backup">
<attribute name="title"> <attribute name="title">
<string>Backup</string> <string>Backup</string>
</attribute> </attribute>
@ -208,7 +208,7 @@
<x>10</x> <x>10</x>
<y>10</y> <y>10</y>
<width>611</width> <width>611</width>
<height>125</height> <height>121</height>
</rect> </rect>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout_4"> <layout class="QHBoxLayout" name="horizontalLayout_4">
@ -313,6 +313,51 @@
</layout> </layout>
</widget> </widget>
</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> </widget>
</item> </item>
<item> <item>

View File

@ -201,16 +201,6 @@
<addaction name="action_menu_help_mage"/> <addaction name="action_menu_help_mage"/>
<addaction name="action_menu_about"/> <addaction name="action_menu_about"/>
</widget> </widget>
<widget class="QMenu" name="menu_language">
<property name="locale">
<locale language="English" country="Europe"/>
</property>
<property name="title">
<string>&amp;Language</string>
</property>
<addaction name="action_english"/>
<addaction name="action_french"/>
</widget>
<addaction name="menu_File"/> <addaction name="menu_File"/>
<addaction name="menu_network"/> <addaction name="menu_network"/>
<addaction name="menu_geometry"/> <addaction name="menu_geometry"/>
@ -218,7 +208,6 @@
<addaction name="menu_run"/> <addaction name="menu_run"/>
<addaction name="menu_plot"/> <addaction name="menu_plot"/>
<addaction name="menu_cartography"/> <addaction name="menu_cartography"/>
<addaction name="menu_language"/>
<addaction name="menu_help"/> <addaction name="menu_help"/>
</widget> </widget>
<widget class="QStatusBar" name="statusbar"/> <widget class="QStatusBar" name="statusbar"/>