2015-07-14 23:31:18 +08:00
|
|
|
import asyncio
|
2015-08-01 19:37:16 +08:00
|
|
|
import logging
|
2015-07-14 23:31:18 +08:00
|
|
|
|
2016-06-17 16:45:26 +08:00
|
|
|
import numpy as np
|
2016-02-15 07:23:47 +08:00
|
|
|
from PyQt5 import QtCore, QtWidgets
|
2021-07-13 12:53:35 +08:00
|
|
|
from sipyco import pyon
|
2015-07-14 23:31:18 +08:00
|
|
|
|
2021-07-13 12:53:35 +08:00
|
|
|
from artiq.tools import short_format, exc_to_warning
|
2016-08-14 18:33:24 +08:00
|
|
|
from artiq.gui.tools import LayoutWidget, QRecursiveFilterProxyModel
|
2015-11-17 19:46:17 +08:00
|
|
|
from artiq.gui.models import DictSyncTreeSepModel
|
2016-10-17 19:57:59 +08:00
|
|
|
from artiq.gui.scientific_spinbox import ScientificSpinBox
|
2015-07-14 23:31:18 +08:00
|
|
|
|
|
|
|
|
2015-08-01 19:37:16 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-06-17 16:45:26 +08:00
|
|
|
class Editor(QtWidgets.QDialog):
|
2016-07-03 12:23:56 +08:00
|
|
|
def __init__(self, parent, dataset_ctl, key, value):
|
2016-06-17 16:45:26 +08:00
|
|
|
QtWidgets.QDialog.__init__(self, parent=parent)
|
|
|
|
self.dataset_ctl = dataset_ctl
|
|
|
|
self.key = key
|
|
|
|
self.initial_type = type(value)
|
|
|
|
|
|
|
|
self.setWindowTitle("Edit dataset")
|
|
|
|
grid = QtWidgets.QGridLayout()
|
|
|
|
self.setLayout(grid)
|
|
|
|
|
|
|
|
grid.addWidget(QtWidgets.QLabel("Name:"), 0, 0)
|
|
|
|
grid.addWidget(QtWidgets.QLabel(key), 0, 1)
|
|
|
|
|
|
|
|
grid.addWidget(QtWidgets.QLabel("Value:"), 1, 0)
|
|
|
|
grid.addWidget(self.get_edit_widget(value), 1, 1)
|
|
|
|
|
|
|
|
buttons = QtWidgets.QDialogButtonBox(
|
|
|
|
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
|
|
|
|
grid.setRowStretch(2, 1)
|
|
|
|
grid.addWidget(buttons, 3, 0, 1, 2)
|
|
|
|
buttons.accepted.connect(self.accept)
|
|
|
|
buttons.rejected.connect(self.reject)
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
value = self.initial_type(self.get_edit_widget_value())
|
2016-07-03 12:23:56 +08:00
|
|
|
asyncio.ensure_future(self.dataset_ctl.set(self.key, value))
|
2016-06-17 16:45:26 +08:00
|
|
|
QtWidgets.QDialog.accept(self)
|
|
|
|
|
|
|
|
def get_edit_widget(self, initial_value):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_edit_widget_value(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class NumberEditor(Editor):
|
|
|
|
def get_edit_widget(self, initial_value):
|
2016-10-17 19:57:59 +08:00
|
|
|
self.edit_widget = ScientificSpinBox()
|
|
|
|
self.edit_widget.setDecimals(13)
|
|
|
|
self.edit_widget.setPrecision()
|
|
|
|
self.edit_widget.setRelativeStep()
|
2016-06-17 16:45:26 +08:00
|
|
|
self.edit_widget.setValue(float(initial_value))
|
|
|
|
return self.edit_widget
|
|
|
|
|
|
|
|
def get_edit_widget_value(self):
|
|
|
|
return self.edit_widget.value()
|
|
|
|
|
|
|
|
|
|
|
|
class BoolEditor(Editor):
|
|
|
|
def get_edit_widget(self, initial_value):
|
|
|
|
self.edit_widget = QtWidgets.QCheckBox()
|
|
|
|
self.edit_widget.setChecked(bool(initial_value))
|
|
|
|
return self.edit_widget
|
|
|
|
|
|
|
|
def get_edit_widget_value(self):
|
|
|
|
return self.edit_widget.isChecked()
|
|
|
|
|
|
|
|
|
|
|
|
class StringEditor(Editor):
|
|
|
|
def get_edit_widget(self, initial_value):
|
|
|
|
self.edit_widget = QtWidgets.QLineEdit()
|
|
|
|
self.edit_widget.setText(initial_value)
|
|
|
|
return self.edit_widget
|
|
|
|
|
|
|
|
def get_edit_widget_value(self):
|
|
|
|
return self.edit_widget.text()
|
|
|
|
|
|
|
|
|
2021-07-13 12:53:35 +08:00
|
|
|
class Creator(QtWidgets.QDialog):
|
|
|
|
def __init__(self, parent, dataset_ctl):
|
|
|
|
QtWidgets.QDialog.__init__(self, parent=parent)
|
|
|
|
self.dataset_ctl = dataset_ctl
|
|
|
|
|
|
|
|
self.setWindowTitle("Create dataset")
|
|
|
|
grid = QtWidgets.QGridLayout()
|
|
|
|
grid.setRowMinimumHeight(1, 40)
|
|
|
|
grid.setColumnMinimumWidth(2, 60)
|
|
|
|
self.setLayout(grid)
|
|
|
|
|
|
|
|
grid.addWidget(QtWidgets.QLabel("Name:"), 0, 0)
|
|
|
|
self.name_widget = QtWidgets.QLineEdit()
|
|
|
|
grid.addWidget(self.name_widget, 0, 1)
|
|
|
|
|
|
|
|
grid.addWidget(QtWidgets.QLabel("Value:"), 1, 0)
|
|
|
|
self.value_widget = QtWidgets.QLineEdit()
|
|
|
|
self.value_widget.setPlaceholderText('PYON (Python)')
|
|
|
|
grid.addWidget(self.value_widget, 1, 1)
|
|
|
|
self.data_type = QtWidgets.QLabel("data type")
|
|
|
|
grid.addWidget(self.data_type, 1, 2)
|
|
|
|
self.value_widget.textChanged.connect(self.dtype)
|
|
|
|
|
|
|
|
grid.addWidget(QtWidgets.QLabel("Persist:"), 2, 0)
|
|
|
|
self.box_widget = QtWidgets.QCheckBox()
|
|
|
|
grid.addWidget(self.box_widget, 2, 1)
|
|
|
|
|
|
|
|
self.ok = QtWidgets.QPushButton('&Ok')
|
|
|
|
self.ok.setEnabled(False)
|
|
|
|
self.cancel = QtWidgets.QPushButton('&Cancel')
|
|
|
|
self.buttons = QtWidgets.QDialogButtonBox(self)
|
|
|
|
self.buttons.addButton(
|
|
|
|
self.ok, QtWidgets.QDialogButtonBox.AcceptRole)
|
|
|
|
self.buttons.addButton(
|
|
|
|
self.cancel, QtWidgets.QDialogButtonBox.RejectRole)
|
|
|
|
grid.setRowStretch(3, 1)
|
|
|
|
grid.addWidget(self.buttons, 4, 0, 1, 3)
|
|
|
|
self.buttons.accepted.connect(self.accept)
|
|
|
|
self.buttons.rejected.connect(self.reject)
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
key = self.name_widget.text()
|
|
|
|
value = self.value_widget.text()
|
|
|
|
persist = self.box_widget.isChecked()
|
|
|
|
asyncio.ensure_future(exc_to_warning(self.dataset_ctl.set(
|
|
|
|
key, pyon.decode(value), persist)))
|
|
|
|
QtWidgets.QDialog.accept(self)
|
|
|
|
|
|
|
|
def dtype(self):
|
|
|
|
txt = self.value_widget.text()
|
|
|
|
try:
|
|
|
|
result = pyon.decode(txt)
|
|
|
|
except:
|
|
|
|
pixmap = self.style().standardPixmap(
|
|
|
|
QtWidgets.QStyle.SP_MessageBoxWarning)
|
|
|
|
self.data_type.setPixmap(pixmap)
|
|
|
|
self.ok.setEnabled(False)
|
|
|
|
else:
|
|
|
|
self.data_type.setText(type(result).__name__)
|
|
|
|
self.ok.setEnabled(True)
|
|
|
|
|
|
|
|
|
2015-11-17 19:46:17 +08:00
|
|
|
class Model(DictSyncTreeSepModel):
|
2015-11-11 12:13:19 +08:00
|
|
|
def __init__(self, init):
|
2015-11-17 19:46:17 +08:00
|
|
|
DictSyncTreeSepModel.__init__(self, ".",
|
2016-04-05 18:15:09 +08:00
|
|
|
["Dataset", "Persistent", "Value"],
|
|
|
|
init)
|
2015-07-14 23:31:18 +08:00
|
|
|
|
|
|
|
def convert(self, k, v, column):
|
2015-11-17 19:46:17 +08:00
|
|
|
if column == 1:
|
2015-10-12 17:18:23 +08:00
|
|
|
return "Y" if v[0] else "N"
|
|
|
|
elif column == 2:
|
|
|
|
return short_format(v[1])
|
2015-07-14 23:31:18 +08:00
|
|
|
else:
|
2016-04-05 18:15:09 +08:00
|
|
|
raise ValueError
|
2015-07-14 23:31:18 +08:00
|
|
|
|
|
|
|
|
2016-02-14 19:15:57 +08:00
|
|
|
class DatasetsDock(QtWidgets.QDockWidget):
|
|
|
|
def __init__(self, datasets_sub, dataset_ctl):
|
|
|
|
QtWidgets.QDockWidget.__init__(self, "Datasets")
|
2016-02-14 20:46:15 +08:00
|
|
|
self.setObjectName("Datasets")
|
2016-02-14 19:15:57 +08:00
|
|
|
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
|
|
|
|
QtWidgets.QDockWidget.DockWidgetFloatable)
|
2016-02-14 05:46:29 +08:00
|
|
|
self.dataset_ctl = dataset_ctl
|
2015-07-14 23:31:18 +08:00
|
|
|
|
|
|
|
grid = LayoutWidget()
|
2016-02-14 19:15:57 +08:00
|
|
|
self.setWidget(grid)
|
2015-07-14 23:31:18 +08:00
|
|
|
|
2016-02-15 07:23:47 +08:00
|
|
|
self.search = QtWidgets.QLineEdit()
|
2015-10-12 17:31:55 +08:00
|
|
|
self.search.setPlaceholderText("search...")
|
|
|
|
self.search.editingFinished.connect(self._search_datasets)
|
2015-10-30 19:58:15 +08:00
|
|
|
grid.addWidget(self.search, 0, 0)
|
2015-10-12 17:31:55 +08:00
|
|
|
|
2016-02-15 07:23:47 +08:00
|
|
|
self.table = QtWidgets.QTreeView()
|
|
|
|
self.table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
|
2016-04-05 18:15:09 +08:00
|
|
|
self.table.setSelectionMode(
|
|
|
|
QtWidgets.QAbstractItemView.SingleSelection)
|
2015-10-12 17:31:55 +08:00
|
|
|
grid.addWidget(self.table, 1, 0)
|
2015-07-14 23:31:18 +08:00
|
|
|
|
2016-02-14 05:46:29 +08:00
|
|
|
self.table.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
|
2021-07-13 12:53:35 +08:00
|
|
|
create_action = QtWidgets.QAction("New dataset", self.table)
|
|
|
|
create_action.triggered.connect(self.create_clicked)
|
|
|
|
create_action.setShortcut("CTRL+N")
|
|
|
|
create_action.setShortcutContext(QtCore.Qt.WidgetShortcut)
|
|
|
|
self.table.addAction(create_action)
|
2016-06-17 16:45:26 +08:00
|
|
|
edit_action = QtWidgets.QAction("Edit dataset", self.table)
|
|
|
|
edit_action.triggered.connect(self.edit_clicked)
|
|
|
|
edit_action.setShortcut("RETURN")
|
|
|
|
edit_action.setShortcutContext(QtCore.Qt.WidgetShortcut)
|
|
|
|
self.table.doubleClicked.connect(self.edit_clicked)
|
|
|
|
self.table.addAction(edit_action)
|
2016-02-15 07:23:47 +08:00
|
|
|
delete_action = QtWidgets.QAction("Delete dataset", self.table)
|
2016-02-14 05:46:29 +08:00
|
|
|
delete_action.triggered.connect(self.delete_clicked)
|
|
|
|
delete_action.setShortcut("DELETE")
|
|
|
|
delete_action.setShortcutContext(QtCore.Qt.WidgetShortcut)
|
|
|
|
self.table.addAction(delete_action)
|
|
|
|
|
2015-11-11 12:13:19 +08:00
|
|
|
self.table_model = Model(dict())
|
|
|
|
datasets_sub.add_setmodel_callback(self.set_model)
|
2015-07-14 23:31:18 +08:00
|
|
|
|
2015-10-12 17:31:55 +08:00
|
|
|
def _search_datasets(self):
|
2015-11-11 12:13:19 +08:00
|
|
|
if hasattr(self, "table_model_filter"):
|
|
|
|
self.table_model_filter.setFilterFixedString(
|
|
|
|
self.search.displayText())
|
2015-10-12 17:31:55 +08:00
|
|
|
|
2015-11-11 12:13:19 +08:00
|
|
|
def set_model(self, model):
|
|
|
|
self.table_model = model
|
2016-08-14 18:33:24 +08:00
|
|
|
self.table_model_filter = QRecursiveFilterProxyModel()
|
2015-10-14 19:29:58 +08:00
|
|
|
self.table_model_filter.setSourceModel(self.table_model)
|
|
|
|
self.table.setModel(self.table_model_filter)
|
2016-02-14 05:46:29 +08:00
|
|
|
|
2021-07-13 12:53:35 +08:00
|
|
|
def create_clicked(self):
|
|
|
|
Creator(self, self.dataset_ctl).open()
|
|
|
|
|
2016-06-17 16:45:26 +08:00
|
|
|
def edit_clicked(self):
|
|
|
|
idx = self.table.selectedIndexes()
|
|
|
|
if idx:
|
|
|
|
idx = self.table_model_filter.mapToSource(idx[0])
|
|
|
|
key = self.table_model.index_to_key(idx)
|
|
|
|
if key is not None:
|
|
|
|
persist, value = self.table_model.backing_store[key]
|
|
|
|
t = type(value)
|
|
|
|
if np.issubdtype(t, np.number):
|
|
|
|
dialog_cls = NumberEditor
|
|
|
|
elif np.issubdtype(t, np.bool_):
|
|
|
|
dialog_cls = BoolEditor
|
|
|
|
elif np.issubdtype(t, np.unicode_):
|
|
|
|
dialog_cls = StringEditor
|
|
|
|
else:
|
|
|
|
logger.error("Cannot edit dataset %s: "
|
|
|
|
"type %s is not supported", key, t)
|
|
|
|
return
|
2016-07-03 12:23:56 +08:00
|
|
|
dialog_cls(self, self.dataset_ctl, key, value).open()
|
2016-06-17 16:45:26 +08:00
|
|
|
|
2016-02-14 05:46:29 +08:00
|
|
|
def delete_clicked(self):
|
|
|
|
idx = self.table.selectedIndexes()
|
|
|
|
if idx:
|
|
|
|
idx = self.table_model_filter.mapToSource(idx[0])
|
|
|
|
key = self.table_model.index_to_key(idx)
|
|
|
|
if key is not None:
|
|
|
|
asyncio.ensure_future(self.dataset_ctl.delete(key))
|
2016-03-25 18:33:22 +08:00
|
|
|
|
|
|
|
def save_state(self):
|
|
|
|
return bytes(self.table.header().saveState())
|
|
|
|
|
|
|
|
def restore_state(self, state):
|
|
|
|
self.table.header().restoreState(QtCore.QByteArray(state))
|