summaryrefslogtreecommitdiff
path: root/src/ui.py
blob: fe9bb83c55b3fd59ce592bb2c43b623772ac13e7 (plain)
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
import numpy as np, scipy.optimize
import traceback
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

import phys, file



# global GUI-related shit
class GUI:
	table = None
	monospace = QFont("monospace")
	bigfont = QFont("sans-serif", pointSize=10, weight=1000)

from ui_widgets import *
from ui_table import *



def update():
	for i, pol in enumerate(system.elements):
		row = GUI.table.rows[i]

		if pol.ref is not False:
			pol.angle += system.elements[pol.ref].angle

	system.recalculate()

	I = 1
	for i, pol in enumerate(system.elements):
		row = GUI.table.rows[i]

		row.ellipse.state = system.states[i]
		row.ellipse.ellipse = system.ellipses[i]
		row.ellipse.is_used = system.elements[i].enable
		row.ellipse.repaint()

		state = system.states[i]
		V = system.Vs[i]
		ellipse = system.ellipses[i]

		if state is None:
			I = system.input_intensity

			for label in row.jones:
				label.setText("-")
			row.stokes[0].setText("%g" % I)
			for label in row.stokes[1:]:
				label.setText("0")
		else:
			I = np.abs(state[0] * state[0].conjugate() \
				   + state[1] * state[1].conjugate())

		if state is None:
			continue

		polar = [(np.abs(z), np.angle(z) * 180 / np.pi) for z in state]
		for i, (a, b) in enumerate(polar):
			row.jones[2 * i].setText("%g" % a)
			row.jones[2 * i + 1].setText("%g°" % b)

		delta = polar[1][1] - polar[0][1]
		row.jones[4].setText("%g°" % delta)

		for i, x in enumerate(phys.jones_to_stokes(state)):
			row.stokes[i].setText("%g" % x)
		row.stokes[4].setText("%g°" % (ellipse.alpha * 180 / np.pi))
		row.stokes[5].setText("%g°" % (ellipse.theta * 180 / np.pi))

	GUI.poincare.update_system(system)

	#if GUI.auto_optimize.isChecked():
	#	optimize()

GUI.do_update = update

# FIXME: refactor
#def optimize(which):
#	if len(system.elements) == 0:
#		return
#
#	op_idx = GUI.opt_operand.currentData()
#	op = system.elements[op_idx]
#
#	def I(theta):
#		op.angle = theta
#		if op.ref is not False:
#			op.angle += system.elements[op.ref].angle
#
#		state = None
#		for i, pol in enumerate(system.elements):
#			if i >= len(system.ignore) or not system.ignore[i]:
#				state = pol.mul(state)
#		if state is None:
#			return 1
#		else:
#			return float(np.abs(state[0] * state[0].conjugate() \
#			                    + state[1] * state[1].conjugate()))
#
#	if which == "min":
#		f = I
#	else:
#		f = lambda theta: -I(theta)
#
#	opt = scipy.optimize.minimize_scalar(f, bounds=[0, np.pi], method="bounded")
#	GUI.table_rows[op_idx].optbox.angle.edit.setText("%g" % round(opt.x * 180 / np.pi, 3))

class MainWindow(QMainWindow):
	file_filter = "Wery Omportant Zejta (*.woz)"

	def __init__(self, system_):
		super().__init__()
		self.last_save_path = None
		global system
		system = system_

		self.setWindowTitle("Polarizzazione italiana")

		# Central widget
		root = QVBoxLayout()
		self.setCentralWidget(LayoutWrapper(root))

		box = QHBoxLayout()
		root.addLayout(box)

		# Top bar
		box.addWidget(QLabel("Input intensity"))
		GUI.input_intensity = QLineEdit("%g" % system.input_intensity)
		GUI.input_intensity.textChanged.connect(self.change_input_intensity)
		box.addWidget(GUI.input_intensity)

		# Splitter
		split = QSplitter()
		root.addWidget(split)

		# Table (LHS)
		GUI.table = SystemTable()
		split.addWidget(GUI.table)

		# Splitter RHS
		rhs = QVBoxLayout()
		split.addWidget(LayoutWrapper(rhs))

		GUI.poincare = PoincareWidget()
		rhs.addWidget(GUI.poincare)

		GUI.table.populate(system)
		update()

		# Menu
		menu_bar = self.menuBar()
		self.statusBar()

		# Menu - File
		menu = menu_bar.addMenu("&File")

		act =  QAction("&Open system", self)
		act.setShortcut("Ctrl+O")
		act.triggered.connect(self.open_system)
		menu.addAction(act)

		act = QAction("&Save system", self)
		act.setShortcut("Ctrl+S")
		act.triggered.connect(lambda: self.save_system(True))
		menu.addAction(act)

		act = QAction("&Save system as...", self)
		act.triggered.connect(lambda: self.save_system(False))
		menu.addAction(act)

		act = QAction("Exit", self)
		act.setShortcut("Ctrl+Q")
		act.triggered.connect(exit)
		menu.addAction(act)

		# Menu - System
		menu = menu_bar.addMenu("&System")

		act = QAction("&Add a new element", self)
		act.setShortcut("Ctrl+N")
		act.triggered.connect(lambda: GUI.table.insert_row(None, False))
		menu.addAction(act)

		act = QAction("&Remove all elements", self)
		act.triggered.connect(GUI.table.clear)
		menu.addAction(act)

	def change_input_intensity(self):
		try:
			system.input_intensity = float(GUI.input_intensity.text())
			update()
		except ValueError:
			pass

	def save_system(self,reuse_old):
		global system

		if reuse_old and self.last_save_path:
			path = self.last_save_path
		else:
			path, _ = QFileDialog.getSaveFileName(self, \
		                  filter=MainWindow.file_filter)
			if path == "":
				return

		try:
			file.save_system(path, system)
		except:
			traceback.print_exc()

		self.last_save_path = path

	def open_system(self):
		global system

		path, _ = QFileDialog.getOpenFileName(self, \
		          filter=MainWindow.file_filter)
		if path == "":
			return

		try:
			system = file.open_system(path)
		except:
			traceback.print_exc()

		GUI.table.populate(system)
		GUI.input_intensity.setText("%g" % system.input_intensity)
		GUI.do_update()