forked from M-Labs/artiq
1
0
Fork 0

gui/experiments: support setting values for simple entries

This commit is contained in:
Sebastien Bourdeauducq 2015-11-28 00:19:47 +08:00
parent c382fac8f2
commit 3825428dbf
2 changed files with 42 additions and 59 deletions

View File

@ -14,80 +14,74 @@ logger = logging.getLogger(__name__)
class _StringEntry(QtGui.QLineEdit): class _StringEntry(QtGui.QLineEdit):
def __init__(self, argdesc): def __init__(self, argument):
QtGui.QLineEdit.__init__(self) QtGui.QLineEdit.__init__(self)
self.setText(argument["value"])
def update():
argument["value"] = self.text()
self.editingFinished.connect(update)
@staticmethod @staticmethod
def default(argdesc): def default(argdesc):
return "" return ""
def get_argument_value(self):
return self.text()
def set_argument_value(self, value):
self.setText(value)
class _BooleanEntry(QtGui.QCheckBox): class _BooleanEntry(QtGui.QCheckBox):
def __init__(self, argdesc): def __init__(self, argument):
QtGui.QCheckBox.__init__(self) QtGui.QCheckBox.__init__(self)
self.setChecked(argument["value"])
def update(checked):
argument["value"] = checked
self.stateChanged.connect(update)
@staticmethod @staticmethod
def default(argdesc): def default(argdesc):
return False return False
def get_argument_value(self):
return self.isChecked()
def set_argument_value(self, value):
self.setChecked(value)
class _EnumerationEntry(QtGui.QComboBox): class _EnumerationEntry(QtGui.QComboBox):
def __init__(self, argdesc): def __init__(self, argument):
QtGui.QComboBox.__init__(self) QtGui.QComboBox.__init__(self)
self.choices = argdesc["choices"] choices = argument["desc"]["choices"]
self.addItems(self.choices) self.addItems(choices)
idx = choices.index(argument["value"])
self.setCurrentIndex(idx)
def update(index):
argument["value"] = choices[index]
self.currentIndexChanged.connect(update)
@staticmethod @staticmethod
def default(argdesc): def default(argdesc):
return argdesc["choices"][0] return argdesc["choices"][0]
def get_argument_value(self):
return self.choices[self.currentIndex()]
def set_argument_value(self, value):
idx = self.choices.index(value)
self.setCurrentIndex(idx)
class _NumberEntry(QtGui.QDoubleSpinBox): class _NumberEntry(QtGui.QDoubleSpinBox):
def __init__(self, argdesc): def __init__(self, argument):
QtGui.QDoubleSpinBox.__init__(self) QtGui.QDoubleSpinBox.__init__(self)
self.scale = argdesc["scale"] argdesc = argument["desc"]
scale = argdesc["scale"]
self.setDecimals(argdesc["ndecimals"]) self.setDecimals(argdesc["ndecimals"])
self.setSingleStep(argdesc["step"]/self.scale) self.setSingleStep(argdesc["step"]/scale)
if argdesc["min"] is not None: if argdesc["min"] is not None:
self.setMinimum(argdesc["min"]/self.scale) self.setMinimum(argdesc["min"]/scale)
else: else:
self.setMinimum(float("-inf")) self.setMinimum(float("-inf"))
if argdesc["max"] is not None: if argdesc["max"] is not None:
self.setMaximum(argdesc["max"]/self.scale) self.setMaximum(argdesc["max"]/scale)
else: else:
self.setMaximum(float("inf")) self.setMaximum(float("inf"))
if argdesc["unit"]: if argdesc["unit"]:
self.setSuffix(" " + argdesc["unit"]) self.setSuffix(" " + argdesc["unit"])
self.setValue(argument["value"]/scale)
def update(value):
argument["value"] = value*scale
self.valueChanged.connect(update)
@staticmethod @staticmethod
def default(argdesc): def default(argdesc):
return 0.0 return 0.0
def get_argument_value(self):
return self.value()*self.scale
def set_argument_value(self, value):
self.setValue(value/self.scale)
_argty_to_entry = { _argty_to_entry = {
"PYONValue": _StringEntry, "PYONValue": _StringEntry,
@ -115,16 +109,15 @@ class _ArgumentEditor(QtGui.QTreeWidget):
if not arguments: if not arguments:
self.addTopLevelItem(QtGui.QTreeWidgetItem(["No arguments", ""])) self.addTopLevelItem(QtGui.QTreeWidgetItem(["No arguments", ""]))
for n, (name, (argdesc, group, value)) in enumerate(arguments.items()): for n, (name, argument) in enumerate(arguments.items()):
entry = _argty_to_entry[argdesc["ty"]](argdesc) entry = _argty_to_entry[argument["desc"]["ty"]](argument)
entry.set_argument_value(value)
self._args_to_entries[name] = entry self._args_to_entries[name] = entry
widget_item = QtGui.QTreeWidgetItem([name, ""]) widget_item = QtGui.QTreeWidgetItem([name, ""])
if group is None: if argument["group"] is None:
self.addTopLevelItem(widget_item) self.addTopLevelItem(widget_item)
else: else:
self._get_group(group).addChild(widget_item) self._get_group(argument["group"]).addChild(widget_item)
self.setItemWidget(widget_item, 1, entry) self.setItemWidget(widget_item, 1, entry)
def _get_group(self, name): def _get_group(self, name):
@ -141,29 +134,14 @@ class _ArgumentEditor(QtGui.QTreeWidget):
self._groups[name] = group self._groups[name] = group
return group return group
def get_argument_values(self):
return {arg: entry.get_argument_value()
for arg, entry in self._args_to_entries.items()}
def save_state(self): def save_state(self):
expanded = [] expanded = []
for k, v in self._groups.items(): for k, v in self._groups.items():
if v.isExpanded(): if v.isExpanded():
expanded.append(k) expanded.append(k)
argument_values = self.get_argument_values() return {"expanded": expanded}
return {
"expanded": expanded,
"argument_values": argument_values
}
def restore_state(self, state): def restore_state(self, state):
for arg, value in state["argument_values"].items():
try:
entry = self._args_to_entries[arg]
entry.set_argument_value(value)
except:
logger.warning("failed to restore value of argument %s", arg,
exc_info=True)
for e in state["expanded"]: for e in state["expanded"]:
try: try:
self._groups[e].setExpanded(True) self._groups[e].setExpanded(True)
@ -282,7 +260,11 @@ class ExperimentManager:
del argdesc["default"] del argdesc["default"]
else: else:
value = _argty_to_entry[argdesc["ty"]].default(argdesc) value = _argty_to_entry[argdesc["ty"]].default(argdesc)
arguments[name] = argdesc, group, value arguments[name] = {
"desc": argdesc,
"group": group,
"value": value # mutated by entries
}
self.submission_arguments[expname] = arguments self.submission_arguments[expname] = arguments
return arguments return arguments
@ -307,7 +289,7 @@ class ExperimentManager:
scheduling = self.get_submission_scheduling(expname) scheduling = self.get_submission_scheduling(expname)
options = self.get_submission_options(expname) options = self.get_submission_options(expname)
arguments = self.get_submission_arguments(expname) arguments = self.get_submission_arguments(expname)
argument_values = {k: v[2] for k, v in arguments.items()} argument_values = {k: v["value"] for k, v in arguments.items()}
expid = { expid = {
"log_level": options["log_level"], "log_level": options["log_level"],

View File

@ -56,12 +56,13 @@ class _Range(LayoutWidget):
class ScanController(LayoutWidget): class ScanController(LayoutWidget):
def __init__(self, argdesc): def __init__(self, argument):
LayoutWidget.__init__(self) LayoutWidget.__init__(self)
self.stack = QtGui.QStackedWidget() self.stack = QtGui.QStackedWidget()
self.addWidget(self.stack, 1, 0, colspan=4) self.addWidget(self.stack, 1, 0, colspan=4)
argdesc = argument["desc"]
self.scale = argdesc["scale"] self.scale = argdesc["scale"]
gmin, gmax = argdesc["global_min"], argdesc["global_max"] gmin, gmax = argdesc["global_min"], argdesc["global_max"]