applets: two column table, remove spurious Qt text editors

This commit is contained in:
Sebastien Bourdeauducq 2016-09-09 09:07:31 +08:00
parent 6aaf6c8789
commit ff20ed2710
1 changed files with 35 additions and 31 deletions

View File

@ -13,7 +13,7 @@ from PyQt5 import QtCore, QtGui, QtWidgets
from artiq.protocols.pipe_ipc import AsyncioParentComm from artiq.protocols.pipe_ipc import AsyncioParentComm
from artiq.protocols.logging import LogParser from artiq.protocols.logging import LogParser
from artiq.protocols import pyon from artiq.protocols import pyon
from artiq.gui.tools import QDockWidgetCloseDetect from artiq.gui.tools import QDockWidgetCloseDetect, LayoutWidget
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -325,8 +325,8 @@ class AppletsDock(QtWidgets.QDockWidget):
self.applet_uids = set() self.applet_uids = set()
self.table = QtWidgets.QTreeWidget() self.table = QtWidgets.QTreeWidget()
self.table.setColumnCount(3) self.table.setColumnCount(2)
self.table.setHeaderLabels(["Enable", "Name", "Command"]) self.table.setHeaderLabels(["Name", "Command"])
self.table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.table.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection) self.table.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
@ -343,7 +343,7 @@ class AppletsDock(QtWidgets.QDockWidget):
self.setWidget(self.table) self.setWidget(self.table)
completer_delegate = _CompleterDelegate() completer_delegate = _CompleterDelegate()
self.table.setItemDelegateForColumn(2, completer_delegate) self.table.setItemDelegateForColumn(1, completer_delegate)
datasets_sub.add_setmodel_callback(completer_delegate.set_model) datasets_sub.add_setmodel_callback(completer_delegate.set_model)
self.table.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu) self.table.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
@ -378,10 +378,10 @@ class AppletsDock(QtWidgets.QDockWidget):
def get_spec(self, item): def get_spec(self, item):
if item.applet_spec_ty == "command": if item.applet_spec_ty == "command":
return {"ty": "command", "command": item.text(2)} return {"ty": "command", "command": item.text(1)}
elif item.applet_spec_ty == "code": elif item.applet_spec_ty == "code":
return {"ty": "code", "code": item.applet_code, return {"ty": "code", "code": item.applet_code,
"command": item.text(2)} "command": item.text(1)}
else: else:
raise ValueError raise ValueError
@ -389,13 +389,13 @@ class AppletsDock(QtWidgets.QDockWidget):
self.table.itemChanged.disconnect() self.table.itemChanged.disconnect()
try: try:
item.applet_spec_ty = spec["ty"] item.applet_spec_ty = spec["ty"]
item.setText(2, spec["command"]) item.setText(1, spec["command"])
if spec["ty"] == "command": if spec["ty"] == "command":
item.setIcon(2, QtGui.QIcon()) item.setIcon(1, QtGui.QIcon())
if hasattr(item, "applet_code"): if hasattr(item, "applet_code"):
del item.applet_code del item.applet_code
elif spec["ty"] == "code": elif spec["ty"] == "code":
item.setIcon(2, QtWidgets.QApplication.style().standardIcon( item.setIcon(1, QtWidgets.QApplication.style().standardIcon(
QtWidgets.QStyle.SP_FileIcon)) QtWidgets.QStyle.SP_FileIcon))
item.applet_code = spec["code"] item.applet_code = spec["code"]
else: else:
@ -416,30 +416,31 @@ class AppletsDock(QtWidgets.QDockWidget):
def item_changed(self, item, column): def item_changed(self, item, column):
if item.ty == "applet": if item.ty == "applet":
new_value = item.text(column)
dock = item.applet_dock
if dock is not None:
if column == 0:
dock.rename(new_value)
else:
dock.spec = self.get_spec(item)
if column == 0: if column == 0:
if item.checkState(0) == QtCore.Qt.Checked: if item.checkState(0) == QtCore.Qt.Checked:
name = item.text(1) if item.applet_dock is None:
spec = self.get_spec(item) name = item.text(0)
dock = self.create(item.applet_uid, name, spec) spec = self.get_spec(item)
item.applet_dock = dock dock = self.create(item.applet_uid, name, spec)
if item.applet_geometry is not None: item.applet_dock = dock
dock.restoreGeometry(item.applet_geometry) if item.applet_geometry is not None:
# geometry is now handled by main window state dock.restoreGeometry(item.applet_geometry)
item.applet_geometry = None # geometry is now handled by main window state
self.dock_to_item[dock] = item item.applet_geometry = None
self.dock_to_item[dock] = item
else: else:
dock = item.applet_dock dock = item.applet_dock
if dock is not None: if dock is not None:
# This calls self.on_dock_closed # This calls self.on_dock_closed
dock.close() dock.close()
elif column == 1 or column == 2:
new_value = item.text(column)
dock = item.applet_dock
if dock is not None:
if column == 1:
dock.rename(new_value)
else:
dock.spec = self.get_spec(item)
elif item.ty == "group": elif item.ty == "group":
# To Qt's credit, it already does everything for us here. # To Qt's credit, it already does everything for us here.
pass pass
@ -459,7 +460,7 @@ class AppletsDock(QtWidgets.QDockWidget):
def walk(wi): def walk(wi):
for i in range(wi.childCount()): for i in range(wi.childCount()):
cwi = wi.child(i) cwi = wi.child(i)
existing_names.add(cwi.text(1)) existing_names.add(cwi.text(0))
walk(cwi) walk(cwi)
walk(self.table.invisibleRootItem()) walk(self.table.invisibleRootItem())
@ -480,7 +481,7 @@ class AppletsDock(QtWidgets.QDockWidget):
if name is None: if name is None:
name = self.get_untitled() name = self.get_untitled()
item = QtWidgets.QTreeWidgetItem(["", name, ""]) item = QtWidgets.QTreeWidgetItem([name, ""])
item.ty = "applet" item.ty = "applet"
item.setFlags(QtCore.Qt.ItemIsSelectable | item.setFlags(QtCore.Qt.ItemIsSelectable |
QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsUserCheckable |
@ -504,7 +505,7 @@ class AppletsDock(QtWidgets.QDockWidget):
def new_group(self, name=None, parent=None): def new_group(self, name=None, parent=None):
if name is None: if name is None:
name = self.get_untitled() name = self.get_untitled()
item = QtWidgets.QTreeWidgetItem(["", name]) item = QtWidgets.QTreeWidgetItem([name])
item.ty = "group" item.ty = "group"
item.setFlags(QtCore.Qt.ItemIsSelectable | item.setFlags(QtCore.Qt.ItemIsSelectable |
QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEditable |
@ -519,6 +520,9 @@ class AppletsDock(QtWidgets.QDockWidget):
self.table.addTopLevelItem(item) self.table.addTopLevelItem(item)
else: else:
parent.addChild(item) parent.addChild(item)
# HACK: make the cell non-editable. Qt doesn't even provide
# a clean API for such a basic feature.
self.table.setItemWidget(item, 1, QtWidgets.QLabel())
return item return item
def new_with_parent(self, cb, **kwargs): def new_with_parent(self, cb, **kwargs):
@ -593,14 +597,14 @@ class AppletsDock(QtWidgets.QDockWidget):
if cwi.ty == "applet": if cwi.ty == "applet":
uid = cwi.applet_uid uid = cwi.applet_uid
enabled = cwi.checkState(0) == QtCore.Qt.Checked enabled = cwi.checkState(0) == QtCore.Qt.Checked
name = cwi.text(1) name = cwi.text(0)
spec = self.get_spec(cwi) spec = self.get_spec(cwi)
geometry = cwi.applet_geometry geometry = cwi.applet_geometry
if geometry is not None: if geometry is not None:
geometry = bytes(geometry) geometry = bytes(geometry)
state.append(("applet", uid, enabled, name, spec, geometry)) state.append(("applet", uid, enabled, name, spec, geometry))
elif cwi.ty == "group": elif cwi.ty == "group":
name = cwi.text(1) name = cwi.text(0)
expanded = cwi.isExpanded() expanded = cwi.isExpanded()
state_child = self.save_state_item(cwi) state_child = self.save_state_item(cwi)
state.append(("group", name, expanded, state_child)) state.append(("group", name, expanded, state_child))