forked from M-Labs/artiq
1
0
Fork 0
artiq/artiq/dashboard/datasets.py

194 lines
7.3 KiB
Python
Raw Normal View History

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
import numpy as np
2016-02-15 07:23:47 +08:00
from PyQt5 import QtCore, QtWidgets
from sipyco import pyon
2015-07-14 23:31:18 +08:00
from artiq.tools import short_format, exc_to_warning
from artiq.gui.tools import LayoutWidget, QRecursiveFilterProxyModel
2015-11-17 19:46:17 +08:00
from artiq.gui.models import DictSyncTreeSepModel
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__)
async def rename(key, new_key, value, persist, dataset_ctl):
if key != new_key:
await dataset_ctl.delete(key)
await dataset_ctl.set(new_key, value, persist)
class CreateEditDialog(QtWidgets.QDialog):
def __init__(self, parent, dataset_ctl, key=None, value=None, persist=False):
QtWidgets.QDialog.__init__(self, parent=parent)
self.dataset_ctl = dataset_ctl
self.setWindowTitle("Create dataset" if key is None else "Edit 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, alignment=QtCore.Qt.AlignHCenter)
self.buttons.accepted.connect(self.accept)
self.buttons.rejected.connect(self.reject)
self.key = key
self.name_widget.setText(key)
self.value_widget.setText(value)
self.box_widget.setChecked(persist)
def accept(self):
key = self.name_widget.text()
value = self.value_widget.text()
persist = self.box_widget.isChecked()
if self.key and self.key != key:
asyncio.ensure_future(exc_to_warning(rename(self.key, key, pyon.decode(value), persist, self.dataset_ctl)))
else:
asyncio.ensure_future(exc_to_warning(self.dataset_ctl.set(key, pyon.decode(value), persist)))
self.key = key
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):
def __init__(self, init):
DictSyncTreeSepModel.__init__(self, ".",
["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:
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
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")
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
QtWidgets.QDockWidget.DockWidgetFloatable)
self.dataset_ctl = dataset_ctl
2015-07-14 23:31:18 +08:00
grid = LayoutWidget()
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
self.table.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
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)
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)
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
self.table_model_filter = QRecursiveFilterProxyModel()
self.table_model_filter.setSourceModel(self.table_model)
self.table.setModel(self.table_model_filter)
def create_clicked(self):
CreateEditDialog(self, self.dataset_ctl).open()
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) or np.issubdtype(t, np.bool_):
value = str(value)
elif np.issubdtype(t, np.unicode_):
value = '"{}"'.format(str(value))
else:
value = pyon.encode(value)
CreateEditDialog(self, self.dataset_ctl, key, value, persist).open()
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))
def save_state(self):
return bytes(self.table.header().saveState())
def restore_state(self, state):
self.table.header().restoreState(QtCore.QByteArray(state))