from PyQt6.QtWidgets import ( QWidget, QVBoxLayout, QPushButton, QLabel, QTableWidget, QTableWidgetItem, ) from PyQt6.QtGui import QColor from config import DISCOUNT_COLOR, OUT_OF_STOCK_COLOR class CatalogView(QWidget): def __init__(self, full_name: str, role: str): super().__init__() self.setWindowTitle("Каталог игрушек") self.layout = QVBoxLayout() self.user_label = QLabel( f"{full_name} ({role})" ) self.refresh_button = QPushButton("Обновить") self.sort_button = QPushButton("Сортировать по цене") self.logout_button = QPushButton("Выйти") self.table = QTableWidget() self.layout.addWidget(self.user_label) self.layout.addWidget(self.refresh_button) self.layout.addWidget(self.sort_button) self.layout.addWidget(self.table) self.layout.addWidget(self.logout_button) self.setLayout(self.layout) self.setFixedSize(860, 500) def load_data(self, toys: list): headers = [ "Название", "Категория", "Производитель", "Возраст", "Поставщик", "Цена", "Скидка", "Остаток", ] self.table.setColumnCount(len(headers)) self.table.setHorizontalHeaderLabels(headers) self.table.setRowCount(len(toys)) for row, toy in enumerate(toys): values = [ toy["toy_name"], toy["category_name"], toy["manufacturer_name"], toy["ages"], toy["suppliers"], str(toy["price"]), str(toy["discount"]), str(toy["quantity"]), ] for col, value in enumerate(values): item = QTableWidgetItem(value) self.table.setItem(row, col, item) if toy["discount"] >= 25: for col in range(len(headers)): self.table.item(row, col).setBackground( QColor(DISCOUNT_COLOR) ) if toy["quantity"] == 0: for col in range(len(headers)): self.table.item(row, col).setBackground( QColor(OUT_OF_STOCK_COLOR) )