-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_version.py
382 lines (316 loc) · 17.1 KB
/
test_version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import sys
import matplotlib.pyplot as plt
import numpy as np
from PyQt5 import QtPrintSupport
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QImage, QPixmap, QPainter, QFont
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QLabel, QLineEdit, QPushButton,
QGraphicsView, QGraphicsScene, QVBoxLayout, QHBoxLayout,
QGroupBox, QGridLayout, QFileDialog, QWidget, QComboBox
)
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QGraphicsOpacityEffect
class ParabolicPlotter(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口标题和大小
self.setWindowTitle("模场直径曲线")
self.setGeometry(100, 100, 1000, 800)
# 输入框
self.N =41
self.x_line_edits = []
self.y_line_edits = []
self.groups = []
self.fiber_number_edits = QLineEdit()
fiber_number_label = QLabel("光纤编号:", self)
fiber_number_label.setFont(QFont("Arial", 12))
hbox2 = QHBoxLayout()
hbox2.addWidget(fiber_number_label)
hbox2.addWidget(self.fiber_number_edits)
#self.fiber_number_edits.setFixedSize(500, 30)
self.name_edits = QLineEdit()
name_label = QLabel("姓名 :", self)
name_label.setFont(QFont("Arial", 12))
hbox4 = QHBoxLayout()
hbox4.addWidget(name_label)
hbox4.addWidget(self.name_edits)
self.student_id_edits = QLineEdit()
student_id_label = QLabel("学号 :", self)
student_id_label.setFont(QFont("Arial", 12))
hbox5 = QHBoxLayout()
hbox5.addWidget(student_id_label)
hbox5.addWidget(self.student_id_edits)
self.exp_date_edits = QLineEdit()
exp_date_label = QLabel("实验日期:", self)
exp_date_label.setFont(QFont("Arial", 12))
hbox6 = QHBoxLayout()
hbox6.addWidget(exp_date_label)
hbox6.addWidget(self.exp_date_edits)
layout = QGridLayout()
row, col = 0, 0
for i in range(self.N):
hbox3 = QHBoxLayout()
label1 = QLabel("角度(deg)")
label2 = QLabel("功率(dBm)")
hbox3.addWidget(label1)
hbox3.addWidget(label2)
if row == 0 or row == 16 or row == 32:
layout.addLayout(hbox3, row, col)
x_line_edit = QLineEdit(str(i - (self.N // 2)))
y_line_edit = QLineEdit()
group_box = QGroupBox()
group_layout = QVBoxLayout()
hbox = QHBoxLayout()
hbox.addWidget(x_line_edit)
hbox.addWidget(y_line_edit)
group_layout.addLayout(hbox)
group_box.setLayout(group_layout)
self.groups.append(group_box)
self.x_line_edits.append(x_line_edit)
self.y_line_edits.append(y_line_edit)
layout.addWidget(self.groups[i], row + 1, col)
row += 1
if row == 15:
col += 1
row = 0
input_widget = QWidget()
input_widget.setObjectName("InputWidget")
input_widget.setLayout(layout)
input_widget.setFixedSize(700, 800)
# 输出窗口
self.scene = QGraphicsScene(self)
self.scene.setBackgroundBrush(Qt.white)
self.graphics_view = QGraphicsView(self.scene, self)
self.graphics_view.setRenderHint(QPainter.Antialiasing)
self.graphics_view.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
font = QFont()
font.setPointSize(20) # 设置字体大小
font.setBold(True) # 设置加粗
text_item = self.scene.addText("测试版本,已内置数据。建议在1920×1080分辨率下运行。")
text_item.setFont(font) # 设置字体
text_item.setPos(10, 10) # 设置文本的位置
# 按钮
self.plot_button = QPushButton("绘制图像", self)
self.plot_button.setFixedSize(250, 50)
self.plot_button.setFont(QFont("Arial", 16))
self.pdf_button = QPushButton("生成 PDF", self)
self.pdf_button.setFixedSize(250, 50)
self.pdf_button.setFont(QFont("Arial", 16))
self.clear_button = QPushButton("清空", self)
self.clear_button.setFixedSize(250, 50)
self.clear_button.setFont(QFont("Arial", 16))
self.save_button = QPushButton("保存图像", self)
self.save_button.setFixedSize(250, 50)
self.save_button.setFont(QFont("Arial", 16))
self.plot_button.clicked.connect(self.on_plot_clicked)
self.pdf_button.clicked.connect(self.on_pdf_clicked)
self.clear_button.clicked.connect(self.on_clear_clicked)
self.save_button.clicked.connect(self.on_save_clicked)
button_widget = QWidget()
button_layout = QHBoxLayout(button_widget)
button_layout.addWidget(self.plot_button)
button_layout.addWidget(self.pdf_button)
button_layout.addWidget(self.clear_button)
button_layout.addWidget(self.save_button)
# 添加切换按钮
self.wl_label = QLabel("波长(\u03BCm)", self)
self.wl_label.setFont(QFont("Arial", 12))
self.wl_combo_box = QComboBox(self)#设置大小
#self.wl_combo_box.setFixedSize(500, 30)
self.wl_combo_box.addItem("1.31", 1.31)
self.wl_combo_box.addItem("1.55", 1.55)
self.wl_combo_box.currentIndexChanged.connect(self.on_wl_changed)
self.wl_combo_box.setCurrentIndex(1) # 默认选中1.55
hbox1 = QHBoxLayout()
hbox1.addWidget(self.wl_label)
hbox1.addWidget(self.wl_combo_box)
self.picture = QLabel(self)
pixmap = QPixmap("C:\\Users\\ASUS\\Desktop\\recent\\material\\BJUTlogo2.png")
self.picture.setPixmap(pixmap) # 将 pixmap 设置为 self.picture 的图像
self.picture.setScaledContents(True) # 设置内容按比例缩放
self.picture.setMaximumSize(419, 130)
opacity_effect = QGraphicsOpacityEffect()
opacity_effect.setOpacity(1)
self.picture.setGraphicsEffect(opacity_effect)
hbox_sign = QHBoxLayout()
self.sign = QLabel("作者:22级硕士研究生吴凡", self)
self.sign.setFont(QFont("Arial", 12))
hbox_sign.addWidget(self.sign)
hbox_all = QVBoxLayout()
hbox_all.addLayout(hbox1)
hbox_all.addLayout(hbox2)
hbox_all.addLayout(hbox4)
hbox_all.addLayout(hbox5)
hbox_all.addLayout(hbox6)
hbox_pic_and_all = QHBoxLayout()
hbox_pic_and_all.addWidget(self.picture)
hbox_pic_and_all.addLayout(hbox_all)
main_layout = QGridLayout()
main_layout.addLayout(hbox_pic_and_all, 0, 0, 6, 2)
main_layout.addWidget(input_widget, 6, 0, 6, 5) # 设置 input_widget 占据 6 行 4 列
main_layout.addWidget(self.graphics_view, 0, 2, 11, 3)
main_layout.addWidget(button_widget, 11, 2, 14, 3)
main_layout.addLayout(hbox_sign, 15, 2, 10, 3)#将main_layout.addLayout(hbox_sign, 11, 2, 10, 3)右对齐
main_layout.setAlignment(hbox_sign, Qt.AlignRight) # 将 hbox_sign 右对齐
main_widget = QWidget()
main_widget.setObjectName("MainWidget")
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
# 设置 QMainWindow 的标题和大小
self.setWindowTitle("光纤模场直径测量")
self.showMaximized()
def on_wl_changed(self, index):
wl = self.wl_combo_box.itemData(index)
print("wl =", wl)
# 在这里可以进行后续操作,使用新的波长值 wl
# 比如更新图表、计算模场直径等
return wl
@staticmethod
def static(x_values, y_values, wl):
# 将角度转化为弧度
x_values = np.deg2rad(x_values)
s3 = 0
s1 = 0
for m in range(2, len(x_values)):
b = (y_values[m] + y_values[m - 1]) / 2 # 强度取1/2
c = np.abs((x_values[m] + x_values[m - 1])) / 2 # 角度取1/2
s3 += np.sin(c) ** 3 * b * (x_values[m] - x_values[m - 1])
s1 += np.sin(c) * b * (x_values[m] - x_values[m - 1])
mfd = wl / np.pi * np.sqrt(2 * s1 / s3)
return mfd
def on_plot_clicked(self):
try:
#x_values = [float(line_edit.text()) for line_edit in self.x_line_edits]
#Y_values = [float(line_edit.text()) for line_edit in self.y_line_edits]#dbm
x_values = [-19.96, -19.62, -19.18, -18.85, -18.43, -18.02, -17.43, -17.21, -16.77, -16.34, -16.03, -15.59,
-15.22, -14.78, -14.35, -14.04, -13.56, -13.2, -12.79, -12.36, -12.04, -11.57, -11.2, -10.83,
-10.34, -10.04, -9.645, -9.234, -8.808, -8.388, -8.035, -7.573, -7.222, -6.823, -6.422, -6.03,
-5.555, -5.21, -4.822, -4.35, -4.031, -3.593, -3.235, -2.797, -2.357, -2.037, -1.59, -1.254,
-0.84, -0.391, -0.0198, 0.434, 0.811, 1.17, 1.626, 1.968, 2.438, 2.8, 3.186, 3.6, 3.964, 4.421,
4.814, 5.171, 5.607, 5.965, 6.416, 6.793, 7.171, 7.612, 7.966, 8.446, 8.785, 9.182, 9.631,
9.966, 10.39, 10.77, 11.17, 11.65, 11.96, 12.4, 12.79, 13.16, 13.64, 13.96, 14.4, 14.79, 15.17,
15.64, 15.96, 16.48, 16.77, 17.16, 17.61, 17.96, 18.41, 18.8, 19.18, 19.63, 19.96]
Y_values = [-70.79, -70.13, -69.64, -68.05, -67.71, -67.12, -66.45, -66.36, -65.98, -65.99, -66.12, -66.89,
-67.18, -70.25, -71.13, -73.32, -72.3, -70.55, -62.79, -61.37, -59.07, -55.08, -54.27, -50.45,
-49.59, -48.24, -45.72, -45.16, -42.26, -41.65, -40.48, -38.33, -37.89, -35.45, -34.96, -33.9,
-32.02, -31.62, -29.44, -28.95, -28.07, -26.57, -26.26, -24.56, -24.22, -23.64, -22.71, -22.56,
-21.88, -21.79, -21.76, -21.84, -21.92, -22.62, -22.85, -23.35, -24.46, -24.76, -26.43, -26.8,
-27.66, -29.27, -29.68, -31.8, -32.32, -33.29, -35.17, -35.63, -38.05, -38.6, -39.73, -41.93,
-42.42, -45.3, -45.96, -47.32, -50.04, -50.67, -54.64, -55.72, -57.7, -62.26, -63.53, -71.13,
-72.54, -72.81, -69.36, -68.6, -66.19, -65.87, -65.45, -65.14, -65.14, -65.47, -65.59, -65.95,
-66.77, -67.02, -68.37, -68.74, -69.39]
y_values = np.power(10, np.array(Y_values) / 10) #线性
print("x_values =", x_values, "deg")
print("y_values =", Y_values, "dBm")
wl = self.on_wl_changed(self.wl_combo_box.currentIndex()) # 获取波长
mfd = self.static(x_values, y_values, wl)
print("模场直径为 =", mfd, "μm")
fig = plt.figure(dpi=300)
canvas = FigureCanvas(fig)
fig.set_size_inches(8, 6)
ax = fig.add_subplot(111)
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 这两行需要手动设置
ax.plot(x_values, Y_values, color='red', linestyle='-', linewidth=3, label='曲线1')
#轴标签
ax.set_xlabel("角度(deg)", fontsize=20)
ax.set_ylabel("功率(dBm)", fontsize=20)
#刻度范围
ax.set_xlim(-20, 20)
max_value = max(Y_values)+5
min_value = min(Y_values)-5
ax.set_ylim(min_value, max_value)
#轴格式
ax.xaxis.set_major_locator(plt.MultipleLocator(5))
ax.xaxis.set_minor_locator(plt.MultipleLocator(1))
ax.yaxis.set_major_locator(plt.MultipleLocator(10))
ax.yaxis.set_minor_locator(plt.MultipleLocator(2))
ax.tick_params(axis='x', direction='in',length=6,width=2.5, color='black',labelcolor='black', labelsize=16,pad=8) # 刻度线与标签之间的距离
ax.tick_params(axis='x',which='minor', direction='in', length=3, width=2.5, color='black', labelcolor='black', labelsize=16,pad=8) # 刻度线与标签之间的距离
ax.tick_params(axis='y', direction='in', length=6, width=2.5, color='black', labelcolor='black', labelsize=16,pad=8) # 刻度线与标签之间的距离
ax.tick_params(axis='y', which='minor', direction='in', length=3, width=2.5, color='black',labelcolor='black', labelsize=16,pad=8) # 刻度线与标签之间的距离
# 设置边框线的样式和属性
ax.spines['top'].set_visible(True) # 显示顶部边框线
ax.spines['right'].set_visible(True) # 显示右侧边框线
ax.spines['bottom'].set_visible(True) # 显示底部边框线
ax.spines['left'].set_visible(True) # 显示左侧边框线
# 设置边框线的颜色
ax.spines['top'].set_color('black')
ax.spines['right'].set_color('black')
ax.spines['bottom'].set_color('black')
ax.spines['left'].set_color('black')
# 设置边框线的粗细
ax.spines['top'].set_linewidth(2.5)
ax.spines['right'].set_linewidth(2.5)
ax.spines['bottom'].set_linewidth(2.5)
ax.spines['left'].set_linewidth(2.5)
# 调整文本位置
xlim = ax.set_xlim()
ylim = ax.set_ylim()
# 计算文本的坐标
text_x = (xlim[0] + xlim[1]) / 2 # 在 x 轴中心位置
text_y = ylim[1] + 0.05 * (ylim[1] - ylim[0]) # 在 y 轴上方位置
# 设置文本相对位置并添加到轴对象中
text = ax.text(text_x+0, text_y+3.5, "模场直径曲线", fontsize=20, fontweight='bold', color='blue',
horizontalalignment='center', transform=ax.transData)
text2 = self.fiber_number_edits.text()
ax.text(text_x+0, text_y+0.5, "光纤编号:"+ text2, fontsize=12, fontweight='bold', color='blue',
horizontalalignment='center')
mfd_str = "{:.3f}".format(mfd)
ax.text(text_x+0, text_y-2.5, "波长\u03BB = " + str(wl) + "(\u03BCm)" + " MFD = " + mfd_str + "(\u03BCm)", fontsize=12, fontweight='bold', color='blue',
horizontalalignment='center')
text3 = self.name_edits.text()
ax.text(text_x+13, text_y+3.5, "姓名:" + text3, fontsize=12, fontweight='bold', color='blue',)
text4 = self.student_id_edits.text()
ax.text(text_x+13, text_y+0.5, "学号:" + text4, fontsize=12, fontweight='bold', color='blue', )
text5 = self.exp_date_edits.text()
ax.text(text_x+13, text_y-2.5, "实验日期:" + text5, fontsize=12, fontweight='bold', color='blue', )
fig.canvas.draw()
#plt.show()
width, height = fig.canvas.get_width_height()
buf = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
buf = buf.reshape((height, width, 3))
image = QImage(buf.data, width, height, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(image)
#plt.show()
self.scene.clear() # 清空场景中的所有内容
self.scene.addPixmap(pixmap) # 将图像添加到场景中
self.graphics_view.setSceneRect(1, 2, width, height) # 设置场景大小
self.graphics_view.fitInView(self.scene.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio) # 调整图形视图的大小
except ValueError:
print("输入错误")
def on_pdf_clicked(self):
# 生成 PDF
pixmap = self.graphics_view.grab()
file_name, _ = QFileDialog.getSaveFileName(
self, "保存 PDF 文件", "", "PDF Files (*.pdf);;All Files (*)"
)
if file_name:
printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.HighResolution)
printer.setOutputFormat(QtPrintSupport.QPrinter.PdfFormat)
printer.setOutputFileName(file_name)
painter = QPainter(printer)
rect = painter.viewport()
size = pixmap.size()
size.scale(rect.size(), Qt.KeepAspectRatio)
painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
painter.setWindow(pixmap.rect())
painter.drawPixmap(0, 0, pixmap)
painter.end()
def on_clear_clicked(self):
for line_edit in self.y_line_edits:
line_edit.setText("")
self.scene.clear()
def on_save_clicked(self):
# 保存图像
file_name, _ = QFileDialog.getSaveFileName(self, '选择图像保存路径', '', '*.png')
if file_name:
pixmap = self.graphics_view.grab()
pixmap.save(file_name, "PNG", quality=100)
if __name__ == "__main__":
app = QApplication(sys.argv)
plotter = ParabolicPlotter()
plotter.show()
sys.exit(app.exec_())