filter's fix

This commit is contained in:
Daniel Haus 2026-02-11 15:43:30 +03:00
parent ff50ea6784
commit 4b4acdcb1b
6 changed files with 144 additions and 171 deletions

View file

@ -1,82 +1,101 @@
from PyQt6.QtWidgets import (
QWidget,
QVBoxLayout,
QPushButton,
QLabel,
QTableWidget,
QTableWidgetItem,
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: str, role: str):
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.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.addWidget(self.refresh_button)
self.layout.addWidget(self.sort_button)
self.layout.addLayout(top_layout)
self.layout.addWidget(self.table)
self.layout.addWidget(self.logout_button)
self.setLayout(self.layout)
self.setFixedSize(860, 500)
self.filter_age_combo.currentTextChanged.connect(self.apply_filters)
self.filter_supplier_combo.currentTextChanged.connect(self.apply_filters)
def load_data(self, toys: list):
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"]),
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)
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)
)
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)
)
self.table.item(row, col).setBackground(QColor(OUT_OF_STOCK_COLOR))