Skip to content

Commit

Permalink
add sav user data
Browse files Browse the repository at this point in the history
  • Loading branch information
danghoangnhan committed Nov 29, 2023
1 parent d57666b commit bd4f06f
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 48 deletions.
15 changes: 2 additions & 13 deletions component/button.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from PyQt6.QtWidgets import QPushButton,QApplication,QPushButton, QLabel, QVBoxLayout, QApplication
from PyQt6.QtCore import QSize,Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import QSize

from PyQt6.QtWidgets import QPushButton,QPushButton, QLabel, QVBoxLayout

class SquareButton(QPushButton):
def __init__(self, icon, text, parent=None):
Expand All @@ -17,11 +13,4 @@ def __init__(self, icon, text, parent=None):
layout = QVBoxLayout()
layout.addWidget(self.icon_label)
layout.addWidget(self.text_label)
self.setLayout(layout)

# def sizeHint(self):
# screen = QApplication.screens()[0]
# screen_size = screen.availableGeometry()
# # Calculate the button size based on screen size
# button_size = screen_size.width() / 10
# return QSize(button_size, button_size)
self.setLayout(layout)
25 changes: 25 additions & 0 deletions component/field/user_infor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from PyQt6.QtWidgets import QLineEdit

class UserInfo:
def __init__(self, parent=None):
self.chinese_name = QLineEdit(parent)
self.english_name = QLineEdit(parent)
self.gender = QLineEdit(parent)
self.birth_year = QLineEdit(parent)
self.education_level = QLineEdit(parent)
self.native_language = QLineEdit(parent)
self.family_language = QLineEdit(parent)
self.friend_language = QLineEdit(parent)

def get_data(self)->dict:
return {
"中文姓名": self.chinese_name.text(),
"英文姓名": self.english_name.text(),
"性別": self.gender.text(),
"出生年": self.birth_year.text(),
"教育程度": self.education_level.text(),
"母語": self.native_language.text(),
"跟家人常說語言": self.family_language.text(),
"跟朋友常說語言": self.friend_language.text()
}

17 changes: 2 additions & 15 deletions component/screen/end_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def __init__(self,navigator,screen_height,screen_width, result):

self.result = result
self.initUI()
if(result is not None):
self.saveResult(result)


def initUI(self):
layout = QVBoxLayout(self)

Expand All @@ -43,15 +41,4 @@ def initUI(self):
quit_btn.setStyleSheet("background-color: red; color: white; font-size: 20px; padding: 10px;")
layout.addWidget(quit_btn)

self.setLayout(layout)


def saveResult(self,history):
user_df = dataHandaler.get_user()
user_df = user_df.drop(user_df.index)
new_history_value = pd.DataFrame([element.to_dict() for element in history])
new_history_value['participate_number'] = dataHandaler.get_new_sessionId()
new_history_value = new_history_value.sort_values(by='question')
user_df = pd.concat([user_df, pd.DataFrame([{"participantID":dataHandaler.get_new_sessionId()}])], ignore_index=True)
dataHandaler.append_history_data(new_history_value)
dataHandaler.append_user_data(user_df)
self.setLayout(layout)
20 changes: 18 additions & 2 deletions component/screen/test_screen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from component.button import SquareButton
from component.model import Answer
import os
from model.answer import Answer
from storage.localStorage import dataHandaler
import random
from PyQt6.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout,QProgressBar,QSizePolicy,QHBoxLayout,QSpacerItem, QGridLayout
Expand All @@ -15,6 +15,8 @@
from config.constant import pofomopo_consonants,similarity_list
from pydub import AudioSegment
from config.dir import volume_icon
import pandas as pd
from model.session import currentSession

# TestScreen
class TestScreen(QWidget):
Expand Down Expand Up @@ -183,7 +185,21 @@ def submit_test(self):
question.set_answer(pofomopo_consonants[question.get_answer()] if question.get_answer()!= -1 else -1)
question.set_similarity(similarity_list[question.get_similarity()] if question.get_similarity()!=-1 else -1)


new_user_value = currentSession.get_user_info()
new_user_value["participantID"]=dataHandaler.get_new_sessionId()

