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