diff --git a/mal_gui/object_explorer/attacker_item.py b/mal_gui/object_explorer/attacker_item.py index ece318b..88c8cd4 100644 --- a/mal_gui/object_explorer/attacker_item.py +++ b/mal_gui/object_explorer/attacker_item.py @@ -1,5 +1,7 @@ from PySide6.QtGui import QColor from PySide6.QtCore import QTimer +from PySide6.QtWidgets import QGraphicsItem +from shiboken6 import isValid from .item_base import ItemBase @@ -19,7 +21,7 @@ def __init__( self.attacker_toggle_state = False self.timer = QTimer() - self.status_color = QColor(0, 255, 0) + self.status_color = QColor(0, 255, 0) self.attacker_toggle_state = False self.timer.timeout.connect(self.update_status_color) self.timer.start(500) @@ -29,7 +31,7 @@ def __init__( def update_type_text_item_position(self): super().update_type_text_item_position() # For Attacker make the background of type As Red - self.asset_type_background_color = QColor(255, 0, 0) #Red + self.asset_type_background_color = QColor(255, 0, 0) # Red def update_name(self): """Update the name of the attacker""" @@ -43,13 +45,30 @@ def get_item_attribute_values(self): } def update_status_color(self): + # Object may already be deleted on C++ side + if not isValid(self): + return + + # Still check if removed from scene + if self.scene() is None: + if self.timer.isActive(): + self.timer.stop() + return + self.attacker_toggle_state = not self.attacker_toggle_state if self.attacker_toggle_state: - self.status_color = QColor(0, 255, 0) # Green + self.status_color = QColor(0, 255, 0) # Green else: - self.status_color = QColor(255, 0, 0) # Red + self.status_color = QColor(255, 0, 0) # Red self.update() + def itemChange(self, change, value): + """Override to stop timer when item is removed from scene""" + if change == QGraphicsItem.ItemSceneChange: + if value is None and self.timer.isActive(): + self.timer.stop() + return super().itemChange(change, value) + def serialize(self): return { 'title': self.title,