Implement header context menu for split view

This commit is contained in:
Kovid Goyal 2018-01-31 21:15:17 +05:30
parent cd40155272
commit 1c20f656d2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -300,11 +300,12 @@ class BooksView(QTableView): # {{{
self.column_header.sortIndicatorChanged.connect(self.user_sort_requested) self.column_header.sortIndicatorChanged.connect(self.user_sort_requested)
self.pin_view.column_header.sortIndicatorChanged.disconnect() self.pin_view.column_header.sortIndicatorChanged.disconnect()
self.pin_view.column_header.sortIndicatorChanged.connect(self.pin_view_user_sort_requested) self.pin_view.column_header.sortIndicatorChanged.connect(self.pin_view_user_sort_requested)
self.column_header.customContextMenuRequested.connect(self.show_column_header_context_menu) self.column_header.customContextMenuRequested.connect(partial(self.show_column_header_context_menu, view=self))
self.column_header.sectionResized.connect(self.column_resized, Qt.QueuedConnection) self.column_header.sectionResized.connect(self.column_resized, Qt.QueuedConnection)
if self.is_library_view: if self.is_library_view:
self.pin_view.column_header.sectionResized.connect(self.pin_view_column_resized, Qt.QueuedConnection) self.pin_view.column_header.sectionResized.connect(self.pin_view_column_resized, Qt.QueuedConnection)
self.pin_view.column_header.sectionMoved.connect(self.pin_view.save_state) self.pin_view.column_header.sectionMoved.connect(self.pin_view.save_state)
self.pin_view.column_header.customContextMenuRequested.connect(partial(self.show_column_header_context_menu, view=self.pin_view))
self.row_header = HeaderView(Qt.Vertical, self) self.row_header = HeaderView(Qt.Vertical, self)
self.row_header.setSectionResizeMode(self.row_header.Fixed) self.row_header.setSectionResizeMode(self.row_header.Fixed)
self.setVerticalHeader(self.row_header) self.setVerticalHeader(self.row_header)
@ -343,19 +344,19 @@ class BooksView(QTableView): # {{{
self.allow_mirroring = True self.allow_mirroring = True
# Column Header Context Menu {{{ # Column Header Context Menu {{{
def column_header_context_handler(self, action=None, column=None): def column_header_context_handler(self, action=None, column=None, view=None):
if not action or not column:
return
if action == 'split': if action == 'split':
self.set_pin_view_visibility(not self.pin_view.isVisible()) self.set_pin_view_visibility(not self.pin_view.isVisible())
gprefs['book_list_split'] = self.pin_view.isVisible() gprefs['book_list_split'] = self.pin_view.isVisible()
self.save_state() self.save_state()
return return
if not action or not column or not view:
return
try: try:
idx = self.column_map.index(column) idx = self.column_map.index(column)
except: except:
return return
h = self.column_header h = view.column_header
if action == 'hide': if action == 'hide':
if h.hiddenSectionCount() >= h.count(): if h.hiddenSectionCount() >= h.count():
@ -372,7 +373,7 @@ class BooksView(QTableView): # {{{
elif action == 'descending': elif action == 'descending':
self.sort_by_column_and_order(idx, False) self.sort_by_column_and_order(idx, False)
elif action == 'defaults': elif action == 'defaults':
self.apply_state(self.get_default_state()) view.apply_state(view.get_default_state())
elif action == 'addcustcol': elif action == 'addcustcol':
self.add_column_signal.emit() self.add_column_signal.emit()
elif action.startswith('align_'): elif action.startswith('align_'):
@ -391,26 +392,14 @@ class BooksView(QTableView): # {{{
self.save_state() self.save_state()
def show_column_header_context_menu(self, pos): def create_context_menu(self, col, name, view):
idx = self.column_header.logicalIndexAt(pos) ans = QMenu(view)
if idx > -1 and idx < len(self.column_map): handler = partial(self.column_header_context_handler, view=view, column=col)
col = self.column_map[idx] if col not in ('ondevice', 'inlibrary'):
name = unicode(self.model().headerData(idx, Qt.Horizontal, ans.addAction(_('Hide column %s') % name, partial(handler, action='hide'))
Qt.DisplayRole) or '') m = ans.addMenu(_('Sort on %s') % name)
self.column_header_context_menu = QMenu(self) a = m.addAction(_('Ascending'), partial(handler, action='ascending'))
if col != 'ondevice': d = m.addAction(_('Descending'), partial(handler, action='descending'))
self.column_header_context_menu.addAction(_('Hide column %s') %
name,
partial(self.column_header_context_handler, action='hide',
column=col))
m = self.column_header_context_menu.addMenu(
_('Sort on %s') % name)
a = m.addAction(_('Ascending'),
partial(self.column_header_context_handler,
action='ascending', column=col))
d = m.addAction(_('Descending'),
partial(self.column_header_context_handler,
action='descending', column=col))
if self._model.sorted_on[0] == col: if self._model.sorted_on[0] == col:
ac = a if self._model.sorted_on[1] else d ac = a if self._model.sorted_on[1] else d
ac.setCheckable(True) ac.setCheckable(True)
@ -419,68 +408,51 @@ class BooksView(QTableView): # {{{
(not self.model().is_custom_column(col) or (not self.model().is_custom_column(col) or
self.model().custom_columns[col]['datatype'] not in ('bool', self.model().custom_columns[col]['datatype'] not in ('bool',
)): )):
m = self.column_header_context_menu.addMenu( m = ans.addMenu(_('Change text alignment for %s') % name)
_('Change text alignment for %s') % name)
al = self._model.alignment_map.get(col, 'left') al = self._model.alignment_map.get(col, 'left')
for x, t in (('left', _('Left')), ('right', _('Right')), ('center', for x, t in (('left', _('Left')), ('right', _('Right')), ('center', _('Center'))):
_('Center'))): a = m.addAction(t, partial(handler, action='align_'+x))
a = m.addAction(t,
partial(self.column_header_context_handler,
action='align_'+x, column=col))
if al == x: if al == x:
a.setCheckable(True) a.setCheckable(True)
a.setChecked(True) a.setChecked(True)
if self.is_library_view:
if not isinstance(self, DeviceBooksView):
if self._model.db.field_metadata[col]['is_category']: if self._model.db.field_metadata[col]['is_category']:
act = self.column_header_context_menu.addAction(_('Quickview column %s') % act = ans.addAction(_('Quickview column %s') % name, partial(handler, action='quickview'))
name,
partial(self.column_header_context_handler, action='quickview',
column=col))
rows = self.selectionModel().selectedRows() rows = self.selectionModel().selectedRows()
if len(rows) > 1: if len(rows) > 1:
act.setEnabled(False) act.setEnabled(False)
hidden_cols = [self.column_map[i] for i in hidden_cols = {self.column_map[i]: i for i in range(view.column_header.count())
range(self.column_header.count()) if if view.column_header.isSectionHidden(i) and self.column_map[i] not in ('ondevice', 'inlibrary')}
self.column_header.isSectionHidden(i)]
try: ans.addSeparator()
hidden_cols.remove('ondevice')
except:
pass
if hidden_cols: if hidden_cols:
self.column_header_context_menu.addSeparator() m = ans.addMenu(_('Show column'))
m = self.column_header_context_menu.addMenu(_('Show column')) for hcol, hidx in hidden_cols.iteritems():
for col in hidden_cols: hname = unicode(self.model().headerData(hidx, Qt.Horizontal, Qt.DisplayRole) or '')
hidx = self.column_map.index(col) m.addAction(hname, partial(handler, action='show', column=hcol))
name = unicode(self.model().headerData(hidx, Qt.Horizontal, ans.addSeparator()
Qt.DisplayRole) or '') ans.addAction(_('Shrink column if it is too wide to fit'),
m.addAction(name, partial(self.resize_column_to_fit, view, col))
partial(self.column_header_context_handler, ans.addAction(_('Restore default layout'), partial(handler, action='defaults'))
action='show', column=col))
self.column_header_context_menu.addSeparator()
self.column_header_context_menu.addAction(
_('Shrink column if it is too wide to fit'),
partial(self.resize_column_to_fit, column=self.column_map[idx]))
self.column_header_context_menu.addAction(
_('Restore default layout'),
partial(self.column_header_context_handler,
action='defaults', column=col))
if self.can_add_columns: if self.can_add_columns:
self.column_header_context_menu.addAction( ans.addAction(
QIcon(I('column.png')), QIcon(I('column.png')), _('Add your own columns'), partial(handler, action='addcustcol'))
_('Add your own columns'), return ans
partial(self.column_header_context_handler,
action='addcustcol', column=col))
def show_column_header_context_menu(self, pos, view=None):
view = view or self
idx = view.column_header.logicalIndexAt(pos)
if idx > -1 and idx < len(self.column_map):
col = self.column_map[idx]
name = unicode(self.model().headerData(idx, Qt.Horizontal, Qt.DisplayRole) or '')
view.column_header_context_menu = self.create_context_menu(col, name, view)
if self.is_library_view: if self.is_library_view:
self.column_header_context_menu.addSeparator() view.column_header_context_menu.addSeparator()
self.column_header_context_menu.addAction( view.column_header_context_menu.addAction(
_('Un-split the book list') if self.pin_view.isVisible() else _('Split the book list'), _('Un-split the book list') if self.pin_view.isVisible() else _('Split the book list'),
partial(self.column_header_context_handler, action='split', column=col or 'title')) partial(self.column_header_context_handler, action='split', column=col or 'title'))
self.column_header_context_menu.popup(self.column_header.mapToGlobal(pos)) view.column_header_context_menu.popup(view.column_header.mapToGlobal(pos))
# }}} # }}}
# Sorting {{{ # Sorting {{{
@ -799,9 +771,10 @@ class BooksView(QTableView): # {{{
self._model.set_row_height(self.rowHeight(0)) self._model.set_row_height(self.rowHeight(0))
self.row_sizing_done = True self.row_sizing_done = True
def resize_column_to_fit(self, column): def resize_column_to_fit(self, view, column):
col = self.column_map.index(column) col = self.column_map.index(column)
self.column_resized(col, self.columnWidth(col), self.columnWidth(col)) w = view.columnWidth(col)
restrict_column_width(view, col, w, w)
def column_resized(self, col, old_size, new_size): def column_resized(self, col, old_size, new_size):
restrict_column_width(self, col, old_size, new_size) restrict_column_width(self, col, old_size, new_size)