Feat: add delete edition dialog with fuzzy search
This commit is contained in:
129
src/ui/dialogs/deletedialog.py
Normal file
129
src/ui/dialogs/deletedialog.py
Normal file
@@ -0,0 +1,129 @@
|
||||
from typing import Any
|
||||
|
||||
from PySide6 import QtCore, QtWidgets
|
||||
|
||||
from src import Icon
|
||||
from src.backend.database import Database
|
||||
|
||||
from .dialog_sources.deletedialog_ui import Ui_Dialog
|
||||
|
||||
|
||||
class DeleteDialog(QtWidgets.QDialog, Ui_Dialog):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setupUi(self)
|
||||
self.setWindowTitle("Medien löschen")
|
||||
self.setWindowIcon(Icon("trash").icon)
|
||||
self.reset_btn.clicked.connect(self.reset_selection)
|
||||
self.cancel_btn.clicked.connect(self.close)
|
||||
self.delete_btn.clicked.connect(self.delete_selected)
|
||||
|
||||
self.db = Database()
|
||||
self.books = self.setBooks()
|
||||
self.lineEdit.textChanged.connect(self.populate_books)
|
||||
self.populate_books()
|
||||
|
||||
def delete_selected(self):
|
||||
to_delete = []
|
||||
for row in range(self.tableWidget.rowCount()):
|
||||
checkbox = self.tableWidget.cellWidget(row, 0)
|
||||
if checkbox is not None and checkbox.isChecked():
|
||||
book_id_item = self.tableWidget.item(row, 6)
|
||||
if book_id_item is not None:
|
||||
book_id = int(book_id_item.text())
|
||||
to_delete.append(book_id)
|
||||
if to_delete:
|
||||
self.db.deleteBooks(to_delete)
|
||||
self.accept()
|
||||
|
||||
def reset_selection(self):
|
||||
for row in range(self.tableWidget.rowCount()):
|
||||
checkbox = self.tableWidget.cellWidget(row, 0)
|
||||
if checkbox is not None:
|
||||
checkbox.setChecked(False)
|
||||
|
||||
def setBooks(self) -> list[dict[str, Any]]:
|
||||
result: list[dict[str, Any]] = []
|
||||
books = self.db.getAllBooks()
|
||||
for book in books:
|
||||
title = book["bookdata"].title
|
||||
signature = book["bookdata"].signature
|
||||
edition = book["bookdata"].edition
|
||||
appnr = self.db.getApparatNrByBookId(book["id"])
|
||||
result.append(
|
||||
{
|
||||
"id": book["id"],
|
||||
"appnr": appnr,
|
||||
"title": title,
|
||||
"signature": signature,
|
||||
"edition": edition,
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
def populate_books(self):
|
||||
searchterm = self.lineEdit.text().lower()
|
||||
self.tableWidget.setRowCount(0)
|
||||
for book in self.books:
|
||||
checkbox = QtWidgets.QCheckBox()
|
||||
app_nr = book["appnr"]
|
||||
title = book["title"]
|
||||
signature = book["signature"]
|
||||
edition = book["edition"] if book["edition"] else ""
|
||||
|
||||
if searchterm in title.lower() or searchterm in signature.lower():
|
||||
self.tableWidget.insertRow(self.tableWidget.rowCount())
|
||||
self.tableWidget.setCellWidget(
|
||||
self.tableWidget.rowCount() - 1, 0, checkbox
|
||||
)
|
||||
self.tableWidget.setItem(
|
||||
self.tableWidget.rowCount() - 1,
|
||||
1,
|
||||
QtWidgets.QTableWidgetItem(str(app_nr)),
|
||||
)
|
||||
self.tableWidget.setItem(
|
||||
self.tableWidget.rowCount() - 1,
|
||||
2,
|
||||
QtWidgets.QTableWidgetItem(signature),
|
||||
)
|
||||
self.tableWidget.setItem(
|
||||
self.tableWidget.rowCount() - 1,
|
||||
3,
|
||||
QtWidgets.QTableWidgetItem(title),
|
||||
)
|
||||
self.tableWidget.setItem(
|
||||
self.tableWidget.rowCount() - 1,
|
||||
4,
|
||||
QtWidgets.QTableWidgetItem(edition),
|
||||
)
|
||||
self.tableWidget.setItem(
|
||||
self.tableWidget.rowCount() - 1,
|
||||
5,
|
||||
QtWidgets.QTableWidgetItem(""),
|
||||
)
|
||||
self.tableWidget.setItem(
|
||||
self.tableWidget.rowCount() - 1,
|
||||
6,
|
||||
QtWidgets.QTableWidgetItem(str(book["id"])),
|
||||
)
|
||||
else:
|
||||
continue
|
||||
# set column signature to be 10px wider than the longest entry
|
||||
self.tableWidget.setColumnWidth(1, 100)
|
||||
self.tableWidget.setColumnWidth(2, 150)
|
||||
self.tableWidget.setColumnWidth(3, 150)
|
||||
self.tableWidget.setColumnWidth(4, 100)
|
||||
|
||||
self.tableWidget.setColumnWidth(0, 50)
|
||||
# horizontal header 0 should be centered
|
||||
self.tableWidget.horizontalHeader().setDefaultAlignment(
|
||||
QtCore.Qt.AlignmentFlag.AlignCenter
|
||||
)
|
||||
|
||||
|
||||
def launch():
|
||||
app = QtWidgets.QApplication.instance()
|
||||
if app is None:
|
||||
app = QtWidgets.QApplication([])
|
||||
dialog = DeleteDialog()
|
||||
dialog.exec()
|
||||
Reference in New Issue
Block a user