2015-12-01 17:10:44 +08:00
|
|
|
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
|
2016-02-14 19:15:57 +08:00
|
|
|
from pyqtgraph import LayoutWidget
|
2015-10-27 17:59:34 +08:00
|
|
|
|
|
|
|
|
2015-12-01 17:10:44 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-10-27 17:59:34 +08:00
|
|
|
|
|
|
|
|
2016-02-14 19:15:57 +08:00
|
|
|
class ShortcutsDock(QtWidgets.QDockWidget):
|
2015-12-01 18:22:12 +08:00
|
|
|
def __init__(self, main_window, exp_manager):
|
2016-02-14 19:15:57 +08:00
|
|
|
QtWidgets.QDockWidget.__init__(self, "Shortcuts")
|
2016-02-14 20:46:15 +08:00
|
|
|
self.setObjectName("Shortcuts")
|
2016-02-14 19:15:57 +08:00
|
|
|
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
|
|
|
|
2015-12-01 17:10:44 +08:00
|
|
|
self.exp_manager = exp_manager
|
|
|
|
self.shortcut_widgets = dict()
|
|
|
|
|
|
|
|
for n, title in enumerate(["Key", "Experiment"]):
|
|
|
|
label = QtGui.QLabel("<b>" + title + "</b>")
|
2016-02-14 19:15:57 +08:00
|
|
|
layout.addWidget(label, 0, n)
|
2015-10-27 17:59:34 +08:00
|
|
|
label.setMaximumHeight(label.sizeHint().height())
|
2016-02-14 19:15:57 +08:00
|
|
|
layout.setColumnStretch(1, 1)
|
2015-10-27 17:59:34 +08:00
|
|
|
|
|
|
|
for i in range(12):
|
|
|
|
row = i + 1
|
|
|
|
|
2016-02-14 19:15:57 +08:00
|
|
|
layout.addWidget(QtGui.QLabel("F" + str(i+1)), row, 0)
|
2015-10-27 17:59:34 +08:00
|
|
|
|
2015-12-01 17:10:44 +08:00
|
|
|
label = QtGui.QLabel()
|
2015-12-01 18:04:43 +08:00
|
|
|
label.setSizePolicy(QtGui.QSizePolicy.Ignored,
|
|
|
|
QtGui.QSizePolicy.Ignored)
|
2016-02-14 19:15:57 +08:00
|
|
|
layout.addWidget(label, row, 1)
|
2015-10-27 17:59:34 +08:00
|
|
|
|
2015-12-01 17:10:44 +08:00
|
|
|
clear = QtGui.QToolButton()
|
|
|
|
clear.setIcon(QtGui.QApplication.style().standardIcon(
|
|
|
|
QtGui.QStyle.SP_DialogResetButton))
|
2016-02-14 19:15:57 +08:00
|
|
|
layout.addWidget(clear, row, 2)
|
2015-12-01 17:10:44 +08:00
|
|
|
clear.clicked.connect(partial(self.set_shortcut, i, ""))
|
|
|
|
|
2015-12-01 18:04:43 +08:00
|
|
|
open = QtGui.QToolButton()
|
|
|
|
open.setIcon(QtGui.QApplication.style().standardIcon(
|
|
|
|
QtGui.QStyle.SP_DialogOpenButton))
|
2016-02-14 19:15:57 +08:00
|
|
|
layout.addWidget(open, row, 3)
|
2015-12-01 18:04:43 +08:00
|
|
|
open.clicked.connect(partial(self._open_experiment, i))
|
|
|
|
|
2015-12-01 17:10:44 +08:00
|
|
|
submit = QtGui.QPushButton("Submit")
|
|
|
|
submit.setIcon(QtGui.QApplication.style().standardIcon(
|
|
|
|
QtGui.QStyle.SP_DialogOkButton))
|
2016-02-14 19:15:57 +08:00
|
|
|
layout.addWidget(submit, row, 4)
|
2015-12-01 17:10:44 +08:00
|
|
|
submit.clicked.connect(partial(self._activated, i))
|
|
|
|
|
2015-12-03 16:30:37 +08:00
|
|
|
clear.hide()
|
|
|
|
open.hide()
|
|
|
|
submit.hide()
|
|
|
|
|
2015-12-01 17:10:44 +08:00
|
|
|
self.shortcut_widgets[i] = {
|
|
|
|
"label": label,
|
|
|
|
"clear": clear,
|
2015-12-01 18:04:43 +08:00
|
|
|
"open": open,
|
2015-12-01 17:10:44 +08:00
|
|
|
"submit": submit
|
|
|
|
}
|
2016-01-10 18:38:24 +08:00
|
|
|
shortcut = QtWidgets.QShortcut("F" + str(i+1), main_window)
|
2015-12-01 18:49:23 +08:00
|
|
|
shortcut.setContext(QtCore.Qt.ApplicationShortcut)
|
2015-10-27 17:59:34 +08:00
|
|
|
shortcut.activated.connect(partial(self._activated, i))
|
|
|
|
|
|
|
|
def _activated(self, nr):
|
2015-12-08 17:52:38 +08:00
|
|
|
expurl = self.shortcut_widgets[nr]["label"].text()
|
|
|
|
if expurl:
|
2015-12-01 17:10:44 +08:00
|
|
|
try:
|
2015-12-08 17:52:38 +08:00
|
|
|
self.exp_manager.submit(expurl)
|
2015-12-01 17:10:44 +08:00
|
|
|
except:
|
2015-12-01 18:22:12 +08:00
|
|
|
# May happen when experiment has been removed
|
|
|
|
# from repository/explist
|
2015-12-01 17:10:44 +08:00
|
|
|
logger.warning("failed to submit experiment %s",
|
2015-12-08 17:52:38 +08:00
|
|
|
expurl, exc_info=True)
|
2015-12-01 17:10:44 +08:00
|
|
|
|
2015-12-01 18:04:43 +08:00
|
|
|
def _open_experiment(self, nr):
|
2015-12-08 17:52:38 +08:00
|
|
|
expurl = self.shortcut_widgets[nr]["label"].text()
|
|
|
|
if expurl:
|
2015-12-01 18:04:43 +08:00
|
|
|
try:
|
2015-12-08 17:52:38 +08:00
|
|
|
self.exp_manager.open_experiment(expurl)
|
2015-12-01 18:04:43 +08:00
|
|
|
except:
|
2015-12-01 18:22:12 +08:00
|
|
|
# May happen when experiment has been removed
|
|
|
|
# from repository/explist
|
2015-12-01 18:04:43 +08:00
|
|
|
logger.warning("failed to open experiment %s",
|
2015-12-08 17:52:38 +08:00
|
|
|
expurl, exc_info=True)
|
2015-12-01 18:04:43 +08:00
|
|
|
|
2015-12-08 17:52:38 +08:00
|
|
|
def set_shortcut(self, nr, expurl):
|
2015-12-01 17:10:44 +08:00
|
|
|
widgets = self.shortcut_widgets[nr]
|
2015-12-08 17:52:38 +08:00
|
|
|
widgets["label"].setText(expurl)
|
|
|
|
if expurl:
|
2015-12-01 17:10:44 +08:00
|
|
|
widgets["clear"].show()
|
2015-12-01 18:04:43 +08:00
|
|
|
widgets["open"].show()
|
2015-12-01 17:10:44 +08:00
|
|
|
widgets["submit"].show()
|
|
|
|
else:
|
|
|
|
widgets["clear"].hide()
|
2015-12-01 18:04:43 +08:00
|
|
|
widgets["open"].hide()
|
2015-12-01 17:10:44 +08:00
|
|
|
widgets["submit"].hide()
|
2015-10-27 17:59:34 +08:00
|
|
|
|
|
|
|
def save_state(self):
|
2015-12-01 17:10:44 +08:00
|
|
|
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):
|
2015-12-08 17:52:38 +08:00
|
|
|
for nr, expurl in state.items():
|
|
|
|
self.set_shortcut(nr, expurl)
|