Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PICARD-3030: Fix crash when merging original values for multiple tags #2577

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions picard/ui/metadatabox.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def contextMenuEvent(self, event):
menu.addAction(remove_from_preserved_tags_action)
removals = []
useorigs = []
mergeorigs = []
item = self.currentItem()
if item:
column = item.column()
Expand All @@ -439,22 +440,20 @@ def contextMenuEvent(self, event):
file_tracks = []
track_albums = set()
for file in self.files:
objects = [file]
extra_objects = []
if file.parent_item in self.tracks and len(self.files & set(file.parent_item.files)) == 1:
objects.append(file.parent_item)
extra_objects.append(file.parent_item)
file_tracks.append(file.parent_item)
track_albums.add(file.parent_item.album)
orig_values = list(file.orig_metadata.getall(tag)) or [""]
useorigs.append(partial(self._set_tag_values, tag, orig_values, objects))
useorigs.append(partial(self._use_orig_tags, file, tag, extra_objects))
mergeorigs.append(partial(self._merge_orig_tags, file, tag, extra_objects))
for track in set(self.tracks)-set(file_tracks):
objects = [track]
orig_values = list(track.orig_metadata.getall(tag)) or [""]
useorigs.append(partial(self._set_tag_values, tag, orig_values, objects))
useorigs.append(partial(self._use_orig_tags, track, tag))
mergeorigs.append(partial(self._merge_orig_tags, track, tag))
track_albums.add(track.album)
for album in track_albums:
objects = [album]
orig_values = list(album.orig_metadata.getall(tag)) or [""]
useorigs.append(partial(self._set_tag_values, tag, orig_values, objects))
useorigs.append(partial(self._use_orig_tags, album, tag))
mergeorigs.append(partial(self._merge_orig_tags, album, tag))
remove_tag_action = QtGui.QAction(_("Remove"), self)
remove_tag_action.triggered.connect(partial(self._apply_update_funcs, removals))
remove_tag_action.setShortcut(self.remove_tag_shortcut.key())
Expand All @@ -466,7 +465,7 @@ def contextMenuEvent(self, event):
use_orig_value_action.triggered.connect(partial(self._apply_update_funcs, useorigs))
menu.addAction(use_orig_value_action)
merge_tags_action = QtGui.QAction(_("Merge Original Values"), self)
merge_tags_action.triggered.connect(partial(self._merge_tags, selected_tag))
merge_tags_action.triggered.connect(partial(self._apply_update_funcs, mergeorigs))
menu.addAction(merge_tags_action)
menu.addSeparator()
if single_tag:
Expand Down Expand Up @@ -494,15 +493,16 @@ def _apply_update_funcs(self, funcs):
f()
self.tagger.window.update_selection(new_selection=False, drop_album_caches=True)

def _merge_tags(self, tag):
with self.tagger.window.ignore_selection_changes:
for obj in self.objects:
values = list(obj.orig_metadata.getall(tag))
for new_value in obj.metadata.getall(tag):
if new_value not in values:
values.append(new_value)
obj.metadata[tag] = values
obj.update()
def _use_orig_tags(self, obj, tag, extra_objects=None):
orig_values = list(obj.orig_metadata.getall(tag)) or [""]
self._set_tag_values_extra(tag, orig_values, obj, extra_objects)

def _merge_orig_tags(self, obj, tag, extra_objects=None):
values = list(obj.orig_metadata.getall(tag))
for new_value in obj.metadata.getall(tag):
if new_value not in values:
values.append(new_value)
self._set_tag_values_extra(tag, values, obj, extra_objects)

def _edit_tag(self, tag):
if self.tag_diff is not None:
Expand All @@ -518,6 +518,12 @@ def _toggle_changes_first(self, checked):
config.persist['show_changes_first'] = checked
self.update()

def _set_tag_values_extra(self, tag, values, obj, extra_objects):
objects = [obj]
if extra_objects:
objects.extend(extra_objects)
self._set_tag_values(tag, values, objects=objects)

def _set_tag_values(self, tag, values, objects=None):
if objects is None:
objects = self.objects
Expand Down
Loading