def get_source_model(self, model: QAbstractItemModel, source_type: type): # pylint: disable=R0201, no-self-use """ The Delegate may belong to a view using a ProxyModel. However, the source model for that Proxy Model(s) should be a QFileSystemLibraryModel and is returned by this function Arguments: model(QAbstractItemModel): Current model source_type(type): Expected source model type Returns: QFileSystemLibraryModel: Source model Raises: QLibraryGUIException: If unable to find the source model for the given model """ while True: # https://stackoverflow.com/questions/50478661/python-isinstance-not-working-as-id-expect if model.__class__.__name__ == source_type.__name__: return model if isinstance(model, QAbstractProxyModel): model = model.sourceModel() else: raise QLibraryGUIException( f"Unable to find source model: " f"\n Expected Type is:" f"\n{source_type}" f"\n First non-proxy model type found is" f"\n{type(model)} for" f"\n{model}")
def setModel(self, model: QtCore.QAbstractItemModel): """Overriding setModel to hook up clean/dirty file signals to model before setting Model Args: model (QtCore.QAbstractItemModel): Model to be set Raises: Exception: QLibraryGUIException if model is not LibraryFileProxyModel """ if not isinstance(model, LibraryFileProxyModel): raise QLibraryGUIException( f"Invalid model. Expected type {LibraryFileProxyModel} but got type {type(model)}" ) source_model = model.sourceModel() self.qlibrary_rebuild_signal.connect(source_model.clean_file) source_model.file_dirtied_signal.connect(self.update) source_model.file_dirtied_signal.connect(self.raise_dirty_file) source_model.file_cleaned_signal.connect(self.update) super().setModel(model)