forked from M-Labs/artiq
1
0
Fork 0

gui: support setting histogram X axis

This commit is contained in:
Sebastien Bourdeauducq 2015-08-06 15:34:58 +08:00
parent cddb5b9ae4
commit 7180552d24
1 changed files with 84 additions and 33 deletions

View File

@ -5,43 +5,63 @@ import pyqtgraph as pg
from pyqtgraph import dockarea from pyqtgraph import dockarea
class _SimpleSettings(QtGui.QDialog): class _BaseSettings(QtGui.QDialog):
def __init__(self, parent, prev_name, prev_settings, def __init__(self, parent, window_title, prev_name, create_cb):
result_list, create_cb):
QtGui.QDialog.__init__(self, parent=parent) QtGui.QDialog.__init__(self, parent=parent)
self.setWindowTitle(self._window_title) self.setWindowTitle(window_title)
grid = QtGui.QGridLayout() self.grid = QtGui.QGridLayout()
self.setLayout(grid) self.setLayout(self.grid)
grid.addWidget(QtGui.QLabel("Name:"), 0, 0) self.grid.addWidget(QtGui.QLabel("Name:"), 0, 0)
self.name = name = QtGui.QLineEdit() self.name = QtGui.QLineEdit()
grid.addWidget(name, 0, 1) self.grid.addWidget(self.name, 0, 1)
if prev_name is not None: if prev_name is not None:
name.insert(prev_name) self.name.setText(prev_name)
grid.addWidget(QtGui.QLabel("Result:")) def on_accept():
self.result = result = QtGui.QComboBox() create_cb(self.name.text(), self.get_input())
grid.addWidget(result, 1, 1) self.accepted.connect(on_accept)
result.addItems(result_list)
result.setEditable(True)
if "result" in prev_settings:
result.setEditText(prev_settings["result"])
def add_buttons(self):
buttons = QtGui.QDialogButtonBox( buttons = QtGui.QDialogButtonBox(
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel) QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
grid.addWidget(buttons, 2, 0, 1, 2) self.grid.addWidget(buttons, self.grid.rowCount(), 0, 1, 2)
buttons.accepted.connect(self.accept) buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject) buttons.rejected.connect(self.reject)
def on_accept():
create_cb(name.text(), {"result": result.currentText()})
self.accepted.connect(on_accept)
def accept(self): def accept(self):
if self.name.text() and self.result.currentText(): if self.name.text() and self.validate_input():
QtGui.QDialog.accept(self) QtGui.QDialog.accept(self)
def validate_input(self):
raise NotImplementedError
def get_input(self):
raise NotImplementedError
class _SimpleSettings(_BaseSettings):
def __init__(self, parent, prev_name, prev_settings,
result_list, create_cb):
_BaseSettings.__init__(self, parent, self._window_title,
prev_name, create_cb)
self.grid.addWidget(QtGui.QLabel("Result:"))
self.result = QtGui.QComboBox()
self.grid.addWidget(self.result, 1, 1)
self.result.addItems(result_list)
self.result.setEditable(True)
if "result" in prev_settings:
self.result.setEditText(prev_settings["result"])
self.add_buttons()
def validate_input(self):
return bool(self.result.currentText())
def get_input(self):
return {"result": self.result.currentText()}
class NumberDisplaySettings(_SimpleSettings): class NumberDisplaySettings(_SimpleSettings):
_window_title = "Number display" _window_title = "Number display"
@ -95,8 +115,30 @@ class XYDisplay(dockarea.Dock):
self.plot.plot(y) self.plot.plot(y)
class HistogramDisplaySettings(_SimpleSettings): class HistogramDisplaySettings(_BaseSettings):
_window_title = "Histogram" def __init__(self, parent, prev_name, prev_settings,
result_list, create_cb):
_BaseSettings.__init__(self, parent, "Histogram",
prev_name, create_cb)
for row, axis in enumerate("yx"):
self.grid.addWidget(QtGui.QLabel(axis.upper() + ":"))
w = QtGui.QComboBox()
self.grid.addWidget(w, row + 1, 1)
if axis == "x":
w.addItem("<None>")
w.addItems(result_list)
w.setEditable(True)
if axis in prev_settings:
w.setEditText(prev_settings["y"])
setattr(self, axis, w)
self.add_buttons()
def validate_input(self):
return bool(self.y.currentText()) and bool(self.x.currentText())
def get_input(self):
return {"y": self.y.currentText(), "x": self.x.currentText()}
class HistogramDisplay(dockarea.Dock): class HistogramDisplay(dockarea.Dock):
@ -108,18 +150,27 @@ class HistogramDisplay(dockarea.Dock):
self.addWidget(self.plot) self.addWidget(self.plot)
def data_sources(self): def data_sources(self):
return {self.settings["result"]} s = {self.settings["y"]}
if self.settings["x"] != "<None>":
s.add(self.settings["x"])
return s
def update_data(self, data): def update_data(self, data):
result = self.settings["result"] result_y = self.settings["y"]
result_x = self.settings["x"]
try: try:
y = data[result] y = data[result_y]
if result_x == "<None>":
x = None
else:
x = data[result_x]
except KeyError: except KeyError:
return return
if x is None:
x = list(range(len(y)+1)) x = list(range(len(y)+1))
if y and len(x) == len(y) + 1:
self.plot.clear() self.plot.clear()
if not y:
return
self.plot.plot(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150)) self.plot.plot(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 150))