from PySide6.QtGui import QGuiApplication, QAction, Qt from PySide6.QtQml import QQmlApplicationEngine from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QLabel, QWidget, QMenu, QSizePolicy, QLayout from PySide6.QtCore import qDebug from ui.book_editor.book_editor import BookEditor from models.book import BookStatusEnum from models.book_overview import BooksOverview STATUS_TO_COLOR_MAP = { BookStatusEnum.available: "#3c702e", BookStatusEnum.borrowed: "#702525", BookStatusEnum.reserved: "#bc7613" } class BookCard(QWidget): def __init__(self, book_overview: BooksOverview): super().__init__() self.book_overview = book_overview self.setAttribute(Qt.WidgetAttribute.WA_Hover, True) # Enable hover events self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) # Enable styling for background # Set initial stylesheet with hover behavior self.setStyleSheet(""" BookCard:hover { background-color: palette(highlight); } """) # Layout setup layout = QHBoxLayout(self) layout.setSizeConstraint(QLayout.SizeConstraint.SetMinimumSize) self.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed) layout.setContentsMargins(10, 10, 10, 10) layout.setSpacing(10) # Left-side content left_side = QVBoxLayout() layout.addLayout(left_side) title_label = QLabel(book_overview.title) title_label.setStyleSheet("font-size: 20px; font-weight: bold;") author_label = QLabel("By: " + book_overview.author_name) isbn_label = QLabel("ISBN: " + (book_overview.isbn or "Not Available")) left_side.addWidget(title_label) left_side.addWidget(author_label) left_side.addWidget(isbn_label) # Right-side content right_side = QVBoxLayout() layout.addLayout(right_side) status_label = QLabel(str(book_overview.status.value.capitalize())) status_label.setStyleSheet(f"color: {STATUS_TO_COLOR_MAP[book_overview.status]}; font-size: 20px; font-weight: bold;") status_label.setAlignment(Qt.AlignmentFlag.AlignRight) right_side.addWidget(status_label) if book_overview.librarian_name and book_overview.borrower_name: borrower_label = QLabel("Borrowed: " + book_overview.borrower_name) borrower_label.setAlignment(Qt.AlignmentFlag.AlignRight) librarian_label = QLabel("By: " + book_overview.librarian_name) librarian_label.setAlignment(Qt.AlignmentFlag.AlignRight) right_side.addWidget(borrower_label) right_side.addWidget(librarian_label) self.setLayout(layout) def mousePressEvent(self, event): if event.button() == Qt.MouseButton.LeftButton: self.contextMenuEvent(event) else: super().mousePressEvent(event) def contextMenuEvent(self, event): context_menu = QMenu(self) action_edit_book = context_menu.addAction("Edit Book") action_edit_author = context_menu.addAction("Edit Author") if self.book_overview.status == BookStatusEnum.borrowed: action_mark_returned = context_menu.addAction("Mark as Returned") if self.book_overview.status == BookStatusEnum.reserved: action_remove_reservation = context_menu.addAction("Remove reservation") action = context_menu.exec_(self.mapToGlobal(event.pos())) if action == action_edit_book: BookEditor(self.book_overview).exec() elif action == action_edit_author: print("Edit Author selected") elif action == action_mark_returned: print("Mark as Returned selected") elif action == action_remove_reservation: print("Remove reservation selected")