from PyQt5 import QtWidgets from PyQt5.QtCore import Qt from PyQt5.QtGui import QStandardItem, QStandardItemModel from ArtNet.gui.dialogs.presence_modify_dialog.presence_modify_dialog import Ui_PresenceModDialog from ArtNet.gui.dialogs.artist_modify_dialog.artist_mod_dialog import ArtistModDialog class PresenceModDialog(QtWidgets.QDialog): def __init__(self, parent=None, edit_presence: bool = False): super().__init__(parent) self.parent = parent self.ui = Ui_PresenceModDialog() self.ui.setupUi(self) self.curr_artist: tuple = None self.__curr_searched_artists: list = [] self.data: dict = None if edit_presence: self.ui.dialog_topic_label.setText("Edit Presence") self.ui.presence_domain_line.setReadOnly(True) self.ui.presence_name_line.setReadOnly(True) else: self.ui.dialog_topic_label.setText("Create Presence") self.ui.presence_domain_line.setReadOnly(False) self.ui.presence_name_line.setReadOnly(False) self.ui.artist_search_line.textChanged.connect(self.on_artist_search_changed) self.ui.create_artist_button.clicked.connect(self.on_create_artist_clicked) self.ui.edit_artist_button.clicked.connect(self.on_edit_artist_clicked) self.ui.delete_artist_button.clicked.connect(self.on_delete_artist_clicked) def set_presence_name(self, name: str): self.ui.presence_name_line.setText(name) def set_presence_domain(self, name: str): self.ui.presence_domain_line.setText(name) def on_create_artist_clicked(self): print("Clicked Create artist!") dialog = ArtistModDialog(self) data = dialog.exec_() if data is None: return self.parent.parent.create_artist(ID=data["id"], description=data["description"]) def on_edit_artist_clicked(self): print("Clicked Edit artist!") def on_delete_artist_clicked(self): print("Clicked Delete artist!") if self.curr_artist is None: QtWidgets.QMessageBox.information(self, "No Artist Selected", "There is no artist selected to delete!") return answer = QtWidgets.QMessageBox.question(self, "Delete this Artist?", "Do you really want to delete the Artist \"{0}\"?" .format(self.curr_artist[1])) if answer == QtWidgets.QMessageBox.Yes: pass # TODO check if presences are associated & warn presences = self.parent.parent.get_artist_presences(self.curr_artist[0]) msg = "" affected_art = 0 for presence in presences: msg += presence[0]+":"+presence[1] + "\n" arts = self.parent.parent.get_presences_art(presence[0], presence[1]) affected_art += len(arts) answer = QtWidgets.QMessageBox.question(self, "Are you sure?", ("Do you really wish to delete the Artist \"{0}\"?\n" + "following Presences are associated with " + "it and deleted also:\n\n" + "{1}\n" + "This will also remove this presence from following " + "amount of art pieces:" + "\n{2}\n") .format(self.curr_artist[1], msg, affected_art) ) if answer == QtWidgets.QMessageBox.Yes: pass self.parent.parent.remove_artist(self.curr_artist[0]) for presence in presences: self.parent.parent.remove_presence(presence[0], presence[1]) self.close() else: return else: return def on_artist_search_changed(self): if len(self.ui.artist_search_line.text()) == 0: # nothing to search for self.set_artist_result_list([]) return artists = self.parent.parent.get_artists(self.ui.artist_search_line.text()) result = [] for ID, desc in artists: result.append(str(ID)+":"+desc) self.set_artist_result_list(result) def set_artist_result_list(self, artists: list): """ Set the list of search result artists. :param artists: [(id, desc)] :return: """ item_model = QStandardItemModel(self.ui.artist_result_list) self.__curr_searched_artists = [] for artist in artists: self.__curr_searched_artists.append(artist) item = QStandardItem(artist) item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setData(Qt.Unchecked, Qt.CheckStateRole) if self.curr_artist is not None: curr_artist = str(self.curr_artist[0]) + ":" + self.curr_artist[1] if artist == curr_artist: item.setCheckState(Qt.Checked) item_model.appendRow(item) item_model.itemChanged.connect(self.on_search_artist_item_changed) self.ui.artist_result_list.setModel(item_model) def set_artist_selected_list(self, artist: tuple): """ Set the list of selected artist :param artists: [(id, desc)] :return: """ if artist is None: return item_model = QStandardItemModel(self.ui.artist_selection_list) item = QStandardItem(str(artist[0])+":"+artist[1]) item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setData(Qt.Unchecked, Qt.CheckStateRole) item.setCheckState(Qt.Checked) item_model.appendRow(item) item_model.itemChanged.connect(self.on_selected_artist_item_changed) self.ui.artist_selection_list.setModel(item_model) def on_search_artist_item_changed(self, item: QStandardItem): artist = item.text().split(":") artist = (artist[0], artist[1]) if item.checkState() == Qt.Checked: self.curr_artist = artist elif item.checkState() == Qt.Unchecked: self.curr_artist = None self.set_artist_result_list(self.__curr_searched_artists) self.set_artist_selected_list(self.curr_artist) def on_selected_artist_item_changed(self, item: QStandardItem): artist = item.text().split(":") artist = (artist[0].strip(), artist[1].strip()) if item.checkState() == Qt.Checked: self.curr_artist = artist elif item.checkState() == Qt.Unchecked: self.curr_artist = artist self.set_artist_result_list(self.__curr_searched_artists) self.set_artist_selected_list(self.curr_artist) def collect_presence_details(self): self.data = { "name": self.ui.presence_name_line.text(), "domain": self.ui.presence_domain_line.text(), "artist": self.curr_artist, "link": self.ui.presence_link_list.text(), } is_null = True for value in self.data.values(): if value is not None and len(value) != 0: is_null = False if is_null: self.data = None def exec_(self) -> dict: if super(PresenceModDialog, self).exec_() == QtWidgets.QDialog.Rejected: return None self.collect_presence_details() return self.data