from .sources.Ui_dialog_addNewTitleEntry import Ui_Dialog from PyQt6 import QtWidgets, QtCore, QtGui from src.logic import Database from src.schemas import Book from src.utils import Icon class NewEntry(QtWidgets.QDialog, Ui_Dialog): def __init__(self, title_id: list[int]): super(NewEntry, self).__init__() self.setupUi(self) self.setWindowTitle("Neues Exemplar hinzufügen") self.setWindowIcon(Icon("newentry").icon) self.tableWidget.horizontalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeMode.Stretch ) self.db = Database() self.titles = title_id self.newIds = [] self.populateTable() self.btn_addNewBook.clicked.connect(self.addEntry) self.buttonBox.accepted.connect(self.insertEntry) #disable buttonbox accepted self.buttonBox.button( QtWidgets.QDialogButtonBox.StandardButton.Ok ).setEnabled(False) def addEntry(self): self.buttonBox.button( QtWidgets.QDialogButtonBox.StandardButton.Ok ).setEnabled(True) # clone last row and its data row = self.tableWidget.rowCount() self.tableWidget.insertRow(row) for i in range(4): self.tableWidget.setItem( row, i, QtWidgets.QTableWidgetItem(self.tableWidget.item(row - 1, i)) ) if i == 2 and "+" in self.tableWidget.item(row, i).text(): entry = self.tableWidget.item(row, i).text().split("+")[1] entry = str(int(entry) + 1) self.tableWidget.setItem( row, i, QtWidgets.QTableWidgetItem( self.tableWidget.item(row, i).text().split("+")[0] + "+" + entry ), ) def populateTable(self): for title in self.titles: #print(title) entries = self.db.getMediaSimilarSignatureByID(title) # sort by signature entries.sort(key=lambda x: x.signature, reverse=True) for entry in entries: self.tableWidget.insertRow(0) self.tableWidget.setItem(0, 0, QtWidgets.QTableWidgetItem(entry.isbn)) self.tableWidget.setItem(0, 1, QtWidgets.QTableWidgetItem(entry.title)) self.tableWidget.setItem( 0, 2, QtWidgets.QTableWidgetItem(entry.signature) ) self.tableWidget.setItem(0, 3, QtWidgets.QTableWidgetItem(entry.ppn)) def insertEntry(self): # get all rows, convert them to Book and insert into database for row in range(self.tableWidget.rowCount()): isbn = self.tableWidget.item(row, 0).text() title = self.tableWidget.item(row, 1).text() signature = self.tableWidget.item(row, 2).text() ppn = self.tableWidget.item(row, 3).text() book = Book( isbn=eval(isbn), title=title, signature=signature, ppn=eval(ppn), ) #print(book) if not self.db.checkMediaExists(book): newBookId = self.db.insertMedia(book) self.newIds.append(newBookId) def launch(): import sys app = QtWidgets.QApplication(sys.argv) window = NewEntry([1]) window.show() sys.exit(app.exec())