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] # 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() GUI.do_update = update 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() # 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 self.statusBar() self.setWindowTitle("Polarizzazione italiana") global system system = system_ # 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: half_assed_element_creation()) menu.addAction(act) act = QAction("&Remove all elements", self) act.triggered.connect(half_assed_clear) menu.addAction(act) # 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)) Widocques.image = QImage("jones.jpg") GUI.widok = Widocques() rhs.addWidget(GUI.widok) GUI.table.populate(system) update() 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()