This commit works the way I want, ensuring the checked button is focused while using 'vanilla' Qt. It doesn't work around the odd behavior of backtab.

Apologies for the multiple commits. I don't know what happens if I remove commits in a pull request so I left them all.
This commit is contained in:
Charles Haley 2022-06-05 21:37:47 +01:00
parent 8d3d28da09
commit 9e5b656bd5

View File

@ -13,7 +13,7 @@ from functools import partial
from qt.core import (
QDialog, Qt, QColor, QIcon, QVBoxLayout, QLabel, QGridLayout,
QDialogButtonBox, QWidget, QLineEdit, QHBoxLayout, QComboBox,
QCheckBox, QSpinBox, QRadioButton, QGroupBox, pyqtSignal
QCheckBox, QSpinBox, QRadioButton, QGroupBox
)
from calibre.gui2 import error_dialog
@ -22,22 +22,6 @@ from calibre.utils.date import parse_date, UNDEFINED_DATE
from polyglot.builtins import iteritems
class GroupBox(QGroupBox):
# A group box for radio buttons that acts passes along focus events so the
# correct child button is given the focus
got_focus = pyqtSignal()
def focusInEvent(self, ev):
# We need this backtab focus stuff because a backtab from a radio button
# gives the focus back to the group box instead of the next widget in the
# focus chain. For some reason a tab doesn't do that.
if ev.reason() != Qt.FocusReason.BacktabFocusReason:
self.got_focus.emit()
else:
self.focusNextPrevChild(False)
class CreateCustomColumn(QDialog):
# Note: in this class, we are treating is_multiple as the boolean that
@ -193,11 +177,11 @@ class CreateCustomColumn(QDialog):
icon = bool(c['display'].get('bools_show_icons', True))
txt = bool(c['display'].get('bools_show_text', False))
if icon and txt:
self.bool_show_both_button.setChecked(True)
self.bool_show_both_button.click()
elif icon:
self.bool_show_icon_button.setChecked(True)
self.bool_show_icon_button.click()
else:
self.bool_show_text_button.setChecked(True)
self.bool_show_text_button.click()
# Default values
dv = c['display'].get('default_value', None)
@ -342,15 +326,16 @@ class CreateCustomColumn(QDialog):
# bool formatting
h1 = QHBoxLayout()
self.bool_show_icon_button = QRadioButton(_('&Icon'))
h1.addWidget(self.bool_show_icon_button)
self.bool_show_text_button = QRadioButton(_('&Text'))
h1.addWidget(self.bool_show_text_button)
self.bool_show_both_button = QRadioButton(_('&Both'))
h1.addWidget(self.bool_show_both_button)
self.bool_button_group = GroupBox()
self.bool_button_group.setFocusPolicy(Qt.StrongFocus)
self.bool_button_group.got_focus.connect(self.bool_groupbox_focused)
def add_bool_radio_button(txt):
b = QRadioButton(txt)
b.clicked.connect(partial(self.bool_radio_button_clicked, b))
h1.addWidget(b)
return b
self.bool_show_icon_button = add_bool_radio_button(_('&Icon'))
self.bool_show_text_button = add_bool_radio_button(_('&Text'))
self.bool_show_both_button = add_bool_radio_button(_('&Both'))
self.bool_button_group = QGroupBox()
self.bool_button_group.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
self.bool_button_group.setLayout(h1)
h = QHBoxLayout()
h.addWidget(self.bool_button_group)
@ -470,13 +455,9 @@ class CreateCustomColumn(QDialog):
self.resize(self.sizeHint())
# }}}
def bool_groupbox_focused(self):
if self.bool_show_icon_button.isChecked():
self.bool_show_icon_button.setFocus()
elif self.bool_show_text_button.isChecked():
self.bool_show_text_button.setFocus()
else:
self.bool_show_both_button.setFocus()
def bool_radio_button_clicked(self, button, clicked):
if clicked:
self.bool_button_group.setFocusProxy(button)
def datatype_changed(self, *args):
try: