Skip to content

Commit

Permalink
Merge pull request #59 from aksharsarvesh/EGUAxes
Browse files Browse the repository at this point in the history
ENH: EGU Axes
  • Loading branch information
zdomke authored Sep 25, 2024
2 parents 5242957 + 2fe2547 commit 79e6a94
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion trace/mixins/traces_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def accept_formula(self) -> None:
""" Retrieve the formula and PV name and perform desired actions
We take in the formula (prepend the formula tag) and attempt to create a curve. Iff it passes, we close the window"""
formula = "f://" + self.field.text()
passed = self.curveModel.replaceToFormula(index = self.curveModel.index(self.parent().selected_index.row(), 0), formula = formula)
passed = self.curveModel.set_data(column_name="Channel", curve=self.curveModel._plot._curves[self.parent().selected_index.row()], value=formula)
if passed:
self.field.setText("")
self.accept()
6 changes: 6 additions & 0 deletions trace/table_models/axis_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ def removeAtIndex(self, index: QModelIndex) -> None:
self.remove_curve.emit(curve)
super().removeAtIndex(index)

def removeAxis(self, axisName: str):
"""Wrapper to remove axis by name. Searches through axes, finds the one with a matching name, removes at index"""
for i, axis in enumerate(self.plot._axes):
if axis.name == axisName:
self.removeAtIndex(self.index(i, 0))

def setHidden(self, axis: BasePlotAxisItem, hidden: bool) -> None:
"""Hides the axis at the given table index.
Expand Down
23 changes: 20 additions & 3 deletions trace/table_models/curve_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __contains__(self, key: str) -> bool:
If the channel already exists in the model
"""
for curve in self._plot._curves:
if hasattr(curve, "channel"):
if isinstance(curve, ArchivePlotCurveItem):
if curve.address == key:
return True
elif curve.formula == key:
Expand Down Expand Up @@ -111,7 +111,6 @@ def set_data(self, column_name: str, curve: BasePlotCurveItem, value: Any) -> bo
return False
curve.show()
# If we are changing the channel, then we need to check the current type, and the type we're going to
index = self.index(self._plot._curves.index(curve),0)
value_is_formula = value.startswith("f://")
curve_is_formula = isinstance(curve, FormulaCurveItem)
if value_is_formula and not curve_is_formula:
Expand Down Expand Up @@ -145,6 +144,7 @@ def set_data(self, column_name: str, curve: BasePlotCurveItem, value: Any) -> bo
if value and self._plot._curves[-1] is curve:
self.append()
curve.setData(name=str(value))
self.plot.plotItem.axes[curve.y_axis_name]["item"].setLabel(curve.name())
elif column_name == "Y-Axis Name":
# If we change the Y-Axis, unlink from previous and link to new
if value == curve.y_axis_name:
Expand All @@ -171,6 +171,7 @@ def set_data(self, column_name: str, curve: BasePlotCurveItem, value: Any) -> bo
else:
ret_code = super(ArchiverCurveModel, self).set_data(column_name, curve, value)
self.plot.plotItem.autoVisible(curve.y_axis_name)
self.dataChanged.emit(index, index)
logger.debug("Finished setting curve data")
return ret_code

Expand Down Expand Up @@ -202,6 +203,7 @@ def append(self, address: Optional[str] = None, name: Optional[str] = None, colo
if self.rowCount() != 1:
logger.debug("Hide blank Y-axis")
self._axis_model.plot.plotItem.axes[y_axis.name]["item"].hide()
self._plot._curves[-1].unitSignal.connect(partial(self.setAxis, curve=self._plot._curves[-1]))
logger.debug("Finished adding new empty curve to plot")

def set_model_curves(self, curves: List[Dict] = []) -> None:
Expand Down Expand Up @@ -261,6 +263,20 @@ def set_model_curves(self, curves: List[Dict] = []) -> None:
self.endResetModel()
logger.debug("Finished setting curves model")

@Slot(str)
def setAxis(self, units: str, curve: BasePlotCurveItem):
"""When we receive a unit of the curve, we will connect it to the correct axis"""
index = self.index(self._plot._curves.index(curve),0)

self.parent().update()
oldYAxis = curve.y_axis_name
if units not in self.plot.plotItem.axes:
self._axis_model.append(name=units)
self.setData(self.index(index.row(), self._column_names.index("Y-Axis Name")), units, Qt.EditRole)
if not self.plot.plotItem.axes[oldYAxis]["item"]._curves:
self._axis_model.removeAxis(oldYAxis)


def replaceToArchivePlot(self, curve: BasePlotCurveItem, index: QModelIndex, address: str, color: Optional[QColor] = None):
"""Replace the existing curve with a new ArchivePlotCurveItem"""
self.append(address=address, name=address, color=color)
Expand Down Expand Up @@ -335,6 +351,7 @@ def replaceToFormula(self, index: QModelIndex, formula: str, color: Optional[str
The curve's color on the plot.
"""
# Find row headers using regex

rowName = self._row_names[index.row()]
pvdict = self.formulaToPVDict(rowName, formula)
curve = self._plot._curves[index.row()]
Expand Down Expand Up @@ -367,7 +384,7 @@ def invalidFormula(self, header):
curve = self._plot._curves[rindex]
self.beginRemoveRows(QModelIndex(), index.row(), index.row())
if curve.y_axis_name in self._plot.plotItem.axes:
self.plot.plotItem.unlinkDataFromAxis(curve.y_axis_name)
self.plot.plotItem.unlinkDataFromAxis(curve)
self.plot.removeItem(curve)
self.plot._curves.remove(curve)
self.endRemoveRows()
Expand Down

0 comments on commit 79e6a94

Please # to comment.