Various changes coming from MR discussions:

1) make the give-focus-to-TB a toggle, giving focus to the current view if the TB already has the focus.
2) Ensure that the TB loses the focus when it is hidden.
3) use coloring to mark the focused item in the TB, taking into consideration dark mode. The colors
come from gui2.library.models. I don't really care what color is used.
4) Disable using the platform edit keys to edit TB items. Those keys don't permit asking the question
about whether virtual libraries should be respected. This leaves using the context menu to start editing,
which is how it has been forever.
5) Keep the focus on the TB when one uses RETURN to toggle the item. While there, ensure that
RETURN is valid only if the TB is allowed to have focus.
This commit is contained in:
Charles Haley 2020-10-31 13:09:09 +00:00
parent c363a73083
commit 19c7694108
2 changed files with 35 additions and 16 deletions

View File

@ -688,22 +688,27 @@ class TagBrowserWidget(QFrame): # {{{
def give_tb_focus(self, *args):
if gprefs['tag_browser_allow_keyboard_focus']:
tb = self.tags_view
idx = tb.currentIndex()
if not idx.isValid:
idx = tb.model().createIndex(0, 0)
tb.setCurrentIndex(idx)
tb.setFocus(Qt.OtherFocusReason)
if tb.hasFocus():
self._parent.shift_esc()
elif self._parent.current_view() == self._parent.library_view:
tb.setFocus()
idx = tb.currentIndex()
if not idx.isValid():
idx = tb.model().createIndex(0, 0)
tb.setCurrentIndex(idx)
def set_pane_is_visible(self, to_what):
self.tags_view.set_pane_is_visible(to_what)
if not to_what:
self._parent.shift_esc()
def find_text_changed(self, str):
def find_text_changed(self, str_):
self.current_find_position = None
def set_focus_to_find_box(self):
self.tb_bar.set_focus_to_find_box()
def do_find(self, str=None):
def do_find(self, str_=None):
self.current_find_position = None
self.find()

View File

@ -12,7 +12,7 @@ from functools import partial
from PyQt5.Qt import (
QStyledItemDelegate, Qt, QTreeView, pyqtSignal, QSize, QIcon, QApplication,
QMenu, QPoint, QToolTip, QCursor, QDrag, QRect, QModelIndex,
QLinearGradient, QPalette, QColor, QPen, QBrush, QFont
QLinearGradient, QPalette, QColor, QPen, QBrush, QFont, QTimer
)
from calibre import sanitize_file_name
@ -22,7 +22,8 @@ from calibre.gui2.complete2 import EditWithComplete
from calibre.gui2.tag_browser.model import (TagTreeItem, TAG_SEARCH_STATES,
TagsModel, DRAG_IMAGE_ROLE, COUNT_ROLE)
from calibre.gui2.widgets import EnLineEdit
from calibre.gui2 import config, gprefs, choose_files, pixmap_to_data, rating_font, empty_index
from calibre.gui2 import (config, gprefs, choose_files, pixmap_to_data,
rating_font, empty_index, is_dark_theme)
from calibre.utils.icu import sort_key
from calibre.utils.serialize import json_loads
from polyglot.builtins import unicode_type, range, zip
@ -198,6 +199,10 @@ class TagsView(QTreeView): # {{{
self.set_look_and_feel()
if not gprefs['tag_browser_allow_keyboard_focus']:
self.setFocusPolicy(Qt.NoFocus)
else:
# This is necessary because the context menu sets things up. F2
# and company don't.
self.setEditTriggers(QTreeView.NoEditTriggers)
QApplication.instance().palette_changed.connect(self.set_style_sheet, type=Qt.QueuedConnection)
def set_style_sheet(self):
@ -208,7 +213,7 @@ class TagsView(QTreeView): # {{{
border: none;
}
'''
self.setStyleSheet('''
self.setStyleSheet(('''
QTreeView::item {
border: 1px solid transparent;
padding-top:PADex;
@ -220,8 +225,13 @@ class TagsView(QTreeView): # {{{
border: 1px solid #bfcde4;
border-radius: 6px;
}
'''.replace('PAD', unicode_type(gprefs['tag_browser_item_padding'])) + (
'' if gprefs['tag_browser_old_look'] else stylish_tb))
QTreeView::item:focus
{
background-color:FOCUS_COLOR;
}
'''.replace('PAD', unicode_type(gprefs['tag_browser_item_padding']))
.replace('FOCUS_COLOR', '#027524' if is_dark_theme() else '#b4ecb4')
+ ('' if gprefs['tag_browser_old_look'] else stylish_tb)))
def set_look_and_feel(self):
self.set_style_sheet()
@ -294,10 +304,14 @@ class TagsView(QTreeView): # {{{
self.collapsed.connect(self.collapse_node_and_children)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return and self.state() != self.EditingState:
# I don't see how it can ever not be valid, but ...
if self.currentIndex().isValid():
self.toggle_current_index()
if (gprefs['tag_browser_allow_keyboard_focus'] and event.key() == Qt.Key_Return
and self.state() != self.EditingState
# I don't see how current_index can ever be not valid, but ...
and self.currentIndex().isValid()):
self.toggle_current_index()
# Reset the focus to the TB. Use the singleshot in case
# some of of searching is done using queued signals.
QTimer.singleShot(0, lambda: self.setFocus())
return
QTreeView.keyPressEvent(self, event)