From cbd3ae80c776103ba4d900c0cd667613cc933f45 Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Fri, 9 Feb 2024 13:17:15 +0100 Subject: [PATCH] Refactor code to improve readability, try diff threads --- src/logic/userInterface.py | 155 ++++++++++++++++++------------------- 1 file changed, 77 insertions(+), 78 deletions(-) diff --git a/src/logic/userInterface.py b/src/logic/userInterface.py index 52bc59b..5f4a75a 100644 --- a/src/logic/userInterface.py +++ b/src/logic/userInterface.py @@ -123,7 +123,6 @@ class MessageCalendar(QtWidgets.QCalendarWidget): ) - class Ui(Ui_Semesterapparat): # use the Ui_MainWindow class from mainwindow.py def __init__(self, MainWindow, username: str) -> None: @@ -161,10 +160,10 @@ class Ui(Ui_Semesterapparat): ) self.calendarWidget.setObjectName("MessageCalendar") self.calendarWidget.clicked.connect(self.open_reminder) - #assign a context menu to the calendar + # assign a context menu to the calendar self.calendarWidget.setContextMenuPolicy(QtCore.Qt.ContextMenuPolicy.CustomContextMenu) self.calendarWidget.customContextMenuRequested.connect(self.calendar_context_menu) - + self.tableWidget_apparat_media.horizontalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeMode.Stretch ) @@ -214,7 +213,7 @@ class Ui(Ui_Semesterapparat): self.tableWidget_apparat_media.customContextMenuRequested.connect( self.media_context_menu ) - #enable automatic resizing of columns for book_search_result + # enable automatic resizing of columns for book_search_result self.book_search_result.horizontalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeMode.Stretch ) @@ -287,40 +286,39 @@ class Ui(Ui_Semesterapparat): self.tabWidget.currentChanged.connect(self.tab_changed) self.btn_search.clicked.connect(self.statistics) # self.thread_check() - + ### Admin interface ### #!admin - create user #!admin - delete user #!admin - change user - #TODO:admin - change faculty + # TODO:admin - change faculty self.select_action_box.addItem("") self.select_action_box.setCurrentText("") self.hide_all() self.select_action_box.currentTextChanged.connect(self.admin_action_changed) self.edit_faculty_member_select_member.currentTextChanged.connect(self.edit_faculty_member_set_data) self.book_search.clicked.connect(self.search_book) - - #enable click functionality for the combobox to allow selection of roles - - #admin buttons + + # enable click functionality for the combobox to allow selection of roles + + # admin buttons self.user_frame_addUser.clicked.connect(self.add_user) self.pushButton.clicked.connect(self.delete_user) self.update_user.clicked.connect(self.update_user_data) self.update_faculty_member.clicked.connect(self.edit_faculty_member_action) - + def populate_appfach_dropdown(self): self.app_fach.clear() self.app_fach.addItem("") self.app_fach.setCurrentText("") self.app_fach.addItems([subject[1] for subject in self.db.getSubjects()]) - + def tabW1_changed(self): if self.tabWidget.currentIndex() == 1: # self.tabWidget.setCurrentIndex(1) self.tabWidget_2.setCurrentIndex(1) self.tabWidget_2.setCurrentIndex(0) - - + def search_book(self): self.book_search_result.setRowCount(0) signature = self.seach_by_signature.text() @@ -335,22 +333,21 @@ class Ui(Ui_Semesterapparat): if retdata == None: return for book in retdata: - + self.book_search_result.insertRow(0) self.book_search_result.setItem(0,0,QtWidgets.QTableWidgetItem(book[0].title)) self.book_search_result.setItem(0,1,QtWidgets.QTableWidgetItem(book[0].signature)) print(book[1]) self.book_search_result.setItem(0,2,QtWidgets.QTableWidgetItem(self.db.getApparatName(book[1],book[2]))) - - + def edit_faculty_member_set_data(self): - #get the selected member + # get the selected member name = self.edit_faculty_member_select_member.currentText() fullname = name.replace(",","") print(fullname,name) - #get the data for the selected member + # get the data for the selected member data = self.db.getProfByName(fullname) - #set the data + # set the data print(data) if data == None: self.edit_faculty_member_title.setText("") @@ -362,15 +359,14 @@ class Ui(Ui_Semesterapparat): self.faculty_member_old_telnr.setText(data[6]) self.faculty_member_oldmail.setText(data[5]) self.edit_faculty_member_title.setText(data[1]) if data[1] != None else self.edit_faculty_member_title.setText("") - - + # self.edit_faculty_member_name.setText(f"{data[3]} {data[2]}") # self.edit_faculty_member_title.setCurrentText(data[1]) # self.edit_faculty_member_mail.setText(data[4]) # self.edit_faculty_member_tel.setText(data[5]) # self.edit_faculty_member_adis_id.setText(str(data[0])) # self.edit_faculty_member_id.setText(str(data[6])) - + def add_user(self): username = self.user_create_frame_username.text() password = self.user_create_frame_password.text() @@ -385,7 +381,7 @@ class Ui(Ui_Semesterapparat): self.user_create_frame_password.clear() self.user_frame_userrole.setCurrentText("") self.admin_action_changed() - + def delete_user(self): if self.user_delete_confirm.isChecked(): username = self.user_delete_frame_user_select.currentText() @@ -395,12 +391,12 @@ class Ui(Ui_Semesterapparat): else: # self.user_delete_err_message.setText("Bitte bestätigen Sie die Löschung des Nutzers") # TODO: implement error message ic("please confirm the deletion of the user") - + def update_user_data(self): username = self.user_edit_frame_user_select.currentText() password = self.user_edit_frame_new_password.text() if self.user_edit_frame_new_password.text() != "" else None role = self.user_edit_frame_role_select.currentText() if self.user_edit_frame_role_select.currentText() != "" else None - + userdata = AdminCommands().create_password(password) data = { "password": f"{userdata[1]}{userdata[0]}", @@ -413,7 +409,7 @@ class Ui(Ui_Semesterapparat): self.user_edit_frame_user_select.setCurrentText("") self.user_edit_frame_new_password.clear() self.user_edit_frame_role_select.setCurrentText("") - + def edit_faculty_member_action(self): def __gen_fullname(fname,lname,data): if fname == "" and lname == "": @@ -424,16 +420,16 @@ class Ui(Ui_Semesterapparat): return f"{data[2]} {fname}" if fname != "" and lname != "": return f"{lname} {fname}" - #get the data and use new value if it is not none and does not mach the old value + # get the data and use new value if it is not none and does not mach the old value if self.edit_faculty_member_select_member.currentText() =="": return olddata = self.db.getFacultyMember(self.edit_faculty_member_select_member.currentText()) - + data = olddata[0] oldlname = data[2] oldfname = data[1] - #take data except first and last entry - + # take data except first and last entry + titel = self.edit_faculty_member_new_title.currentText() if self.edit_faculty_member_new_title.currentText() != "Kein Titel" else None fname = self.edit_faculty_member_new_surname.text() if self.edit_faculty_member_new_surname.text() != "" else self.edit_faculty_member_select_member.currentText().split(" ")[1].strip() lname = self.user_faculty_member_new_name.text() if self.user_faculty_member_new_name.text() != "" else self.edit_faculty_member_select_member.currentText().split(" ")[0].strip() @@ -456,18 +452,18 @@ class Ui(Ui_Semesterapparat): self.user_faculty_member_new_name.clear() self.user_faculty_member_new_telnr.clear() self.user_faculty_member_new_mail.clear() - + def hide_all(self): self.user_create_frame.hide() self.user_edit_frame.hide() self.user_delete_frame.hide() self.edit_faculty_member.hide() - + def admin_action_changed(self): action = self.select_action_box.currentText() roles = self.db.getRoles() roles = [role[0] for role in roles] - #remove duplicates + # remove duplicates roles = list(dict.fromkeys(roles)) users = self.db.getUsers() users = [user[2] for user in users] @@ -489,18 +485,17 @@ class Ui(Ui_Semesterapparat): self.user_delete_frame_user_select.addItems(users) self.user_delete_frame_user_select.setCurrentText("") self.user_delete_frame_user_select.addItems(users) - + elif action == "Lehrperson bearbeiten": self.hide_all() self.edit_faculty_member.show() self.add_faculty_member_data() self.edit_faculty_member_new_title.addItems(PROF_TITLES) - - + else: self.hide_all() return - + def add_faculty_member_data(self): faculty_members = self.db.getFacultyMembers() names = [f"{member[5]}" for member in faculty_members] @@ -508,7 +503,7 @@ class Ui(Ui_Semesterapparat): self.edit_faculty_member_select_member.addItems(names) self.edit_faculty_member_select_member.addItem("") self.edit_faculty_member_select_member.setCurrentText("") - + def tabW2_changed(self): if self.tabWidget_2.currentIndex() == 0: @@ -567,22 +562,22 @@ class Ui(Ui_Semesterapparat): self.gridLayout_4.addWidget(deleted_status, 1, 1) created_status.person_double_clicked.connect(self.open_apparat) deleted_status.person_double_clicked.connect(self.open_apparat) - + def open_apparat(self, header:str, apparat:str, parent_depth:int): print(header) if header == "deleted" and parent_depth == 2: - #TODO: warn message here - print("warning") + # TODO: warn message here + print("warning") if parent_depth == 1: print(apparat) - #person selected case - open all apparats from this person in the tableWidget + # person selected case - open all apparats from this person in the tableWidget self.tableWidget.setRowCount(0) prof_id = self.db.getProfId(apparat.split("(")[0].strip()) apparats = self.db.getApparatsByProf(prof_id) for app in apparats: print(app) - #set the items 0 = clickable checkbox, 1 = appname, 2 = profname, 3 = fach - #insert new row + # set the items 0 = clickable checkbox, 1 = appname, 2 = profname, 3 = fach + # insert new row self.tableWidget.insertRow(0) self.tableWidget.setItem(0,0, QtWidgets.QTableWidgetItem("")) self.tableWidget.setItem(0, 1, QtWidgets.QTableWidgetItem(app[1])) @@ -606,9 +601,9 @@ class Ui(Ui_Semesterapparat): "Dieser Semesterapparat kann nicht gelöscht werden, da er bereits gelöscht wurde" ) elif parent_depth == 2: - #apparat selected case - open the apparat in the frame + # apparat selected case - open the apparat in the frame self.load_app_data(apparat) - #change tab focus to tab 0 + # change tab focus to tab 0 self.tabWidget.setCurrentIndex(0) return @@ -636,7 +631,7 @@ class Ui(Ui_Semesterapparat): self.tab_changed() def populate_tab(self): - #add default values to the dropdowns + # add default values to the dropdowns self.box_appnrs.clear() self.box_appnrs.addItem("") self.box_appnrs.setCurrentText("") @@ -655,7 +650,7 @@ class Ui(Ui_Semesterapparat): self.box_dauerapp.clear() self.box_dauerapp.addItems(["Ja", "Nein", ""]) self.box_dauerapp.setCurrentText("") - #add custom vaules + # add custom vaules appnrs = self.db.getUnavailableApparatNumbers() apparats = natsorted(appnrs) apparats = [str(apparat) for apparat in apparats] @@ -668,8 +663,8 @@ class Ui(Ui_Semesterapparat): semester = self.db.getSemersters() self.box_erstellsemester.addItems(semester) self.statistics_table.setRowCount(0) - - #set data for table and graph in tab 2 tableWidget_3 + + # set data for table and graph in tab 2 tableWidget_3 data = self.db.getApparatCountBySemester() data = c_sort.custom_sort(data) # self.tabWidget_3.clear() @@ -708,16 +703,16 @@ class Ui(Ui_Semesterapparat): self.populate_tab() elif curr_tab == 2: #admin tab self.populate_admin_tab() - + def populate_admin_tab(self): pass - + def populate_dropdown(self, box, data): box.clear() box.addItem("") box.setCurrentText("") box.addItems(data) - + def delete_selected_apparats(self): # get all selected apparats selected_apparats = [] @@ -794,8 +789,6 @@ class Ui(Ui_Semesterapparat): self.tableWidget.cellWidget(i, 0).setToolTip( "Dieser Semesterapparat kann nicht gelöscht werden, da er bereits gelöscht wurde" ) - - def populate_frame(self, appdata: ApparatData): # populate the frame with the data from the database @@ -837,7 +830,7 @@ class Ui(Ui_Semesterapparat): self.apparat_id_adis.setText(str(appdata.apparat_adis_id)) self.frame.setEnabled(True) self.validateLoadedData() - + def validateLoadedData(self): self.validate_prof_mail() self.validate_prof_name() @@ -870,8 +863,6 @@ class Ui(Ui_Semesterapparat): self.update_app_media_list() self.cancel_active_selection.click() - - def confirm_popup(self, message: str): dial = QtWidgets.QDialog() popup = popus_confirm() @@ -1029,9 +1020,9 @@ class Ui(Ui_Semesterapparat): def load_app_data(self,app_id=None): print(type(app_id)) if isinstance(app_id, str): - #double click the tableWidget_apparate row with the given app_id + # double click the tableWidget_apparate row with the given app_id row,column = self.get_index_of_value(self.tableWidget_apparate,app_id) - #set the current index to the row + # set the current index to the row self.tableWidget_apparate.setCurrentCell(row,0) app_pos = self.tableWidget_apparate.currentIndex() appnr = self.tableWidget_apparate.item(app_pos.row(), 0).text() @@ -1077,7 +1068,7 @@ class Ui(Ui_Semesterapparat): # clear dokumemt_list self.dokument_list.setRowCount(0) self.frame.setEnabled(True) - + self.sem_year.setEnabled(True) self.sem_sommer.setEnabled(True) self.sem_winter.setEnabled(True) @@ -1109,12 +1100,14 @@ class Ui(Ui_Semesterapparat): text = f"Medium {curr}/{total}" self.logger.log_info(text) self.progress_label.setText(text) + self.MainWindow.setStatusTip(f"Medien werden hinzugefügt, bitte warten... {curr}/{total}") # update tableWidget_apparat_media self.update_app_media_list() def hide_progress_label(self): self.logger.log_info("Finished adding media, hiding progress label") self.progress_label.hide() + self.progress_label.setText("Bitte warten...") self.line_2.hide() self.label_info.hide() @@ -1148,9 +1141,12 @@ class Ui(Ui_Semesterapparat): # create apparat self.btn_save_apparat() # create a thread that updates the progress label after each medium - count = len(data) thread = QThread() - grabber = BookGrabber(mode, data, app_id, prof_id) + grabber = BookGrabber(mode=mode, app_id=app_id, prof_id=prof_id, data=data) + # grabber.mode = mode + # grabber.data = data + # grabber.app_id = app_id + # grabber.prof_id = prof_id grabber.moveToThread(thread) grabber.finished.connect(thread.quit) grabber.finished.connect(grabber.deleteLater) @@ -1313,7 +1309,7 @@ class Ui(Ui_Semesterapparat): return if "http" not in link: link = "https://" + link - + webbrowser.open(link) # get the name of the column columnname = self.tableWidget_apparat_media.horizontalHeaderItem( @@ -1354,7 +1350,7 @@ class Ui(Ui_Semesterapparat): # set tooltip of row 3 to the file path for each row self.dokument_list.item(0, 3).setToolTip(file) -# self.db.insert_file(files, self.active_apparat(), self.db.get_prof_id(self.drpdwn_prof_name.currentText())) + # self.db.insert_file(files, self.active_apparat(), self.db.get_prof_id(self.drpdwn_prof_name.currentText())) def open_document(self): _selected_doc_name = "" @@ -1387,7 +1383,6 @@ class Ui(Ui_Semesterapparat): # if "~" in path: # path = path.replace("~", str(Path.home())) # path = Path(path) - def add_media_from_file(self): def __open_dialog(signatures): @@ -1491,8 +1486,8 @@ class Ui(Ui_Semesterapparat): app_id = self.active_apparat() prof_id = self.db.getProfId(self.drpdwn_prof_name.currentText()) # check if apparat in database - - #if app_id not in database, create apparat + + # if app_id not in database, create apparat if not self.db.checkApparatExistsById(app_id): # create apparat print("Creating apparat") @@ -1503,10 +1498,10 @@ class Ui(Ui_Semesterapparat): self.tableWidget_apparate.setToolTip("") return else: - # if file is selected, check for books in the file + # if file is selected, check for books in the file print("File selected") file = self.dokument_list.item(self.dokument_list.currentRow(), 3).text() - + file_type = self.dokument_list.item( self.dokument_list.currentRow(), 1 ).text() @@ -1539,7 +1534,11 @@ class Ui(Ui_Semesterapparat): ic(signatures) print("starting thread") thread = QThread() - grabber = BookGrabber("ARRAY", signatures, app_id, prof_id) + grabber = BookGrabber(mode = "ARRAY", app_id = app_id,prof_id = prof_id,data = signatures) + # grabber.mode = "ARRAY" + # grabber.data = signatures + # grabber.app_id = app_id + # grabber.prof_id = prof_id grabber.moveToThread(thread) self.label_info.show() self.progress_label.show() @@ -1685,7 +1684,7 @@ class Ui(Ui_Semesterapparat): self.logger.log_info(f"Inserted apparat {apparat[4]}") def calendar_context_menu(self): - #create a context menu for the calendar + # create a context menu for the calendar menu = QtWidgets.QMenu() create_reminder = menu.addAction("Erinnerung erstellen") create_reminder.triggered.connect(self.reminder(date=self.calendarWidget.selectedDate().toString("yyyy-MM-dd"))) @@ -1786,13 +1785,13 @@ class Ui(Ui_Semesterapparat): data = settings.return_data() print(data) OmegaConf.save(data, "config.yaml") - #re-load the config + # re-load the config config = OmegaConf.load("config.yaml") self.logger.log_info("Saved settings to config.yaml") self.reload() - + def reload(self): - #create a new connection to the database, refresh table data and replace the old connection + # create a new connection to the database, refresh table data and replace the old connection self.db = Database() self.apparats = self.db.getAllAparats(deleted=0) self.apparats = natsorted(self.apparats, key=lambda x: x[4], reverse=True) @@ -1819,7 +1818,7 @@ class Ui(Ui_Semesterapparat): signature=book, prof_id=self.db.getProfId(self.drpdwn_prof_name.currentText()), ) - + widget = QtWidgets.QDialog() bookedit = edit_bookdata_ui() bookedit.setupUi(widget) @@ -1985,7 +1984,7 @@ def launch_gui(): # console = "" print("admin") atexit.register(delete_temp_contents) - + if __name__ == "__main__": print("This is the main window")