Link up save/restore column layout functionality

This commit is contained in:
Kovid Goyal 2010-05-17 23:08:30 -06:00
parent 428cebd365
commit e0e0093fe5
5 changed files with 39 additions and 23 deletions

View File

@ -7,4 +7,4 @@ __docformat__ = 'restructuredtext en'
from PyQt4.Qt import Qt from PyQt4.Qt import Qt
DEFAULT_SORT = ('timestamp', Qt.AscendingOrder) DEFAULT_SORT = ('timestamp', Qt.DescendingOrder)

View File

@ -231,7 +231,7 @@ class BooksModel(QAbstractTableModel): # {{{
def resort(self, reset=True): def resort(self, reset=True):
try: try:
col = self.column_map.index(self.sorted_on[0]) col = self.column_map.index(self.sorted_on[0])
except: except ValueError:
col = 0 col = 0
self.sort(col, self.sorted_on[1], reset=reset) self.sort(col, self.sorted_on[1], reset=reset)
@ -853,9 +853,6 @@ class DeviceBooksModel(BooksModel): # {{{
self.searched.emit(False) self.searched.emit(False)
def resort(self, reset):
self.sort(self.sorted_on[0], self.sorted_on[1], reset=reset)
def sort(self, col, order, reset=True): def sort(self, col, order, reset=True):
descending = order != Qt.AscendingOrder descending = order != Qt.AscendingOrder
def strcmp(attr): def strcmp(attr):

View File

@ -49,8 +49,8 @@ class BooksView(QTableView): # {{{
self.column_header.sectionMoved.connect(self.save_state) self.column_header.sectionMoved.connect(self.save_state)
self.column_header.setContextMenuPolicy(Qt.CustomContextMenu) self.column_header.setContextMenuPolicy(Qt.CustomContextMenu)
self.column_header.customContextMenuRequested.connect(self.show_column_header_context_menu) self.column_header.customContextMenuRequested.connect(self.show_column_header_context_menu)
# }}} # }}}
self._model.database_changed.connect(self.database_changed) self._model.database_changed.connect(self.database_changed)
hv = self.verticalHeader() hv = self.verticalHeader()
hv.setClickable(True) hv.setClickable(True)
@ -76,6 +76,8 @@ class BooksView(QTableView): # {{{
self.sortByColumn(idx, Qt.AscendingOrder) self.sortByColumn(idx, Qt.AscendingOrder)
elif action == 'descending': elif action == 'descending':
self.sortByColumn(idx, Qt.DescendingOrder) self.sortByColumn(idx, Qt.DescendingOrder)
elif action == 'defaults':
self.apply_state(self.get_default_state())
self.save_state() self.save_state()
@ -117,6 +119,13 @@ class BooksView(QTableView): # {{{
m.addAction(name, m.addAction(name,
partial(self.column_header_context_handler, partial(self.column_header_context_handler,
action='show', column=col)) action='show', column=col))
self.column_header_context_menu.addSeparator()
self.column_header_context_menu.addAction(
_('Restore default layout'),
partial(self.column_header_context_handler,
action='defaults', column=col))
self.column_header_context_menu.popup(self.column_header.mapToGlobal(pos)) self.column_header_context_menu.popup(self.column_header.mapToGlobal(pos))
@ -209,25 +218,30 @@ class BooksView(QTableView): # {{{
h.resizeSection(cmap[col], sizes[col]) h.resizeSection(cmap[col], sizes[col])
self.apply_sort_history(state.get('sort_history', None)) self.apply_sort_history(state.get('sort_history', None))
def get_default_state(self):
old_state = {'hidden_columns': [],
'sort_history':[DEFAULT_SORT],
'column_positions': {},
'column_sizes': {}}
h = self.column_header
cm = self.column_map
for i in range(h.count()):
name = cm[i]
old_state['column_positions'][name] = h.logicalIndex(i)
if name != 'ondevice':
old_state['column_sizes'][name] = \
max(self.sizeHintForColumn(i), h.sectionSizeHint(i))
if name == 'timestamp':
old_state['column_sizes'][name] += 12
return old_state
def restore_state(self): def restore_state(self):
name = unicode(self.objectName()) name = unicode(self.objectName())
old_state = None old_state = None
if name: if name:
old_state = gprefs.get(name + ' books view state', None) old_state = gprefs.get(name + ' books view state', None)
if old_state is None: if old_state is None:
# Default layout old_state = self.get_default_state()
old_state = {'hidden_columns': [],
'sort_history':[DEFAULT_SORT],
'column_positions': {},
'column_sizes': {}}
h = self.column_header
cm = self.column_map
for i in range(h.count()):
name = cm[i]
old_state['column_positions'][name] = h.logicalIndex(i)
if name != 'ondevice':
old_state['column_sizes'][name] = \
max(self.sizeHintForColumn(i), h.sectionSizeHint(i))
if tweaks['sort_columns_at_startup'] is not None: if tweaks['sort_columns_at_startup'] is not None:
old_state['sort_history'] = tweaks['sort_columns_at_startup'] old_state['sort_history'] = tweaks['sort_columns_at_startup']
@ -379,6 +393,7 @@ class DeviceBooksView(BooksView): # {{{
def set_database(self, db): def set_database(self, db):
self._model.set_database(db) self._model.set_database(db)
self.restore_state()
def resizeColumnsToContents(self): def resizeColumnsToContents(self):
QTableView.resizeColumnsToContents(self) QTableView.resizeColumnsToContents(self)

