404 lines
16 KiB
Python
404 lines
16 KiB
Python
"""PySide6 复刻主窗口:与 hk4e-GM v2.0.1 完全对齐的侧边栏 / 页面布局。
|
|
|
|
布局坐标来自原程序 1400x1000 窗口的 UIA 树(去掉 32px 标题栏),
|
|
配色来自原程序截图采样。所有网络操作走后台线程,界面不阻塞。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import ctypes
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
from typing import Callable, Dict, List, Optional
|
|
|
|
from PySide6.QtCore import QObject, Qt, QTimer, Signal
|
|
from PySide6.QtGui import QColor, QFont, QIcon, QPixmap, QTextCursor
|
|
from PySide6.QtWidgets import (QApplication, QComboBox, QFrame, QHBoxLayout, QLabel,
|
|
QLineEdit, QListWidget, QListWidgetItem, QMainWindow,
|
|
QPushButton, QStackedWidget, QTextEdit, QVBoxLayout,
|
|
QWidget)
|
|
|
|
from core import config as cfg_mod
|
|
from core import templates as tpl
|
|
from core.data import DataStore
|
|
from core.db import DbClient
|
|
from core.muip import MuipClient
|
|
from core.ssh import SERVICES, SshClient
|
|
|
|
from . import theme, widgets as W
|
|
|
|
TITLE_H = 32
|
|
SIDEBAR_W = 60
|
|
APP_TITLE = "GM - NEW UI"
|
|
ICON_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "icons")
|
|
BASE_SKINS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "skins")
|
|
|
|
NAV = [
|
|
("item", "物品", "\uE7B8"),
|
|
("weapon", "武器", "\uE7C8"),
|
|
("avatar", "角色", "\uE77B"),
|
|
("artifact", "圣遗物", "\uE735"),
|
|
("generate", "生成", "\uE7B8"),
|
|
("quest", "任务", "\uE8A5"),
|
|
("misc", "其它", "\uE71D"),
|
|
("template", "模板", "\uE8FD"),
|
|
("start", "应用", "\uE71D"),
|
|
("log", "日志", "\uE7C3"),
|
|
("about", "关于", "\uE946"),
|
|
]
|
|
SETTINGS_NAV = ("settings", "设置", "\uE713")
|
|
|
|
|
|
def _dark_titlebar(hwnd: int) -> None:
|
|
try:
|
|
value = ctypes.c_int(1)
|
|
ctypes.windll.dwmapi.DwmSetWindowAttribute(ctypes.c_void_p(hwnd), 20, ctypes.byref(value), 4)
|
|
ctypes.windll.dwmapi.DwmSetWindowAttribute(ctypes.c_void_p(hwnd), 19, ctypes.byref(value), 4)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
class Bridge(QObject):
|
|
finished = Signal(object)
|
|
|
|
|
|
class GMWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setWindowTitle(APP_TITLE)
|
|
self.resize(1400, 1000)
|
|
self.setMinimumSize(1100, 700)
|
|
icon = QIcon(os.path.join(ICON_DIR.replace("icons", "skins"), "app_icon.png"))
|
|
self.setWindowIcon(icon)
|
|
self._bridges = set()
|
|
self._thread = threading.current_thread()
|
|
|
|
self.cfg = cfg_mod.Config()
|
|
self.data = DataStore()
|
|
self.muip = MuipClient(self.cfg)
|
|
self.ssh = SshClient(self.cfg, log=lambda m: self.log(m))
|
|
self.db = DbClient(self.cfg, log=lambda m: self.log(m))
|
|
|
|
root = QWidget(self)
|
|
root.setObjectName("Root")
|
|
self.setCentralWidget(root)
|
|
|
|
# 侧边栏
|
|
self.sidebar = QWidget(root)
|
|
self.sidebar.setObjectName("Sidebar")
|
|
self.sidebar.setGeometry(0, 0, 60, 968)
|
|
self.nav_buttons: Dict[str, QWidget] = {}
|
|
self._nav_icons: Dict[str, QLabel] = {}
|
|
self._build_sidebar()
|
|
|
|
# 页面容器
|
|
self.stack = QStackedWidget(root)
|
|
self.stack.setGeometry(SIDEBAR_W, 0, 1340, 968)
|
|
|
|
self.pages: Dict[str, QWidget] = {}
|
|
for key, cls in PAGES.items():
|
|
page = cls(self, self.stack)
|
|
self.pages[key] = page
|
|
self.stack.addWidget(page)
|
|
|
|
self.current = None
|
|
self.show_page("item")
|
|
|
|
# 状态仓
|
|
self.logs: List[str] = []
|
|
self.status = QLabel("", root)
|
|
self.status.setStyleSheet("color:#7A7A7A;font-size:8pt;")
|
|
self.status.setGeometry(66, 946, 900, 18)
|
|
|
|
self.toast = QLabel("", root)
|
|
self.toast.setStyleSheet("background:#3E3E3E;color:#EEEEEE;padding:6px 12px;border-radius:4px;")
|
|
self.toast.hide()
|
|
|
|
QTimer.singleShot(300, self._refresh_status)
|
|
|
|
# ------------------------------------------------------------------ 侧边栏
|
|
def _build_sidebar(self) -> None:
|
|
self.nav_indicator = QFrame(self.sidebar)
|
|
self.nav_indicator.setStyleSheet("background:#80B9EE;")
|
|
self.nav_indicator.setGeometry(0, 64, 3, 60)
|
|
self.nav_indicator.hide()
|
|
|
|
y = 0
|
|
for key, text, glyph in NAV:
|
|
self._make_nav_item(key, text, glyph, y)
|
|
y += 60
|
|
# 设置固定在底部
|
|
self._make_nav_item(SETTINGS_NAV[0], SETTINGS_NAV[1], SETTINGS_NAV[2], 908)
|
|
|
|
def _make_nav_item(self, key: str, text: str, glyph: str, y: int) -> None:
|
|
item = QWidget(self.sidebar)
|
|
item.setGeometry(0, y, 60, 60)
|
|
icon = QLabel(item)
|
|
icon.setStyleSheet("background:transparent;")
|
|
icon.setAlignment(Qt.AlignCenter)
|
|
icon.setGeometry(0, 6, 60, 26)
|
|
icon.setProperty("nav_key", key)
|
|
self._nav_icons[key] = icon
|
|
idx = [k for k, _, _ in NAV].index(key) if key in [k for k, _, _ in NAV] else -1
|
|
pix = QPixmap(os.path.join(ICON_DIR, f"nav_{idx}_gray.png" if idx >= 0 else "nav_settings.png"))
|
|
if not pix.isNull():
|
|
icon.setPixmap(pix.scaled(26, 26, Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
|
label = QLabel(text, item)
|
|
label.setAlignment(Qt.AlignCenter)
|
|
label.setStyleSheet("color:#C7C7C7;font-size:12px;background:transparent;")
|
|
label.setGeometry(0, 40, 60, 18)
|
|
|
|
item.mousePressEvent = lambda e, k=key: self.show_page(k)
|
|
item.setCursor(Qt.PointingHandCursor)
|
|
item.enterEvent = lambda e, w=item: self._nav_hover(w, True)
|
|
item.leaveEvent = lambda e, w=item: self._nav_hover(w, False)
|
|
icon.setAttribute(Qt.WA_TransparentForMouseEvents)
|
|
label.setAttribute(Qt.WA_TransparentForMouseEvents)
|
|
self.nav_buttons[key] = (item, icon, label)
|
|
|
|
def _nav_hover(self, item, entering: bool) -> None:
|
|
if getattr(item, "_active", False):
|
|
return
|
|
bg = "#282828" if entering else "#202020"
|
|
item.setStyleSheet(f"background:{bg};")
|
|
for child in item.findChildren(QLabel):
|
|
child.setStyleSheet(child.styleSheet().replace("#282828", "#202020") if not entering else child.styleSheet())
|
|
|
|
def _nav_select(self, key: str) -> None:
|
|
for k, (item, icon, label) in self.nav_buttons.items():
|
|
active = (k == key)
|
|
item._active = active
|
|
item.setStyleSheet(f"background:{'#252525' if active else '#202020'};")
|
|
color = "#80B9EE" if active else "#C7C7C7"
|
|
label.setStyleSheet(f"color:{color};font-size:12px;background:transparent;")
|
|
widget = self._nav_icons.get(k)
|
|
if widget is not None:
|
|
keys = [n[0] for n in NAV]
|
|
if k in keys:
|
|
idx = keys.index(k)
|
|
name = f"nav_{idx}_{'blue' if active else 'gray'}.png"
|
|
else:
|
|
name = "nav_settings.png"
|
|
pix = QPixmap(os.path.join(ICON_DIR, name))
|
|
if not pix.isNull():
|
|
widget.setPixmap(pix.scaled(26, 26, Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
|
if active:
|
|
self.nav_indicator.setGeometry(0, item.y(), 3, 60)
|
|
self.nav_indicator.show()
|
|
|
|
def show_page(self, key: str) -> None:
|
|
page = self.pages.get(key)
|
|
if not page:
|
|
return
|
|
self.stack.setCurrentWidget(page)
|
|
self.current = key
|
|
self._nav_select(key)
|
|
page.on_show() if hasattr(page, "on_show") else None
|
|
|
|
# ------------------------------------------------------------------ 后台任务
|
|
def run_bg(self, fn: Callable, on_done: Optional[Callable] = None) -> None:
|
|
bridge = Bridge()
|
|
|
|
def finish(result):
|
|
if on_done:
|
|
try:
|
|
on_done(result)
|
|
except Exception as exc:
|
|
self.log(f"回调错误:{exc}")
|
|
|
|
bridge.finished.connect(finish)
|
|
self._bridges.add(bridge)
|
|
|
|
def work():
|
|
try:
|
|
result = fn()
|
|
except Exception as exc:
|
|
result = exc
|
|
bridge.finished.emit(result)
|
|
self._bridges.discard(bridge)
|
|
|
|
threading.Thread(target=work, daemon=True).start()
|
|
|
|
# ------------------------------------------------------------------ 服务
|
|
def log(self, msg: str, level: str = "info") -> None:
|
|
import time
|
|
line = f"{time.strftime('%H:%M:%S')} {msg}"
|
|
self.logs.append(line)
|
|
if len(self.logs) > 1500:
|
|
self.logs = self.logs[-800:]
|
|
self.status.setText(line)
|
|
page = self.pages.get("log")
|
|
if page is not None:
|
|
try:
|
|
page.append(line, level)
|
|
except Exception:
|
|
pass
|
|
|
|
def toast_msg(self, text: str, kind: str = "info") -> None:
|
|
color = {"ok": "#43C08A", "warn": "#E0B04A", "error": "#E05C6E"}.get(kind, "#EEEEEE")
|
|
self.toast.setText(text)
|
|
self.toast.setStyleSheet(f"background:#3E3E3E;color:{color};padding:6px 12px;border-radius:4px;")
|
|
self.toast.adjustSize()
|
|
self.toast.move(680 - self.toast.width() // 2, 900)
|
|
self.toast.show()
|
|
QTimer.singleShot(2600, self.toast.hide)
|
|
|
|
# ------------------------------------------------------------------ 动作
|
|
def cmd(self, text: str, uid: Optional[str] = None) -> None:
|
|
text = (text or "").strip()
|
|
if not text:
|
|
self.toast_msg("指令为空", "warn")
|
|
return
|
|
self.log(f"执行:{text}")
|
|
|
|
def work():
|
|
return self.muip.exec_msg(text, uid)
|
|
|
|
def done(result):
|
|
if isinstance(result, Exception):
|
|
self.log(f"执行失败:{result}")
|
|
self.toast_msg("无法连接 MUIP 服务,详见日志", "error")
|
|
return
|
|
ok = int(result.get("retcode", 0)) == 0
|
|
msg = str(result.get("msg", "") or "succ")
|
|
self.log(f"→ {msg}")
|
|
self.toast_msg(f"{text[:40]} → {'成功' if ok else msg[:40]}", "ok" if ok else "error")
|
|
|
|
self.run_bg(work, done)
|
|
|
|
def online(self) -> None:
|
|
def done(result):
|
|
if isinstance(result, Exception):
|
|
self.log(f"查询在线人数失败:{result}")
|
|
self.toast_msg("查询失败,详见日志", "error")
|
|
return
|
|
self.log(f"当前在线人数:{result}")
|
|
self.toast_msg(f"当前在线人数:{result}", "ok")
|
|
self.run_bg(lambda: self.muip.online_count(), done)
|
|
|
|
def _refresh_status(self) -> None:
|
|
pass
|
|
|
|
# ------------------------------------------------------------------ 通用小部件
|
|
def page_icon_file(self, name: str, x: int, y: int, size: int = 32) -> QLabel:
|
|
lbl = QLabel(self)
|
|
lbl.setStyleSheet("background:transparent;")
|
|
pix = QPixmap(os.path.join(BASE_SKINS, name))
|
|
if not pix.isNull():
|
|
lbl.setPixmap(pix.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
|
lbl.setGeometry(x - SIDEBAR_W, y - TITLE_H, size, size)
|
|
return lbl
|
|
|
|
def page_icon(self, index: int, x: int, y: int, size: int = 32) -> QLabel:
|
|
lbl = QLabel(self)
|
|
lbl.setStyleSheet("background:transparent;")
|
|
pix = QPixmap(os.path.join(ICON_DIR, f"nav_{index}_blue.png"))
|
|
if not pix.isNull():
|
|
lbl.setPixmap(pix.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
|
lbl.setGeometry(x - SIDEBAR_W, y - TITLE_H, size, size)
|
|
return lbl
|
|
|
|
def card(self, parent: QWidget, x: int, y: int, w: int, h: int) -> QFrame:
|
|
f = QFrame(parent)
|
|
f.setObjectName("Card")
|
|
f.setAttribute(Qt.WA_StyledBackground, True)
|
|
f.setGeometry(x - SIDEBAR_W, y - TITLE_H, w, h)
|
|
f.lower()
|
|
return f
|
|
|
|
|
|
class Page(QWidget):
|
|
key = ""
|
|
|
|
def __init__(self, win: GMWindow, parent=None):
|
|
super().__init__(parent)
|
|
self.win = win
|
|
self.setObjectName("Page")
|
|
self.build()
|
|
|
|
def build(self) -> None:
|
|
pass
|
|
|
|
def on_show(self) -> None:
|
|
pass
|
|
|
|
def page_icon_file(self, name: str, x: int, y: int, size: int = 32) -> QLabel:
|
|
lbl = QLabel(self)
|
|
lbl.setStyleSheet("background:transparent;")
|
|
pix = QPixmap(os.path.join(BASE_SKINS, name))
|
|
if not pix.isNull():
|
|
lbl.setPixmap(pix.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
|
lbl.setGeometry(x - SIDEBAR_W, y - TITLE_H, size, size)
|
|
return lbl
|
|
|
|
def page_icon(self, index: int, x: int, y: int, size: int = 32) -> QLabel:
|
|
lbl = QLabel(self)
|
|
lbl.setStyleSheet("background:transparent;")
|
|
pix = QPixmap(os.path.join(ICON_DIR, f"nav_{index}_blue.png"))
|
|
if not pix.isNull():
|
|
lbl.setPixmap(pix.scaled(size, size, Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
|
lbl.setGeometry(x - SIDEBAR_W, y - TITLE_H, size, size)
|
|
return lbl
|
|
|
|
# 便捷:坐标为原程序 UIA 坐标(含 32px 标题栏),这里统一减去
|
|
def lbl(self, text: str, x: int, y: int, w: int = 0, h: int = 24, **kw) -> QLabel:
|
|
lbl = W.label(self, text, x - SIDEBAR_W, y - TITLE_H, w, h, **kw)
|
|
if kw.get("bold"):
|
|
lbl.setObjectName("H2" if kw.get("size", 0) and kw.get("size", 0) < 16 else "Title")
|
|
lbl.style().unpolish(lbl)
|
|
lbl.style().polish(lbl)
|
|
return lbl
|
|
|
|
def btn(self, text: str, x: int, y: int, w: int, h: int, cb=None, **kw) -> QPushButton:
|
|
return W.button(self, text, x - SIDEBAR_W, y - TITLE_H, w, h, cb, **kw)
|
|
|
|
def edit(self, x: int, y: int, w: int, h: int, text: str = "", ph: str = "", on_enter=None) -> QLineEdit:
|
|
return W.field(self, x - SIDEBAR_W, y - TITLE_H, w, h, text, ph, on_enter)
|
|
|
|
def combo(self, x: int, y: int, w: int, h: int, items=(), current: int = 0, on_change=None):
|
|
return W.combo(self, x - SIDEBAR_W, y - TITLE_H, w, h, items, current, on_change)
|
|
|
|
def search(self, x: int, y: int, w: int, h: int, provider, on_pick=None, ph="") -> "W.SearchCombo":
|
|
return W.SearchCombo(self, x - SIDEBAR_W, y - TITLE_H, w, h, provider, ph, on_pick)
|
|
|
|
def listbox(self, x: int, y: int, w: int, h: int) -> QListWidget:
|
|
return W.listbox(self, x - SIDEBAR_W, y - TITLE_H, w, h)
|
|
|
|
def slider(self, x: int, y: int, w: int, h: int, lo: int, hi: int, val: int):
|
|
return W.slider(self, x - SIDEBAR_W, y - TITLE_H, w, h, lo, hi, val)
|
|
|
|
def chip(self, text: str, x: int, y: int, w: int, h: int, checked=False, group=None, cb=None):
|
|
return W.chip(self, text, x - SIDEBAR_W, y - TITLE_H, w, h, checked, group, cb)
|
|
|
|
|
|
# ---------------------------------------------------------------------------- 页面
|
|
from .pages_item import ItemPage # noqa: E402
|
|
from .pages_weapon import WeaponPage # noqa: E402
|
|
from .pages_avatar import AvatarPage # noqa: E402
|
|
from .pages_artifact import ArtifactPage # noqa: E402
|
|
from .pages_generate import GeneratePage # noqa: E402
|
|
from .pages_quest import QuestPage # noqa: E402
|
|
from .pages_misc import MiscPage # noqa: E402
|
|
from .pages_template import TemplatePage # noqa: E402
|
|
from .pages_start import StartPage # noqa: E402
|
|
from .pages_log import LogPage # noqa: E402
|
|
from .pages_about import AboutPage # noqa: E402
|
|
from .pages_settings import SettingsPage # noqa: E402
|
|
|
|
PAGES = {
|
|
"item": ItemPage, "weapon": WeaponPage, "avatar": AvatarPage, "artifact": ArtifactPage,
|
|
"generate": GeneratePage, "quest": QuestPage, "misc": MiscPage, "template": TemplatePage,
|
|
"start": StartPage, "log": LogPage, "about": AboutPage, "settings": SettingsPage,
|
|
}
|
|
|
|
|
|
def run() -> int:
|
|
app = QApplication(sys.argv)
|
|
theme.install(app)
|
|
win = GMWindow()
|
|
win.show()
|
|
hwnd = int(win.winId())
|
|
_dark_titlebar(hwnd)
|
|
return app.exec()
|