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

85 lines
2.9 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
2016-02-15 07:23:47 +08:00
from PyQt5 import QtCore, QtWidgets
2015-07-14 23:31:18 +08:00
from artiq.tools import short_format
2016-02-15 07:05:30 +08:00
from artiq.gui.tools import LayoutWidget
2015-11-17 19:46:17 +08:00
from artiq.gui.models import DictSyncTreeSepModel
2015-07-14 23:31:18 +08:00
2015-08-01 19:37:16 +08:00
logger = logging.getLogger(__name__)
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:
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)
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
2016-01-10 18:38:24 +08:00
self.table_model_filter = QtCore.QSortFilterProxyModel()
self.table_model_filter.setSourceModel(self.table_model)
self.table.setModel(self.table_model_filter)
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))