Skip to content

Commit

Permalink
Merge pull request #2476 from zas/metadatabox_cleanup
Browse files Browse the repository at this point in the history
Metadatabox cleanup
  • Loading branch information
zas authored May 19, 2024
2 parents ab86843 + 72c7c60 commit 38304af
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 140 deletions.
41 changes: 25 additions & 16 deletions picard/ui/edittagdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(self, window, tag):
self.metadata_box = window.metadata_box
self.tag = tag
self.modified_tags = {}
self.different = False
self.is_grouped = False
self.default_tags = sorted(
set(list(TAG_NAMES.keys()) + self.metadata_box.tag_diff.tag_names))
if len(self.metadata_box.files) == 1:
Expand Down Expand Up @@ -162,12 +162,15 @@ def add_or_edit_value(self):
else:
self.add_value()

def _group(self, is_grouped):
self.is_grouped = is_grouped
self.ui.add_value.setEnabled(not is_grouped)

def remove_value(self):
value_list = self.value_list
row = value_list.currentRow()
if row == 0 and self.different:
self.different = False
self.ui.add_value.setEnabled(True)
if row == 0 and self.is_grouped:
self._group(False)
value_list.takeItem(row)

def on_rows_inserted(self, parent, first, last):
Expand Down Expand Up @@ -240,36 +243,42 @@ def tag_changed(self, tag):
values = self.modified_tags.get(self.tag, None)
if values is None:
new_tags = self.metadata_box.tag_diff.new
display_value, self.different = new_tags.display_value(self.tag)
values = [display_value] if self.different else new_tags[self.tag]
self.ui.add_value.setEnabled(not self.different)
display_value = new_tags.display_value(self.tag)
if display_value.is_grouped:
# grouped values have a special text, which isn't a valid tag value
values = [display_value.text]
self._group(True)
else:
# normal tag values
values = new_tags[self.tag]
self._group(False)

self.value_list.model().rowsInserted.disconnect(self.on_rows_inserted)
self._add_value_items(values)
self.value_list.model().rowsInserted.connect(self.on_rows_inserted)
self.value_list.setCurrentItem(self.value_list.item(0), QtCore.QItemSelectionModel.SelectionFlag.SelectCurrent)
tag_names.editTextChanged.connect(self.tag_changed)

def _set_item_style(self, item):
font = item.font()
font.setItalic(self.is_grouped)
item.setFont(font)

def _add_value_items(self, values):
values = [v for v in values if v] or [""]
for value in values:
item = QtWidgets.QListWidgetItem(value)
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled | QtCore.Qt.ItemFlag.ItemIsEditable | QtCore.Qt.ItemFlag.ItemIsDragEnabled)
font = item.font()
font.setItalic(self.different)
item.setFont(font)
self._set_item_style(item)
self.value_list.addItem(item)

def value_edited(self, item):
row = self.value_list.row(item)
value = item.text()
if row == 0 and self.different:
if row == 0 and self.is_grouped:
self.modified_tags[self.tag] = [value]
self.different = False
font = item.font()
font.setItalic(False)
item.setFont(font)
self.ui.add_value.setEnabled(True)
self._group(False)
self._set_item_style(item)
else:
self._modified_tag()[row] = value
# add tags to the completer model once they get values
Expand Down
Loading

0 comments on commit 38304af

Please sign in to comment.