Commit ca52ceff authored by Valentin Valls's avatar Valentin Valls

Refactor Flint plot item selection as an event managed by the property view

parent 021f3098
...@@ -207,11 +207,10 @@ class YAxesPropertyItemDelegate(qt.QStyledItemDelegate): ...@@ -207,11 +207,10 @@ class YAxesPropertyItemDelegate(qt.QStyledItemDelegate):
class _AddItemAction(qt.QWidgetAction): class _AddItemAction(qt.QWidgetAction):
def __init__(self, parent: qt.QObject, tree: qt.QTreeView): def __init__(self, parent: qt.QObject):
assert isinstance(parent, CurvePlotPropertyWidget)
super(_AddItemAction, self).__init__(parent) super(_AddItemAction, self).__init__(parent)
self.__tree = tree parent.plotItemSelected.connect(self.__selectionChanged)
selectionModel = self.__tree.selectionModel()
selectionModel.currentChanged.connect(self.__selectionChanged)
widget = qt.QToolButton(parent) widget = qt.QToolButton(parent)
icon = icons.getQIcon("flint:icons/add-item") icon = icons.getQIcon("flint:icons/add-item")
...@@ -231,7 +230,7 @@ class _AddItemAction(qt.QWidgetAction): ...@@ -231,7 +230,7 @@ class _AddItemAction(qt.QWidgetAction):
menu: qt.QMenu = self.sender() menu: qt.QMenu = self.sender()
menu.clear() menu.clear()
item = self.__getSelectedPlotItem() item = self.parent().selectedPlotItem()
if isinstance(item, plot_item_model.CurveMixIn): if isinstance(item, plot_item_model.CurveMixIn):
menu.addSection("Statistics") menu.addSection("Statistics")
...@@ -270,26 +269,11 @@ class _AddItemAction(qt.QWidgetAction): ...@@ -270,26 +269,11 @@ class _AddItemAction(qt.QWidgetAction):
action.setEnabled(False) action.setEnabled(False)
menu.addAction(action) menu.addAction(action)
def __selectionChanged(self, current: qt.QModelIndex, previous: qt.QModelIndex): def __selectionChanged(self, current: plot_model.Item):
item = self.__getSelectedPlotItem() self.defaultWidget().setEnabled(current is not None)
self.defaultWidget().setEnabled(item is not None)
def __getSelectedPlotItem(self):
index = self.__tree.currentIndex()
if not index.isValid():
self.setEnabled(False)
return
model = self.__tree.model()
index = model.index(index.row(), 0, index.parent())
model = self.__tree.model()
item = model.itemFromIndex(index)
if isinstance(item, _DataItem):
plotItem = item.plotItem()
return plotItem
return None
def __createMax(self): def __createMax(self):
parentItem = self.__getSelectedPlotItem() parentItem = self.parent().selectedPlotItem()
if parentItem is not None: if parentItem is not None:
plot = parentItem.plot() plot = parentItem.plot()
newItem = plot_state_model.MaxCurveItem(plot) newItem = plot_state_model.MaxCurveItem(plot)
...@@ -298,7 +282,7 @@ class _AddItemAction(qt.QWidgetAction): ...@@ -298,7 +282,7 @@ class _AddItemAction(qt.QWidgetAction):
plot.addItem(newItem) plot.addItem(newItem)
def __createMin(self): def __createMin(self):
parentItem = self.__getSelectedPlotItem() parentItem = self.parent().selectedPlotItem()
if parentItem is not None: if parentItem is not None:
plot = parentItem.plot() plot = parentItem.plot()
newItem = plot_state_model.MinCurveItem(plot) newItem = plot_state_model.MinCurveItem(plot)
...@@ -307,7 +291,7 @@ class _AddItemAction(qt.QWidgetAction): ...@@ -307,7 +291,7 @@ class _AddItemAction(qt.QWidgetAction):
plot.addItem(newItem) plot.addItem(newItem)
def __createDerivative(self): def __createDerivative(self):
parentItem = self.__getSelectedPlotItem() parentItem = self.parent().selectedPlotItem()
if parentItem is not None: if parentItem is not None:
plot = parentItem.plot() plot = parentItem.plot()
newItem = plot_state_model.DerivativeItem(plot) newItem = plot_state_model.DerivativeItem(plot)
...@@ -316,7 +300,7 @@ class _AddItemAction(qt.QWidgetAction): ...@@ -316,7 +300,7 @@ class _AddItemAction(qt.QWidgetAction):
plot.addItem(newItem) plot.addItem(newItem)
def __createGaussianFit(self): def __createGaussianFit(self):
parentItem = self.__getSelectedPlotItem() parentItem = self.parent().selectedPlotItem()
if parentItem is not None: if parentItem is not None:
plot = parentItem.plot() plot = parentItem.plot()
newItem = plot_state_model.GaussianFitItem(plot) newItem = plot_state_model.GaussianFitItem(plot)
...@@ -581,6 +565,8 @@ class CurvePlotPropertyWidget(qt.QWidget): ...@@ -581,6 +565,8 @@ class CurvePlotPropertyWidget(qt.QWidget):
StyleColumn = 4 StyleColumn = 4
RemoveColumn = 5 RemoveColumn = 5
plotItemSelected = qt.Signal(object)
def __init__(self, parent=None): def __init__(self, parent=None):
super(CurvePlotPropertyWidget, self).__init__(parent=parent) super(CurvePlotPropertyWidget, self).__init__(parent=parent)
self.__scan: Optional[scan_model.Scan] = None self.__scan: Optional[scan_model.Scan] = None
...@@ -597,8 +583,10 @@ class CurvePlotPropertyWidget(qt.QWidget): ...@@ -597,8 +583,10 @@ class CurvePlotPropertyWidget(qt.QWidget):
self.__removeDelegate = delegates.RemovePropertyItemDelegate(self) self.__removeDelegate = delegates.RemovePropertyItemDelegate(self)
model = qt.QStandardItemModel(self) model = qt.QStandardItemModel(self)
self.__tree.setModel(model) self.__tree.setModel(model)
selectionModel = self.__tree.selectionModel()
selectionModel.currentChanged.connect(self.__selectionChanged)
self.__scan = None self.__scan = None
self.__focusWidget = None self.__focusWidget = None
...@@ -612,10 +600,28 @@ class CurvePlotPropertyWidget(qt.QWidget): ...@@ -612,10 +600,28 @@ class CurvePlotPropertyWidget(qt.QWidget):
def __createToolBar(self): def __createToolBar(self):
toolBar = qt.QToolBar(self) toolBar = qt.QToolBar(self)
toolBar.setMovable(False) toolBar.setMovable(False)
action = _AddItemAction(self, self.__tree) action = _AddItemAction(self)
toolBar.addAction(action) toolBar.addAction(action)
return toolBar return toolBar
def __selectionChanged(self, current: qt.QModelIndex, previous: qt.QModelIndex):
item = self.selectedPlotItem()
self.plotItemSelected.emit(item)
def selectedPlotItem(self) -> Optional[plot_model.Item]:
index = self.__tree.currentIndex()
if not index.isValid():
self.setEnabled(False)
return
model = self.__tree.model()
index = model.index(index.row(), 0, index.parent())
model = self.__tree.model()
item = model.itemFromIndex(index)
if isinstance(item, _DataItem):
plotItem = item.plotItem()
return plotItem
return None
def setFlintModel(self, flintModel: flint_model.FlintState = None): def setFlintModel(self, flintModel: flint_model.FlintState = None):
if self.__flintModel is not None: if self.__flintModel is not None:
self.__flintModel.currentScanChanged.disconnect(self.__currentScanChanged) self.__flintModel.currentScanChanged.disconnect(self.__currentScanChanged)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment