Save and restore split view state

This commit is contained in:
Kovid Goyal 2018-01-31 17:13:34 +05:30
parent ca986e5976
commit 039bdb7c85
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 105 additions and 1 deletions

View File

@ -595,6 +595,8 @@ class BooksView(QTableView): # {{{
if len(self.column_map) > 0 and self.was_restored:
state = self.get_state()
self.write_state(state)
if self.is_library_view:
self.pin_view.save_state()
def cleanup_sort_history(self, sort_history, ignore_column_map=False):
history = []
@ -750,6 +752,9 @@ class BooksView(QTableView): # {{{
old_state['sort_history'] = sh
max_levels = max(3, len(sh))
if self.is_library_view:
self.pin_view.restore_state()
self.column_header.blockSignals(True)
self.apply_state(old_state, max_sort_levels=max_levels)
self.column_header.blockSignals(False)
@ -766,7 +771,10 @@ class BooksView(QTableView): # {{{
# Resize all rows to have the correct height
if not self.row_sizing_done and self.model().rowCount(QModelIndex()) > 0:
vh = self.verticalHeader()
vh.setDefaultSectionSize(max(vh.minimumSectionSize(), self.default_row_height + gprefs['book_list_extra_row_spacing']))
h = max(vh.minimumSectionSize(), self.default_row_height + gprefs['book_list_extra_row_spacing'])
vh.setDefaultSectionSize(h)
if self.is_library_view:
self.pin_view.verticalHeader().setDefaultSectionSize(h)
self._model.set_row_height(self.rowHeight(0))
self.row_sizing_done = True

View File

@ -6,6 +6,8 @@ from __future__ import absolute_import, division, print_function, unicode_litera
from PyQt5.Qt import QSplitter, QTableView
from calibre.gui2.library import DEFAULT_SORT
class PinTableView(QTableView):
@ -14,12 +16,106 @@ class PinTableView(QTableView):
self.books_view = books_view
self.verticalHeader().close()
@property
def column_map(self):
return self.books_view.column_map
def set_context_menu(self, menu):
self.context_menu = menu
def contextMenuEvent(self, event):
self.books_view.show_context_menu(self.context_menu, event)
def get_default_state(self):
old_state = {
'hidden_columns': ['last_modified', 'languages'],
'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] = i
if name != 'ondevice':
old_state['column_sizes'][name] = \
min(350, max(self.sizeHintForColumn(i),
h.sectionSizeHint(i)))
if name in ('timestamp', 'last_modified'):
old_state['column_sizes'][name] += 12
return old_state
def apply_state(self, state):
h = self.column_header
cmap = {}
hidden = state.get('hidden_columns', [])
for i, c in enumerate(self.column_map):
cmap[c] = i
if c != 'ondevice':
h.setSectionHidden(i, c in hidden)
positions = state.get('column_positions', {})
pmap = {}
for col, pos in positions.items():
if col in cmap:
pmap[pos] = col
for pos in sorted(pmap.keys()):
col = pmap[pos]
idx = cmap[col]
current_pos = h.visualIndex(idx)
if current_pos != pos:
h.moveSection(current_pos, pos)
# Because of a bug in Qt 5 we have to ensure that the header is actually
# relaid out by changing this value, without this sometimes ghost
# columns remain visible when changing libraries
for i in xrange(h.count()):
val = h.isSectionHidden(i)
h.setSectionHidden(i, not val)
h.setSectionHidden(i, val)
sizes = state.get('column_sizes', {})
for col, size in sizes.items():
if col in cmap:
sz = sizes[col]
if sz < 3:
sz = h.sectionSizeHint(cmap[col])
h.resizeSection(cmap[col], sz)
for i in range(h.count()):
if not h.isSectionHidden(i) and h.sectionSize(i) < 3:
sz = h.sectionSizeHint(i)
h.resizeSection(i, sz)
def get_state(self):
h = self.column_header
cm = self.column_map
state = {}
state['hidden_columns'] = [cm[i] for i in range(h.count())
if h.isSectionHidden(i) and cm[i] != 'ondevice']
state['column_positions'] = {}
state['column_sizes'] = {}
for i in range(h.count()):
name = cm[i]
state['column_positions'][name] = h.visualIndex(i)
if name != 'ondevice':
state['column_sizes'][name] = h.sectionSize(i)
return state
def save_state(self):
db = getattr(self.model(), 'db', None)
if db is not None:
state = self.get_state()
db.new_api.set_pref('books view split pane state', state)
def restore_state(self):
db = getattr(self.model(), 'db', None)
if db is not None:
state = db.prefs.get('books view split pane state', None)
if state:
self.apply_state(state)
class PinContainer(QSplitter):