Make sure editing the name of a node doesn't break anything. Prevent renaming of constructed nodes.

It isn't quite right because inner-node renaming will create another hierarchy. Will deal with that at some point.
This commit is contained in:
Charles Haley 2011-02-22 18:12:33 +00:00
parent 14f9f53bca
commit 166db2e4de

View File

@ -258,9 +258,13 @@ class TagsView(QTreeView): # {{{
if item.type == TagTreeItem.TAG: if item.type == TagTreeItem.TAG:
tag_item = item tag_item = item
tag_name = item.tag.name t = item.tag
tag_id = item.tag.id tag_name = t.name
item = item.parent tag_id = t.id
can_edit = getattr(t, 'can_edit', True)
print can_edit, getattr(t, 'original_name', t.name), t.name
while item.type != TagTreeItem.CATEGORY:
item = item.parent
if item.type == TagTreeItem.CATEGORY: if item.type == TagTreeItem.CATEGORY:
if not item.category_key.startswith('@'): if not item.category_key.startswith('@'):
@ -276,13 +280,14 @@ class TagsView(QTreeView): # {{{
if tag_name: if tag_name:
# If the user right-clicked on an editable item, then offer # If the user right-clicked on an editable item, then offer
# the possibility of renaming that item. # the possibility of renaming that item.
if key in ['authors', 'tags', 'series', 'publisher', 'search'] or \ if can_edit and \
key in ['authors', 'tags', 'series', 'publisher', 'search'] or \
(self.db.field_metadata[key]['is_custom'] and \ (self.db.field_metadata[key]['is_custom'] and \
self.db.field_metadata[key]['datatype'] != 'rating'): self.db.field_metadata[key]['datatype'] != 'rating'):
# Add the 'rename' items # Add the 'rename' items
self.context_menu.addAction(_('Rename %s')%tag_name, self.context_menu.addAction(_('Rename %s')%tag_name,
partial(self.context_menu_handler, action='edit_item', partial(self.context_menu_handler, action='edit_item',
category=tag_item, index=index)) category=tag_item, index=index))
if key == 'authors': if key == 'authors':
self.context_menu.addAction(_('Edit sort for %s')%tag_name, self.context_menu.addAction(_('Edit sort for %s')%tag_name,
partial(self.context_menu_handler, partial(self.context_menu_handler,
@ -530,7 +535,7 @@ class TagTreeItem(object): # {{{
else: else:
return QVariant('[%d] %s'%(tag.count, name)) return QVariant('[%d] %s'%(tag.count, name))
if role == Qt.EditRole: if role == Qt.EditRole:
return QVariant(tag.name) return QVariant(getattr(tag, 'original_name', tag.name))
if role == Qt.DecorationRole: if role == Qt.DecorationRole:
return self.icon_state_map[tag.state] return self.icon_state_map[tag.state]
if role == Qt.ToolTipRole: if role == Qt.ToolTipRole:
@ -926,11 +931,12 @@ class TagsModel(QAbstractItemModel): # {{{
if i < len(components)-1: if i < len(components)-1:
t = copy.copy(tag) t = copy.copy(tag)
t.original_name = '.'.join(components[:i+1]) t.original_name = '.'.join(components[:i+1])
t.use_prefix = True t.can_edit = False
else: else:
t = tag t = tag
t.original_name = t.name t.original_name = t.name
t.use_prefix = False t.can_edit = True
t.use_prefix = True
t.name = comp t.name = comp
self.beginInsertRows(category_index, 999999, 1) self.beginInsertRows(category_index, 999999, 1)
node_parent = TagTreeItem(parent=node_parent, data=t, node_parent = TagTreeItem(parent=node_parent, data=t,
@ -980,7 +986,10 @@ class TagsModel(QAbstractItemModel): # {{{
_('An item cannot be set to nothing. Delete it instead.')).exec_() _('An item cannot be set to nothing. Delete it instead.')).exec_()
return False return False
item = index.internalPointer() item = index.internalPointer()
key = item.parent.category_key itm = item.parent
while itm.type != TagTreeItem.CATEGORY:
itm = itm.parent
key = itm.category_key
# make certain we know about the item's category # make certain we know about the item's category
if key not in self.db.field_metadata: if key not in self.db.field_metadata:
return False return False