From 9e61d45ea502d8d9deb7cf59245361a3f3232536 Mon Sep 17 00:00:00 2001 From: DrRetro Date: Wed, 24 Apr 2024 18:11:41 -0400 Subject: [PATCH] Rewrote Multi-Select to use field templates. --- tagstudio/src/qt/ts_qt.py | 114 +++++--------------------------------- 1 file changed, 13 insertions(+), 101 deletions(-) diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index 62a36af0..c5d2f942 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -292,7 +292,7 @@ class FieldWidget(QWidget): class TagBoxWidget(FieldWidget): updated = Signal() - + def __init__(self, item, title, field_index, library:Library, tags:list[int], driver:'QtDriver') -> None: super().__init__(title) # QObject.__init__(self) @@ -397,58 +397,17 @@ class TagBoxWidget(FieldWidget): # self.tags.append(tag) logging.info(f'[TAG BOX WIDGET] ADD TAG CALLBACK: T:{tag_id} to E:{self.item.id}') logging.info(f'[TAG BOX WIDGET] SELECTED T:{self.driver.selected}') - # TODO: Make this more efficient. - # Iterate through the fields of the item and find the field that - # contains the tags. - correct_field_id = 0 # Initialize the field index - skip = False # Initialize the skip flag - temp_tags = self.tags.copy() # Make a copy of the tags - while correct_field_id < len(self.item.fields): - field = list(self.item.fields)[correct_field_id] - for key in dict(field).keys(): - temp = field[key] - if type(temp) == list: - # Check if the this field's tags list to see if it matches the current field's list - if self.tags == temp: - # Update the correct_field_id if a match is found - correct_field_id = key - skip = True - break - if skip: - break - correct_field_id += 1 + logging.info(f'[TAG BOX WIDGET] SELECTED T:{self.title}') + id = list(self.lib.filter_field_templates(str(self.title).removesuffix(' (Tag Box)')))[0] for x in self.driver.selected: - if x[0] == ItemType.ENTRY: - logging.info(f'[TAG BOX WIDGET] FIELDS:{self.driver.lib.get_entry(x[1]).fields}') - index = 0 # Initialize the index - skip = False # Initialize the skip flag - if not skip: - # Iterate through the fields of the current entry - while index < len(self.driver.lib.get_entry(x[1]).fields): - # Get the current fields - field = list(self.driver.lib.get_entry(x[1]).fields)[index] - for key in dict(field).keys(): - temp = field[key] - if type(temp) == list: - if temp_tags == temp and key == correct_field_id: - skip = True - break - if skip: - break - else: - index += 1 # Increment the index - if not skip and index >= len(self.driver.lib.get_entry(x[1]).fields): - # Add the field to the entry - self.driver.lib.add_field_to_entry(x[1],correct_field_id) - index = 0 - self.driver.lib.get_entry(x[1]).add_tag(self.driver.lib, tag_id, field_id=correct_field_id, field_index=index) + self.driver.lib.get_entry(x[1]).add_tag(self.driver.lib, tag_id, field_id=id, field_index=-1) self.updated.emit() - # logging.info(f'I want to add tag ID {tag_id} to entry {self.item.filename}') - # self.updated.emit() - # if tag_id not in self.tags: - # self.tags.append(tag_id) - # self.set_tags(self.tags) - # elif type((x[0]) == ThumbButton): + # logging.info(f'I want to add tag ID {tag_id} to entry {self.item.filename}') + # self.updated.emit() + # if tag_id not in self.tags: + # self.tags.append(tag_id) + # self.set_tags(self.tags) + # elif type((x[0]) == ThumbButton): def edit_tag_callback(self, tag:Tag): @@ -456,57 +415,10 @@ class TagBoxWidget(FieldWidget): def remove_tag(self, tag_id): logging.info(f'[TAG BOX WIDGET] SELECTED T:{self.driver.selected}') - - # Iterate through the fields of the item and find the field that - # contains the tags. - # TODO: Make this more efficient. - correct_field_id = 0 # Initialize the field index - skip = False # Initialize the skip flag - temp_tags = self.tags.copy() # Make a copy of the tags - while correct_field_id < len(self.item.fields): - # Get the current field - field = list(self.item.fields)[correct_field_id] - for key in dict(field).keys(): - temp = field[key] - if type(temp) == list: - # Check if the this field's tags list to see if it matches the current field's list - if self.tags == temp: - # Update the correct_field_id if a match is found - correct_field_id = key - skip = True - break - if skip: - break - correct_field_id += 1 - # Iterate through the selected entries + id = list(self.lib.filter_field_templates(str(self.title).removesuffix(' (Tag Box)')))[0] for x in self.driver.selected: - # Check if the current entry is of type Entry - if x[0] == ItemType.ENTRY: - index = 0 # Initialize the index - skip = False # Initialize the skip flag - # Check if the skip flag is False - if not skip: - # Iterate through the fields of the current entry - while index < len(self.driver.lib.get_entry(x[1]).fields): - # Get the current fields - field = list(self.driver.lib.get_entry(x[1]).fields)[index] - for key in dict(field).keys(): - temp = field[key] - if type(temp) == list: - if temp_tags == temp and key == correct_field_id: - skip = True - break - if skip: - break - else: - index += 1 # Increment the index - # Check if the skip flag is False and the index is out of bounds - if not skip and index >= len(self.driver.lib.get_entry(x[1]).fields): - # Add the field to the entry - self.driver.lib.add_field_to_entry(x[1],correct_field_id) - index = 0 # Reset the index - self.driver.lib.get_entry(x[1]).remove_tag(self.driver.lib, tag_id, field_index=index) - self.updated.emit() + self.driver.lib.get_entry(x[1]).remove_tag(self.driver.lib, tag_id, field_id=id, field_index=-1) + self.updated.emit() # def show_add_button(self, value:bool): # self.add_button.setHidden(not value)