From 7841368a1f5e43cf72a8f9f63556267707d238fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Thu, 11 Apr 2024 23:20:52 +0800 Subject: [PATCH] fix Update dialog_savedgame.py --- .../LunaTranslator/gui/dialog_savedgame.py | 90 ++++++++++++++----- .../LunaTranslator/gui/specialwidget.py | 10 ++- 2 files changed, 77 insertions(+), 23 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py index d04bc419..bd964fe7 100644 --- a/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py +++ b/LunaTranslator/LunaTranslator/gui/dialog_savedgame.py @@ -33,6 +33,7 @@ from PyQt5.QtWidgets import ( from PyQt5.QtGui import ( QCloseEvent, QIntValidator, + QMouseEvent, QResizeEvent, QPixmap, QPainter, @@ -68,16 +69,33 @@ from gui.inputdialog import noundictconfigdialog1 class ItemWidget(QWidget): focuschanged = pyqtSignal(bool, str) doubleclicked = pyqtSignal(str) + globallashfocus = None + + @classmethod + def clearfocus(cls): + try: # 可能已被删除 + if ItemWidget.globallashfocus: + ItemWidget.globallashfocus.focusOut() + except: + pass + ItemWidget.globallashfocus = None - def focusInEvent(self, ev) -> None: - self.bottommask.setStyleSheet( - "QLabel { background-color: " - + globalconfig["dialog_savegame_layout"]["onselectcolor"] - + "; }" - ) - self.focuschanged.emit(True, self.exe) + def mousePressEvent(self, ev) -> None: + try: + self.bottommask.setStyleSheet( + "QLabel { background-color: " + + globalconfig["dialog_savegame_layout"]["onselectcolor"] + + "; }" + ) - def focusOutEvent(self, event): + if self != ItemWidget.globallashfocus: + ItemWidget.clearfocus() + ItemWidget.globallashfocus = self + self.focuschanged.emit(True, self.exe) + except: + print_exc() + + def focusOut(self): self.bottommask.setStyleSheet( "QLabel { background-color: rgba(255,255,255, 0); }" ) @@ -128,7 +146,7 @@ class ItemWidget(QWidget): self.imgh = self.itemh - textH - 2 * margin # self.setFixedSize(QSize(self.itemw, self.itemh)) - self.setFocusPolicy(Qt.StrongFocus) + # self.setFocusPolicy(Qt.StrongFocus) self.maskshowfileexists = QLabel(self) self.bottommask = QLabel(self) layout = QVBoxLayout() @@ -1331,9 +1349,7 @@ class dialog_savedgame_new(saveposwindow): def clicked3(self): - f = QFileDialog.getOpenFileName( - directory="", options=QFileDialog.DontResolveSymlinks - ) + f = QFileDialog.getOpenFileName(options=QFileDialog.DontResolveSymlinks) res = f[0] if res != "": @@ -1347,9 +1363,11 @@ class dialog_savedgame_new(saveposwindow): _ = list(tags) _.remove(_TR("存在")) tags = tuple(_) + ItemWidget.clearfocus() self.formLayout.removeWidget(self.flow) self.idxsave.clear() self.flow = ScrollFlow() + self.flow.bgclicked.connect(ItemWidget.clearfocus) self.formLayout.insertWidget(self.formLayout.count() - 1, self.flow) for k in savehook_new_list: if checkexists and os.path.exists(k) == False: @@ -1371,6 +1389,41 @@ class dialog_savedgame_new(saveposwindow): self.newline(k) QApplication.processEvents() + def showmenu(self, p): + menu = QMenu(self) + startgame = QAction(_TR("开始游戏")) + gamesetting = QAction(_TR("游戏设置")) + delgame = QAction(_TR("删除游戏")) + opendir = QAction(_TR("打开目录")) + addgame = QAction(_TR("添加游戏")) + batchadd = QAction(_TR("批量添加")) + othersetting = QAction(_TR("其他设置")) + + if self.currentfocuspath: + menu.addAction(startgame) + menu.addAction(gamesetting) + menu.addAction(delgame) + menu.addAction(opendir) + else: + menu.addAction(addgame) + menu.addAction(batchadd) + menu.addAction(othersetting) + action = menu.exec(self.mapToGlobal(p)) + if action == startgame: + self.startgame(self.currentfocuspath) + elif action == gamesetting: + self.showsettingdialog() + elif action == delgame: + self.clicked2() + elif action == opendir: + self.clicked4() + elif action == addgame: + self.clicked3() + elif action == batchadd: + self.clicked3_batch() + elif action == othersetting: + dialog_syssetting(self) + def __init__(self, parent) -> None: super().__init__( parent, @@ -1386,7 +1439,8 @@ class dialog_savedgame_new(saveposwindow): self.tagswidget.tagschanged.connect(self.tagschanged) formLayout.addWidget(self.tagswidget) self.flow = ScrollFlow() - + self.setContextMenuPolicy(Qt.CustomContextMenu) + self.customContextMenuRequested.connect(self.showmenu) formLayout.addWidget(self.flow) self.formLayout = formLayout buttonlayout = QHBoxLayout() @@ -1440,18 +1494,12 @@ class dialog_savedgame_new(saveposwindow): def itemfocuschanged(self, b, k): if b: - if self.currentfocuspath == k: - return - self.activategamenum += 1 self.currentfocuspath = k else: - self.activategamenum -= 1 self.currentfocuspath = None - _able = self.activategamenum > 0 - for _btn, exists in self.savebutton: - _able1 = _able and ( + _able1 = b and ( (not exists) or (self.currentfocuspath) and (os.path.exists(self.currentfocuspath)) @@ -1479,4 +1527,4 @@ class dialog_savedgame_new(saveposwindow): else: self.flow.addwidget(gameitem) - self.idxsave.append(k) \ No newline at end of file + self.idxsave.append(k) diff --git a/LunaTranslator/LunaTranslator/gui/specialwidget.py b/LunaTranslator/LunaTranslator/gui/specialwidget.py index 82fbc5b5..58278ddc 100644 --- a/LunaTranslator/LunaTranslator/gui/specialwidget.py +++ b/LunaTranslator/LunaTranslator/gui/specialwidget.py @@ -1,5 +1,5 @@ from PyQt5.QtWidgets import QWidget, QSizePolicy, QListWidget, QScrollArea -from PyQt5.QtGui import QPainter, QPen, QFont, QFontMetrics +from PyQt5.QtGui import QMouseEvent, QPainter, QPen, QFont, QFontMetrics from PyQt5.QtCore import Qt from PyQt5.QtWidgets import ( QSpacerItem, @@ -119,6 +119,8 @@ class chartwidget(QWidget): class ScrollFlow(QWidget): + bgclicked = pyqtSignal() + def resizeEvent(self, a0) -> None: self.qscrollarea.resize(self.size()) return super().resizeEvent(a0) @@ -126,7 +128,11 @@ class ScrollFlow(QWidget): def __init__(self): super(ScrollFlow, self).__init__() - self.listWidget = QListWidget(self) + class _QListWidget(QListWidget): + def mousePressEvent(_, _2) -> None: + self.bgclicked.emit() + + self.listWidget = _QListWidget(self) # self.listWidget.setFixedWidth(600) self.l = FlowLayout()