Skip to content

Commit c6b6e64

Browse files
First Draft for a context Menu (#71)
* First Draft for a context Menu * Fixed segmentation fault when adding new rows in copy/paste by superfluous layoutChanged.
1 parent 3cdb29b commit c6b6e64

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

src/petab_gui/controllers/mother_controller.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ def __init__(self, view, model: PEtabModel):
105105

106106
self.setup_connections()
107107
self.setup_task_bar()
108+
self.setup_context_menu()
109+
110+
def setup_context_menu(self):
111+
"""Sets up context menus for the tables."""
112+
self.measurement_controller.setup_context_menu(self.actions)
113+
self.observable_controller.setup_context_menu(self.actions)
114+
self.parameter_controller.setup_context_menu(self.actions)
115+
self.condition_controller.setup_context_menu(self.actions)
108116

109117
def setup_task_bar(self):
110118
"""Create shortcuts for the main window."""

src/petab_gui/controllers/table_controllers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ def setup_connections(self):
9393
self.update_defaults
9494
)
9595

96+
def setup_context_menu(self, actions):
97+
"""Setup context menu for this table."""
98+
view = self.view.table_view
99+
view.setup_context_menu(actions)
100+
96101
def validate_changed_cell(self, row, column):
97102
"""Validate the changed cell and whether its linting is correct."""
98103
if not self.check_petab_lint_mode:

src/petab_gui/models/pandas_table_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ def maybe_add_rows(self, start_row, n_rows):
445445
self._data_frame.shape[0],
446446
start_row + n_rows - self._data_frame.shape[0]
447447
)
448-
self.layoutChanged.emit()
449448

450449
def determine_background_color(self, row, column):
451450
"""Determine the background color of a cell.

src/petab_gui/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ def __init__(self, parent=None, width=5, height=4, dpi=100):
506506
self.axes = fig.add_subplot(111)
507507
super(PlotWidget, self).__init__(fig)
508508

509+
509510
class SignalForwarder(QObject):
510511
"""Forward signals from one object to another."""
511512
forwarded_signal = Signal()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from PySide6.QtWidgets import QMenu
2+
3+
4+
class ContextMenuManager:
5+
"""Manage context menu actions for the tables."""
6+
def __init__(self, actions, table_view, parent=None):
7+
self.parent = parent()
8+
self.actions = actions
9+
self.table_view = table_view
10+
11+
def create_context_menu(self, position):
12+
"""Create the context menu."""
13+
menu = QMenu(self.parent)
14+
# Copy, Paste
15+
menu.addAction(self.actions["copy"])
16+
menu.addAction(self.actions["paste"])
17+
menu.addSeparator()
18+
menu.addAction(self.actions["add_row"])
19+
menu.addAction(self.actions["delete_row"])
20+
menu.addAction(self.actions["add_column"])
21+
menu.addAction(self.actions["delete_column"])
22+
menu.addSeparator()
23+
24+
# execute the menu
25+
menu.exec_(self.table_view.viewport().mapToGlobal(position))

src/petab_gui/views/table_view.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from PySide6.QtGui import QGuiApplication, QColor
66

77
from ..utils import get_selected_rectangles
8+
from .context_menu_mananger import ContextMenuManager
89
import re
910
import pandas as pd
1011

@@ -191,6 +192,16 @@ def __init__(self, parent=None):
191192
self.autofit_column
192193
)
193194

195+
def setup_context_menu(self, actions):
196+
"""Setup the context menu for the table view."""
197+
self.context_menu_manager = ContextMenuManager(
198+
actions, self, self.parent
199+
)
200+
self.setContextMenuPolicy(Qt.CustomContextMenu)
201+
self.customContextMenuRequested.connect(
202+
self.context_menu_manager.create_context_menu
203+
)
204+
194205
def setModel(self, model):
195206
"""Ensures selection model exists before connecting signals"""
196207
super().setModel(model)

0 commit comments

Comments
 (0)