summaryrefslogtreecommitdiff
path: root/src/ui.py
blob: f54134f0d48234da3242a720874c3f765c8a2055 (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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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
	table_rows = list()
	monospace = QFont("monospace")
	bigfont = QFont("sans-serif", pointSize=10, weight=1000)

class Pens:
	axes = QPen(Qt.gray)

	ellipse = QPen(Qt.black)
	ellipse.setWidth(2)

	axis_linear = QPen(QColor(201, 141, 0))
	axis_linear.setWidth(2)
	axis_linear.setStyle(Qt.DashDotLine)

	axis_fast = QPen(QColor(51, 87, 123))
	axis_fast.setWidth(2)
	axis_fast.setStyle(Qt.DashDotLine)

	axis_slow = QPen(QColor(55, 123, 51))
	axis_slow.setWidth(2)
	axis_slow.setStyle(Qt.DashDotLine)

	alpha = QPen(Qt.red)
	theta = QPen(Qt.blue)

	radii = QPen(Qt.black)
	radii.setStyle(Qt.DashDotLine)

from ui_widgets import *



class ElementEditorWindow(QMainWindow):
	windows = list()

	@staticmethod
	def open(pol):
		win = ElementEditorWindow(pol)
		win.show()
		ElementEditorWindow.windows.append(win)

	@staticmethod
	def close_all(pol=None):
		for win in ElementEditorWindow.windows:
			if pol is None or pol == win.pol:
				win.close()
				del(win)

	def __init__(self, pol):
		super().__init__(flags=Qt.Dialog)
		self.pol = pol

		self.setWindowTitle("Element editor")
		root = QVBoxLayout()
		root_fucking_random_container = QWidget()
		root_fucking_random_container.setLayout(root)
		self.setCentralWidget(root_fucking_random_container)

		# Name
		box = QHBoxLayout()
		root.addLayout(box)
		box.addWidget(QLabel("Name"))
		self.name = QLineEdit(self.pol.name)
		self.name.textChanged.connect(self.change_name)
		box.addWidget(self.name)

		# OPTICS
		root.addWidget(MeinGroßLabel("Optics"))

		# Type combo
		self.type = QComboBox()
		self.type.addItem("Linear", "linear",)
		self.type.addItem("Quarterwave plate", "quarterwave")
		self.type.setCurrentIndex(0 if pol.type == "linear" else 1)
		self.type.currentIndexChanged.connect(self.change_type)
		root.addWidget(self.type)

		# TRANSMITTANCE
		root.addWidget(MeinGroßLabel("Transmittance"))

		box = QHBoxLayout()
		root.addLayout(box)

		box.addWidget(QLabel("T"))
		self.t1 = QLineEdit("%g" % self.pol.t1)
		self.t1.textChanged.connect(self.change_t)
		box.addWidget(self.t1)
		self.t2 = QLineEdit("%g" % self.pol.t2)
		self.t2.textChanged.connect(self.change_t)
		box.addWidget(self.t2)

		# ARRANGEMENT
		root.addWidget(MeinGroßLabel("Arrangement"))

		# Angle reference
		self.ref = QComboBox()
		self.populate_ref()
		self.ref.currentIndexChanged.connect(update)
		root.addWidget(self.ref)

		# Delta angle
		self.delta = AngleSlider("Delta")
		self.delta.on_change = self.change_delta
		root.addLayout(self.delta)

		root.addItem(ExpandingSpacer())

		# Done
		self.done = QPushButton("Done")
		self.done.pressed.connect(self.close)
		root.addWidget(self.done)

	def change_name(self):
		self.pol.name = self.name.text()
		self.pol.row.optbox.name.setText(self.pol.name)

	def change_type(self):
		self.pol.set_type(self.type.currentData())
		update()

	def change_delta(self):
		try:
			self.pol.delta = self.delta.angle / 180 * np.pi
			update()
		except ValueError:
			pass

	def change_t(self):
		try:
			self.pol.t1 = float(self.t1.text())
			self.pol.t2 = float(self.t2.text())
			update()
		except ValueError:
			pass

	def populate_ref(self):
		self.ref.addItem("Absolute", False)
		for i, _ in enumerate(system.elements):
			if i >= self.pol.rownum - 1:
				continue
			self.ref.addItem("Relative to #%d" % (i + 1), i)

			if self.pol.ref is not False and i == self.pol.ref:
				self.ref.setCurrentIndex(self.pol.ref + 1)



class OptBox(QVBoxLayout):
	def __init__(self, row, pol, rownum):
		super().__init__()
		self.row = row
		self.rownum = rownum

		# Name
		self.name = MeinGroßLabel(pol.name)
		self.name.setContextMenuPolicy(Qt.CustomContextMenu)
		self.name.customContextMenuRequested.connect(lambda pos: OptBox.open_menu(self, pos))
		self.addWidget(self.name)

		# Enable checkbox
		self.enable = QCheckBox("Enable")
		self.enable.setChecked(True)
		self.enable.stateChanged.connect(self.change_enable)
		self.addWidget(self.enable)

		# Angle
		self.angle = AngleSlider()
		self.angle.setValue(pol.angle * 180 / np.pi)
		self.angle.on_change = lambda: TableRow.angle_change(row)
		self.addLayout(self.angle)
		self.addItem(ExpandingSpacer())

	def open_menu(self, pos):
		menu = QMenu()
		menu.addAction("Edit", lambda: ElementEditorWindow.open(self.row.pol))
		menu.addAction("Insert before", lambda: half_assed_element_creation(self.rownum - 1))
		menu.addAction("Insert after", lambda: half_assed_element_creation(self.rownum))
		menu.addAction("Delete", lambda: half_assed_element_deletion(self.rownum - 1))
		menu.exec_(self.name.mapToGlobal(pos))

	def change_enable(self):
		self.row.pol.enable = self.enable.isChecked()
		update()


class TableRow:
	def __init__(self, pol, grid, rownum):
		self.pol = pol
		pol.row = self
		pol.rownum = rownum
		self.grid = grid
		self.rownum = rownum
		self.angle = pol.angle
		self.delta = pol.delta
		self.ref = pol.ref

		self.optbox = OptBox(self, pol, rownum)
		grid.addLayout(self.optbox, rownum, 1)

		self.ellipse = EllipseWidget(pol)
		grid.addWidget(self.ellipse, rownum, 2)

		self.info = QVBoxLayout()
		self.info_angle = MeinLabel()
		self.info_angle.setAlignment(Qt.AlignLeft)
		self.info.addWidget(self.info_angle)
		self.info.addWidget(QLabel("Polarization state"))
		self.info_jones = MeinLabel()
		self.info_jones.setAlignment(Qt.AlignLeft)
		self.info.addWidget(self.info_jones)
		self.info.addItem(ExpandingSpacer())
		grid.addLayout(self.info, rownum, 3)

	@staticmethod
	def angle_change(row):
		row.angle = row.optbox.angle.angle / 180 * np.pi
		row.pol.angle = row.angle
		update()



def populate_table():
	GUI.input_intensity.setText("%g" % system.input_intensity)

	GUI.table = QGridLayout()
	GUI.table.setColumnStretch(1, 1)
	GUI.table.setColumnStretch(2, 2)
	GUI.table.setColumnStretch(3, 5)

	while GUI.opt_operand.count() > 0: # FIXME
		GUI.opt_operand.removeItem(0)

	GUI.table_rows = list()
	for i, pol in enumerate(system.elements):
		GUI.table_rows.append(TableRow(pol, GUI.table, i + 1))
		GUI.opt_operand.addItem("Element #%d" % (i + 1), i)

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]

		# update all the diagrams
		row.ellipse.state = system.states[i]
		row.ellipse.ellipse = system.ellipses[i]
		row.ellipse.is_used = system.elements[i].enable
		row.ellipse.repaint()
		
		text = "%s at %f°" \
			% (pol.type, (pol.angle + pol.delta) * 180 / np.pi) 
		row.info_angle.setText(text)

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

		if state is None:
			I = 1
			row.info_jones.setText("Unpolarized")
		else:
			I = np.abs(state[0] * state[0].conjugate() \
				   + state[1] * state[1].conjugate())

		if state is None:
			continue
		
		A, B = state
		text = "I = %f\n" % I
		text += "%f ∠ %+f°\n%f ∠ %+f°\n" % \
		       (np.abs(A), 180 / np.pi * np.angle(A), \
			np.abs(B), 180 / np.pi * np.angle(B))
		text += "α = %f°\nφ = %+f°" % \
		       (180 / np.pi * ellipse.alpha, 180 / np.pi * ellipse.theta)
		text = text.replace("-", "- ").replace("+", "+ ")
		row.info_jones.setText(text)

	GUI.widok.intensity = I
	GUI.widok.repaint()

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


