added intermediate function to survey engine creation

This commit is contained in:
Faraphel 2023-12-30 17:19:31 +01:00
parent cd4619fa00
commit 2f6c6860d5
9 changed files with 48 additions and 42 deletions

View file

@ -3,7 +3,7 @@ import sys
from PyQt6.QtCore import QTranslator, QLocale from PyQt6.QtCore import QTranslator, QLocale
from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QApplication
import translate from source import translate
from source import assets_path from source import assets_path
from source.widget import SurveyWindow from source.widget import SurveyWindow
@ -25,7 +25,7 @@ if __name__ == "__main__":
translator.load(str(assets_path / f"language/{language_code}.qm")) translator.load(str(assets_path / f"language/{language_code}.qm"))
# create the window # create the window
window = SurveyWindow() window = SurveyWindow("./surveys.json")
window.show() window.show()
# start the application # start the application

View file

@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QFont from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QVBoxLayout, QLabel, QSpinBox from PyQt6.QtWidgets import QVBoxLayout, QLabel, QSpinBox
import translate from source import translate
from source.survey.base import BaseSurvey from source.survey.base import BaseSurvey

View file

@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QFont from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QFrame, QVBoxLayout, QLabel, QCheckBox, QLineEdit from PyQt6.QtWidgets import QFrame, QVBoxLayout, QLabel, QCheckBox, QLineEdit
import translate from source import translate
from source.survey.base import BaseSurvey from source.survey.base import BaseSurvey

View file

@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QFont from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QFrame, QVBoxLayout, QLabel, QRadioButton, QButtonGroup, QLineEdit, QAbstractButton from PyQt6.QtWidgets import QFrame, QVBoxLayout, QLabel, QRadioButton, QButtonGroup, QLineEdit, QAbstractButton
import translate from source import translate
from source.survey.base import BaseSurvey from source.survey.base import BaseSurvey

View file

@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QFont from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QVBoxLayout, QLabel from PyQt6.QtWidgets import QVBoxLayout, QLabel
import translate from source import translate
from source.survey.base import BaseSurvey from source.survey.base import BaseSurvey

View file

@ -4,7 +4,7 @@ from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QFont from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QVBoxLayout, QLabel, QTextEdit from PyQt6.QtWidgets import QVBoxLayout, QLabel, QTextEdit
import translate from source import translate
from source.survey.base import BaseSurvey from source.survey.base import BaseSurvey

View file

@ -6,7 +6,7 @@ from PyQt6.QtCore import Qt, QTimer, pyqtSignal, QUrl, QEvent, QObject
from PyQt6.QtGui import QFont, QMouseEvent, QResizeEvent, QKeyEvent from PyQt6.QtGui import QFont, QMouseEvent, QResizeEvent, QKeyEvent
from PyQt6.QtWidgets import QLabel, QVBoxLayout, QSizePolicy from PyQt6.QtWidgets import QLabel, QVBoxLayout, QSizePolicy
import translate from source import translate
from source import assets_path from source import assets_path
from source.survey.base import BaseSurvey from source.survey.base import BaseSurvey
from source.widget import Browser from source.widget import Browser

View file

@ -15,7 +15,7 @@ from source.survey.base import BaseSurvey
from source.survey import Empty, survey_get from source.survey import Empty, survey_get
result_path = Path("./result/") result_path = Path("./results/")
result_path.mkdir(parents=True, exist_ok=True) result_path.mkdir(parents=True, exist_ok=True)
@ -24,7 +24,7 @@ class SurveyEngine(QWidget):
signal_skip = pyqtSignal() signal_skip = pyqtSignal()
signal_success = 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__() super().__init__()
# signals # signals
@ -34,9 +34,8 @@ class SurveyEngine(QWidget):
# prepare the survey collected data # prepare the survey collected data
self.collected_datas: dict[str, dict] = {"time": time.time(), "surveys": {}} 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.current_survey_index = 0
self.discord_webhook_result_url: Optional[str] = None
# set the layout # set the layout
self._layout = QVBoxLayout() self._layout = QVBoxLayout()
@ -78,29 +77,7 @@ class SurveyEngine(QWidget):
self.progress.setTextVisible(False) self.progress.setTextVisible(False)
self.progress.setFixedHeight(8) self.progress.setFixedHeight(8)
# load the survey configuration file # load the survey screens
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)
self.survey_screens = [ self.survey_screens = [
( (
survey_id, 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 # update the progress bar
self.progress.setMaximum(len(data["surveys"])) self.progress.setMaximum(len(self.survey_screens))
# get the result webhook url # finalize the initialisation
self.discord_webhook_result_url = data.get("discord_webhook_result") 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): def next_survey(self):
# get the collected data from the survey # get the collected data from the survey

View file

@ -1,3 +1,5 @@
from pathlib import Path
from PyQt6.QtGui import QIcon from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QMainWindow from PyQt6.QtWidgets import QMainWindow
@ -8,10 +10,10 @@ icon_path = assets_path / "icon.png"
class SurveyWindow(QMainWindow): class SurveyWindow(QMainWindow):
def __init__(self): def __init__(self, survey_path: Path | str):
super().__init__() super().__init__()
self.setWindowIcon(QIcon(str(icon_path.resolve()))) self.setWindowIcon(QIcon(str(icon_path.resolve())))
self.setWindowTitle(self.tr("SURVEY")) self.setWindowTitle(self.tr("SURVEY"))
self.setCentralWidget(widget.SurveyEngine("./surveys.json")) self.setCentralWidget(widget.SurveyEngine.from_file(survey_path))