|
| 1 | +""" |
| 2 | + :file: main.py |
| 3 | + :author: Zhu Dengda ([email protected]) |
| 4 | + :date: 2024-11 |
| 5 | +
|
| 6 | + PyFMM二维模型下的交互界面,基于PyQt5开发 |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget |
| 11 | +from PyQt5 import QtCore, QtGui, QtWidgets |
| 12 | +from PyQt5 import uic |
| 13 | +import matplotlib as mpl |
| 14 | +from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
| 15 | +import matplotlib.pyplot as plt |
| 16 | +import numpy as np |
| 17 | +import os, sys |
| 18 | +from scipy.ndimage import gaussian_filter |
| 19 | + |
| 20 | +from .subwidget import MatplotlibWidget |
| 21 | +from .utils import try_except_decorator |
| 22 | + |
| 23 | + |
| 24 | +class MainWindow(QMainWindow): |
| 25 | + def __init__(self): |
| 26 | + super().__init__() |
| 27 | + uic.loadUi(os.path.join(os.path.dirname(__file__), "main.ui"), self) # 加载 UI 文件 |
| 28 | + |
| 29 | + |
| 30 | + self.mplwidget = MatplotlibWidget(self) |
| 31 | + |
| 32 | + self.mplwidget.canvas.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor)) |
| 33 | + self.mplwidget.canvas.setMouseTracking(True) |
| 34 | + # 将 Canvas 嵌入到 layout 中 |
| 35 | + self.verticalLayout_mpl.addWidget(self.mplwidget.canvas) |
| 36 | + |
| 37 | + # 绑定更新按钮 |
| 38 | + self.updateSrcLocButton.clicked.connect(self.update_plot) |
| 39 | + self.clearRcvButton.clicked.connect(self.clear_rcv) |
| 40 | + self.updateVelButton.clicked.connect(self.update_velocity) |
| 41 | + self.redoButton.clicked.connect(self.redo_rcv) |
| 42 | + |
| 43 | + # 定义参数 |
| 44 | + self.plot_param = {} |
| 45 | + self.update_srcloc() |
| 46 | + self.clear_rcv() |
| 47 | + |
| 48 | + self.update_velocity() |
| 49 | + |
| 50 | + def update_srcloc(self): |
| 51 | + self.plot_param['srcloc'] = [float(self.lineEdit_srcX.text()), float(self.lineEdit_srcY.text())] |
| 52 | + |
| 53 | + def clear_rcv(self): |
| 54 | + self.textBrowser_rcv.clear() |
| 55 | + self.textBrowser_rcv.append(f"{'X':>8s} {'Y':>8s} {'T':>6s}") |
| 56 | + for _ in range(len(self.mplwidget.plot_handle['rays'])): |
| 57 | + h = self.mplwidget.plot_handle['rays'].pop() |
| 58 | + h.remove() |
| 59 | + h = self.mplwidget.plot_handle['rcvdots'].pop() |
| 60 | + h.remove() |
| 61 | + |
| 62 | + self.mplwidget.canvas_redraw() |
| 63 | + |
| 64 | + def redo_rcv(self): |
| 65 | + self.delete_textBrowser_rcv_last_line() |
| 66 | + if len(self.mplwidget.plot_handle['rays']) == 0: |
| 67 | + return |
| 68 | + |
| 69 | + h = self.mplwidget.plot_handle['rays'].pop() |
| 70 | + h.remove() |
| 71 | + h = self.mplwidget.plot_handle['rcvdots'].pop() |
| 72 | + h.remove() |
| 73 | + self.mplwidget.canvas_redraw() |
| 74 | + |
| 75 | + def delete_textBrowser_rcv_last_line(self): |
| 76 | + # 获取当前文本内容 |
| 77 | + content = self.textBrowser_rcv.toPlainText() |
| 78 | + |
| 79 | + # 按行分割文本 |
| 80 | + lines = content.split('\n') |
| 81 | + |
| 82 | + # 删除最后一行,保留X,Y,T,如果有内容的话 |
| 83 | + if len(lines) > 1: |
| 84 | + lines.pop() |
| 85 | + |
| 86 | + # 更新 QTextBrowser 内容 |
| 87 | + # self.textBrowser_rcv.setPlainText('\n'.join(lines)) |
| 88 | + self.textBrowser_rcv.setPlainText("") |
| 89 | + for line in lines: |
| 90 | + self.textBrowser_rcv.append(line) |
| 91 | + |
| 92 | + @try_except_decorator("statusBar") |
| 93 | + def update_velocity(self, *args): |
| 94 | + namespace = {"np":np} |
| 95 | + exec(self.textEdit_vel.toPlainText(), namespace) |
| 96 | + |
| 97 | + self.plot_param['xarr'] = np.linspace(0, namespace['xmax'], namespace['nx']).copy() |
| 98 | + self.plot_param['yarr'] = np.linspace(0, namespace['ymax'], namespace['ny']).copy() |
| 99 | + vel2d = namespace['vel2d'].copy() |
| 100 | + vel2d[vel2d < 0.0] = 0.1 |
| 101 | + |
| 102 | + self.plot_param['vel2d'] = vel2d |
| 103 | + |
| 104 | + self.clear_rcv() |
| 105 | + self.mplwidget.plot_velocity(self.plot_param['xarr'], self.plot_param['yarr'], self.plot_param['vel2d']) |
| 106 | + self.update_plot() |
| 107 | + |
| 108 | + def update_plot(self): |
| 109 | + # 读入参数 |
| 110 | + self.update_srcloc() |
| 111 | + |
| 112 | + self.clear_rcv() |
| 113 | + |
| 114 | + # 衡量范围 |
| 115 | + if self.plot_param['srcloc'][0] > self.plot_param['xarr'][-1] or \ |
| 116 | + self.plot_param['srcloc'][0] < self.plot_param['xarr'][0] or \ |
| 117 | + self.plot_param['srcloc'][1] > self.plot_param['yarr'][-1] or \ |
| 118 | + self.plot_param['srcloc'][1] < self.plot_param['yarr'][0]: |
| 119 | + |
| 120 | + self.statusBar().showMessage(f"source location out of bound!", 3000) |
| 121 | + return |
| 122 | + |
| 123 | + self.statusBar().showMessage(f"Fast Marcing ......") |
| 124 | + QApplication.processEvents() # 强制刷新事件循环 |
| 125 | + # 调用 canvas 的 plot 方法刷新绘图 |
| 126 | + self.mplwidget.plot( |
| 127 | + self.plot_param['srcloc'], |
| 128 | + self.plot_param['xarr'], self.plot_param['yarr'], self.plot_param['vel2d']) |
| 129 | + self.statusBar().showMessage(f"Done.", 1000) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + app = QApplication(sys.argv) |
| 134 | + window = MainWindow() |
| 135 | + window.show() |
| 136 | + sys.exit(app.exec_()) |
0 commit comments