From b35d3e4e76ce4fad133316861f1962efd9b9184f Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Thu, 2 May 2019 19:20:02 +0200 Subject: Element edition window --- src/file.py | 6 +- src/main.py | 3 +- src/phys.py | 7 +- src/ui.py | 190 ++++++++++++++++++++++++++++++++---------------------- src/ui_widgets.py | 2 +- 5 files changed, 125 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/file.py b/src/file.py index 7c5f570..417c77b 100644 --- a/src/file.py +++ b/src/file.py @@ -1,7 +1,7 @@ import re, sys, traceback, json import phys -file_format_version = 2 +file_format_version = 4 def save_system(path, system): ser = dict() @@ -11,7 +11,9 @@ def save_system(path, system): ser["elements"] = [] for pol in system.elements: el = { + "name": pol.name, "type": pol.type, + "enable": pol.enable, "angle": pol.angle, "delta": pol.delta, "ref": pol.ref, @@ -38,6 +40,8 @@ def open_system(path): for el in ser["elements"]: pol = phys.Polarizer(str(el["type"])) + pol.name = str(el["name"]) + pol.enable = bool(el["enable"]) pol.angle = float(el["angle"]) pol.delta = float(el["delta"]) pol.ref = el["ref"] diff --git a/src/main.py b/src/main.py index 3997835..ea1122c 100644 --- a/src/main.py +++ b/src/main.py @@ -5,8 +5,7 @@ from PyQt5.QtCore import * import ui, phys, file -#system = phys.System() -system = file.open_system("3.woz") +system = phys.System() class Window(QMainWindow): def __init__(self): diff --git a/src/phys.py b/src/phys.py index 96f3bad..cf30667 100644 --- a/src/phys.py +++ b/src/phys.py @@ -53,12 +53,14 @@ class Ellipse: class Polarizer: def __init__(self, type, delta=0): + self.name = "New element" # FIXME self.type = type self.angle = 0 self.delta = delta - self.ref = False # FIXME: move this to UI or System + self.ref = False self.t1 = 1 self.t2 = 0 + self.enable = True self.set_type(type) def set_type(self, type): @@ -92,7 +94,6 @@ class Polarizer: class System: def __init__(self): self.elements = list() - self.ignore = list() def recalculate(system): system.states = [None] * len(system.elements) @@ -100,7 +101,7 @@ class System: state = None for i, pol in enumerate(system.elements): - if i >= len(system.ignore) or not system.ignore[i]: + if pol.enable: state = pol.mul(state) system.states[i] = state system.ellipses.append(Ellipse(state)) diff --git a/src/ui.py b/src/ui.py index 6fced54..84322b6 100644 --- a/src/ui.py +++ b/src/ui.py @@ -39,71 +39,133 @@ class Pens: from ui_widgets import * -class OptBox(QVBoxLayout): - def __init__(self, row, pol, rownum): - super().__init__() - self.rownum = rownum - # Name - self.name = MeinGroßLabel("Element %d" % rownum) - self.name.setContextMenuPolicy(Qt.CustomContextMenu) - self.name.customContextMenuRequested.connect(lambda pos: OptBox.open_menu(self, pos)) - self.addWidget(self.name) - # Type / Enable box +class ElementEditorWindow(QMainWindow): + windows = list() + + @staticmethod + def open(pol): + win = ElementEditorWindow(pol) + win.show() + ElementEditorWindow.windows.append(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() - self.addLayout(box) + 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(lambda : TableRow.type_change(row)) - box.addWidget(self.type) + self.type.currentIndexChanged.connect(self.change_type) + root.addWidget(self.type) - # Enable checkbox - self.enable = QCheckBox("Enable") - self.enable.setChecked(True) - self.enable.stateChanged.connect(update) - box.addWidget(self.enable) + # TRANSMITTANCE + root.addWidget(MeinGroßLabel("Transmittance")) - # Delta / Reference layout box = QHBoxLayout() - self.addLayout(box) + root.addLayout(box) + + box.addWidget(QLabel("T")) + self.t1 = QLineEdit("1") + self.t1.textChanged.connect(self.change_t) + box.addWidget(self.t1) + self.t2 = QLineEdit("0") + 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 >= rownum - 1: + if i >= self.pol.rownum - 1: continue self.ref.addItem("Relative to #%d" % (i + 1), i) - if row.ref is not False and i == row.ref: - self.ref.setCurrentIndex(row.ref + 1) + if self.pol.ref is not False and i == self.pol.ref: + self.ref.setCurrentIndex(self.pol.ref + 1) - self.ref.currentIndexChanged.connect(update) - box.addWidget(self.ref) - # Delta angle - box.addWidget(QLabel("Delta")) - self.delta = QLineEdit("0") - self.delta.textChanged.connect(lambda: TableRow.angle_change(row)) - box.addWidget(self.delta) - box.addWidget(QLabel("°")) - # Transmittance layout - box = QHBoxLayout() - self.addLayout(box) +class OptBox(QVBoxLayout): + def __init__(self, row, pol, rownum): + super().__init__() + self.row = row + self.rownum = rownum - box.addWidget(QLabel("T")) - self.t1 = QLineEdit("1") - self.t1.textChanged.connect(lambda: TableRow.t_change(row)) - box.addWidget(self.t1) - self.t2 = QLineEdit("0") - self.t2.textChanged.connect(lambda: TableRow.t_change(row)) - box.addWidget(self.t2) + # 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(update) + self.addWidget(self.enable) # Angle self.angle = AngleSlider("Angle") @@ -114,6 +176,7 @@ class OptBox(QVBoxLayout): 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)) @@ -123,6 +186,8 @@ class OptBox(QVBoxLayout): 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 @@ -150,15 +215,7 @@ class TableRow: @staticmethod def angle_change(row): row.angle = row.optbox.angle.angle / 180 * np.pi - try: - row.delta = float(row.optbox.delta.text()) / 180 * np.pi - except ValueError: - pass - update() - - @staticmethod - def type_change(row): - row.pol.set_type(row.optbox.type.currentData()) + row.pol.angle = row.angle update() @staticmethod @@ -190,9 +247,6 @@ def update(): for i, pol in enumerate(system.elements): row = GUI.table_rows[i] - pol.angle = row.angle - pol.delta = row.delta - pol.ref = row.optbox.ref.currentData() if pol.ref is not False: pol.angle += system.elements[pol.ref].angle @@ -245,34 +299,18 @@ def update(): # optimize() -#TODO later -#class AddElementWindow(QMainWindow): -# dialog = None -# -# def __init__(self): -# super().__init__() -# -# chuj = QLabel("adolf") -# self.setCentralWidget(chuj) -# self.show() -# -# -# def __del__(self): -# AddElementWindow.dialog = None -# -# def open(): -# if not AddElementWindow.dialog: -# AddElementWindow.dialog = AddElementWindow() def half_assed_element_creation(index=None): + pol = phys.Polarizer("linear") if index is None: - system.elements.append(phys.Polarizer("linear")) + system.elements.append(pol) else: - system.elements.insert(index, phys.Polarizer("linear")) + 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(): @@ -335,13 +373,13 @@ def setup_menubar(win): menu = win.menuBar() # File - open = QAction("&Open a system", win) + open = QAction("&Open system", win) open.setShortcut("Ctrl+O") open.triggered.connect(lambda: do_open(win)) - save = QAction("&Save a system", win) + save = QAction("&Save system", win) save.setShortcut("Ctrl+S") save.triggered.connect(lambda: do_save(win, True)) - save_as = QAction("&Save a system as...", win) + save_as = QAction("&Save system as...", win) save_as.triggered.connect(lambda: do_save(win, False)) close = QAction("Exit", win) close.setShortcut("Ctrl+Q") diff --git a/src/ui_widgets.py b/src/ui_widgets.py index 636f2ed..36ff333 100644 --- a/src/ui_widgets.py +++ b/src/ui_widgets.py @@ -158,7 +158,7 @@ class AngleSlider(QHBoxLayout): self.edit = QLineEdit() self.edit.setText("0") - self.edit.setMinimumWidth(30) + self.edit.setFixedWidth(45) self.edit.textChanged.connect(\ lambda: AngleSlider.change(self, "edit")) self.addWidget(self.edit) -- cgit