Rationalize collections column in device view

This commit is contained in:
Kovid Goyal 2010-05-20 10:28:41 -06:00
parent a28c63dc1f
commit 036c2fe68c
5 changed files with 47 additions and 20 deletions

View File

@ -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):

View File

@ -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']

View File

@ -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([])

View File

@ -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 <b>edit</b> me<br><br>"))
elif role == Qt.DecorationRole and cname == 'inlibrary':
if self.db[self.map[row]].in_library:

View File

@ -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