From 1ffcf5cdb88e96e2485f1e598a2f03421592b031 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Mon, 1 Jan 2024 11:30:08 +0100 Subject: [PATCH] added a proper language selection menu --- assets/language/en.ts | 10 ++-- assets/language/{sp.qm => es.qm} | Bin assets/language/{sp.ts => es.ts} | 10 ++-- assets/language/fr.ts | 10 ++-- main.py | 17 +----- source/__init__.py | 2 +- source/survey/IntegerQuestion.py | 4 +- source/survey/Text.py | 4 +- source/survey/TextQuestion.py | 4 +- source/survey/WebMission.py | 6 +- source/survey/base/BaseChoiceQuestion.py | 4 +- source/{widget => ui}/Browser.py | 0 source/ui/LanguageSelection.py | 67 ++++++++++++++++++++++ source/{widget => ui}/SurveyEngine.py | 6 +- source/{widget => ui}/SurveyNavigation.py | 0 source/{widget => ui}/SurveyWindow.py | 10 +++- source/{widget => ui}/__init__.py | 1 + 17 files changed, 107 insertions(+), 48 deletions(-) rename assets/language/{sp.qm => es.qm} (100%) rename assets/language/{sp.ts => es.ts} (65%) rename source/{widget => ui}/Browser.py (100%) create mode 100644 source/ui/LanguageSelection.py rename source/{widget => ui}/SurveyEngine.py (96%) rename source/{widget => ui}/SurveyNavigation.py (100%) rename source/{widget => ui}/SurveyWindow.py (56%) rename source/{widget => ui}/__init__.py (75%) diff --git a/assets/language/en.ts b/assets/language/en.ts index 03d8176..95c42ba 100644 --- a/assets/language/en.ts +++ b/assets/language/en.ts @@ -4,7 +4,7 @@ SurveyEngine - + WARNING Warning @@ -12,17 +12,17 @@ SurveyNavigation - + ABANDON Abandon - + SKIP Skip - + NEXT Next @@ -30,7 +30,7 @@ SurveyWindow - + SURVEY Survey diff --git a/assets/language/sp.qm b/assets/language/es.qm similarity index 100% rename from assets/language/sp.qm rename to assets/language/es.qm diff --git a/assets/language/sp.ts b/assets/language/es.ts similarity index 65% rename from assets/language/sp.ts rename to assets/language/es.ts index 5dfd4a5..b30617d 100644 --- a/assets/language/sp.ts +++ b/assets/language/es.ts @@ -4,7 +4,7 @@ SurveyEngine - + WARNING Atención @@ -12,17 +12,17 @@ SurveyNavigation - + ABANDON Abandonar - + SKIP Pasar - + NEXT Siguiente @@ -30,7 +30,7 @@ SurveyWindow - + SURVEY Encuesta diff --git a/assets/language/fr.ts b/assets/language/fr.ts index 9716302..c0b0980 100644 --- a/assets/language/fr.ts +++ b/assets/language/fr.ts @@ -4,7 +4,7 @@ SurveyEngine - + WARNING Avertissement @@ -12,17 +12,17 @@ SurveyNavigation - + ABANDON Abandonner - + SKIP Passer - + NEXT Suivant @@ -30,7 +30,7 @@ SurveyWindow - + SURVEY Sondage diff --git a/main.py b/main.py index 74a0ab4..b47eb90 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,14 @@ import sys -from PyQt6.QtCore import QTranslator, QLocale from PyQt6.QtWidgets import QApplication -from source import translate -from source import assets_path -from source.widget import SurveyWindow +from source.ui import SurveyWindow if __name__ == "__main__": # create the application application = QApplication(sys.argv) - # get the user language - local = QLocale() - language_code: str = local.languageToCode(local.language(), QLocale.LanguageCodeType.ISO639) - - # apply the language on the translator - translate.set_language(language_code) - - # load the translator to support multiple languages - translator = QTranslator() - application.installTranslator(translator) - translator.load(str(assets_path / f"language/{language_code}.qm")) - # create the window window = SurveyWindow("./surveys.json") window.show() diff --git a/source/__init__.py b/source/__init__.py index 76d3c77..4c64968 100644 --- a/source/__init__.py +++ b/source/__init__.py @@ -3,7 +3,7 @@ from pathlib import Path assets_path = Path("./assets/") __appname__ = "Survey Engine" -__version__ = (1,0,0) +__version__ = (1,0,2) __str_version__ = ".".join(map(str, __version__)) __author__ = "Faraphel" __mail__ = "rc60650@hotmail.com" diff --git a/source/survey/IntegerQuestion.py b/source/survey/IntegerQuestion.py index c8c0ffb..b7029a9 100644 --- a/source/survey/IntegerQuestion.py +++ b/source/survey/IntegerQuestion.py @@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QFont from PyQt6.QtWidgets import QVBoxLayout, QLabel, QSpinBox -from source import translate, widget +from source import translate, ui from source.survey.base import BaseSurvey @@ -47,7 +47,7 @@ class IntegerQuestion(BaseSurvey): self._layout.addWidget(self.entry_response) # navigation - self.navigation = widget.SurveyNavigation(signals=signals) + self.navigation = ui.SurveyNavigation(signals=signals) self._layout.addWidget(self.navigation) self.navigation.show_forward() diff --git a/source/survey/Text.py b/source/survey/Text.py index 7c2f909..988d510 100644 --- a/source/survey/Text.py +++ b/source/survey/Text.py @@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QFont from PyQt6.QtWidgets import QVBoxLayout, QLabel -from source import translate, widget +from source import translate, ui from source.survey.base import BaseSurvey @@ -43,7 +43,7 @@ class Text(BaseSurvey): self.label_description.setAlignment(Qt.AlignmentFlag.AlignCenter) # navigation - self.navigation = widget.SurveyNavigation(signals=signals) + self.navigation = ui.SurveyNavigation(signals=signals) self._layout.addWidget(self.navigation) self.navigation.show_forward() # always show forward diff --git a/source/survey/TextQuestion.py b/source/survey/TextQuestion.py index eed3d38..eb67eb5 100644 --- a/source/survey/TextQuestion.py +++ b/source/survey/TextQuestion.py @@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QFont from PyQt6.QtWidgets import QVBoxLayout, QLabel, QTextEdit -from source import translate, widget +from source import translate, ui from source.survey.base import BaseSurvey @@ -34,7 +34,7 @@ class TextQuestion(BaseSurvey): self._layout.addWidget(self.entry_response) # navigation - self.navigation = widget.SurveyNavigation(signals=signals) + self.navigation = ui.SurveyNavigation(signals=signals) self._layout.addWidget(self.navigation) self.navigation.show_forward() diff --git a/source/survey/WebMission.py b/source/survey/WebMission.py index 51102a5..f3a5fcd 100644 --- a/source/survey/WebMission.py +++ b/source/survey/WebMission.py @@ -5,7 +5,7 @@ from PyQt6.QtCore import Qt, QTimer, pyqtSignal, QUrl, QEvent, QObject, QPointF from PyQt6.QtGui import QFont, QMouseEvent, QResizeEvent, QKeyEvent from PyQt6.QtWidgets import QLabel, QVBoxLayout, QSizePolicy -from source import translate, widget +from source import translate, ui from source.survey.base import BaseSurvey @@ -47,7 +47,7 @@ class WebMission(BaseSurvey): self.label_title.setFont(font_title) # web page - self.browser = widget.Browser() + self.browser = ui.Browser() self._layout.addWidget(self.browser) self.browser.web.focusProxy().installEventFilter(self) # capture the event in eventFilter self.browser.web.urlChanged.connect(self._on_url_changed) # NOQA: connect exist @@ -73,7 +73,7 @@ class WebMission(BaseSurvey): self.browser.web.page().scrollPositionChanged.connect(self._on_scroll_position_changed) # navigation - self.navigation = widget.SurveyNavigation(signals=signals) + self.navigation = ui.SurveyNavigation(signals=signals) self._layout.addWidget(self.navigation) # initialize the start time diff --git a/source/survey/base/BaseChoiceQuestion.py b/source/survey/base/BaseChoiceQuestion.py index 4ba9e47..5218b15 100644 --- a/source/survey/base/BaseChoiceQuestion.py +++ b/source/survey/base/BaseChoiceQuestion.py @@ -5,7 +5,7 @@ from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QFont from PyQt6.QtWidgets import QFrame, QVBoxLayout, QLabel, QButtonGroup, QLineEdit, QAbstractButton -from source import translate, widget +from source import translate, ui from source.survey.base import BaseSurvey @@ -54,7 +54,7 @@ class BaseChoiceQuestion(BaseSurvey): self.label_question.setFont(font_title) # prepare navigation - self.navigation = widget.SurveyNavigation(signals=signals) + self.navigation = ui.SurveyNavigation(signals=signals) # responses self.frame_responses = QFrame() diff --git a/source/widget/Browser.py b/source/ui/Browser.py similarity index 100% rename from source/widget/Browser.py rename to source/ui/Browser.py diff --git a/source/ui/LanguageSelection.py b/source/ui/LanguageSelection.py new file mode 100644 index 0000000..1f07071 --- /dev/null +++ b/source/ui/LanguageSelection.py @@ -0,0 +1,67 @@ +from typing import Callable + +from PyQt6.QtCore import Qt, QLocale, QTranslator +from PyQt6.QtWidgets import QWidget, QLabel, QVBoxLayout, QComboBox, QApplication, QPushButton + +from source import assets_path, translate + + +class LanguageSelection(QWidget): + def __init__(self, after: Callable): + super().__init__() + + self.after = after + + # layout + self._layout = QVBoxLayout() + self.setLayout(self._layout) + + # title + self.title = QLabel() + self._layout.addWidget(self.title) + self.title.setText("Select your language.") + self.title.setAlignment(Qt.AlignmentFlag.AlignCenter) + + # language selection + self.select_language = QComboBox() + self._layout.addWidget(self.select_language) + + language_default = QLocale().language() # get the default locale + + for i, language_file in enumerate((assets_path / "language").glob("*.qm")): + # add every translated language to the selection + + language_code: str = language_file.stem + language = QLocale.codeToLanguage(language_code, QLocale.LanguageCodeType.ISO639) + + self.select_language.addItem(language.name, language_code) + + # if the added language is the default one, select it directly + if language == language_default: + self.select_language.setCurrentIndex(i) + + # start button + self.button_start = QPushButton() + self._layout.addWidget(self.button_start) + self.button_start.setText("Start") + self.button_start.clicked.connect(self.start) # NOQA: connect exist + + def start(self) -> None: + language_code = self.select_language.currentData() + + # load the correct translator + translator = QTranslator() + translator.load(str(assets_path / f"language/{language_code}.qm")) + + # apply the language on the custom translator + translate.set_language(language_code) + + # install the translator on the application + application = QApplication.instance() + application.installTranslator(translator) + + # call the after event + self.after() + + # delete the language selection + self.deleteLater() diff --git a/source/widget/SurveyEngine.py b/source/ui/SurveyEngine.py similarity index 96% rename from source/widget/SurveyEngine.py rename to source/ui/SurveyEngine.py index d12e7ca..9426ee8 100644 --- a/source/widget/SurveyEngine.py +++ b/source/ui/SurveyEngine.py @@ -8,7 +8,7 @@ from typing import Optional from PyQt6.QtCore import pyqtSignal from PyQt6.QtWidgets import QVBoxLayout, QProgressBar, QWidget, QMessageBox -from source import translate, widget, save +from source import translate, ui, save from source.survey.base import BaseSurvey from source.survey import Empty, survey_get from source.utils import compress @@ -93,7 +93,7 @@ class SurveyEngine(QWidget): def _on_signal_abandon(self): # on abandon, quit the survey - window = typing.cast(widget.SurveyWindow, self.window()) + window = typing.cast(ui.SurveyWindow, self.window()) window.quit() def _on_signal_skip(self): @@ -166,7 +166,7 @@ class SurveyEngine(QWidget): ) # finally, close the window - window = typing.cast(widget.SurveyWindow, self.window()) + window = typing.cast(ui.SurveyWindow, self.window()) window.quit() # signals diff --git a/source/widget/SurveyNavigation.py b/source/ui/SurveyNavigation.py similarity index 100% rename from source/widget/SurveyNavigation.py rename to source/ui/SurveyNavigation.py diff --git a/source/widget/SurveyWindow.py b/source/ui/SurveyWindow.py similarity index 56% rename from source/widget/SurveyWindow.py rename to source/ui/SurveyWindow.py index 67e942d..253e0d0 100644 --- a/source/widget/SurveyWindow.py +++ b/source/ui/SurveyWindow.py @@ -3,7 +3,7 @@ from pathlib import Path from PyQt6.QtGui import QIcon from PyQt6.QtWidgets import QMainWindow -from source import widget, assets_path, __icon_png__ +from source import ui, assets_path, __icon_png__ icon_path = assets_path / "icon.png" @@ -12,10 +12,16 @@ class SurveyWindow(QMainWindow): def __init__(self, survey_path: Path | str): super().__init__() + # window style self.setWindowIcon(QIcon(__icon_png__)) self.setWindowTitle(self.tr("SURVEY")) - self.setCentralWidget(widget.SurveyEngine.from_file(survey_path)) + # start by asking the user his language + language_selection = ui.LanguageSelection( + # after the language is selected, start the survey + after=lambda: self.setCentralWidget(ui.SurveyEngine.from_file(survey_path)) + ) + self.setCentralWidget(language_selection) def quit(self): # quit the application by closing and deleting the window diff --git a/source/widget/__init__.py b/source/ui/__init__.py similarity index 75% rename from source/widget/__init__.py rename to source/ui/__init__.py index b96e266..ba53fce 100644 --- a/source/widget/__init__.py +++ b/source/ui/__init__.py @@ -1,4 +1,5 @@ from .Browser import Browser +from .LanguageSelection import LanguageSelection from .SurveyEngine import SurveyEngine from .SurveyWindow import SurveyWindow from .SurveyNavigation import SurveyNavigation