Skip to content

Commit

Permalink
REF/ENH: move common model methods to a common view submodule, start …
Browse files Browse the repository at this point in the history
…work on LivePVTableModel and NestableTableModel
  • Loading branch information
tangkong committed Aug 1, 2024
1 parent dc7ded7 commit 845d4a6
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 88 deletions.
2 changes: 1 addition & 1 deletion superscore/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from superscore.model import Collection, Root
from superscore.widgets.core import DataWidget
from superscore.widgets.tree import RootTree
from superscore.widgets.views import RootTree


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion superscore/widgets/page/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from superscore.model import Collection
from superscore.widgets.core import DataWidget, Display, NameDescTagsWidget
from superscore.widgets.manip_helpers import insert_widget
from superscore.widgets.tree import RootTree
from superscore.widgets.views import RootTree


class CollectionPage(Display, DataWidget):
Expand Down
73 changes: 2 additions & 71 deletions superscore/widgets/page/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from superscore.model import Collection, Entry, Readback, Setpoint, Snapshot
from superscore.widgets import ICON_MAP
from superscore.widgets.core import Display
from superscore.widgets.views import BaseTableEntryModel, ButtonDelegate

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -150,7 +151,7 @@ def subfilter_results(self) -> None:
self.proxy_model.invalidateFilter()


class ResultModel(QtCore.QAbstractTableModel):
class ResultModel(BaseTableEntryModel):
headers: List[str] = ['Name', 'Type', 'Description', 'Created', 'Open']

def __init__(self, *args, entries: List[Entry] = None, **kwargs) -> None:
Expand Down Expand Up @@ -198,76 +199,6 @@ def data(self, index: QtCore.QModelIndex, role: int) -> Any:
# if nothing is found, return invalid QVariant
return QtCore.QVariant()

def rowCount(self, index):
return len(self.entries)

def columnCount(self, index):
return len(self.headers)

def headerData(
self,
section: int,
orientation: QtCore.Qt.Orientation,
role: int
) -> Any:
"""
Returns the header data for the model.
Currently only displays horizontal header data
"""
if role != QtCore.Qt.DisplayRole:
return

if orientation == QtCore.Qt.Horizontal:
return self.headers[section]

def flags(self, index: QtCore.QModelIndex) -> QtCore.Qt.ItemFlag:
"""
Returns the item flags for the given ``index``. The returned
item flag controls what behaviors the item supports.
Parameters
----------
index : QtCore.QModelIndex
the index referring to a cell of the TableView
Returns
-------
QtCore.Qt.ItemFlag
the ItemFlag corresponding to the cell
"""
if (index.column() == len(self.headers) - 1):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled
else:
return QtCore.Qt.ItemIsEnabled


class ButtonDelegate(QtWidgets.QStyledItemDelegate):
clicked = QtCore.Signal(QtCore.QModelIndex)

def __init__(self, *args, button_text: str = '', **kwargs):
self.button_text = button_text
super().__init__(*args, **kwargs)

def createEditor(
self,
parent: QtWidgets.QWidget,
option,
index: QtCore.QModelIndex
) -> QtWidgets.QWidget:
button = QtWidgets.QPushButton(self.button_text, parent)
button.clicked.connect(
lambda _, index=index: self.clicked.emit(index)
)
return button

def updateEditorGeometry(
self,
editor: QtWidgets.QWidget,
option: QtWidgets.QStyleOptionViewItem,
index: QtCore.QModelIndex
) -> None:
return editor.setGeometry(option.rect)


class ResultFilterProxyModel(QtCore.QSortFilterProxyModel):
"""
Expand Down
Loading

0 comments on commit 845d4a6

Please sign in to comment.