artiq/artiq/gui/shortcuts.py

109 lines
3.7 KiB
Python
Raw Normal View History

import logging
2015-10-27 17:59:34 +08:00
from functools import partial
2016-01-10 18:38:24 +08:00
from quamash import QtGui, QtCore, QtWidgets
from pyqtgraph import dockarea
2015-10-27 17:59:34 +08:00
logger = logging.getLogger(__name__)
2015-10-27 17:59:34 +08:00
class ShortcutsDock(dockarea.Dock):
2015-12-01 18:22:12 +08:00
def __init__(self, main_window, exp_manager):
2015-12-11 00:20:20 +08:00
dockarea.Dock.__init__(self, "Shortcuts")
self.layout.setSpacing(5)
self.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"]):
label = QtGui.QLabel("<b>" + title + "</b>")
self.addWidget(label, 0, n)
2015-10-27 17:59:34 +08:00
label.setMaximumHeight(label.sizeHint().height())
self.layout.setColumnStretch(1, 1)
2015-10-27 17:59:34 +08:00
for i in range(12):
row = i + 1
self.addWidget(QtGui.QLabel("F" + str(i+1)), row, 0)
2015-10-27 17:59:34 +08:00
label = QtGui.QLabel()
label.setSizePolicy(QtGui.QSizePolicy.Ignored,
QtGui.QSizePolicy.Ignored)
self.addWidget(label, row, 1)
2015-10-27 17:59:34 +08:00
clear = QtGui.QToolButton()
clear.setIcon(QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_DialogResetButton))
self.addWidget(clear, row, 2)
clear.clicked.connect(partial(self.set_shortcut, i, ""))
open = QtGui.QToolButton()
open.setIcon(QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_DialogOpenButton))
self.addWidget(open, row, 3)
open.clicked.connect(partial(self._open_experiment, i))
submit = QtGui.QPushButton("Submit")
submit.setIcon(QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_DialogOkButton))
self.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)