From 2f6c6860d57f47e93ed9df39928c21fa7e7f1123 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sat, 30 Dec 2023 17:19:31 +0100 Subject: [PATCH] added intermediate function to survey engine creation --- main.py | 4 +- source/survey/IntegerQuestion.py | 2 +- source/survey/MultipleChoiceQuestion.py | 2 +- source/survey/SingleChoiceQuestion.py | 2 +- source/survey/Text.py | 2 +- source/survey/TextQuestion.py | 2 +- source/survey/WebMission.py | 2 +- source/widget/SurveyEngine.py | 68 +++++++++++++------------ source/widget/SurveyWindow.py | 6 ++- 9 files changed, 48 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index a9e112d..74a0ab4 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import sys from PyQt6.QtCore import QTranslator, QLocale from PyQt6.QtWidgets import QApplication -import translate +from source import translate from source import assets_path from source.widget import SurveyWindow @@ -25,7 +25,7 @@ if __name__ == "__main__": translator.load(str(assets_path / f"language/{language_code}.qm")) # create the window - window = SurveyWindow() + window = SurveyWindow("./surveys.json") window.show() # start the application diff --git a/source/survey/IntegerQuestion.py b/source/survey/IntegerQuestion.py index 3bb9bde..fe2786d 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 -import translate +from source import translate from source.survey.base import BaseSurvey diff --git a/source/survey/MultipleChoiceQuestion.py b/source/survey/MultipleChoiceQuestion.py index a7a9684..c5bafa1 100644 --- a/source/survey/MultipleChoiceQuestion.py +++ b/source/survey/MultipleChoiceQuestion.py @@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QFont from PyQt6.QtWidgets import QFrame, QVBoxLayout, QLabel, QCheckBox, QLineEdit -import translate +from source import translate from source.survey.base import BaseSurvey diff --git a/source/survey/SingleChoiceQuestion.py b/source/survey/SingleChoiceQuestion.py index 5db79c1..5d4f877 100644 --- a/source/survey/SingleChoiceQuestion.py +++ b/source/survey/SingleChoiceQuestion.py @@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QFont from PyQt6.QtWidgets import QFrame, QVBoxLayout, QLabel, QRadioButton, QButtonGroup, QLineEdit, QAbstractButton -import translate +from source import translate from source.survey.base import BaseSurvey diff --git a/source/survey/Text.py b/source/survey/Text.py index be195ee..003b5a3 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 -import translate +from source import translate from source.survey.base import BaseSurvey diff --git a/source/survey/TextQuestion.py b/source/survey/TextQuestion.py index 652364d..1d57d43 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 -import translate +from source import translate from source.survey.base import BaseSurvey diff --git a/source/survey/WebMission.py b/source/survey/WebMission.py index 65f3608..9466f7d 100644 --- a/source/survey/WebMission.py +++ b/source/survey/WebMission.py @@ -6,7 +6,7 @@ from PyQt6.QtCore import Qt, QTimer, pyqtSignal, QUrl, QEvent, QObject from PyQt6.QtGui import QFont, QMouseEvent, QResizeEvent, QKeyEvent from PyQt6.QtWidgets import QLabel, QVBoxLayout, QSizePolicy -import translate +from source import translate from source import assets_path from source.survey.base import BaseSurvey from source.widget import Browser diff --git a/source/widget/SurveyEngine.py b/source/widget/SurveyEngine.py index ea9875d..e7bc837 100644 --- a/source/widget/SurveyEngine.py +++ b/source/widget/SurveyEngine.py @@ -15,7 +15,7 @@ from source.survey.base import BaseSurvey from source.survey import Empty, survey_get -result_path = Path("./result/") +result_path = Path("./results/") result_path.mkdir(parents=True, exist_ok=True) @@ -24,7 +24,7 @@ class SurveyEngine(QWidget): signal_skip = pyqtSignal() signal_success = pyqtSignal() - def __init__(self, survey_path: Path | str): # TODO: real arguments here, cls.from_file ? + def __init__(self, surveys_data: dict, discord_webhook_result_url: Optional[str] = None): super().__init__() # signals @@ -34,9 +34,8 @@ class SurveyEngine(QWidget): # prepare the survey collected data self.collected_datas: dict[str, dict] = {"time": time.time(), "surveys": {}} - self.survey_screens: list[tuple[str, BaseSurvey]] = [] + self.discord_webhook_result_url = discord_webhook_result_url self.current_survey_index = 0 - self.discord_webhook_result_url: Optional[str] = None # set the layout self._layout = QVBoxLayout() @@ -78,29 +77,7 @@ class SurveyEngine(QWidget): self.progress.setTextVisible(False) self.progress.setFixedHeight(8) - # load the survey configuration file - self.load_file(survey_path) - - # finalize the initialisation - self.update_survey() - - def _on_signal_abandon(self): - # on success, show the button to give up - self.button_abandon.show() - - def _on_signal_skip(self): - # on success, show the button to skip - self.button_skip.show() - - def _on_signal_success(self): - # on success, show the button to go forward - self.button_forward.show() - - def load_file(self, survey_path: Path | str): - # load the surveys screens - with open(survey_path, encoding="utf-8") as file: - data = json.load(file) - + # load the survey screens self.survey_screens = [ ( survey_id, @@ -113,15 +90,42 @@ class SurveyEngine(QWidget): } ) ) - for survey_id, survey_data in data["surveys"].items() + for survey_id, survey_data in surveys_data.items() ] - self.current_survey_index = 0 # update the progress bar - self.progress.setMaximum(len(data["surveys"])) + self.progress.setMaximum(len(self.survey_screens)) - # get the result webhook url - self.discord_webhook_result_url = data.get("discord_webhook_result") + # finalize the initialisation + self.update_survey() + + @classmethod + def from_dict(cls, data: dict) -> "SurveyEngine": + return cls( + surveys_data=data["surveys"], + discord_webhook_result_url=data.get("discord_webhook_result"), + ) + + @classmethod + def from_file(cls, path: Path | str) -> "SurveyEngine": + with open(path, encoding="utf-8") as file: + data = json.load(file) + + return cls.from_dict(data) + + # events + + def _on_signal_abandon(self): + # on success, show the button to give up + self.button_abandon.show() + + def _on_signal_skip(self): + # on success, show the button to skip + self.button_skip.show() + + def _on_signal_success(self): + # on success, show the button to go forward + self.button_forward.show() def next_survey(self): # get the collected data from the survey diff --git a/source/widget/SurveyWindow.py b/source/widget/SurveyWindow.py index 39be301..c15b06c 100644 --- a/source/widget/SurveyWindow.py +++ b/source/widget/SurveyWindow.py @@ -1,3 +1,5 @@ +from pathlib import Path + from PyQt6.QtGui import QIcon from PyQt6.QtWidgets import QMainWindow @@ -8,10 +10,10 @@ icon_path = assets_path / "icon.png" class SurveyWindow(QMainWindow): - def __init__(self): + def __init__(self, survey_path: Path | str): super().__init__() self.setWindowIcon(QIcon(str(icon_path.resolve()))) self.setWindowTitle(self.tr("SURVEY")) - self.setCentralWidget(widget.SurveyEngine("./surveys.json")) + self.setCentralWidget(widget.SurveyEngine.from_file(survey_path))