From 25de03bb8f991b184460c16e747ab7dacc159739 Mon Sep 17 00:00:00 2001 From: Hanjin Liu Date: Mon, 16 Sep 2024 22:23:57 +0900 Subject: [PATCH] fix some keyevents --- magicclass/utils/qt.py | 2 +- magicclass/widgets/eval.py | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/magicclass/utils/qt.py b/magicclass/utils/qt.py index db168e43..31031c62 100644 --- a/magicclass/utils/qt.py +++ b/magicclass/utils/qt.py @@ -48,7 +48,7 @@ def show_messagebox( """ show_dialog = _QMESSAGE_MODES[MessageBoxMode(mode)] result = show_dialog(parent, title, text) - return result in (QMessageBox.Ok, QMessageBox.Yes) + return result in (QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Yes) def open_url(link: str) -> None: diff --git a/magicclass/widgets/eval.py b/magicclass/widgets/eval.py index b84054ee..db517935 100644 --- a/magicclass/widgets/eval.py +++ b/magicclass/widgets/eval.py @@ -12,6 +12,8 @@ from magicgui.widgets.bases import ValueWidget from magicclass.widgets._const import FONT +_BUILTINS = "__builtins__" + def _get_last_group(text: str) -> str | None: if text.endswith(" "): @@ -96,6 +98,19 @@ def setFirst(self): self.setCurrentRow(0) +_EVENTS_TO_HIDE = frozenset( + [QtCore.QEvent.Type.Move, QtCore.QEvent.Type.Resize, QtCore.QEvent.Type.Hide] +) + + +def _is_int(s: str) -> bool: + try: + int(s) + return True + except ValueError: + return False + + class QEvalLineEdit(QtW.QLineEdit): def __init__(self, parent=None): super().__init__(parent) @@ -111,7 +126,7 @@ def namespace(self) -> dict[str, Any]: def setNamespace(self, ns: Mapping[str, Any]): self._namespace = dict(ns) - self._namespace["__builtins__"] = {} # for safety + self._namespace.setdefault(_BUILTINS, {}) # for safety def setAutoSuggest(self, auto_suggest: bool): self._auto_suggest = auto_suggest @@ -149,6 +164,11 @@ def _create_list_widget(self): self._list_widget.setCurrentRow(0) self.setFocus() + def _has_completion(self) -> bool: + if self._list_widget is None: + return False + return self._list_widget.count() > 0 + def _get_completion_list(self, text: str) -> tuple[str, list[str]]: last_found = _get_last_group(text) if last_found is None: @@ -156,12 +176,14 @@ def _get_completion_list(self, text: str) -> tuple[str, list[str]]: ns = self._namespace if len(last_found) == 0: - return "", list(k for k in ns.keys() if k != "__builtins__") + return "", list(k for k in ns.keys() if k != _BUILTINS) *strs, last = last_found.split(".") if len(strs) == 0: return last, [ - k for k in ns.keys() if k.startswith(last_found) and k != "__builtins__" + k for k in ns.keys() if k.startswith(last_found) and k != _BUILTINS ] + elif len(strs) == 1 and _is_int(strs[0]): + return last, [] else: try: val = eval(".".join(strs), self.namespace(), {}) @@ -211,8 +233,9 @@ def event(self, event: QtCore.QEvent): if event.type() == QtCore.QEvent.Type.KeyPress: assert isinstance(event, QtGui.QKeyEvent) if event.key() == Qt.Key.Key_Tab: - self.show_completion() - return True + if self._has_completion(): + self.show_completion() + return True elif event.key() == Qt.Key.Key_Down: if self._list_widget is not None: if event.modifiers() == Qt.KeyboardModifier.NoModifier: @@ -248,7 +271,7 @@ def event(self, event: QtCore.QEvent): self._list_widget.deleteLater() self._list_widget = None return True - elif event.type() == QtCore.QEvent.Type.Move: + elif event.type() in _EVENTS_TO_HIDE: if self._list_widget is not None: self._list_widget.close() self._list_widget = None