87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
# gui/main_window.py
|
|
|
|
from PyQt5.QtWidgets import QMainWindow, QTabWidget, QMessageBox
|
|
from gui.tabs.accounts.account_tab import AccountTab
|
|
from gui.tabs.products.product_tab import ProductTab
|
|
from gui.tabs.import_tab import ImportTab
|
|
from gui.tabs.listeds.listed_tab import ListedTab
|
|
from gui.tabs.settings.settings_tab import SettingsTab
|
|
from gui.global_signals import global_signals
|
|
from gui.core.login_handle_dialog import LoginHandleDialog
|
|
|
|
# Task runner
|
|
from tasks.task_runner import TaskRunner
|
|
from tasks.listed_tasks import process_all_listed # Hàm queue mới, 2 dialog
|
|
|
|
class MainWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setWindowTitle("Facebook Marketplace Manager")
|
|
self.resize(1200, 600)
|
|
|
|
# --- Tạo QTabWidget ---
|
|
self.tabs = QTabWidget()
|
|
self.account_tab = AccountTab()
|
|
self.product_tab = ProductTab()
|
|
self.import_tab = ImportTab()
|
|
self.listed_tab = ListedTab()
|
|
self.setting_tab = SettingsTab()
|
|
|
|
self.tabs.addTab(self.account_tab, "Accounts")
|
|
self.tabs.addTab(self.product_tab, "Products")
|
|
self.tabs.addTab(self.listed_tab, "Queue Handle")
|
|
self.tabs.addTab(self.import_tab, "Import Data")
|
|
self.tabs.addTab(self.setting_tab, "Setting")
|
|
|
|
# Gắn sự kiện khi tab thay đổi
|
|
self.tabs.currentChanged.connect(self.on_tab_changed)
|
|
|
|
self.setCentralWidget(self.tabs)
|
|
|
|
# Khi mở app thì chỉ load tab đầu tiên (Accounts)
|
|
self.on_tab_changed(0)
|
|
|
|
# --- Kết nối signals ---
|
|
global_signals.open_login_dialog.connect(self.show_login_dialog)
|
|
global_signals.listed_finished.connect(self.on_listed_task_finished)
|
|
|
|
# --- 🔥 Khởi chạy queue background khi app mở ---
|
|
self.start_background_tasks()
|
|
|
|
# ---------------- Dialog handler ----------------
|
|
def show_login_dialog(self, account_id):
|
|
"""Mở dialog xử lý account_id, modeless để không block MainWindow"""
|
|
dialog = LoginHandleDialog(account_id=account_id)
|
|
dialog.show() # modeless
|
|
|
|
# ---------------- Background tasks ----------------
|
|
def start_background_tasks(self):
|
|
"""Khởi động các background task khi app mở."""
|
|
self.task_runner = TaskRunner()
|
|
self.task_runner.start()
|
|
|
|
# Thêm task xử lý listed pending (dùng hàm queue + 2 dialog cùng lúc)
|
|
self.task_runner.add_task(process_all_listed)
|
|
print("[App] Background task runner started")
|
|
|
|
# ---------------- Listed finished ----------------
|
|
def on_listed_task_finished(self):
|
|
"""Hiển thị thông báo khi listed task hoàn tất"""
|
|
QMessageBox.information(
|
|
self,
|
|
"Auto Listing",
|
|
"All pending listed items have been processed!"
|
|
)
|
|
|
|
# ---------------- Tab handling ----------------
|
|
def on_tab_changed(self, index):
|
|
"""Chỉ load nội dung tab khi được active."""
|
|
tab = self.tabs.widget(index)
|
|
|
|
# Mỗi tab có thể có hàm load_data() riêng
|
|
if hasattr(tab, "load_data"):
|
|
# Thêm cờ để tránh load lại nhiều lần không cần thiết
|
|
if not getattr(tab, "is_loaded", False):
|
|
tab.load_data()
|
|
tab.is_loaded = True
|