def half_assed_element_creation(index=None):
	pol = phys.Polarizer("linear")
	if index is None:
		system.elements.append(pol)
	else:
		system.elements.insert(index, pol)

	populate_table()
	GUI.table_frame = QFrame()
	GUI.table_frame.setLayout(GUI.table)
	GUI.scroll.setWidget(GUI.table_frame)

	ElementEditorWindow.open(pol)
	update()

def half_assed_clear():
	ElementEditorWindow.close_all()
	system.elements = list()
	populate_table()
	GUI.table_frame = QFrame()
	GUI.table_frame.setLayout(GUI.table)
	GUI.scroll.setWidget(GUI.table_frame)
	update()

def half_assed_element_deletion(index):
	ElementEditorWindow.close_all(pol=system.elements[pol])
	populate_table()
	GUI.table_frame = QFrame()
	GUI.table_frame.setLayout(GUI.table)
	GUI.scroll.setWidget(GUI.table_frame)
	update()


last_save_path = None
file_filter = "Wery Omportant Zejta (*.woz)"

def do_save(win, reuse_old):
	global system, last_save_path

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

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

	last_save_path = path

def do_open(win):
	global system

	path, _ = QFileDialog.getOpenFileName(win, filter=file_filter)
	if path == "":
		return

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

	populate_table()
	GUI.table_frame = QFrame()
	GUI.table_frame.setLayout(GUI.table)
	GUI.scroll.setWidget(GUI.table_frame)
	update()

