Skip to content

Commit

Permalink
Merge pull request #1122 from zdomke/dev_model_test
Browse files Browse the repository at this point in the history
FIX: Check Validity of QAbstractTableModel Classes
  • Loading branch information
YektaY authored Nov 11, 2024
2 parents 08724bf + 02ed4df commit 1b3c197
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
30 changes: 30 additions & 0 deletions pydm/tests/widgets/test_curve_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
RedrawModeColumnDelegate,
PlotStyleColumnDelegate,
)
from ...widgets import PyDMArchiverTimePlot
from ...widgets.axis_table_model import BasePlotAxesModel
from ...widgets.baseplot_table_model import BasePlotCurvesModel
from ...widgets.archiver_time_plot_editor import PyDMArchiverTimePlotCurvesModel
from ...widgets.scatterplot_curve_editor import ScatterPlotCurveEditorDialog
from ...widgets.timeplot_curve_editor import TimePlotCurveEditorDialog
from ...widgets.waveformplot import WaveformCurveItem
Expand Down Expand Up @@ -120,6 +123,33 @@ def test_axis_editor(qtbot):
assert type(axis_view.itemDelegateForColumn(axis_orientation_index)) is AxisColumnDelegate


def test_axis_table_model(qtmodeltester):
"""Check the validity of the BasePlotAxesModel with pytest-qt"""
base_plot = BasePlot()
axis_model = BasePlotAxesModel(plot=base_plot)
axis_model.append("FooBar")

qtmodeltester.check(axis_model, force_py=True)


def test_curves_table_model(qtmodeltester):
"""Check the validity of the BasePlotCurvesModel with pytest-qt"""
base_plot = BasePlot()
curves_model = BasePlotCurvesModel(plot=base_plot)
curves_model.append()

qtmodeltester.check(curves_model, force_py=True)


def test_archive_table_model(qtmodeltester):
"""Check the validity of the PyDMArchiverTimePlotCurvesModel with pytest-qt"""
archiver_plot = PyDMArchiverTimePlot()
archive_model = PyDMArchiverTimePlotCurvesModel(plot=archiver_plot)
archive_model.append()

qtmodeltester.check(archive_model, force_py=True)


def test_plot_style_column_delegate(qtbot):
"""Verify the functionality of the show/hide column feature"""

Expand Down
16 changes: 10 additions & 6 deletions pydm/widgets/archiver_time_plot_editor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any, Optional
from qtpy.QtCore import Qt, QModelIndex, QObject, QVariant
from qtpy.QtCore import Qt, QModelIndex, QObject
from qtpy.QtGui import QColor
from .archiver_time_plot import ArchivePlotCurveItem, FormulaCurveItem
from .baseplot import BasePlot, BasePlotCurveItem
Expand All @@ -16,15 +16,19 @@ def __init__(self, plot: BasePlot, parent: Optional[QObject] = None):

self.checkable_cols = {self.getColumnIndex("Live Data"), self.getColumnIndex("Archive Data")}

def flags(self, index):
def flags(self, index: QModelIndex) -> Qt.ItemFlags:
"""Return flags that determine how users can interact with the items in the table"""
if not index.isValid():
return Qt.NoItemFlags

flags = super().flags(index)
if index.column() in self.checkable_cols:
flags = Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsSelectable
return flags

def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
return None
if role == Qt.CheckStateRole and index.column() in self.checkable_cols:
value = super().data(index, Qt.DisplayRole)
return Qt.Checked if value else Qt.Unchecked
Expand All @@ -37,12 +41,12 @@ def get_data(self, column_name: str, curve: BasePlotCurveItem) -> Any:
if column_name == "Channel":
if isinstance(curve, FormulaCurveItem):
if curve.formula is None:
return QVariant()
return ""
return str(curve.formula)
# We are either a Formula or a PV (for now at leasts)
else:
if curve.address is None:
return QVariant()
return ""
return str(curve.address)

elif column_name == "Live Data":
Expand All @@ -53,7 +57,7 @@ def get_data(self, column_name: str, curve: BasePlotCurveItem) -> Any:

def setData(self, index, value, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
return None
elif role == Qt.CheckStateRole and index.column() in self.checkable_cols:
return super().setData(index, value, Qt.EditRole)
elif index.column() not in self.checkable_cols:
Expand Down
13 changes: 8 additions & 5 deletions pydm/widgets/axis_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def plot(self):
def plot(self, new_plot):
self._plot = new_plot

def flags(self, index):
def flags(self, index: QModelIndex) -> Qt.ItemFlags:
"""Return flags that determine how users can interact with the items in the table"""
if not index.isValid():
return Qt.NoItemFlags
return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable

def rowCount(self, parent=None):
Expand All @@ -42,17 +45,17 @@ def columnCount(self, parent=None):

def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
return None
if index.row() >= self.rowCount():
return QVariant()
return None
if index.column() >= self.columnCount():
return QVariant()
return None
column_name = self._column_names[index.column()]
axis = self.plot._axes[index.row()]
if role == Qt.DisplayRole or role == Qt.EditRole:
return self.get_data(column_name, axis)
else:
return QVariant()
return None

def get_data(self, column_name, axis):
if column_name == "Y-Axis Name":
Expand Down
17 changes: 10 additions & 7 deletions pydm/widgets/baseplot_table_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from qtpy.QtCore import QAbstractTableModel, Qt, QVariant
from qtpy.QtCore import QAbstractTableModel, Qt, QVariant, QModelIndex
from qtpy.QtGui import QBrush
from .baseplot import BasePlotCurveItem

Expand Down Expand Up @@ -42,7 +42,10 @@ def plot(self, new_plot):
def clear(self):
self.plot.clearCurves()

def flags(self, index):
def flags(self, index: QModelIndex) -> Qt.ItemFlags:
"""Return flags that determine how users can interact with the items in the table"""
if not index.isValid():
return None
column_name = self._column_names[index.column()]
if column_name == "Color" or column_name == "Limit Color":
return Qt.ItemIsSelectable | Qt.ItemIsEnabled
Expand All @@ -58,11 +61,11 @@ def columnCount(self, parent=None):

def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
return None
if index.row() >= self.rowCount():
return QVariant()
return None
if index.column() >= self.columnCount():
return QVariant()
return None
column_name = self._column_names[index.column()]
curve = self.plot._curves[index.row()]
if role == Qt.DisplayRole or role == Qt.EditRole:
Expand All @@ -72,12 +75,12 @@ def data(self, index, role=Qt.DisplayRole):
elif role == Qt.BackgroundRole and column_name == "Limit Color":
return QBrush(curve.threshold_color)
else:
return QVariant()
return None

def get_data(self, column_name, curve):
if column_name == "Label":
if curve.name() is None:
return QVariant()
return ""
return str(curve.name())
elif column_name == "Y-Axis Name":
return curve.y_axis_name
Expand Down

0 comments on commit 1b3c197

Please sign in to comment.