new_history_value = pd.DataFrame([element.to_dict() for element in self.answerList])
new_history_value['participate_number'] = dataHandaler.get_new_sessionId()
new_history_value = new_history_value.sort_values(by='question')


dataHandaler.append_history_data(new_history_value)
print(new_user_value)
dataHandaler.append_user_data(pd.DataFrame([new_user_value]))

def submit_session(self):
pass

def updateProcess(self):
# Update the progress label
self.progress_label.setText(f"Test Number: {self.current_index + 1}/{len(self.answerList)}")
Expand Down
44 changes: 26 additions & 18 deletions component/screen/user_information_screen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

from component.field.user_infor import UserInfo
from component.modal import ConfirmDialog
from PyQt6.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout,QLineEdit,QDialog
from PyQt6.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout,QHBoxLayout,QDialog
import logging
from model.session import currentSession

class UserInformationScreen(QWidget):
def __init__(self,navigator,screen_height:int,screen_width:int):
Expand All @@ -16,20 +18,17 @@ def __init__(self,navigator,screen_height:int,screen_width:int):
def initUI(self)->None:
logging.info("Setting up UI for StartScreen")
layout = QVBoxLayout()
self.fields = {
"中文姓名": QLineEdit(self),
"英文姓名": QLineEdit(self),
"性別": QLineEdit(self),
"出生年": QLineEdit(self),
"教育程度": QLineEdit(self),
"母語": QLineEdit(self),
"跟家人常說語言": QLineEdit(self),
"跟朋友常說語言": QLineEdit(self)
}

for label, edit in self.fields.items():
layout.addWidget(QLabel(label))
layout.addWidget(edit)
self.user_info_fields = UserInfo(self)

layout.addLayout(self.create_labeled_field("中文姓名", self.user_info_fields.chinese_name))
layout.addLayout(self.create_labeled_field("英文姓名", self.user_info_fields.english_name))
layout.addLayout(self.create_labeled_field("性別", self.user_info_fields.gender))
layout.addLayout(self.create_labeled_field("出生年", self.user_info_fields.birth_year))
layout.addLayout(self.create_labeled_field("教育程度", self.user_info_fields.education_level))
layout.addLayout(self.create_labeled_field("母語", self.user_info_fields.native_language))
layout.addLayout(self.create_labeled_field("跟家人常說語言", self.user_info_fields.family_language))
layout.addLayout(self.create_labeled_field("跟朋友常說語言", self.user_info_fields.friend_language))


# Save button
save_btn = QPushButton('Save', self)
Expand All @@ -41,8 +40,8 @@ def initUI(self)->None:
self.setWindowTitle('User Information')

def save_data(self):
user_info = {label: edit.text() for label, edit in self.fields.items()}
print("User Info Saved:", user_info)
user_info = self.user_info_fields.get_data()
currentSession.set_user_info(user_info)

def show_confirm_dialog(self):
dialog = ConfirmDialog("save the information?",self)
Expand All @@ -53,4 +52,13 @@ def show_confirm_dialog(self):
self.save_data()
self.navigator.navigate_to_test_screen()
else:
print("Save cancelled")
print("Save cancelled")



def create_labeled_field(self, label_text: str, field: QWidget) -> QHBoxLayout:
labeled_field_layout = QHBoxLayout()
label = QLabel(label_text)
labeled_field_layout.addWidget(label)
labeled_field_layout.addWidget(field)
return labeled_field_layout
3 changes: 3 additions & 0 deletions model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from model.session import Session

currentSession = Session()
File renamed without changes.
12 changes: 12 additions & 0 deletions model/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

class Session:
def __init__(self):
self.user_info = None

def set_user_info(self, info:dict):
self.user_info = info

def get_user_info(self)->dict:
return self.user_info

currentSession = Session()
Empty file added model/user.py
Empty file.
1 change: 1 addition & 0 deletions storage/localStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def append_data(self,new_df):

class DataHandaler:
def __init__(self):

self.user_data_handaler =xlsxHandler(dir=user_dir)
self.history_data_handaler =xlsxHandler(dir=history_dir)
self.exam_data_handler = xlsxHandler(dir=exam_dir)
Expand Down

0 comments on commit bd4f06f

Please sign in to comment.