Add per-item delete button to search history dropdown
Each recent search now has an x button to remove it individually. Clicking the search text still loads it as before.
This commit is contained in:
parent
c8d38edf06
commit
8c64f20171
@ -429,6 +429,10 @@ class Database:
|
|||||||
self.conn.execute("DELETE FROM search_history")
|
self.conn.execute("DELETE FROM search_history")
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
|
def remove_search_history(self, query: str) -> None:
|
||||||
|
self.conn.execute("DELETE FROM search_history WHERE query = ?", (query,))
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
# -- Saved Searches --
|
# -- Saved Searches --
|
||||||
|
|
||||||
def add_saved_search(self, name: str, query: str, site_id: int | None = None) -> None:
|
def add_saved_search(self, name: str, query: str, site_id: int | None = None) -> None:
|
||||||
|
|||||||
@ -110,13 +110,40 @@ class SearchBar(QWidget):
|
|||||||
hist_header = menu.addAction("-- Recent --")
|
hist_header = menu.addAction("-- Recent --")
|
||||||
hist_header.setEnabled(False)
|
hist_header.setEnabled(False)
|
||||||
hist_actions = {}
|
hist_actions = {}
|
||||||
|
hist_delete_actions = {}
|
||||||
for query in history:
|
for query in history:
|
||||||
a = menu.addAction(f" {query}")
|
row = QWidget()
|
||||||
hist_actions[id(a)] = query
|
row_layout = QHBoxLayout(row)
|
||||||
|
row_layout.setContentsMargins(8, 2, 4, 2)
|
||||||
|
label = QPushButton(query)
|
||||||
|
label.setFlat(True)
|
||||||
|
label.setStyleSheet("text-align: left; border: none; padding: 2px 4px;")
|
||||||
|
delete_btn = QPushButton("x")
|
||||||
|
delete_btn.setFixedWidth(20)
|
||||||
|
delete_btn.setFlat(True)
|
||||||
|
delete_btn.setToolTip("Remove from history")
|
||||||
|
row_layout.addWidget(label, stretch=1)
|
||||||
|
row_layout.addWidget(delete_btn)
|
||||||
|
|
||||||
|
from PySide6.QtWidgets import QWidgetAction
|
||||||
|
wa = QWidgetAction(menu)
|
||||||
|
wa.setDefaultWidget(row)
|
||||||
|
menu.addAction(wa)
|
||||||
|
hist_actions[id(label)] = query
|
||||||
|
hist_delete_actions[id(delete_btn)] = query
|
||||||
|
|
||||||
|
label.clicked.connect(lambda checked, q=query, m=menu: (
|
||||||
|
self._input.setText(q), self._do_search(), m.close()
|
||||||
|
))
|
||||||
|
delete_btn.clicked.connect(lambda checked, q=query, m=menu: (
|
||||||
|
self._db.remove_search_history(q), m.close(), self._show_history_menu()
|
||||||
|
))
|
||||||
|
|
||||||
menu.addSeparator()
|
menu.addSeparator()
|
||||||
clear_action = menu.addAction("Clear History")
|
clear_action = menu.addAction("Clear History")
|
||||||
else:
|
else:
|
||||||
hist_actions = {}
|
hist_actions = {}
|
||||||
|
hist_delete_actions = {}
|
||||||
clear_action = None
|
clear_action = None
|
||||||
|
|
||||||
if not saved and not history:
|
if not saved and not history:
|
||||||
@ -129,9 +156,6 @@ class SearchBar(QWidget):
|
|||||||
|
|
||||||
if clear_action and action == clear_action:
|
if clear_action and action == clear_action:
|
||||||
self._db.clear_search_history()
|
self._db.clear_search_history()
|
||||||
elif id(action) in hist_actions:
|
|
||||||
self._input.setText(hist_actions[id(action)])
|
|
||||||
self._do_search()
|
|
||||||
elif saved and id(action) in saved_actions:
|
elif saved and id(action) in saved_actions:
|
||||||
_, query = saved_actions[id(action)]
|
_, query = saved_actions[id(action)]
|
||||||
self._input.setText(query)
|
self._input.setText(query)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user