219 lines
8.6 KiB
Python
219 lines
8.6 KiB
Python
from PySide6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QDialog
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtGui import QKeyEvent, QWheelEvent
|
|
import sys
|
|
from productCard import ProductCard # ProductCard 클래스 임포트
|
|
from searchResultCard import SearchResultCard # SearchResultCard 클래스 임포트
|
|
|
|
|
|
class ProductViewer(QWidget):
|
|
def __init__(self, db_manager, logger):
|
|
super().__init__()
|
|
self.db_manager = db_manager
|
|
self.logger =logger
|
|
self.conn = self.db_manager.conn
|
|
self.cursor = self.conn.cursor() # 여기에 cursor를 올바르게 초기화
|
|
self.product_index = 0
|
|
self.products = self.load_products()
|
|
self.total_products = len(self.products)
|
|
self.resize(600, 680) # 윈도우 크기를 고정 (1024x768)
|
|
|
|
# UI 초기화
|
|
self.init_ui()
|
|
# self.load_product_data()
|
|
|
|
def init_ui(self):
|
|
self.setWindowTitle("Product Viewer")
|
|
main_layout = QVBoxLayout()
|
|
|
|
self.upper_layout = QHBoxLayout()
|
|
|
|
# 상단 레이아웃에 ProductCard를 추가
|
|
self.product_card = ProductCard()
|
|
self.product_card.setFixedSize(700,400)
|
|
self.upper_layout.addWidget(self.product_card)
|
|
main_layout.addLayout(self.upper_layout)
|
|
|
|
# 중간 레이아웃에 여러 개의 SearchResultCard를 추가
|
|
self.middle_layout = QHBoxLayout()
|
|
self.search_result_cards = [SearchResultCard(i+1) for i in range(4)]
|
|
for card in self.search_result_cards:
|
|
self.middle_layout.addWidget(card,2)
|
|
# card.setFixedHeight(300) # 각 카드의 크기를 고정
|
|
|
|
main_layout.addLayout(self.middle_layout)
|
|
|
|
# 하단 레이아웃
|
|
self.bottom_layout = QHBoxLayout()
|
|
self.left_button = QPushButton("◀")
|
|
self.left_button.setFixedSize(200,30)
|
|
self.left_button.clicked.connect(self.previous_product)
|
|
self.page_button = QPushButton("0/0")
|
|
self.page_button.setFixedSize(150,30)
|
|
self.page_button.clicked.connect(self.show_product_dialog)
|
|
self.right_button = QPushButton("▶")
|
|
self.right_button.setFixedSize(200,30)
|
|
self.right_button.clicked.connect(self.next_product)
|
|
self.bottom_layout.addWidget(self.left_button, 3)
|
|
self.bottom_layout.addWidget(self.page_button, 1)
|
|
self.bottom_layout.addWidget(self.right_button, 3)
|
|
|
|
main_layout.addLayout(self.bottom_layout)
|
|
self.setLayout(main_layout)
|
|
|
|
def load_products(self):
|
|
# DB에서 products 테이블 데이터를 로드
|
|
|
|
self.cursor.execute("SELECT * FROM products")
|
|
products = self.cursor.fetchall()
|
|
return products
|
|
|
|
def load_product_data(self):
|
|
# 현재 상품 데이터를 로드하여 ProductCard에 설정
|
|
product = self.products[self.product_index]
|
|
product_id = product[0] # products 테이블의 ID
|
|
product_name = product[1]
|
|
product_img_path = product[8]
|
|
# print(f"product_img_path:{product_img_path}")
|
|
product_tag = product[3]
|
|
product_price = product[4]
|
|
product_cat = product[5]
|
|
|
|
self.product_card.set_data(
|
|
name=product_name,
|
|
price=product_price,
|
|
tag=product_tag,
|
|
cat=product_cat,
|
|
product_img_path=product_img_path
|
|
)
|
|
|
|
# SearchResultCard 초기화 및 데이터 설정
|
|
search_results = self.load_search_results(product_id)
|
|
for i, result in enumerate(search_results):
|
|
if i < len(self.search_result_cards):
|
|
title, source, price, saved_img_path, is_selected, search_result_id = result[1], result[2], result[3], result[4], result[5], result[0]
|
|
self.search_result_cards[i].set_data(
|
|
name=title, price=price, source=source, saved_img_path=saved_img_path, is_selected=is_selected
|
|
)
|
|
self.search_result_cards[i].product_id = product_id
|
|
self.search_result_cards[i].search_result_id = search_result_id
|
|
self.search_result_cards[i].cursor = self.cursor
|
|
|
|
if self.total_products > 0:
|
|
# product = self.products[self.product_index]
|
|
total_products = self.db_manager.count_products()
|
|
self.logger.info(f"Total products in the database: {total_products}")
|
|
|
|
# 데이터 UI에 표시
|
|
self.page_button.setText(f"{self.product_index + 1}/{self.total_products}")
|
|
else:
|
|
# 데이터가 없을 경우 기본 UI 상태로 설정
|
|
self.page_button.setText("0/0")
|
|
|
|
def reset_search_result_cards(self):
|
|
# 각 SearchResultCard를 초기화
|
|
for card in self.search_result_cards:
|
|
card.reset()
|
|
|
|
def load_search_results(self, product_id):
|
|
# DB에서 search_results 테이블의 데이터를 로드
|
|
# self.cursor.execute("SELECT * FROM search_results WHERE product_id=?", (product_id,))
|
|
self.cursor.execute("SELECT id, title, source, price, saved_img_path, is_selected FROM search_results WHERE product_id=?", (product_id,))
|
|
|
|
return self.cursor.fetchall()
|
|
|
|
def previous_product(self):
|
|
if self.product_index > 0:
|
|
self.product_index -= 1
|
|
self.reset_search_result_cards()
|
|
self.load_product_data()
|
|
|
|
def next_product(self):
|
|
if self.product_index < self.total_products - 1:
|
|
self.product_index += 1
|
|
self.reset_search_result_cards()
|
|
self.load_product_data()
|
|
|
|
def show_product_dialog(self):
|
|
# 현재 상품 정보를 다이얼로그로 보여줌
|
|
dialog = ProductDialog(self)
|
|
dialog.exec()
|
|
|
|
def keyPressEvent(self, event: QKeyEvent):
|
|
# 키보드 이벤트 처리
|
|
if event.key() == Qt.Key_Left:
|
|
self.previous_product()
|
|
elif event.key() == Qt.Key_Right:
|
|
self.next_product()
|
|
elif event.key() == Qt.Key_Escape:
|
|
self.close()
|
|
elif event.key() in [Qt.Key_1, Qt.Key_2, Qt.Key_3, Qt.Key_4, Qt.Key_5]:
|
|
index = event.key() - Qt.Key_1
|
|
if index < len(self.search_result_cards) and self.search_result_cards[index].name_value.text():
|
|
self.select_card(index)
|
|
elif event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
|
|
self.show_product_dialog()
|
|
|
|
def wheelEvent(self, event: QWheelEvent):
|
|
"""
|
|
마우스 휠 이벤트를 오버라이드하여 휠의 방향에 따라 상품을 이동합니다.
|
|
"""
|
|
if event.angleDelta().y() > 0:
|
|
# 마우스 휠을 위로 올릴 때
|
|
self.previous_product()
|
|
elif event.angleDelta().y() < 0:
|
|
# 마우스 휠을 아래로 내릴 때
|
|
self.next_product()
|
|
|
|
def select_card(self, index):
|
|
# 선택된 카드의 UI 갱신 및 데이터베이스 업데이트
|
|
print(f"{index}번째 카드 선택")
|
|
for i, card in enumerate(self.search_result_cards):
|
|
if i == index:
|
|
print(f"i = {i} | index = {index} - True")
|
|
print(f"card.index : {card.index}")
|
|
card.update_selection(True) # 선택 상태로 업데이트
|
|
else:
|
|
print(f"i = {i} | index = {index} - False")
|
|
print(f"card.index : {card.index}")
|
|
card.update_selection(False) # 선택 해제 상태로 업데이트
|
|
|
|
def refresh_data(self, db_conn):
|
|
"""DB 연결을 업데이트하고 데이터를 다시 로드합니다."""
|
|
self.db_conn = db_conn
|
|
self.products = self.load_products()
|
|
self.total_products = self.db_manager.count_products()
|
|
self.load_product_data()
|
|
|
|
def showEvent(self, event):
|
|
"""show() 호출 시 데이터를 로드"""
|
|
super().showEvent(event)
|
|
self.load_product_data()
|
|
|
|
class ProductDialog(QDialog):
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.setWindowTitle("상품 리스트")
|
|
layout = QVBoxLayout(self)
|
|
|
|
cursor = parent.conn.cursor()
|
|
cursor.execute("SELECT * FROM products")
|
|
products = cursor.fetchall()
|
|
|
|
for product in products:
|
|
label = QLabel(f"{product[1]} - {product[3]}원")
|
|
layout.addWidget(label)
|
|
|
|
self.setLayout(layout)
|
|
|
|
def keyPressEvent(self, event):
|
|
if event.key() in {Qt.Key_Escape, Qt.Key_Return, Qt.Key_Enter}:
|
|
self.accept() # 다이얼로그 닫기
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
viewer = ProductViewer("products.db")
|
|
viewer.show()
|
|
sys.exit(app.exec())
|