-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui.py
246 lines (189 loc) · 7.92 KB
/
gui.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
import sys, os
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QWidget, QFormLayout, QPushButton, QLineEdit, QInputDialog
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from simulation import MichelsonSimulation
from visual import showPattern
progname = os.path.basename(sys.argv[0])
'''
Illustrition for interface:
------------------------
| |
| Screen |
| |
------------------------
| Control |
| Panel |
------------------------
Here, the "screen" part is for display of the interference pattern on the screen,
the "control panel" part is for controlling components.
'''
class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
class ScreenCanvas(MyMplCanvas):
"""Simple canvas with a sine plot."""
def __init__(self, *args, **kwargs):
self.is_colored = True
#initialize a simulation
self.simulation = MichelsonSimulation()
self.simulation.initialMirrorG([0, 0, 0], [-1, 1, 0])
self.simulation.initialMirrorM1([0, 100, 0], [0, -1, 0])
self.simulation.initialMirrorM2([100, 0, 0], [-1, 1e-3, 0])
self.simulation.insertSource([-20, 0, 0], 635)
self.simulation.changeToNonlocal()
MyMplCanvas.__init__(self, *args, **kwargs)
def compute_initial_figure(self):
showPattern(self.axes, self.simulation, self.is_colored)
def changeLocality(self, text):
if text=='local' and self.simulation.islocalInterference==False:
self.simulation.islocalInterference = True
self.simulation.changeToLocal()
self.axes.cla()
showPattern(self.axes, self.simulation, self.is_colored)
self.draw()
elif text=='nonlocal' and self.simulation.islocalInterference==True:
self.simulation.islocalInterference = False
self.simulation.changeToNonlocal()
self.axes.cla()
showPattern(self.axes, self.simulation, self.is_colored)
self.draw()
def changeColor(self, text):
if text=='colored' and self.is_colored==False:
self.is_colored = True
self.axes.cla()
showPattern(self.axes, self.simulation, self.is_colored)
self.draw()
elif text=='mono' and self.is_colored==True:
self.is_colored = False
self.axes.cla()
showPattern(self.axes, self.simulation, self.is_colored)
self.draw()
def changeSource(self, text):
wavelenth = int(text)
self.axes.cla()
self.simulation.clearSource()
self.simulation.insertSource([-20, 0, 0], wavelenth)
showPattern(self.axes, self.simulation, self.is_colored)
self.draw()
def changeM1(self, text):
y = float(text)
self.simulation.setMirrorM1([0,y,0])
self.axes.cla()
showPattern(self.axes, self.simulation, self.is_colored)
self.draw()
def changeM2(self, text):
ny = float(text) # small angle approx.
self.simulation.setMirrorM2([-1,ny,0])
self.axes.cla()
showPattern(self.axes, self.simulation, self.is_colored)
self.draw()
class InputdialogDemo(QWidget):
def __init__(self,parent=None):
super(InputdialogDemo,self).__init__(parent)
layout=QFormLayout()
self.btn_loc=QPushButton("Locality?")
self.btn_loc.clicked.connect(self.getLoc)
self.le_loc=QLineEdit()
self.le_loc.setText('nonlocal')
layout.addRow(self.btn_loc,self.le_loc)
self.btn_color=QPushButton("Colored or mono. screen?")
self.btn_color.clicked.connect(self.getColor)
self.le_color=QLineEdit()
self.le_color.setText('colored')
layout.addRow(self.btn_color,self.le_color)
self.btn_spec=QPushButton("Wavelenth in nm?")
self.btn_spec.clicked.connect(self.getSpec)
self.le_spec=QLineEdit()
self.le_spec.setText('635')
layout.addRow(self.btn_spec,self.le_spec)
self.btn_M1=QPushButton("M1 location:")
self.btn_M1.clicked.connect(self.getM1)
self.le_M1=QLineEdit()
self.le_M1.setText('100')
layout.addRow(self.btn_M1,self.le_M1)
self.btn_M2=QPushButton("M2 angle:")
self.btn_M2.clicked.connect(self.getM2)
self.le_M2=QLineEdit()
self.le_M2.setText('1e-3')
layout.addRow(self.btn_M2,self.le_M2)
self.setLayout(layout)
self.setWindowTitle("Input Dialog例子")
def getLoc(self):
items = ("local","nonlocal")
item, ok = QInputDialog.getItem(self,"select Input dialog","Interference type?",items,0,False)
if ok and item:
self.le_loc.setText(item)
def getColor(self):
items=("colored","mono")
item, ok = QInputDialog.getItem(self,"select Input dialog","Interference type?",items,0,False)
if ok and item:
self.le_color.setText(item)
def getM1(self):
text, ok = QInputDialog.getText(self,'Text Input Dialog','M1 location?')
if ok:
self.le_M1.setText(str(text))
def getM2(self):
text, ok = QInputDialog.getText(self,'Text Input Dialog','M2 angle?')
if ok:
self.le_M2.setText(str(text))
def getSpec(self):
text, ok = QInputDialog.getText(self,"Text Input Dialog","Wavelength?")
if ok:
self.le_spec.setText(str(text))
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.file_menu = QtWidgets.QMenu('&File', self)
self.file_menu.addAction('&Quit', self.fileQuit,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
self.menuBar().addMenu(self.file_menu)
self.help_menu = QtWidgets.QMenu('&Help', self)
self.menuBar().addSeparator()
self.menuBar().addMenu(self.help_menu)
self.help_menu.addAction('&About', self.about)
self.main_widget = QtWidgets.QWidget(self)
l = QtWidgets.QVBoxLayout(self.main_widget)
sc = ScreenCanvas(self.main_widget, width=5, height=4, dpi=100)
l.addWidget(sc)
idd = InputdialogDemo()
l.addWidget(idd)
idd.le_loc.textChanged.connect(sc.changeLocality)
idd.le_color.textChanged.connect(sc.changeColor)
idd.le_spec.textChanged.connect(sc.changeSource)
idd.le_M1.textChanged.connect(sc.changeM1)
idd.le_M2.textChanged.connect(sc.changeM2)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
self.statusBar().showMessage("All hail matplotlib!", 2000)
def fileQuit(self):
self.close()
def closeEvent(self, ce):
self.fileQuit()
def about(self):
QtWidgets.QMessageBox.about(self, "About",
"""GUI for RealMichelson."""
)
if __name__=='__main__':
qApp = QtWidgets.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("%s" % progname)
aw.show()
sys.exit(qApp.exec_())
#qApp.exec_()