dataCreatingGUI/bin/GUIWindows.py

213 lines
6.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding:utf-8 -*-
import sys
import logging
from file import fileDataCreating
from common import global_var
from pathlib import Path
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import Signal, QThread, QObject, QEventLoop, QTimer
from PySide6.QtWidgets import QApplication, QFileDialog, QMessageBox
from PySide6.QtGui import QTextCursor
# 声明全局变量
global_var._init()
global_var.set_value('FromGUI', True)
# 控制台重定向到GUI
class EmittingStr(QObject):
# 定义信号
textWritten = Signal(str)
# 将控制台的内容输出到QTextBrowser
def write(self, text):
# 调用 emit方法 发信号时,传入参数 必须是这里指定的 参数类型
self.textWritten.emit(str(text))
loop = QEventLoop()
QTimer.singleShot(100, loop.quit)
loop.exec_()
QApplication.processEvents()
# logging重定向到print
class PrintHandler(logging.Handler):
def emit(self, record):
pass
# 处理文件的线程
class FileThread(QThread):
def __init__(self, parent=None):
super().__init__(parent=parent)
# 覆盖原来run去调用文件造数据脚本
def run(self):
fileDataCreating.main()
# GUI的主程序
class Window:
def __init__(self):
# 载入ui
path = Path(__file__)
ui_file_patch = path.parent.parent / "ui" / "ui_main.ui"
self.ui = QUiLoader().load(str(ui_file_patch))
# 根据UI尺寸设定值禁止拉伸窗体
self.ui.setFixedSize(self.ui.width(), self.ui.height())
# 指定按钮和槽函数
self.ui.startButton.clicked.connect(self.start)
self.ui.stopButton.clicked.connect(self.stop)
self.ui.conn_Button.clicked.connect(self.conntest)
self.ui.choosedir_Button.clicked.connect(self.choosedir)
self.ui.actionabout.triggered.connect(self.about)
self.setup_thread()
# QTextBrowser输出设置限制条数为100
self.ui.Log_Output.document().setMaximumBlockCount(100)
self.ui.Log_Output.ensureCursorVisible()
# 将控制台输出重定向到QTextBrowser
sys.stdout = EmittingStr()
sys.stdout.textWritten.connect(self.outputWritten)
# 创建并配置logger
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
# 创建并配置处理程序
handler = PrintHandler()
handler.setFormatter(logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'))
self.logger.addHandler(handler)
# 输出到GUI信号处理函数
def outputWritten(self, text):
cursor = self.ui.Log_Output.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.ui.Log_Output.setTextCursor(cursor)
self.ui.Log_Output.ensureCursorVisible()
def start(self):
self.logger.info("点击了开始按钮")
# 按钮置灰
self.ui.startButton.setEnabled(False)
# 输入框参数校验
num = self.ui.tabWidget.currentIndex()
if num == 0:
if not self.check_file_input():
self.logger.info("参数校验失败,请检查输入的值")
self.ui.startButton.setEnabled(True)
return
self.logger.info("参数校验通过,开始执行")
# 持续性生成和一次性的
if self.ui.checkBox.isChecked():
self.start_thread()
else:
fileDataCreating.new()
self.ui.startButton.setEnabled(True)
self.logger.info("*"*5 + "一次性任务执行完成" + "*"*5)
else:
self.conntest()
self.start_run_db()
def stop(self):
self.logger.info("点击了停止按钮")
num = self.ui.tabWidget.currentIndex()
if num == 0:
self.stop_run_file()
else:
self.stop_run_db()
# 按钮恢复
# self.startButton.isEnabled() 获取按钮的状态
self.ui.startButton.setEnabled(True)
def conntest(self):
self.logger.info("点击了测试连接按钮")
self.check_db_input()
# TODO(MH):校验连接信息
self.logger.info("数据库连接成功")
def choosedir(self):
self.logger.info("点击了选择目录按钮")
dir_path = QFileDialog.getExistingDirectory(
self.ui, "选择目录", "", QFileDialog.ShowDirsOnly)
self.ui.fileOutputText.setText(dir_path)
self.logger.info("已经选择文件夹:{}".format(dir_path))
def setup_thread(self):
self.thread = FileThread()
global_var.set_value('start_flag', True)
self.thread_running = global_var.get_value('start_flag')
def start_thread(self):
if self.thread_running:
self.thread.start()
else:
self.setup_thread()
self.thread.start()
def check_common(self, elemnet):
# 为空标红处理
elemnet.setStyleSheet("")
if not elemnet.text():
elemnet.setStyleSheet("border: 1px solid red;")
# 改变状态
self.check_flag = False
return elemnet.text()
def check_file_input(self):
self.check_flag = True
# 获取界面输入
file_dict = {}
file_dict['filename'] = self.check_common(self.ui.fileNameText)
file_dict['outputpath'] = self.ui.fileOutputText.text()
file_dict['fileinit'] = self.check_common(self.ui.fileInitRowsText)
# 是否持续
file_dict['filecreating'] = self.ui.checkBox.isChecked()
if file_dict['filecreating']:
file_dict['fileinsert'] = self.check_common(
self.ui.fileInsertRowsText)
file_dict['filedel'] = self.check_common(self.ui.fileDelRowsText)
# 将值放到全局变量中
global_var.set_value('file_args', file_dict)
return self.check_flag
def check_db_input(self):
self.logger.info("数据库参数校验通过,开始连接数据库测试。。。")
def start_run_file(self):
# 调用文件处理脚本
self.logger.info("开始生成文件。。。")
def start_run_db(self):
pass
def stop_run_file(self):
# 终止线程的事件循环
global_var.set_value('start_flag', False)
self.thread.quit()
# 标记线程停止
self.thread_running = global_var.get_value('start_flag')
def stop_run_db(self):
pass
def about(self):
msg = "版本: <a>v1.0.0</a><br>\
项目地址: <a href='https://gitea.kiki.kim/ha/dataCreatingGUI'>https://gitea.kiki.kim/ha/dataCreatingGUI</a><br>\
E-mail: <a href='mailto:halliday2023@163.com'>halliday2023@163.com</a>"
QMessageBox.about(
self.ui,
'关于',
msg)