View File

@ -2408,6 +2408,9 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI):
config.set('main_window_geometry', self.saveGeometry()) config.set('main_window_geometry', self.saveGeometry())
dynamic.set('sort_history', self.library_view.model().sort_history) dynamic.set('sort_history', self.library_view.model().sort_history)
self.sidebar.save_state() self.sidebar.save_state()
for view in ('library_view', 'memory_view', 'card_a_view',
'card_b_view'):
getattr(self, view).save_state()
def restart(self): def restart(self):
self.quit(restart=True) self.quit(restart=True)

View File

@ -182,13 +182,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
columns = ['id', 'title', columns = ['id', 'title',
# col table link_col query # col table link_col query
('authors', 'authors', 'author', 'sortconcat(link.id, name)'), ('authors', 'authors', 'author', 'sortconcat(link.id, name)'),
('publisher', 'publishers', 'publisher', 'name'),
('rating', 'ratings', 'rating', 'ratings.rating'),
'timestamp', 'timestamp',
'(SELECT MAX(uncompressed_size) FROM data WHERE book=books.id) size', '(SELECT MAX(uncompressed_size) FROM data WHERE book=books.id) size',
('rating', 'ratings', 'rating', 'ratings.rating'),
('tags', 'tags', 'tag', 'group_concat(name)'), ('tags', 'tags', 'tag', 'group_concat(name)'),
'(SELECT text FROM comments WHERE book=books.id) comments', '(SELECT text FROM comments WHERE book=books.id) comments',
('series', 'series', 'series', 'name'), ('series', 'series', 'series', 'name'),
('publisher', 'publishers', 'publisher', 'name'),
'series_index', 'series_index',
'sort', 'sort',
'author_sort', 'author_sort',
@ -212,8 +212,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
custom_cols = list(sorted(custom_map.keys())) custom_cols = list(sorted(custom_map.keys()))
lines.extend([custom_map[x] for x in custom_cols]) lines.extend([custom_map[x] for x in custom_cols])
self.FIELD_MAP = {'id':0, 'title':1, 'authors':2, 'publisher':3, 'rating':4, 'timestamp':5, self.FIELD_MAP = {'id':0, 'title':1, 'authors':2, 'timestamp':3,
'size':6, 'tags':7, 'comments':8, 'series':9, 'series_index':10, 'size':4, 'rating':5, 'tags':6, 'comments':7, 'series':8,
'publisher':9, 'series_index':10,
'sort':11, 'author_sort':12, 'formats':13, 'isbn':14, 'path':15, 'sort':11, 'author_sort':12, 'formats':13, 'isbn':14, 'path':15,
'lccn':16, 'pubdate':17, 'flags':18, 'uuid':19} 'lccn':16, 'pubdate':17, 'flags':18, 'uuid':19}