压测工具开发实战篇(六)——client子窗口整体功能

在这里插入图片描述

你好,我是安然无虞。

文章目录


在这里插入图片描述

前篇文章已经实现了client子窗口的大部分功能:
压测工具开发实战篇(五)——代码助手框功能

对于 client 子窗口的工具栏动作, 主要是 添加、刷新、保存、运行四个功能, 对应的信号和槽函数如下:

# 设置工具栏条目图标
self.ui.action_addone.setIcon(qta.icon('fa5.file'))
self.ui.action_refresh.setIcon(qta.icon('mdi.refresh'))
self.ui.action_savefile.setIcon(qta.icon('fa5.save'))
self.ui.action_run.setIcon(qta.icon('msc.run'))

# 定义点击工具栏action事件处理
# 添加、刷新、保存、运行
self.ui.action_addone.triggered.connect(self.action_addone)
self.ui.action_refresh.triggered.connect(self.list_file_on_tree)
self.ui.action_savefile.triggered.connect(self.action_savefile)
self.ui.action_run.triggered.connect(self.action_run)

这里是前面前面文章提到的树控件相关操作:

# 设置树控件上下文策略 - 也可以在 Qt Designer 中设置
self.ui.tree_file.setContextMenuPolicy(Qt.CustomContextMenu)
# 定义信号处理方法
self.ui.tree_file.customContextMenuRequested.connect(self.show_context_menu_on_filetree)

# 树节点名称修改会触发ItemChanged信号
self.ui.tree_file.itemChanged.connect(self.item_changed)

# 树节点被点击 - 选择该代码文件
self.ui.tree_file.itemClicked.connect(self.item_clicked)

还有编辑框中代码改动设计的内容:

# 编辑框文本内容改动事件
self.ui.te_code.textChanged.connect(self.handle_text_change)

现在需要对里面功能的细节进行完善, 比如选择代码文件后才可以使用右侧代码助手框来编辑代码,

所以我们在处理 代码助手框被点击 的事件时, 需要先判断是否选择了代码文件, 如果没有, 会跳出提示框提示, 只有选择了代码文件, 才可以编辑代码.

def code_helper_clicked(self, code):
    """代码助手被点击"""
    if not self.curFilePath:
        QMessageBox.warning(self.ui, '', '请先打开代码文件')
        return
    self.ui.te_code.insertPlainText(code)

当我们点击树节点的时候. 会加载新文件, 如果之前的代码文件没有保存的话需要先保存, 我们还要设置当前子窗口的标题栏, 在修改后的代码文件没有保存和已经保存的要有区分.

TITLEBAR_PREFIX = '客户端定义'
def item_clicked(self, item, column):
    """树节点被点击, 加载新文件"""
    # 获取被点击的节点文本
    fileName = item.text(column)
    filePath = os.path.join(self.thisFolderPath, fileName)

    # 如果这个要打开的文件就是当前文件, 直接返回
    if filePath == self.curFilePath:
        return

    # 如果前面的当前文件还没有保存, 先保存
    if self.textChanged:
        self.action_savefile()

    try:
        with open(filePath, encoding='utf-8') as f:
            self.ui.te_code.setEnabled(True) # 解除编辑框的禁用状态
            self.ui.te_code.setPlainText(f.read())

        self.curFilePath = filePath
        self.curTitleBarText = f'{
      
      self.TITLEBAR_PREFIX} {
      
      fileName}'
        self.ui.setWindowTitle(self.curTitleBarText)
        self.textChanged = False

    except:
        SI.logInfo(traceback.format_exc())

文本框中代码文件改动的时候, 需要这样处理:

def handle_text_change(self):
    """处理代码文件内容改动"""
    if self.textChanged:
        return
    self.textChanged = True
    self.ui.setWindowTitle(self.curTitleBarText + ' * ')

这样实现之后, 如果没有对代码进行保存, 最上层的子窗口标题栏那块会有 * 号提示:
在这里插入图片描述
我们写好代码后, 需要对代码文件进行保存:

def action_savefile(self):
    """保存代码文件"""
    if not self.textChanged:
        return

    if not self.curFilePath:
        return

    try:
        with open(self.curFilePath, 'w', encoding='utf-8') as f:
            f.write(self.ui.te_code.toPlainText())

        self.curTitleBarText = f'{
      
      self.TITLEBAR_PREFIX} {
      
      os.path.basename(self.curFilePath)}'
        self.ui.setWindowTitle(self.curTitleBarText)
        self.textChanged = False
    except:
        SI.logInfo(traceback.format_exc())

运行代码暂时给出部分示例代码, 后面用到了再说:

testFileTopLines = '''
import time
from pprint import pprint
from time import sleep
from hyload.httpclient import HttpsClient,HttpClient
from hyload.logger import TestLogger
from hyload.stats import Stats
# 此参数只有被性能场景调用时才会传入
arg = None
'''
def action_run(self):
    """运行"""
    pyFile = os.path.join(SI.projectPath, 'run.py')
    if os.path.exists(pyFile):
        os.remove(pyFile)

    # 先确保保存文件内容了
    self.action_savefile()

    code = self.ui.te_code.toPlainText()

    # 加上开头的一些固定代码行, 主要是导入必须的名字
    code = self.testFileTopLines + code

    with open(pyFile, 'w', encoding='utf-8') as f:
        f.write(code)

    cmd = f'cd /d {
      
      SI.projectPath} &&start call "{
      
      sys.executable}" run.py'
    SI.logInfo(f'产生代码文件 {
      
      pyFile} , 运行 ===>')
    os.system(cmd)
遇见安然遇见你,不负代码不负卿。
谢谢老铁的时间,咱们下篇再见!