import os.path import error from ui.mainwindow import Ui_mainWindow from PyQt6 import QtWidgets from PyQt6.QtWidgets import QMainWindow, QFileDialog, QMessageBox, QToolBar from PyQt6.QtCore import QSettings, QFileInfo, Qt from PyQt6.QtGui import QAction import datetime from PWFile import ControlFile class MainWindow(QMainWindow, Ui_mainWindow): @staticmethod def _test_if_file_occupied(file_path): if not os.path.exists(file_path): return False try: with open(file_path, "wb") as file: pass return False except PermissionError: return True def open_dialog(self): z_control_file_paths = QFileDialog.getOpenFileNames( self, "打开Excel文件", filter="Excel 文件(*.xlsx)", directory=self._setting.value("last_working_directory"), )[0] for z_control_file_path in z_control_file_paths: if z_control_file_path != "": cf = ControlFile( z_control_file_path, close_cad_document=self.cBCloseCadDoc.isChecked(), ) if self._test_if_file_occupied(cf.get_zt_dwg_file_path()): QMessageBox.warning( self, "注意", f"{cf.get_zt_dwg_file_path()}被占用,请先关闭。" ) return try: cf.draw() except error.DWGFileNotExistError as dwg_not_exist_exception: QMessageBox.warning( self, "错误", f"DWG文件{dwg_not_exist_exception.not_exist_dwg_file_path}不存在。", ) continue self.statusBar().showMessage( f"{datetime.datetime.now()} Finished.", 8000 ) file_info = QFileInfo(z_control_file_path) self._setting.setValue( "last_working_directory", file_info.absoluteDir().absolutePath() ) zfile_name = file_info.baseName() QMessageBox.information(self, "提示", zfile_name + "断面图已生成。") def _cBCloseCadDocClicked(self): self._setting.setValue("close_cad", self.cBCloseCadDoc.isChecked()) def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.pBOpenControlZFile.clicked.connect(self.open_dialog) self._setting = QSettings("NWEPDI", "Plate", self) if not self._setting.value("close_cad"): self.cBCloseCadDoc.setChecked(False) else: self.cBCloseCadDoc.setChecked(self._setting.value("close_cad", type=bool)) self.cBCloseCadDoc.clicked.connect(self._cBCloseCadDocClicked) self._toolbar = QToolBar(self) toolbar = self._toolbar toolbar.setAllowedAreas(Qt.ToolBarArea.TopToolBarArea) toolbar.setMovable(False) about_action = QAction("注意事项", self) about_action.triggered.connect( lambda: QMessageBox.information( self, "注意", "注意检查excel表格是否有电气科权限,wps是否安装了vba。" ) ) toolbar.addAction(about_action) self.addToolBar(toolbar) pass