2021-04-13 11:05:49 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-04-14 10:22:02 +02:00
|
|
|
|
2021-04-13 11:05:49 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import aqt
|
2022-02-13 04:40:47 +01:00
|
|
|
import aqt.browser
|
|
|
|
import aqt.gui_hooks
|
2021-04-13 11:05:49 +02:00
|
|
|
from aqt.qt import *
|
|
|
|
|
|
|
|
|
|
|
|
class SidebarSearchBar(QLineEdit):
|
|
|
|
def __init__(self, sidebar: aqt.browser.sidebar.SidebarTreeView) -> None:
|
|
|
|
QLineEdit.__init__(self, sidebar)
|
|
|
|
self.setPlaceholderText(sidebar.col.tr.browsing_sidebar_filter())
|
|
|
|
self.sidebar = sidebar
|
|
|
|
self.timer = QTimer(self)
|
|
|
|
self.timer.setInterval(600)
|
|
|
|
self.timer.setSingleShot(True)
|
|
|
|
self.setFrame(False)
|
2021-11-24 22:17:41 +01:00
|
|
|
|
|
|
|
qconnect(self.timer.timeout, self.onSearch)
|
|
|
|
qconnect(self.textChanged, self.onTextChanged)
|
|
|
|
|
2021-04-13 11:05:49 +02:00
|
|
|
def onTextChanged(self, text: str) -> None:
|
|
|
|
if not self.timer.isActive():
|
|
|
|
self.timer.start()
|
|
|
|
|
|
|
|
def onSearch(self) -> None:
|
|
|
|
self.sidebar.search_for(self.text())
|
|
|
|
|
|
|
|
def keyPressEvent(self, evt: QKeyEvent) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
if evt.key() in (Qt.Key.Key_Up, Qt.Key.Key_Down):
|
2021-04-13 11:05:49 +02:00
|
|
|
self.sidebar.setFocus()
|
2021-10-05 05:53:01 +02:00
|
|
|
elif evt.key() in (Qt.Key.Key_Enter, Qt.Key.Key_Return):
|
2021-04-13 11:05:49 +02:00
|
|
|
self.onSearch()
|
|
|
|
else:
|
|
|
|
QLineEdit.keyPressEvent(self, evt)
|