diff --git a/src/main.py b/src/main.py index 3f047db..90d86fe 100644 --- a/src/main.py +++ b/src/main.py @@ -1,14 +1,34 @@ # from fbs_runtime.application_context.PyQt5 import ApplicationContext +import time from PyQt5 import QtWidgets from PyQt5 import QtCore +from PyQt5.QtWidgets import QApplication,QMessageBox +from PyQt5.QtNetwork import QLocalSocket,QLocalServer import sys, os import mainWindowGUI as mainWindowGUI import mineSweeperGUI as mineSweeperGUI import ctypes from ctypes import wintypes - os.environ["QT_FONT_DPI"] = "96" +def on_new_connection(localServer:QLocalServer): + """当新连接进来时,接受连接并将文件路径传递给主窗口""" + socket = localServer.nextPendingConnection() + if socket: + socket.readyRead.connect(lambda: on_ready_read(socket)) + +def on_ready_read(socket:QLocalSocket): + """从socket读取文件路径并传递给主窗口""" + if socket and socket.state() == QLocalSocket.ConnectedState: + # 读取文件路径并调用打开文件 + socket.waitForReadyRead(500) + file_path = socket.readAll().data().decode() + for win in QApplication.topLevelWidgets(): + if isinstance(win, mainWindowGUI.MainWindow): + win.dropFileSignal.emit(file_path) + socket.disconnectFromServer() # 断开连接 + + def find_window(class_name, window_name): """ @@ -37,26 +57,38 @@ def find_window(class_name, window_name): if __name__ == "__main__": # QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) - - app = QtWidgets.QApplication (sys.argv) - mainWindow = mainWindowGUI.MainWindow() - ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv) - ui.mainWindow.show() - ui.mainWindow.game_setting_path = ui.game_setting_path - - _translate = QtCore.QCoreApplication.translate - hwnd = find_window(None, _translate("MainWindow", "元扫雷")) - - SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity - ui.disable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000011) else 1/0 - ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000000) else 1/0 - - - - - sys.exit(app.exec_()) - ... - + try: + app = QtWidgets.QApplication (sys.argv) + serverName = "MineSweeperServer" + socket = QLocalSocket() + socket.connectToServer(serverName) + if socket.waitForConnected(500): + if len(sys.argv) == 2: + filePath = sys.argv[1] + socket.write(filePath.encode()) + socket.flush() + time.sleep(0.5) + app.quit() + else: + localServer = QLocalServer() + localServer.listen(serverName) + localServer.newConnection.connect(lambda: on_new_connection(localServer=localServer)) + mainWindow = mainWindowGUI.MainWindow() + ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv) + ui.mainWindow.show() + ui.mainWindow.game_setting_path = ui.game_setting_path + + _translate = QtCore.QCoreApplication.translate + hwnd = find_window(None, _translate("MainWindow", "元扫雷")) + + SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity + ui.disable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000011) else 1/0 + ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000000) else 1/0 + + sys.exit(app.exec_()) + ... + except Exception as e: + pass # 最高优先级 # 计时器快捷键切换 diff --git a/src/mainWindowGUI.py b/src/mainWindowGUI.py index 92fb006..11a51ec 100644 --- a/src/mainWindowGUI.py +++ b/src/mainWindowGUI.py @@ -1,23 +1,30 @@ from PyQt5 import QtCore, QtWidgets from PyQt5.QtCore import Qt -from PyQt5.QtCore import QTimer +from PyQt5.QtCore import QTimer, QFileInfo from PyQt5.QtWidgets import QApplication - +from PyQt5.QtGui import QDragEnterEvent, QDropEvent # 重写QMainWindow + + class MainWindow(QtWidgets.QMainWindow): keyRelease = QtCore.pyqtSignal(str) closeEvent_ = QtCore.pyqtSignal() + dropFileSignal = QtCore.pyqtSignal(str) flag_drag_border = False minimum_counter = 0 + def __init__(self): + super(MainWindow, self).__init__() + self.setAcceptDrops(True) + def closeEvent(self, event): self.closeEvent_.emit() def keyReleaseEvent(self, event): if event.key() == Qt.Key_Space and not event.isAutoRepeat(): self.keyRelease.emit('Space') - - def resizeEvent(self,event): + + def resizeEvent(self, event): # 拖拽边框后resize尺寸 if QApplication.mouseButtons() & Qt.LeftButton: self.flag_drag_border = True @@ -35,4 +42,22 @@ def __minimumWindowRelease(self): self.minimum_counter = 0 self.timer_.stop() + def dragEnterEvent(self, event: QDragEnterEvent): + super().dragEnterEvent(event) + if event.mimeData().hasUrls(): + url = event.mimeData().urls()[0] + if url.isLocalFile(): + fileType = QFileInfo(url.toLocalFile()).suffix() + if fileType in ('evf', 'avf', 'rmv', 'mvf'): + event.acceptProposedAction() + return + event.ignore() + def dropEvent(self, event: QDropEvent): + super().dropEvent(event) + files = event.mimeData().urls() + if len(files) > 0: + url = files[0] + if url.isLocalFile(): + filePath = url.toLocalFile() + self.dropFileSignal.emit(filePath) diff --git a/src/mineSweeperGUI.py b/src/mineSweeperGUI.py index 2e2343e..2b9cbbc 100644 --- a/src/mineSweeperGUI.py +++ b/src/mineSweeperGUI.py @@ -19,10 +19,10 @@ # from PyQt5.QtWidgets import QApplication # from country_name import country_name import metaminesweeper_checksum - +from mainWindowGUI import MainWindow class MineSweeperGUI(superGUI.Ui_MainWindow): - def __init__(self, MainWindow, args): + def __init__(self, MainWindow: MainWindow, args): self.mainWindow = MainWindow self.checksum_guard = metaminesweeper_checksum.ChecksumGuard() @@ -110,7 +110,7 @@ def save_evf_file_integrated(): self.relative_path = args[0] # 用本软件打开录像 if len(args) == 2: - self.action_OpenFile(args[1]) + self.action_OpenFile(openfile_name=args[1]) self.trans_language() self.score_board_manager.with_namespace({ @@ -124,6 +124,7 @@ def save_evf_file_integrated(): self.score_board_manager.visible() self.mainWindow.closeEvent_.connect(self.closeEvent_) + self.mainWindow.dropFileSignal.connect(self.action_OpenFile) @property def pixSize(self):