artiq/artiq/dashboard/shortcuts.py

118 lines
4.1 KiB
Python
Raw Normal View History

import logging
2015-10-27 17:59:34 +08:00
from functools import partial
2016-02-15 07:23:47 +08:00
from PyQt5 import QtCore, QtWidgets
2016-02-15 07:05:30 +08:00
from artiq.gui.tools import LayoutWidget
2015-10-27 17:59:34 +08:00
logger = logging.getLogger(__name__)
2015-10-27 17:59:34 +08:00
class ShortcutsDock(QtWidgets.QDockWidget):
2015-12-01 18:22:12 +08:00
def __init__(self, main_window, exp_manager):
QtWidgets.QDockWidget.__init__(self, "Shortcuts")
2016-02-14 20:46:15 +08:00
self.setObjectName("Shortcuts")
self.setFeatures(QtWidgets.QDockWidget.DockWidgetMovable |
QtWidgets.QDockWidget.DockWidgetFloatable)
layout = QtWidgets.QGridLayout()
top_widget = QtWidgets.QWidget()
top_widget.setLayout(layout)
self.setWidget(top_widget)
layout.setSpacing(5)
layout.setContentsMargins(5, 5, 5, 5)
2015-10-27 17:59:34 +08:00
self.exp_manager = exp_manager
self.shortcut_widgets = dict()
for n, title in enumerate(["Key", "Experiment"]):
2016-02-15 07:23:47 +08:00
label = QtWidgets.QLabel("<b>" + title + "</b>")
layout.addWidget(label, 0, n)
2015-10-27 17:59:34 +08:00
label.setMaximumHeight(label.sizeHint().height())
layout.setColumnStretch(1, 1)
2015-10-27 17:59:34 +08:00
for i in range(12):
row = i + 1
2016-02-15 07:23:47 +08:00
layout.addWidget(QtWidgets.QLabel("F" + str(i+1)), row, 0)
2015-10-27 17:59:34 +08:00
2016-02-15 07:23:47 +08:00
label = QtWidgets.QLabel()
label.setSizePolicy(QtWidgets.QSizePolicy.Ignored,
QtWidgets.QSizePolicy.Ignored)
layout.addWidget(label, row, 1)
2015-10-27 17:59:34 +08:00
2016-02-15 07:23:47 +08:00
clear = QtWidgets.QToolButton()
clear.setIcon(QtWidgets.QApplication.style().standardIcon(
QtWidgets.QStyle.SP_DialogDiscardButton))
layout.addWidget(clear, row, 2)
clear.clicked.connect(partial(self.set_shortcut, i, ""))
2016-02-15 07:23:47 +08:00
open = QtWidgets.QToolButton()
open.setIcon(QtWidgets.QApplication.style().standardIcon(
QtWidgets.QStyle.SP_DialogOpenButton))
layout.addWidget(open, row, 3)
open.clicked.connect(partial(self._open_experiment, i))
2016-02-15 07:23:47 +08:00
submit = QtWidgets.QPushButton("Submit")
submit.setIcon(QtWidgets.QApplication.style().standardIcon(
QtWidgets.QStyle.SP_DialogOkButton))
layout.addWidget(submit, row, 4)
submit.clicked.connect(partial(self._activated, i))
clear.hide()
open.hide()
submit.hide()
self.shortcut_widgets[i] = {
"label": label,
"clear": clear,
"open": open,
"submit": submit
}
2016-01-10 18:38:24 +08:00
shortcut = QtWidgets.QShortcut("F" + str(i+1), main_window)
shortcut.setContext(QtCore.Qt.ApplicationShortcut)
2015-10-27 17:59:34 +08:00
shortcut.activated.connect(partial(self._activated, i))
def _activated(self, nr):
expurl = self.shortcut_widgets[nr]["label"].text()
if expurl:
try:
self.exp_manager.submit(expurl)
except:
2015-12-01 18:22:12 +08:00
# May happen when experiment has been removed
# from repository/explist
logger.warning("failed to submit experiment %s",
expurl, exc_info=True)
def _open_experiment(self, nr):
expurl = self.shortcut_widgets[nr]["label"].text()
if expurl:
try:
self.exp_manager.open_experiment(expurl)
except:
2015-12-01 18:22:12 +08:00
# May happen when experiment has been removed
# from repository/explist
logger.warning("failed to open experiment %s",
expurl, exc_info=True)
def set_shortcut(self, nr, expurl):
widgets = self.shortcut_widgets[nr]
widgets["label"].setText(expurl)
if expurl:
widgets["clear"].show()
widgets["open"].show()
widgets["submit"].show()
else:
widgets["clear"].hide()
widgets["open"].hide()
widgets["submit"].hide()
2015-10-27 17:59:34 +08:00
def save_state(self):
return {nr: widgets["label"].text()
for nr, widgets in self.shortcut_widgets.items()}
2015-10-27 17:59:34 +08:00
def restore_state(self, state):
for nr, expurl in state.items():
self.set_shortcut(nr, expurl)