Skip to content

Commit

Permalink
custom models now work
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhroom committed Jan 20, 2025
1 parent 04245ae commit addbea6
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions rascal2/widgets/project/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import RATapi
from PyQt6 import QtCore, QtGui, QtWidgets
from RATapi.utils.enums import BackgroundActions
from RATapi.utils.enums import BackgroundActions, LayerModels

from rascal2.config import path_for
from rascal2.widgets.delegates import ProjectFieldDelegate
Expand Down Expand Up @@ -95,6 +95,8 @@ def delete_item(self, row: int):
class AbstractProjectListWidget(QtWidgets.QWidget):
"""An abstract base widget for editing items kept in a list."""

item_type = "item"

def __init__(self, field: str, parent):
super().__init__(parent)
self.field = field
Expand Down Expand Up @@ -158,7 +160,7 @@ def update_item_view(self):
# if there are no items, replace the widget with information
if self.model.rowCount() == 0:
self.view_stack = QtWidgets.QLabel(
"No contrasts are currently defined! Edit the project to add a contrast."
f"No {self.item_type}s are currently defined! Edit the project to add a {self.item_type}."
)
self.view_stack.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)

Expand Down Expand Up @@ -251,38 +253,40 @@ def supportedDropActions(self):
return QtCore.Qt.DropAction.MoveAction


class ContrastModelWidget(QtWidgets.QWidget):
"""Widget for contrast models."""
class StandardLayerModelWidget(QtWidgets.QWidget):
"""Widget for standard layer contrast models."""

def __init__(self, init_list: list[str] = None, parent=None):
def __init__(self, init_list: list[str], parent=None):
super().__init__(parent)

self.model = LayerStringListModel(init_list, self)
self.list = QtWidgets.QListView(parent)
self.list.setModel(self.model)
self.list.setItemDelegateForColumn(0, ProjectFieldDelegate(parent.project_widget, "layers", self))
self.list.setDragEnabled(True)
self.list.setAcceptDrops(True)
self.list.setDropIndicatorShown(True)
self.list.setDragDropOverwriteMode(False)
model = LayerStringListModel(init_list, self)
standard_layer_list = QtWidgets.QListView(parent)
standard_layer_list.setModel(model)
standard_layer_list.setItemDelegateForColumn(0, ProjectFieldDelegate(parent.project_widget, "layers", self))
standard_layer_list.setDragEnabled(True)
standard_layer_list.setAcceptDrops(True)
standard_layer_list.setDropIndicatorShown(True)
standard_layer_list.setDragDropOverwriteMode(False)

layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.list)
layout.addWidget(standard_layer_list)

self.setLayout(layout)


class ContrastWidget(AbstractProjectListWidget):
"""Widget for viewing and editing Contrasts."""

item_type = "contrast"

def compose_widget(self, i: int, data_widget: Callable[[str], QtWidgets.QWidget]) -> QtWidgets.QWidget:
"""Create the base grid layouts for the widget.
Parameters
----------
i : int
The row of the contrasts list to display in this widget.
data_widget_creator : Callable[[str], QtWidgets.QWidget]
data_widget : Callable[[str], QtWidgets.QWidget]
A function which takes a field name and returns the data widget for that field.
Returns
Expand Down Expand Up @@ -322,7 +326,7 @@ def compose_widget(self, i: int, data_widget: Callable[[str], QtWidgets.QWidget]
model_grid = QtWidgets.QGridLayout()
model_grid.addWidget(QtWidgets.QLabel("Bulk in:"), 0, 0)
model_grid.addWidget(data_widget("bulk_in"), 0, 1)
model_grid.addWidget(QtWidgets.QLabel("Layers:"), 1, 0)
model_grid.addWidget(QtWidgets.QLabel("Model:"), 1, 0)
model_grid.addWidget(data_widget("model"), 1, 1)
model_grid.addWidget(QtWidgets.QLabel("Bulk out:"), 2, 0)
model_grid.addWidget(data_widget("bulk_out"), 2, 1)
Expand All @@ -332,7 +336,6 @@ def compose_widget(self, i: int, data_widget: Callable[[str], QtWidgets.QWidget]
layout.addLayout(settings_row)
layout.addStretch()
layout.addLayout(model_grid)
layout.setContentsMargins(0, 0, 0, 0)

widget = QtWidgets.QWidget(self)
widget.setLayout(layout)
Expand All @@ -344,8 +347,12 @@ def data_box(field: str) -> QtWidgets.QWidget:
"""Create a read only line edit box for display."""
current_data = getattr(self.model.get_item(i), field)
if field == "model":
widget = QtWidgets.QListWidget(parent=self)
widget.addItems(current_data)
if self.project_widget.parent_model.project.model == LayerModels.StandardLayers:
widget = QtWidgets.QListWidget(parent=self)
widget.addItems(current_data)
else:
widget = QtWidgets.QLineEdit(current_data[0])
widget.setReadOnly(True)
else:
widget = QtWidgets.QLineEdit(current_data)
widget.setReadOnly(True)
Expand Down Expand Up @@ -374,8 +381,18 @@ def data_combobox(field: str) -> QtWidgets.QWidget:
)
return widget
case "model":
widget = ContrastModelWidget(current_data, self)
return widget
if self.project_widget.draft_project["model"] == LayerModels.StandardLayers:
widget = StandardLayerModelWidget(current_data, self)
widget.dataChanged.connect(lambda: self.model.set_data(i, field, widget.stringList()))
return widget
else:
widget = QtWidgets.QComboBox(self)
widget.addItem("", [])
for file in self.project_widget.draft_project["custom_files"]:
widget.addItem(file.name, [file.name])
widget.setCurrentText(current_data[0])
widget.currentTextChanged.connect(lambda: self.model.set_data(i, field, widget.currentData()))
return widget
# all other cases are comboboxes with data from other widget tables
case "data" | "bulk_in" | "bulk_out":
project_field_name = field
Expand Down

0 comments on commit addbea6

Please sign in to comment.