def setup_menubar(win):
	menu = win.menuBar()

	# File
	open = QAction("&Open system", win)
	open.setShortcut("Ctrl+O")
	open.triggered.connect(lambda: do_open(win))
	save = QAction("&Save system", win)
	save.setShortcut("Ctrl+S")
	save.triggered.connect(lambda: do_save(win, True))
	save_as = QAction("&Save system as...", win)
	save_as.triggered.connect(lambda: do_save(win, False))
	close = QAction("Exit", win)
	close.setShortcut("Ctrl+Q")
	close.triggered.connect(exit)

	file = menu.addMenu("&File")
	file.addAction(open)
	file.addAction(save)
	file.addAction(save_as)
	file.addAction(close)

	# System
	add = QAction("&Add a new element", win)
	add.setShortcut("Ctrl+N")
	add.triggered.connect(lambda: half_assed_element_creation())
	clear = QAction("&Remove all elements", win)
	clear.triggered.connect(half_assed_clear)

	system = menu.addMenu("&System")
	system.addAction(add)
	system.addAction(clear)

	win.statusBar()

# 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))



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

def setup(win, system_):
	global system
	system = system_

	# Needless to say, I'm getting real tired of this whole Layout/Widget
	# clusterfuck in Qt
	root = QVBoxLayout()
	root_fucking_random_container = QWidget()
	root_fucking_random_container.setLayout(root)
	win.setCentralWidget(root_fucking_random_container)

	# Top bar (input intensity)
	box = QHBoxLayout()
	root.addLayout(box)

	box.addWidget(QLabel("Input intensity"))
	GUI.input_intensity = QLineEdit("1")
	GUI.input_intensity.textChanged.connect(change_input_intensity)
	box.addWidget(GUI.input_intensity)

	hbox = QHBoxLayout()
	root.addLayout(hbox)

	rhs = QVBoxLayout()

	Widocques.image = QImage("jones.jpg")
	GUI.widok = Widocques()
	rhs.addWidget(GUI.widok)

	optbox = QHBoxLayout()
	rhs.addLayout(optbox)

	GUI.opt_operand = QComboBox()
	optbox.addWidget(GUI.opt_operand)
	button = QPushButton("Find a minimum")
	button.pressed.connect(lambda: optimize("min"))
	optbox.addWidget(button)
	button = QPushButton("Find a maximum")
	button.pressed.connect(lambda: optimize("max"))
	optbox.addWidget(button)

	GUI.scroll = QScrollArea()
	populate_table()
	GUI.table_frame = QFrame()
	GUI.table_frame.setLayout(GUI.table)
	GUI.scroll.setWidget(GUI.table_frame)

	hbox.addWidget(GUI.scroll)
	hbox.addLayout(rhs)

	setup_menubar(win)

	update()
	win.show()