Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions rkdeveloptool-gui/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from PySide6.QtCore import QEvent, QObject, QPoint, Qt, QTimer
from PySide6.QtGui import QColor, QPainter, QPalette, QPen
from PySide6.QtWidgets import (
QAbstractButton, QFrame, QGroupBox, QLineEdit, QListWidget, QListWidgetItem, QScrollArea,
QVBoxLayout, QWidget,
QAbstractButton, QApplication, QFrame, QGraphicsDropShadowEffect, QGroupBox, QLineEdit,
QListWidget, QListWidgetItem, QScrollArea, QVBoxLayout, QWidget,
)

from .i18n import TRANSLATIONS
Expand Down Expand Up @@ -200,16 +200,28 @@ def scroll():


class SearchPalette(QFrame):
"""The search window: opened with Ctrl/Cmd+K or the status-bar button,
"""The search box: opened with Ctrl/Cmd+K or the status-bar button,
over the top of the window, with the results listed under the field.
Enter or a click opens a result; Escape or a click elsewhere closes it."""
Enter or a click opens a result; Escape, a click elsewhere or moving
the focus away closes it.

It's a panel inside the main window, not a popup window of its own:
macOS gives a Qt.Popup window no input method, so Chinese (pinyin)
couldn't be typed into it."""

WIDTH = 520

def __init__(self, gui):
super().__init__(gui, Qt.WindowType.Popup)
super().__init__(gui)
self.gui = gui
self.hide()
self.setFrameShape(QFrame.Shape.StyledPanel)
self.setAutoFillBackground(True)
shadow = QGraphicsDropShadowEffect(self)
shadow.setBlurRadius(24)
shadow.setOffset(0, 4)
shadow.setColor(QColor(0, 0, 0, 120))
self.setGraphicsEffect(shadow)
layout = QVBoxLayout(self)
layout.setContentsMargins(8, 8, 8, 8)
self.field = QLineEdit()
Expand All @@ -223,20 +235,40 @@ def __init__(self, gui):
self.field.textChanged.connect(self._update)
self.field.returnPressed.connect(lambda: self._open(self.results.currentItem()))
self.field.installEventFilter(self)
QApplication.instance().focusChanged.connect(self._focus_changed)
self._index = None

def open(self):
self._index = None # the window may have changed
self.field.clear()
self._update("")
top_left = self.gui.mapToGlobal(QPoint((self.gui.width() - self.WIDTH) // 2, 60))
self.move(top_left)
self.move((self.gui.width() - self.WIDTH) // 2, 60)
# Watch clicks elsewhere only while open: an application-wide
# filter sees every event.
QApplication.instance().installEventFilter(self)
self.show()
self.activateWindow()
self.raise_()
self.field.setFocus(Qt.FocusReason.ShortcutFocusReason)

def hideEvent(self, event):
QApplication.instance().removeEventFilter(self)
super().hideEvent(event)

def _focus_changed(self, _old, new):
if self.isVisible() and (new is None or not self.isAncestorOf(new)):
self.hide()

def eventFilter(self, obj, event):
if obj is self.field and event.type() == QEvent.Type.KeyPress:
kind = event.type()
if kind == QEvent.Type.MouseButtonPress and self.isVisible() and isinstance(obj, QWidget) \
and obj.window() is self.gui and obj is not self and not self.isAncestorOf(obj):
self.hide() # a click elsewhere in the window
return False
if obj is self.field and kind == QEvent.Type.ShortcutOverride \
and event.key() == Qt.Key.Key_Escape:
event.accept() # Escape closes search, not the window's Cancel
return True
if obj is self.field and kind == QEvent.Type.KeyPress:
key = event.key()
if key in (Qt.Key.Key_Down, Qt.Key.Key_Up) and self.results.count():
row = self.results.currentRow() + (1 if key == Qt.Key.Key_Down else -1)
Expand Down
13 changes: 13 additions & 0 deletions tests/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -2821,6 +2821,19 @@ def check_search(app):
app.processEvents()
if not palette.isVisible() or palette.field.text():
errors.append("the search button didn't open an empty search")
# Chinese is typed through the input method; a Qt.Popup window got
# none on macOS. The panel's field takes a composed string.
from PySide6.QtCore import Qt
from PySide6.QtGui import QInputMethodEvent
if not palette.field.testAttribute(Qt.WidgetAttribute.WA_InputMethodEnabled) \
or palette.isWindow():
errors.append("the search field can't take input-method text")
QApplication.sendEvent(palette.field, QInputMethodEvent("bei", []))
commit = QInputMethodEvent()
commit.setCommitString("备份")
QApplication.sendEvent(palette.field, commit)
if palette.field.text() != "备份":
errors.append(f"pinyin into search gave {palette.field.text()!r}")
palette.field.setText(erase)
app.processEvents()
if palette.results.count() == 0:
Expand Down
Loading