Skip to content

Commit 3657614

Browse files
committed
Properly delete all selected tags when image list filter is set
1 parent 1bc4962 commit 3657614

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

taggui/models/image_list_model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def write_image_tags_to_disk(self, image: Image):
100100

101101
def update_image_tags(self, image_index: QModelIndex, tags: list[str]):
102102
image: Image = self.data(image_index, Qt.UserRole)
103+
if image.tags == tags:
104+
return
103105
image.tags = tags
104106
self.dataChanged.emit(image_index, image_index)
105107
self.write_image_tags_to_disk(image)

taggui/widgets/image_tags_editor.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from pathlib import Path
22

3-
from PySide6.QtCore import (QItemSelectionModel, QModelIndex,
4-
QPersistentModelIndex, QStringListModel, QTimer,
5-
Qt, Slot)
3+
from PySide6.QtCore import (QItemSelectionModel, QModelIndex, QStringListModel,
4+
QTimer, Qt, Slot)
65
from PySide6.QtGui import QKeyEvent
76
from PySide6.QtWidgets import (QAbstractItemView, QCompleter, QDockWidget,
87
QLabel, QLineEdit, QListView, QVBoxLayout,
@@ -78,15 +77,11 @@ def keyPressEvent(self, event: QKeyEvent):
7877
if event.key() != Qt.Key_Delete:
7978
super().keyPressEvent(event)
8079
return
81-
# The selected indices must be converted to `QPersistentModelIndex`
82-
# objects to properly delete multiple tags.
83-
selected_indices = [QPersistentModelIndex(index) for index
84-
in self.selectedIndexes()]
85-
for index in selected_indices:
86-
self.image_tag_list_model.removeRow(index.row())
87-
# The current index is set but not selected automatically after the
88-
# tags are deleted, so select it.
89-
self.setCurrentIndex(self.currentIndex())
80+
rows_to_remove = [index.row() for index in self.selectedIndexes()]
81+
remaining_tags = [tag for i, tag
82+
in enumerate(self.image_tag_list_model.stringList())
83+
if i not in rows_to_remove]
84+
self.image_tag_list_model.setStringList(remaining_tags)
9085

9186

9287
class ImageTagsEditor(QDockWidget):
@@ -127,6 +122,7 @@ def __init__(self, proxy_image_list_model: ProxyImageListModel,
127122
self.image_tags_list.scrollToBottom)
128123
# `rowsInserted` does not have to be connected because `dataChanged`
129124
# is emitted when a tag is added.
125+
self.image_tag_list_model.modelReset.connect(self.count_tokens)
130126
self.image_tag_list_model.dataChanged.connect(self.count_tokens)
131127
self.image_tag_list_model.rowsRemoved.connect(self.count_tokens)
132128

taggui/widgets/main_window.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ def update_image_list_model_tags(self):
260260
def connect_image_tags_editor_signals(self):
261261
# `rowsInserted` does not have to be connected because `dataChanged`
262262
# is emitted when a tag is added.
263+
self.image_tag_list_model.modelReset.connect(
264+
self.update_image_list_model_tags)
263265
self.image_tag_list_model.dataChanged.connect(
264266
self.update_image_list_model_tags)
265267
self.image_tag_list_model.rowsRemoved.connect(

0 commit comments

Comments
 (0)