Skip to content

Commit 9fffa1a

Browse files
committed
FEAT: Create a separate class for model toolbox.
1 parent 55f9e99 commit 9fffa1a

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

faslr/common/model.py

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import typing
4848
from faslr.model import FIBNRModel
4949
from pandas import DataFrame
50+
from PyQt6.QtCore import QAbstractItemModel
5051
from typing import Any
5152

5253
class FSelectionModel(FAbstractTableModel):
@@ -270,8 +271,6 @@ def select_average_row(self, index: QModelIndex) -> None:
270271
"""
271272
Makes the ratio selection, i.e., selects the LDFs or loss ratios.
272273
"""
273-
for row in self.included_averages_rows:
274-
print(row)
275274
# If double-clicked row is not in the included averages, do nothing.
276275
if index.row() not in self.included_averages_rows:
277276
return None
@@ -399,62 +398,77 @@ def __init__(
399398
self.setWindowTitle(window_title)
400399

401400
self.layout = QVBoxLayout()
402-
self.add_average_button = QPushButton("Available Averages")
403-
self.add_average_button.setFixedWidth(self.add_average_button.sizeHint().width())
404-
405-
self.add_average_button.setContentsMargins(
406-
2,
407-
2,
408-
2,
409-
2
410-
)
411401

412402
# Container widget for upper-right hand tools (add average button, etc.).
413-
self.tool_container = QWidget()
414-
self.tool_layout = QHBoxLayout()
415-
self.tool_container.setLayout(self.tool_layout)
416-
417-
self.tool_layout.setContentsMargins(
418-
0,
419-
0,
420-
0,
421-
0
422-
)
423-
424-
self.tool_layout.addWidget(
425-
self.add_average_button
403+
self.toolbox = FSelectionModelToolbox(
404+
parent=self,
405+
averages=averages
426406
)
427407

428408
self.layout.addWidget(
429-
self.tool_container,
409+
self.toolbox,
430410
alignment=Qt.AlignmentFlag.AlignRight
431411
)
432412

413+
# If selection model and view has already been subclass, skip, otherwise use base classes.
433414
if not (hasattr(self, 'selection_model') and hasattr(self, 'selection_model_view')):
434415
self.selection_model = FSelectionModel(data=data, averages=averages, parent=self)
435416
self.selection_model_view = FModelView(parent=self)
436417

437-
self.average_box = FAverageBox(data=averages, parent=self)
438-
439418
self.selection_model_view.setModel(self.selection_model)
440419

441420
self.layout.addWidget(self.selection_model_view)
442421

443422
self.setLayout(self.layout)
444423

445-
self.add_average_button.clicked.connect(self.open_average_box) # noqa
446424

447-
def open_average_box(self):
425+
class FSelectionModelToolbox(QWidget):
426+
"""
427+
Toolbox that appears above the model view in a FSelectionModelWidget. Contains utilities such
428+
as a button for adding averages, and a heatmap button.
429+
"""
430+
def __init__(
431+
self,
432+
parent: FSelectionModelWidget,
433+
averages: DataFrame
434+
):
435+
super().__init__()
436+
self.parent: FSelectionModelWidget = parent
437+
self.add_average_button = QPushButton("Available Averages")
438+
self.add_average_button.setFixedWidth(self.add_average_button.sizeHint().width())
439+
440+
self.add_average_button.setContentsMargins(
441+
2,
442+
2,
443+
2,
444+
2
445+
)
446+
447+
self.layout = QHBoxLayout()
448+
self.setLayout(self.layout)
448449

449-
self.average_box.show()
450+
self.setContentsMargins(
451+
0,
452+
0,
453+
0,
454+
0
455+
)
450456

457+
self.average_box = FAverageBox(
458+
parent=self,
459+
data=averages,
460+
selection_model=self.parent.selection_model
461+
)
451462

452-
class FSelectionModelToolBox(QWidget):
453-
def __init__(self):
454-
super().__init__()
463+
self.layout.addWidget(self.add_average_button)
455464

456-
self.layout = QHBoxLayout()
465+
self.add_average_button.clicked.connect(self.open_average_box) # noqa
457466

467+
def open_average_box(self) -> None:
468+
"""
469+
Opens the dialog box for adding averages to a selection model.
470+
"""
471+
self.average_box.show()
458472

459473
class FModelView(FTableView):
460474
def __init__(
@@ -470,9 +484,8 @@ def __init__(
470484
def keyPressEvent(self, e: QKeyEvent) -> None:
471485
clipboard = QApplication.clipboard()
472486
if e.matches(QKeySequence.StandardKey.Paste):
473-
print('asdfasdf')
474487
value = clipboard.text()
475-
model: FSelectionModel = self.model()
488+
model: QAbstractItemModel = self.model()
476489
for index in self.selectedIndexes():
477490
model.setData(index=index, value=value, role=Qt.ItemDataRole.EditRole)
478491

faslr/model/average.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333

3434
if TYPE_CHECKING:
3535
from pandas import DataFrame
36-
from faslr.common.model import FSelectionModelWidget
36+
from faslr.common.model import FSelectionModel
3737

3838
class FAverageBox(QDialog):
3939
def __init__(
4040
self,
41-
parent: FSelectionModelWidget = None,
41+
parent,
42+
selection_model: FSelectionModel = None,
4243
title: str = None,
4344
data: DataFrame = None
4445
):
@@ -51,6 +52,8 @@ def __init__(
5152

5253
self.parent = parent
5354

55+
self.selection_model = selection_model
56+
5457
self.model = FAverageModel(
5558
parent=None,
5659
data=data,
@@ -85,7 +88,7 @@ def __init__(
8588
self.setLayout(self.layout)
8689
self.set_dimensions()
8790

88-
self.button_box.accepted.connect(self.accept_changes)
91+
self.button_box.accepted.connect(self.accept_changes) # noqa
8992
self.button_box.clicked.connect(self.handle_button_box) # noqa
9093

9194
def set_dimensions(self):
@@ -120,7 +123,7 @@ def accept_changes(self) -> None:
120123
return
121124

122125
index = QModelIndex()
123-
self.parent.selection_model.setData(index=index, value=None)
126+
self.selection_model.setData(index=index, value=None)
124127

125128
self.close()
126129

0 commit comments

Comments
 (0)