From 036c2fe68ce37c2a69a05bd51fea92046843a0f5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 May 2010 10:28:41 -0600 Subject: [PATCH] Rationalize collections column in device view --- src/calibre/devices/interface.py | 4 +-- src/calibre/devices/prs505/driver.py | 3 ++ src/calibre/devices/usbms/books.py | 13 ++++++-- src/calibre/gui2/library/models.py | 46 +++++++++++++++++++--------- src/calibre/gui2/library/views.py | 1 - 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/calibre/devices/interface.py b/src/calibre/devices/interface.py index f71585fad0..80c0b3d339 100644 --- a/src/calibre/devices/interface.py +++ b/src/calibre/devices/interface.py @@ -391,8 +391,8 @@ class BookList(list): def __init__(self, oncard, prefix, settings): pass - def supports_tags(self): - ''' Return True if the the device supports tags (collections) for this book list. ''' + def supports_collections(self): + ''' Return True if the the device supports collections for this book list. ''' raise NotImplementedError() def add_book(self, book, replace_metadata): diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index 74e1bf0a7e..734c49edbb 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -14,6 +14,7 @@ from calibre.devices.prs505 import MEDIA_XML from calibre.devices.prs505 import CACHE_XML from calibre.devices.prs505.sony_cache import XMLCache from calibre import __appname__ +from calibre.devices.usbms.books import CollectionsBookList class PRS505(USBMS): @@ -23,6 +24,8 @@ class PRS505(USBMS): author = 'Kovid Goyal' supported_platforms = ['windows', 'osx', 'linux'] path_sep = '/' + booklist_class = CollectionsBookList + FORMATS = ['epub', 'lrf', 'lrx', 'rtf', 'pdf', 'txt'] diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index 4de1341c41..6e8811432a 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -97,8 +97,8 @@ class Book(MetaInformation): class BookList(_BookList): - def supports_tags(self): - return True + def supports_collections(self): + return False def add_book(self, book, replace_metadata): if book not in self: @@ -109,6 +109,15 @@ class BookList(_BookList): def remove_book(self, book): self.remove(book) + def get_collections(self): + return {} + + +class CollectionsBookList(BookList): + + def supports_collections(self): + return True + def get_collections(self, collection_attributes): collections = {} series_categories = set([]) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index cb911d4106..2a5e009675 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -729,6 +729,17 @@ class BooksModel(QAbstractTableModel): # {{{ class OnDeviceSearch(SearchQueryParser): # {{{ + DEFAULT_LOCATIONS = [ + 'collections', + 'title', + 'author', + 'format', + 'search', + 'date', + 'all', + ] + + def __init__(self, model): SearchQueryParser.__init__(self) self.model = model @@ -738,6 +749,8 @@ class OnDeviceSearch(SearchQueryParser): # {{{ def get_matches(self, location, query): location = location.lower().strip() + if location == 'authors': + location = 'author' matchkind = CONTAINS_MATCH if len(query) > 1: @@ -752,14 +765,15 @@ class OnDeviceSearch(SearchQueryParser): # {{{ if matchkind != REGEXP_MATCH: ### leave case in regexps because it can be significant e.g. \S \W \D query = query.lower() - if location not in ('title', 'author', 'tag', 'all', 'format'): + if location not in self.DEFAULT_LOCATIONS: return set([]) matches = set([]) - locations = ['title', 'author', 'tag', 'format'] if location == 'all' else [location] + all_locs = set(self.DEFAULT_LOCATIONS) - set(['all']) + locations = all_locs if location == 'all' else [location] q = { 'title' : lambda x : getattr(x, 'title').lower(), 'author': lambda x: ' & '.join(getattr(x, 'authors')).lower(), - 'tag':lambda x: ','.join(getattr(x, 'tags')).lower(), + 'collections':lambda x: ','.join(getattr(x, 'device_collections')).lower(), 'format':lambda x: os.path.splitext(x.path)[1].lower() } for index, row in enumerate(self.model.db): @@ -774,7 +788,7 @@ class OnDeviceSearch(SearchQueryParser): # {{{ else: m = matchkind - if locvalue == 'tag': + if locvalue == 'collections': vals = accessor(row).split(',') else: vals = [accessor(row)] @@ -800,14 +814,14 @@ class DeviceBooksModel(BooksModel): # {{{ self.sort_history = [self.sorted_on] self.unknown = _('Unknown') self.column_map = ['inlibrary', 'title', 'authors', 'timestamp', 'size', - 'tags'] + 'collections'] self.headers = { - 'inlibrary' : _('In Library'), - 'title' : _('Title'), - 'authors' : _('Author(s)'), - 'timestamp' : _('Date'), - 'size' : _('Size'), - 'tags' : _('Collections') + 'inlibrary' : _('In Library'), + 'title' : _('Title'), + 'authors' : _('Author(s)'), + 'timestamp' : _('Date'), + 'size' : _('Size'), + 'collections' : _('Collections') } self.marked_for_deletion = {} self.search_engine = OnDeviceSearch(self) @@ -846,7 +860,8 @@ class DeviceBooksModel(BooksModel): # {{{ flags = QAbstractTableModel.flags(self, index) if index.isValid() and self.editable: cname = self.column_map[index.column()] - if cname in ('title', 'authors') or (cname == 'tags' and self.db.supports_tags()): + if cname in ('title', 'authors') or (cname == 'collection' and \ + self.db.supports_collections()): flags |= Qt.ItemIsEditable return flags @@ -918,7 +933,7 @@ class DeviceBooksModel(BooksModel): # {{{ 'authors' : authorcmp, 'size' : sizecmp, 'timestamp': datecmp, - 'tags': tagscmp, + 'collections': tagscmp, 'inlibrary': libcmp, }[cname] self.map.sort(cmp=fcmp, reverse=descending) @@ -1000,14 +1015,15 @@ class DeviceBooksModel(BooksModel): # {{{ dt = self.db[self.map[row]].datetime dt = dt_factory(dt, assume_utc=True, as_utc=False) return QVariant(strftime(TIME_FMT, dt.timetuple())) - elif cname == 'tags': + elif cname == 'collections': tags = self.db[self.map[row]].device_collections if tags: return QVariant(', '.join(tags)) elif role == Qt.ToolTipRole and index.isValid(): if self.map[row] in self.indices_to_be_deleted(): return QVariant(_('Marked for deletion')) - if cname in ['title', 'authors'] or (cname == 'tags' and self.db.supports_tags()): + if cname in ['title', 'authors'] or (cname == 'collections' and \ + self.db.supports_collections()): return QVariant(_("Double click to edit me

")) elif role == Qt.DecorationRole and cname == 'inlibrary': if self.db[self.map[row]].in_library: diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py index d2c3839466..7f6edd1b3d 100644 --- a/src/calibre/gui2/library/views.py +++ b/src/calibre/gui2/library/views.py @@ -222,7 +222,6 @@ class BooksView(QTableView): # {{{ return for col, order in reversed(self.cleanup_sort_history(saved_history)[:3]): self.sortByColumn(self.column_map.index(col), order) - #self.model().sort_history = saved_history def apply_state(self, state): h = self.column_header