op13-cp-3/views/catalog_view.py
2026-02-11 15:43:30 +03:00

101 lines
4.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PyQt6.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel,
QTableWidget, QTableWidgetItem, QComboBox
)
from PyQt6.QtGui import QColor
from config import DISCOUNT_COLOR, OUT_OF_STOCK_COLOR
class CatalogView(QWidget):
def __init__(self, full_name, role, age_groups, suppliers):
super().__init__()
self.user_role = role
self.setWindowTitle("Каталог игрушек")
self.setFixedSize(860, 500)
self.layout = QVBoxLayout()
self.user_label = QLabel(f"{full_name} ({role})")
# Кнопки
self.refresh_button = QPushButton("Обновить")
self.sort_button = QPushButton("Сортировать по цене")
self.logout_button = QPushButton("Выйти")
# Фильтры
self.filter_age_combo = QComboBox()
self.filter_supplier_combo = QComboBox()
self.filter_age_combo.addItem("Все возрастные категории")
for age in age_groups:
self.filter_age_combo.addItem(age)
self.filter_supplier_combo.addItem("Все поставщики")
for sup in suppliers:
self.filter_supplier_combo.addItem(sup)
# Включаем фильтры по роли
self.filter_age_combo.setEnabled(role in ("Покупатель", "Сотрудник", "Администратор"))
self.filter_supplier_combo.setEnabled(role in ("Сотрудник", "Администратор"))
# Панель фильтров и кнопок
top_layout = QHBoxLayout()
top_layout.addWidget(self.refresh_button)
top_layout.addWidget(self.sort_button)
top_layout.addWidget(self.filter_age_combo)
top_layout.addWidget(self.filter_supplier_combo)
top_layout.addWidget(self.logout_button)
self.table = QTableWidget()
self.layout.addWidget(self.user_label)
self.layout.addLayout(top_layout)
self.layout.addWidget(self.table)
self.setLayout(self.layout)
self.filter_age_combo.currentTextChanged.connect(self.apply_filters)
self.filter_supplier_combo.currentTextChanged.connect(self.apply_filters)
self.all_toys = []
def load_data(self, toys):
self.all_toys = toys
self.apply_filters()
def apply_filters(self):
filtered = self.all_toys
age_filter = self.filter_age_combo.currentText()
supplier_filter = self.filter_supplier_combo.currentText()
if self.user_role in ("Покупатель", "Сотрудник", "Администратор"):
if age_filter != "Все возрастные категории":
filtered = [t for t in filtered if age_filter in t["ages"].split(", ")]
if self.user_role in ("Сотрудник", "Администратор"):
if supplier_filter != "Все поставщики":
filtered = [t for t in filtered if supplier_filter in t["suppliers"].split(", ")]
self._populate_table(filtered)
def _populate_table(self, toys):
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):
self.table.setItem(row, col, QTableWidgetItem(value))
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))