diff --git a/defaults.py b/defaults.py index 2048485..57504e5 100644 --- a/defaults.py +++ b/defaults.py @@ -47,6 +47,7 @@ class FieldType: "obs_source_name": "Home score", "format_regex": format_prefixes[5], "ordinal_indicator": False, + "is_custom": False, }, { "name": "Away Score", @@ -58,6 +59,7 @@ class FieldType: "obs_source_name": "Away score", "format_regex": format_prefixes[5], "ordinal_indicator": False, + "is_custom": False, }, { "name": "Time", @@ -69,6 +71,7 @@ class FieldType: "obs_source_name": "Clock", "format_regex": format_prefixes[0], "ordinal_indicator": False, + "is_custom": False, }, { "name": "Period", @@ -80,6 +83,7 @@ class FieldType: "obs_source_name": "Period", "format_regex": format_prefixes[7], "ordinal_indicator": True, + "is_custom": False, }, { "name": "Home Fouls", @@ -91,6 +95,7 @@ class FieldType: "obs_source_name": "#Home Fouls", "format_regex": format_prefixes[11], "ordinal_indicator": False, + "is_custom": False, }, { "name": "Away Fouls", @@ -102,6 +107,7 @@ class FieldType: "obs_source_name": "#Away Fouls", "format_regex": format_prefixes[11], "ordinal_indicator": False, + "is_custom": False, }, { "name": "Shot Clock", @@ -113,6 +119,7 @@ class FieldType: "obs_source_name": "shotclock", "format_regex": format_prefixes[4], "ordinal_indicator": False, + "is_custom": False, }, ] default_custom_box_info = { @@ -124,10 +131,11 @@ class FieldType: "obs_source_name": "", "format_regex": format_prefixes[11], "ordinal_indicator": False, + "is_custom": True, } -def info_for_box_name(name): +def default_info_for_box_name(name): # Get the info for a box name for box in default_boxes: if box["name"] == name: @@ -145,8 +153,12 @@ def normalize_settings_dict(settings, box_info): "format_regex": format_prefixes[11], "type": FieldType.NUMBER, "ordinal_indicator": False, + "is_custom": True, } return { + "is_custom": ( + settings["is_custom"] if "is_custom" in settings else box_info["is_custom"] + ), "obs_source_name": ( settings["obs_source_name"] if "obs_source_name" in settings diff --git a/main.py b/main.py index 6821e4c..e7d4698 100644 --- a/main.py +++ b/main.py @@ -35,7 +35,7 @@ from source_view import ImageViewer from defaults import ( default_boxes, - info_for_box_name, + default_info_for_box_name, normalize_settings_dict, format_prefixes, ) @@ -543,7 +543,7 @@ def editSettings(self, settingsMutatorCallback): def restoreDefaults(self): # restore the default settings for the selected item def restoreDefaultsSettings(item_obj): - info = info_for_box_name(item_obj.name) + info = default_info_for_box_name(item_obj.name) item_obj.settings = normalize_settings_dict({}, info) return item_obj @@ -684,15 +684,25 @@ def updatevMixTable(self, detectionTargets): def detectionTargetsChanged(self, detectionTargets): for box in detectionTargets: + logger.debug(f"Change: Detection target: {box.name}") # change the list icon to green checkmark items = self.ui.tableWidget_boxes.findItems( box.name, Qt.MatchFlag.MatchExactly ) if len(items) == 0: + logger.warning(f"Item not found: {box.name}. Adding it to the list.") # add the item to the list + self.ui.tableWidget_boxes.insertRow( + self.ui.tableWidget_boxes.rowCount() + ) item = QTableWidgetItem(box.name) self.ui.tableWidget_boxes.setItem( - self.ui.tableWidget_boxes.rowCount(), 0, item + self.ui.tableWidget_boxes.rowCount() - 1, 0, item + ) + disabledItem = QTableWidgetItem() + disabledItem.setFlags(Qt.ItemFlag.NoItemFlags) + self.ui.tableWidget_boxes.setItem( + self.ui.tableWidget_boxes.rowCount() - 1, 1, disabledItem ) else: item = items[0] @@ -763,7 +773,7 @@ def populateSettings(self, name): self.ui.comboBox_binarizationMethod.setCurrentIndex(0) else: item_obj.settings = normalize_settings_dict( - item_obj.settings, info_for_box_name(item_obj.name) + item_obj.settings, default_info_for_box_name(item_obj.name) ) self.ui.label_selectedInfo.setText(f"{item_obj.name}") self.ui.lineEdit_format.setText(item_obj.settings["format_regex"]) @@ -1254,12 +1264,20 @@ def addBox(self): if item.text() not in [o["name"] for o in default_boxes]: custom_boxes.append(item.text()) - store_custom_box_name("Custom") + i = len(custom_boxes) + new_box_name = f"Custom {i + 1}" + custom_boxes_names = fetch_data("scoresight.json", "custom_boxes_names", []) + # find if the name already exists + while new_box_name in custom_boxes or new_box_name in custom_boxes_names: + i += 1 + new_box_name = f"Custom {i + 1}" + + store_custom_box_name(new_box_name) item = QTableWidgetItem( QIcon( path.abspath(path.join(path.dirname(__file__), "icons/circle-x.svg")) ), - "Custom", + new_box_name, ) item.setData(Qt.ItemDataRole.UserRole, "unchecked") self.ui.tableWidget_boxes.insertRow(self.ui.tableWidget_boxes.rowCount()) @@ -1325,7 +1343,7 @@ def makeBox(self): self.listItemClicked(item) # get the size of the box from the name - info = info_for_box_name(item.text()) + info = default_info_for_box_name(item.text()) self.detectionTargetsStorage.add_item( TextDetectionTarget( diff --git a/storage.py b/storage.py index 045cc94..3543bfe 100644 --- a/storage.py +++ b/storage.py @@ -2,7 +2,7 @@ import os from PySide6.QtCore import QObject, Signal from platformdirs import user_data_dir -from defaults import info_for_box_name, normalize_settings_dict +from defaults import default_info_for_box_name, normalize_settings_dict from text_detection_target import TextDetectionTarget from sc_logging import logger @@ -203,9 +203,11 @@ def loadBoxesFromDict(self, boxes) -> bool: self._data.clear() try: for box in boxes: - box_info = info_for_box_name(box["name"]) + logger.debug("loading box: " + box["name"]) if "settings" not in box: box["settings"] = {} + + default_box_info = default_info_for_box_name(box["name"]) # set the position of the box self._data.append( TextDetectionTarget( @@ -214,9 +216,11 @@ def loadBoxesFromDict(self, boxes) -> bool: box["rect"]["width"], box["rect"]["height"], box["name"], - normalize_settings_dict(box["settings"], box_info), + normalize_settings_dict(box["settings"], default_box_info), ) ) + if "is_custom" in box["settings"] and box["settings"]["is_custom"]: + store_custom_box_name(box["name"]) logger.debug("loaded boxes") self.data_changed.emit(self._data) except Exception as e: @@ -230,7 +234,8 @@ def getBoxesForStorage(self): boxes = [] for detectionTarget in self._data: detectionTarget.settings = normalize_settings_dict( - detectionTarget.settings, info_for_box_name(detectionTarget.name) + detectionTarget.settings, + default_info_for_box_name(detectionTarget.name), ) boxes.append( { @@ -242,6 +247,7 @@ def getBoxesForStorage(self): "height": detectionTarget.height(), }, "settings": { + "is_custom": detectionTarget.settings.get("is_custom"), "obs_source_name": detectionTarget.settings.get( "obs_source_name" ),