artiq/artiq/gui/shortcuts.py

117 lines
4.0 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 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"]):
label = QtGui.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
layout.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)
layout.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))
layout.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))
layout.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))
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)