diff --git a/PyReconstruct/modules/gui/main/field_widget.py b/PyReconstruct/modules/gui/main/field_widget.py index 3aa2e9b0d..d7d9c598a 100644 --- a/PyReconstruct/modules/gui/main/field_widget.py +++ b/PyReconstruct/modules/gui/main/field_widget.py @@ -5,6 +5,7 @@ import time from PySide6.QtWidgets import ( + QLabel, QMainWindow, QWidget, QGestureEvent, @@ -99,6 +100,18 @@ def __init__(self, series : Series, mainwindow : QMainWindow): self.mouse_mode = POINTER self.setCursor(QCursor(Qt.ArrowCursor)) + # focus mode label, shown and placed by updateFocusHint. Its own style + # sheet so a theme's QLabel rule does not repaint it; plain text so an + # object name is never read as markup; clicks go through to the field. + self.focus_hint = QLabel(self) + self.focus_hint.setTextFormat(Qt.PlainText) + self.focus_hint.setAttribute(Qt.WA_TransparentForMouseEvents) + self.focus_hint.setStyleSheet( + "QLabel { background-color: rgba(0, 0, 0, 170); color: white;" + " border-radius: 4px; padding: 4px 8px; }" + ) + self.focus_hint.hide() + self.createField(series) self.show() diff --git a/PyReconstruct/modules/gui/main/field_widget_1_base.py b/PyReconstruct/modules/gui/main/field_widget_1_base.py index f29b57e75..a66d1b150 100644 --- a/PyReconstruct/modules/gui/main/field_widget_1_base.py +++ b/PyReconstruct/modules/gui/main/field_widget_1_base.py @@ -2,13 +2,15 @@ import time from PySide6.QtWidgets import ( + QLabel, QMainWindow, QTextEdit, ) from PySide6.QtGui import ( QPainter, QCursor, - QAction + QAction, + QKeySequence, ) from PySide6.QtCore import ( QTimer, @@ -48,6 +50,8 @@ def initAttrs(self, series : Series, mainwindow : QMainWindow): self.propagate_tform : bool = False self.focus_mode : bool = False + # label over the field naming the focused object; built by FieldWidget + self.focus_hint : QLabel = None self.hide_trace_layer : bool = False self.show_all_traces : bool = False self.hide_image : bool = False @@ -326,11 +330,49 @@ def generateView(self, generate_image=True, generate_traces=True, update=True) - self.mainwindow.mouse_palette.setScale() self.mainwindow.checkActions() + # every focus mode change and every resize ends in a redraw + self.updateFocusHint() + if update: self.update() if generate_image: self.is_image_loading = False + + def focusHintText(self) -> str: + """The focus mode label's text, naming the live `focus_act` shortcut.""" + act = getattr(self.mainwindow, "focus_act", None) + key = act.shortcut().toString(QKeySequence.NativeText) if act is not None else "" + if key: + how = f"Press {key} to exit." + else: + how = "Use View > Toggle focus mode in the right-click menu to exit." + return f"Focus: {self.focus_mode}. {how}" + + def updateFocusHint(self) -> None: + """Show the focus mode label while focus mode is on, centered at the top.""" + hint = getattr(self, "focus_hint", None) + if hint is None: + return + if not self.focus_mode: + hint.hide() + return + text = self.focusHintText() + if hint.text() != text: + hint.setText(text) + # one line when it fits, wrapped to the field's width when it does not + hint.setWordWrap(False) + w = hint.sizeHint().width() + max_w = max(self.width() - 20, 1) + if w > max_w: + hint.setWordWrap(True) + w = max_w + hint.resize(w, hint.heightForWidth(w)) + else: + hint.resize(w, hint.sizeHint().height()) + hint.move((self.width() - w) // 2, 10) + hint.show() + hint.raise_() def clearStates(self) -> None: """Create/clear the states for each section.""" diff --git a/PyReconstruct/modules/gui/main/main_window.py b/PyReconstruct/modules/gui/main/main_window.py index 6aec014c8..b03e24491 100644 --- a/PyReconstruct/modules/gui/main/main_window.py +++ b/PyReconstruct/modules/gui/main/main_window.py @@ -2706,6 +2706,11 @@ def resetShortcuts(self, shortcuts_dict : dict = None): for act_name, kbd in shortcuts_dict.items(): getattr(self, act_name).setShortcut(kbd) self.series.setOption(act_name, kbd) + + # the focus mode label names the focus_act shortcut + field = getattr(self, "field", None) + if field is not None: + field.updateFocusHint() def displayAbout(self): """Display the widget display information about the series."""