Skip to content

Commit

Permalink
Simplify and add possibility to op-out keybinding in tooltip. Fix usa…
Browse files Browse the repository at this point in the history
…ge of single key as key combination with Qt6
  • Loading branch information
dalthviz committed Aug 15, 2024
1 parent 56b7a09 commit b994559
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
12 changes: 4 additions & 8 deletions src/app_model/backends/qt/_qaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ def _on_triggered(self, checked: bool) -> None:
# to raise any exceptions.
self._app.commands.execute_command(self._command_id).result()

def setToolTip(self, tooltip: str) -> None:
"""Override Qt method to append keybinding information to tooltip."""
tooltip_with_keybinding = f"{tooltip} {self._keybinding_tooltip}".rstrip()
super().setToolTip(tooltip_with_keybinding)


class QCommandRuleAction(QCommandAction):
"""QAction for a CommandRule.
Expand Down Expand Up @@ -91,9 +86,6 @@ def __init__(
self.setText(command_rule.short_title) # pragma: no cover
else:
self.setText(command_rule.title)
# Since the default tooltip value is the action `text`, call `setToolTip`
# logic to include keybinding information.
self.setToolTip(self.text())
if command_rule.icon:
self.setIcon(to_qicon(command_rule.icon))
self.setIconVisibleInMenu(command_rule.icon_visible_in_menu)
Expand All @@ -104,6 +96,10 @@ def __init__(
if command_rule.toggled is not None:
self.setCheckable(True)
self._refresh()
tooltip_with_keybinding = (
f"{self.toolTip()} {self._keybinding_tooltip}".rstrip()
)
self.setToolTip(tooltip_with_keybinding)

def update_from_context(self, ctx: Mapping[str, object]) -> None:
"""Update the enabled state of this menu item from `ctx`."""
Expand Down
4 changes: 3 additions & 1 deletion src/app_model/backends/qt/_qkeymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def simple_keybinding_to_qint(skb: SimpleKeyBinding) -> int:
)
key = modelkey2qkey(skb.key) if skb.key else Qt.Key.Key_unknown
mods = (v for k, v in lookup.items() if getattr(skb, k))
combo = QKeyCombination(reduce(operator.or_, mods), key)
combo = QKeyCombination(
reduce(operator.or_, mods, Qt.KeyboardModifier.NoModifier), key
)
return int(combo.toCombined())

else:
Expand Down
19 changes: 15 additions & 4 deletions tests/test_qt/test_qactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,22 @@ def test_tooltip(


@pytest.mark.parametrize(
("tooltip", "expected_tooltip"),
("tooltip", "tooltip_with_keybinding", "tooltip_without_keybinding"),
[
("", "Test keybinding tooltip (K)"),
("", "Test keybinding tooltip (K)", "Test keybinding tooltip"),
(
"Test action with a tooltip and a keybinding",
"Test action with a tooltip and a keybinding (K)",
"Test action with a tooltip and a keybinding",
),
],
)
def test_keybinding_in_tooltip(
qapp, simple_app: "Application", tooltip: str, expected_tooltip: str
qapp,
simple_app: "Application",
tooltip: str,
tooltip_with_keybinding: str,
tooltip_without_keybinding: str,
) -> None:
action = Action(
id="test.keybinding.tooltip",
Expand All @@ -114,5 +119,11 @@ def test_keybinding_in_tooltip(
keybindings=[KeyBindingRule(primary=KeyCode.KeyK)],
)
simple_app.register_action(action)

# check initial action instance shows keybinding info in its tooltip if available
q_action = QCommandRuleAction(action, simple_app)
assert q_action.toolTip() == expected_tooltip
assert q_action.toolTip() == tooltip_with_keybinding

# check setting tooltip manually removes keybinding info
q_action.setToolTip(tooltip)
assert q_action.toolTip() == tooltip_without_keybinding

0 comments on commit b994559

Please sign in to comment.