rework config/parameter setting

This commit is contained in:
2022-09-05 10:30:48 +02:00
parent c11e0acfcd
commit 8fc532d3ae
2 changed files with 197 additions and 31 deletions

View File

@@ -9,11 +9,13 @@ import logging
_log = logging.getLogger(__name__)
from PyQt5.QtCore import QSettings
from PyQt5.QtWidgets import QApplication, QLineEdit
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QGridLayout, QLabel
import json
import pyqtgraph as pg
import numpy as np
import GenericDialog
#from pyqtgraph.Qt import QtCore, QtGui
class MyJsonEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
@@ -31,6 +33,7 @@ class MyJsonEncoder(json.JSONEncoder):
class AppCfg(QSettings):
GBL_FLD_SCR_SHOT="global/folder_screenshot"
GBL_DEV_PREFIX="global/device_prefix" #SAR-EXPMX
GEO_OPT_CTR='geometry/opt_ctr'
GEO_PIX2POS='geometry/pix2pos'
@@ -44,6 +47,11 @@ class AppCfg(QSettings):
WINDOW_SPLITTER="window/splitter"
WINDOW_STATE= "window/state"
DFT_POS_DET ="default_position/detector" #json
DFT_POS_PST ="default_position/post_sample_tube" #json
DFT_POS_COL ="default_position/collimator" #json
DFT_POS_BKLGT ="default_position/backlight" #json
PST_X_UP ="post_sample_tube/x_up"
PST_Y_UP ="post_sample_tube/y_up"
PST_X_DOWN="post_sample_tube/x_down"
@@ -96,6 +104,11 @@ class AppCfg(QSettings):
# print(k, self.value(k))
#set default keys if not existing
if AppCfg.GBL_DEV_PREFIX not in keys:
_log.warning(f'{AppCfg.GBL_DEV_PREFIX} not defined. set default')
self.setValue(AppCfg.GBL_DEV_PREFIX, ['SAR-EXPMX','SARES30-ESBMX']) #prefix deltatau,smaract
if AppCfg.GEO_BEAM_SZ not in keys:
_log.warning(f'{AppCfg.GEO_BEAM_SZ} not defined. set default')
self.setValue(AppCfg.GEO_BEAM_SZ, [30.2, 25.6]) #([40, 20) -> tuples are not supported natively
@@ -200,7 +213,6 @@ class AppCfg(QSettings):
self.setValue(key, not v)
self.sync()
#inst_folder = Path(__file__).absolute().parent
#config_file = inst_folder / "swissmx.yaml"
#configs = yaml.load(config_file.read_text(),Loader=yaml.FullLoader)
@@ -410,3 +422,114 @@ class AppCfg(QSettings):
settings.setValue(k, v)
settings.sync()
# ----------------------------------------------
class WndParameter():
def __init__(self,mainWnd):
from pyqtgraph.parametertree import Parameter, ParameterTree, ParameterItem, registerParameterType
params=[
{'name':'Basic parameter data types', 'type':'group', 'children':[
{'name':'Integer', 'type':'int', 'value':10},
{'name':'Float', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'Subgroup', 'type':'group', 'children':[
{'name':'Sub-param 1', 'type':'int', 'value':10},
{'name':'Sub-param 2', 'type':'float', 'value':1.2e6},
]},
]},
{'name':'geometry', 'type':'group', 'children':[
{'name':'size of the beam', 'type':'group', 'children':[
{'name':'width', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'height', 'type':'float', 'value':10.5, 'step':0.1},
]},
]},
{'name':'post sample tube reference positions', 'type':'group', 'children':[
{'name':'Up X', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'Up Y', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'Down X', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'Down Y', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'out delta X', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'out delta Y', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'tube Z in position', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'tube Z OUT position', 'type':'float', 'value':10.5, 'step':0.1},
]},
{'name':'collimator reference positions', 'type':'group', 'children':[
{'name':'in X', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'in Y', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'out deltaX', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'out deltaY', 'type':'float', 'value':10.5, 'step':0.1},
]},
{'name':'Back Light reference positions', 'type':'group', 'children':[
{'name':'In position', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'Out position', 'type':'float', 'value':0.0, 'step':0.1},
]},
{'name':'Delta Tau Parameters', 'type':'group', 'children':[
{'name':'host name (host[:port:port_gather])', 'type':'float', 'value':10.5, 'step':0.1},
{'name':'show plots after collection', 'type':'bool', 'value':True, 'tip':"This is a checkbox"},
]},
{'name':'Save/Restore functionality', 'type':'group', 'children':[
{'name':'Save State', 'type':'action'},
{'name':'Restore State', 'type':'action', 'children':[
{'name':'Add missing items', 'type':'bool', 'value':True},
{'name':'Remove extra items', 'type':'bool', 'value':True},
]},
]},
]
self._p=p=Parameter.create(name='params', type='group', children=params)
p.sigTreeStateChanged.connect(lambda a,b: self.cb_change(a,b))
# Too lazy for recursion:
for child in p.children():
child.sigValueChanging.connect(lambda a,b: self.cb_valueChanging(a,b))
for ch2 in child.children():
ch2.sigValueChanging.connect(lambda a,b: self.cb_valueChanging(a,b))
p.param('Save/Restore functionality', 'Save State').sigActivated.connect(self.cb_save)
p.param('Save/Restore functionality', 'Restore State').sigActivated.connect(self.cb_restore)
t=ParameterTree()
t.setParameters(p, showTop=False)
t.setWindowTitle('pyqtgraph example: Parameter Tree')
t.resize(400, 800)
self._wnd=t
def cb_change(self,param, changes):
p=self._p
print("tree changes:")
for param, change, data in changes:
path=p.childPath(param)
if path is not None:
childName='.'.join(path)
else:
childName=param.name()
print(' parameter: %s'%childName)
print(' change: %s'%change)
print(' data: %s'%str(data))
print(' ----------')
def cb_valueChanging(self,param, value):
print("Value changing (not finalized): %s %s"%(param, value))
def cb_save(self):
self._state=p.saveState()
def cb_restore(self):
add=p['Save/Restore functionality', 'Restore State', 'Add missing items']
rem=p['Save/Restore functionality', 'Restore State', 'Remove extra items']
p.restoreState(self._state, addChildren=add, removeChildren=rem)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
app=QApplication([])
## Create tree of Parameter objects
cfg=AppCfg()
w=WndParameter(None)
w._wnd.show()
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QApplication.instance().exec_()