81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
# gui/core/login_handle_dialog.py
|
|
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QPushButton, QApplication
|
|
from PyQt5.QtCore import QTimer
|
|
from services.core.log_service import log_service
|
|
from stores.shared_store import SharedStore
|
|
from gui.global_signals import global_signals
|
|
|
|
|
|
class LoginHandleDialog(QDialog):
|
|
dialog_width = 300
|
|
dialog_height = 100
|
|
margin = 10 # khoảng cách giữa các dialog
|
|
|
|
# Lưu danh sách dialog đang mở (dùng class variable để chia sẻ giữa tất cả dialog)
|
|
open_dialogs = []
|
|
|
|
def __init__(self, account_id: int, listed_id: int):
|
|
super().__init__()
|
|
self.account_id = account_id
|
|
self.listed_id = listed_id
|
|
|
|
self.setWindowTitle(f"Handle Listing {self.listed_id}")
|
|
self.setModal(False) # modeless
|
|
self.resize(self.dialog_width, self.dialog_height)
|
|
|
|
# --- UI đơn giản ---
|
|
layout = QVBoxLayout()
|
|
self.btn_finish = QPushButton("Finish Listing")
|
|
self.btn_finish.clicked.connect(self.finish_listing)
|
|
layout.addWidget(self.btn_finish)
|
|
self.setLayout(layout)
|
|
|
|
# --- Tính vị trí để xếp dialog từ góc trái trên cùng sang phải theo hàng ngang ---
|
|
self.move_to_corner()
|
|
LoginHandleDialog.open_dialogs.append(self)
|
|
|
|
def move_to_corner(self):
|
|
screen_geometry = QApplication.primaryScreen().availableGeometry()
|
|
start_x = screen_geometry.left() + self.margin
|
|
start_y = screen_geometry.top() + self.margin
|
|
|
|
row_height = 0
|
|
current_x = start_x
|
|
current_y = start_y
|
|
|
|
for dlg in LoginHandleDialog.open_dialogs:
|
|
# Nếu vượt chiều ngang màn hình, xuống hàng mới
|
|
if current_x + dlg.width() + self.margin > screen_geometry.right():
|
|
current_x = start_x
|
|
current_y += row_height + self.margin
|
|
row_height = 0
|
|
|
|
dlg.move(current_x, current_y)
|
|
current_x += dlg.width() + self.margin
|
|
row_height = max(row_height, dlg.height())
|
|
|
|
# vị trí dialog mới
|
|
if current_x + self.width() + self.margin > screen_geometry.right():
|
|
current_x = start_x
|
|
current_y += row_height + self.margin
|
|
|
|
self.move(current_x, current_y)
|
|
|
|
def finish_listing(self):
|
|
"""Remove khỏi SharedStore, emit signal và đóng dialog"""
|
|
try:
|
|
store = SharedStore.get_instance()
|
|
store.remove(self.listed_id)
|
|
log_service.info(f"[Dialog] Removed listed_id={self.listed_id} from SharedStore")
|
|
|
|
# Emit signal để MainWindow biết
|
|
QTimer.singleShot(0, lambda: global_signals.dialog_finished.emit(self.account_id, self.listed_id))
|
|
|
|
# Close dialog an toàn
|
|
self.hide()
|
|
self.deleteLater()
|
|
if self in LoginHandleDialog.open_dialogs:
|
|
LoginHandleDialog.open_dialogs.remove(self)
|
|
except Exception as e:
|
|
log_service.error(f"[Dialog] Exception in finish_listing for listed_id={self.listed_id}: {e}")
|