Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prevent selection of suffix text in preferences spinboxes #3842

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ Mike Hardy <[email protected]>
Danika_Dakika <https://github.com/Danika-Dakika>
Mumtaz Hajjo Alrifai <[email protected]>
Jakub Fidler <[email protected]>
Adnane Taghi <[email protected]>

********************

Expand Down
1 change: 1 addition & 0 deletions qt/aqt/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def on_dialog_destroyed() -> None:
"Mumtaz Hajjo Alrifai",
"Luc Mcgrady",
"Brayan Oliveira",
"Adnane Taghi",
)
)

Expand Down
31 changes: 31 additions & 0 deletions qt/aqt/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,37 @@ def __init__(self, mw: AnkiQt) -> None:
):
spinbox.setSuffix(f" {spinbox.suffix()}")

# Prevent selecting spinboxes' suffixes or placing the cursor after them
class UnitSpinBoxEventFilter(QObject):
def eventFilter(
self, spinbox: QObject | None, evt: QEvent | None
) -> bool:
if not isinstance(spinbox, QSpinBox) or not isinstance(evt, QEvent):
return False
# maximum position of the cursor
posAfterValue = len(str(spinbox.value()))
lineEdit = spinbox.lineEdit()
if lineEdit is None:
return False
if (
evt.type() == QEvent.Type.InputMethodQuery
and lineEdit.cursorPosition() > posAfterValue
):
if lineEdit.hasSelectedText():
absoluteSelectionEnd = (
lineEdit.selectionStart() + lineEdit.selectionLength()
)
if absoluteSelectionEnd > posAfterValue:
lineEdit.setSelection(
lineEdit.selectionStart(),
posAfterValue - lineEdit.selectionStart(),
)
else:
lineEdit.setCursorPosition(posAfterValue)
return False

spinbox.installEventFilter(UnitSpinBoxEventFilter(mw))

disable_help_button(self)
help_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Help)
assert help_button is not None
Expand Down