Slight speedup by ensuring that original_name always exists as a Tag attribute

This commit is contained in:
Charles Haley 2011-03-04 17:18:47 +00:00
parent eb6abcadf1
commit 584e989537
2 changed files with 21 additions and 24 deletions

View File

@ -33,9 +33,6 @@ from calibre.gui2.dialogs.tag_list_editor import TagListEditor
from calibre.gui2.dialogs.edit_authors_dialog import EditAuthorsDialog
from calibre.gui2.widgets import HistoryLineEdit
def original_name(t):
return getattr(t, 'original_name', t.name)
class TagDelegate(QItemDelegate): # {{{
def paint(self, painter, option, index):
@ -240,9 +237,9 @@ class TagsView(QTreeView): # {{{
tag = index.tag
if len(index.children) > 0:
for c in index.children:
self.add_item_to_user_cat.emit(category, original_name(c.tag),
self.add_item_to_user_cat.emit(category, c.tag.original_name,
c.tag.category)
self.add_item_to_user_cat.emit(category, original_name(tag),
self.add_item_to_user_cat.emit(category, tag.original_name,
tag.category)
return
if action == 'add_subcategory':
@ -258,9 +255,9 @@ class TagsView(QTreeView): # {{{
tag = index.tag
if len(index.children) > 0:
for c in index.children:
self.del_item_from_user_cat.emit(key, original_name(c.tag),
self.del_item_from_user_cat.emit(key, c.tag.original_name,
c.tag.category)
self.del_item_from_user_cat.emit(key, original_name(tag), tag.category)
self.del_item_from_user_cat.emit(key, tag.original_name, tag.category)
return
if action == 'manage_searches':
self.saved_search_edit.emit(category)
@ -403,7 +400,7 @@ class TagsView(QTreeView): # {{{
self.db.field_metadata[key]['is_custom']:
self.context_menu.addAction(_('Manage %s')%category,
partial(self.context_menu_handler, action='open_editor',
category=original_name(tag) if tag else None,
category=tag.original_name if tag else None,
key=key))
elif key == 'authors':
self.context_menu.addAction(_('Manage %s')%category,
@ -632,7 +629,7 @@ class TagTreeItem(object): # {{{
while p.parent.type != self.ROOT:
p = p.parent
if not tag.is_hierarchical:
name = original_name(tag)
name = tag.original_name
else:
name = tag.name
tt_author = False
@ -644,7 +641,7 @@ class TagTreeItem(object): # {{{
else:
return QVariant('[%d] %s'%(count, name))
if role == Qt.EditRole:
return QVariant(original_name(tag))
return QVariant(tag.original_name)
if role == Qt.DecorationRole:
return self.icon_state_map[tag.state]
if role == Qt.ToolTipRole:
@ -810,7 +807,7 @@ class TagsModel(QAbstractItemModel): # {{{
p = node
while p.type != TagTreeItem.CATEGORY:
p = p.parent
d = (node.type, p.category_key, p.is_gst, original_name(t),
d = (node.type, p.category_key, p.is_gst, t.original_name,
t.category, path)
data.append(d)
else:
@ -861,7 +858,7 @@ class TagsModel(QAbstractItemModel): # {{{
Copy/move an item and all its children to the destination
'''
copied = False
src_name = original_name(node.tag)
src_name = node.tag.original_name
src_cat = node.tag.category
# delete the item if the source is a user category and action is move
if is_uc and not src_parent_is_gst and src_parent in user_cats and \
@ -1019,7 +1016,7 @@ class TagsModel(QAbstractItemModel): # {{{
fm = self.db.metadata_for_field(key)
is_multiple = fm['is_multiple']
val = original_name(on_node.tag)
val = on_node.tag.original_name
for id in ids:
mi = self.db.get_metadata(id, index_is_id=True)
@ -1152,7 +1149,8 @@ class TagsModel(QAbstractItemModel): # {{{
not fm['is_custom'] and \
not fm['kind'] == 'user' \
else False
tt = key if fm['kind'] == 'user' else None
in_uc = fm['kind'] == 'user'
tt = key if in_uc else None
if collapse_model == 'first letter':
# Build a list of 'equal' first letters by looking for
@ -1227,11 +1225,10 @@ class TagsModel(QAbstractItemModel): # {{{
# category display order is important here. The following works
# only of all the non-user categories are displayed before the
# user categories
components = [t.strip() for t in original_name(tag).split('.')
components = [t.strip() for t in tag.original_name.split('.')
if t.strip()]
if len(components) == 0 or '.'.join(components) != original_name(tag):
components = [original_name(tag)]
in_uc = fm['kind'] == 'user'
if len(components) == 0 or '.'.join(components) != tag.original_name:
components = [tag.original_name]
if (not tag.is_hierarchical) and (in_uc or
key in ['authors', 'publisher', 'news', 'formats', 'rating'] or
key not in self.db.prefs.get('categories_using_hierarchy', []) or
@ -1239,8 +1236,8 @@ class TagsModel(QAbstractItemModel): # {{{
self.beginInsertRows(category_index, 999999, 1)
n = TagTreeItem(parent=node_parent, data=tag, tooltip=tt,
icon_map=self.icon_state_map)
if tag.id_set is not None:
n.id_set |= tag.id_set
# if tag.id_set is not None:
# n.id_set |= tag.id_set
category_child_map[tag.name, tag.category] = n
self.endInsertRows()
tag.is_editable = key != 'formats' and (key == 'news' or \
@ -1365,7 +1362,7 @@ class TagsModel(QAbstractItemModel): # {{{
return True
key = item.tag.category
name = original_name(item.tag)
name = item.tag.original_name
# make certain we know about the item's category
if key not in self.db.field_metadata:
return False
@ -1595,7 +1592,7 @@ class TagsModel(QAbstractItemModel): # {{{
if tag.name and tag.name[0] == u'\u2605': # char is a star. Assume rating
ans.append('%s%s:%s'%(prefix, category, len(tag.name)))
else:
name = original_name(tag)
name = tag.original_name
use_prefix = tag.state in [TAG_SEARCH_STATES['mark_plusplus'],
TAG_SEARCH_STATES['mark_minusminus']]
if category == 'tags':
@ -1638,7 +1635,7 @@ class TagsModel(QAbstractItemModel): # {{{
tag = tag_item.tag
if tag is None:
return False
name = original_name(tag)
name = tag.original_name
if (equals_match and strcmp(name, txt) == 0) or \
(not equals_match and lower(name).find(txt) >= 0):
self.path_found = path

View File

@ -48,7 +48,7 @@ class Tag(object):
def __init__(self, name, id=None, count=0, state=0, avg=0, sort=None,
tooltip=None, icon=None, category=None, id_set=None):
self.name = name
self.name = self.original_name = name
self.id = id
self.count = count
self.state = state