-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_view.py
More file actions
352 lines (293 loc) · 12.9 KB
/
main_view.py
File metadata and controls
352 lines (293 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""Main Window View."""
import copy
from PySide6.QtCore import QEvent, QSettings, Qt, QTimer
from PySide6.QtWidgets import (
QDockWidget,
QMainWindow,
QSizePolicy,
QTabWidget,
QVBoxLayout,
QWidget,
)
from ..C import APP_NAME
from ..models.tooltips import (
COND_TABLE_TOOLTIP,
DATA_PLOT_TOOLTIP,
DATA_TABLES_TAB_TOOLTIP,
INFO_TOOLTIP,
MEAS_TABLE_TOOLTIP,
OBS_TABLE_TOOLTIP,
PAR_TABLE_TOOLTIP,
SBML_MODEL_TAB_TOOLTIP,
SIM_TABLE_TOOLTIP,
VIS_TABLE_TOOLTIP,
)
from ..settings_manager import settings_manager
from .find_replace_bar import FindReplaceBar
from .logger import Logger
from .sbml_view import SbmlViewer
from .simple_plot_view import MeasurementPlotter
from .table_view import TableViewer
from .whats_this import WHATS_THIS
class MainWindow(QMainWindow):
DATA_TAB_INDEX = 0 # Index of the Data Tables tab
def __init__(self):
super().__init__()
self.allow_close = False
self.setWindowTitle(APP_NAME)
self.setGeometry(100, 100, 1200, 800)
# Logger: used in both tabs
self.logger_views = [Logger(self), Logger(self)]
# Main layout: Two tabs
self.tab_widget = QTabWidget(self)
# Tab for the data tables
self.data_tab = QMainWindow()
tab = self.tab_widget.addTab(self.data_tab, "Data Tables")
self.tab_widget.setTabToolTip(tab, DATA_TABLES_TAB_TOOLTIP)
self.tab_widget.setTabWhatsThis(tab, WHATS_THIS["tabs"]["data_tables"])
# Tab for the SBML model
self.sbml_viewer = SbmlViewer(logger_view=self.logger_views[0])
tab = self.tab_widget.addTab(self.sbml_viewer, "SBML Model")
self.tab_widget.setTabToolTip(tab, SBML_MODEL_TAB_TOOLTIP)
self.tab_widget.setTabWhatsThis(tab, WHATS_THIS["tabs"]["sbml_model"])
# Set the QTabWidget as the central widget
self.setCentralWidget(self.tab_widget)
# Create dock widgets for each table
self.condition_dock = TableViewer("Condition Table")
self.condition_dock.setToolTip(COND_TABLE_TOOLTIP)
self.tab_widget.setTabWhatsThis(
tab, WHATS_THIS["tables"]["condition"]["table"]
)
self.measurement_dock = TableViewer("Measurement Table")
self.measurement_dock.setToolTip(MEAS_TABLE_TOOLTIP)
self.observable_dock = TableViewer("Observable Table")
self.observable_dock.setToolTip(OBS_TABLE_TOOLTIP)
self.parameter_dock = TableViewer("Parameter Table")
self.parameter_dock.setToolTip(PAR_TABLE_TOOLTIP)
self.logger_dock = QDockWidget("Info")
self.logger_dock.setToolTip(INFO_TOOLTIP)
self.logger_dock.setObjectName("logger_dock")
self.logger_dock.setWidget(self.logger_views[1])
self.plot_dock = MeasurementPlotter(self)
self.plot_dock.setToolTip(DATA_PLOT_TOOLTIP)
self.visualization_dock = TableViewer("Visualization Table")
self.visualization_dock.setToolTip(VIS_TABLE_TOOLTIP)
self.simulation_dock = TableViewer("Simulation Table")
self.simulation_dock.setToolTip(SIM_TABLE_TOOLTIP)
self.dock_visibility = {
self.condition_dock: self.condition_dock.isVisible(),
self.measurement_dock: self.measurement_dock.isVisible(),
self.observable_dock: self.observable_dock.isVisible(),
self.parameter_dock: self.parameter_dock.isVisible(),
self.logger_dock: self.logger_dock.isVisible(),
self.plot_dock: self.plot_dock.isVisible(),
self.visualization_dock: self.visualization_dock.isVisible(),
self.simulation_dock: self.simulation_dock.isVisible(),
}
self.default_view()
self.condition_dock.visibilityChanged.connect(
self.save_dock_visibility
)
self.measurement_dock.visibilityChanged.connect(
self.save_dock_visibility
)
self.observable_dock.visibilityChanged.connect(
self.save_dock_visibility
)
self.parameter_dock.visibilityChanged.connect(
self.save_dock_visibility
)
self.logger_dock.visibilityChanged.connect(self.save_dock_visibility)
self.plot_dock.visibilityChanged.connect(self.save_dock_visibility)
self.visualization_dock.visibilityChanged.connect(
self.save_dock_visibility
)
self.simulation_dock.visibilityChanged.connect(
self.save_dock_visibility
)
# Allow docking in multiple areas
self.data_tab.setDockOptions(QMainWindow.AllowNestedDocks)
self.tab_widget.currentChanged.connect(self.set_docks_visible)
settings_manager.load_ui_settings(self)
# drag drop
self.setAcceptDrops(True)
self.find_replace_bar = None
# Track if we're in a minimize/restore cycle
self._was_minimized = False
def default_view(self):
"""Reset the view to a fixed 3x2 grid using manual geometry."""
if hasattr(self, "dock_visibility"):
for dock in self.dock_visibility:
dock.setParent(None) # fully remove from layout
self.tab_widget.setCurrentIndex(self.DATA_TAB_INDEX)
self.data_tab.updateGeometry()
self.data_tab.repaint()
# Get available geometry
available_rect = self.data_tab.contentsRect()
width = available_rect.width() // 2
height = available_rect.height() // 4
x_left = available_rect.left()
x_right = x_left + width
y_positions = [available_rect.top() + i * height for i in range(4)]
# Define dock + positions
layout = [
(self.measurement_dock, x_left, y_positions[0]),
(self.parameter_dock, x_left, y_positions[1]),
(self.logger_dock, x_left, y_positions[2]),
(self.visualization_dock, x_left, y_positions[3]),
(self.observable_dock, x_right, self.measurement_dock),
(self.condition_dock, x_right, self.parameter_dock),
(self.plot_dock, x_right, self.logger_dock),
(self.simulation_dock, x_right, self.visualization_dock),
]
for dock, x, y in layout:
area = Qt.LeftDockWidgetArea
if x == x_left:
self.data_tab.addDockWidget(area, dock)
dock.setFloating(True)
dock.setGeometry(x, y, width, height)
dock.setFloating(False)
if x == x_right:
self.data_tab.splitDockWidget(y, dock, Qt.Horizontal)
if hasattr(self, "dock_visibility"):
for dock in self.dock_visibility:
dock.setVisible(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
return
event.ignore()
def dropEvent(self, event):
for url in event.mimeData().urls():
event.setDropAction(Qt.CopyAction)
event.accept()
self.controller.open_file(url.toLocalFile())
return
event.ignore()
def setup_toolbar(self, actions):
# add a toolbar with actions from self.task_bar
tb = self.addToolBar("MainToolbar")
tb.setObjectName("MainToolbar")
self.setUnifiedTitleAndToolBarOnMac(True)
# spacer for later
spacer = QWidget()
spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
# first the normal open / save operations
tb.addAction(actions["new"])
tb.addAction(actions["open"])
tb.addAction(actions["add"])
tb.addAction(actions["save"])
tb.addAction(actions["check_petab"])
tb.addAction(actions["add_row"])
tb.addAction(actions["delete_row"])
tb.addAction(actions["add_column"])
tb.addAction(actions["delete_column"])
tb.addAction(actions["simulate"])
tb.addWidget(spacer)
tb.addWidget(actions["filter_widget"])
tb.addAction(actions["whats_this"])
def add_menu_action(self, dock_widget, name):
"""Add actions to the menu to show dock widgets."""
action = self.view_menu.addAction(name)
action.setCheckable(True)
action.setChecked(True)
# Show or hide the dock widget based on the menu action
action.toggled.connect(lambda checked: dock_widget.setVisible(checked))
# Sync the menu action with the visibility of the dock widget
dock_widget.visibilityChanged.connect(action.setChecked)
def save_dock_visibility(self, visible):
"""Save the visibility status of a QDockWidget when it changes."""
# Don't save visibility when window is minimized - Qt hides docks automatically
if self.isMinimized():
return
# if current tab is not the data tab return
if self.tab_widget.currentIndex() != self.DATA_TAB_INDEX:
return
dock = self.sender() # Get the QDockWidget that emitted the signal
self.dock_visibility[dock] = dock.isVisible()
def set_docks_visible(self, index):
"""Set all QDockWidgets to their previous visibility on tab-change."""
if index != self.DATA_TAB_INDEX: # Another tab is selected
self._apply_dock_visibility()
def _apply_dock_visibility(self):
"""Apply saved visibility state to all docks."""
for dock, visible in self.dock_visibility.items():
dock.setVisible(visible)
def changeEvent(self, event):
"""Handle window state changes, including minimize/restore."""
super().changeEvent(event)
if event.type() != QEvent.Type.WindowStateChange:
return
if self.isMinimized():
self._was_minimized = True
elif (
self._was_minimized
and self.tab_widget.currentIndex() == self.DATA_TAB_INDEX
):
# Short delay to not create a segmentation fault.
QTimer.singleShot(50, self._apply_dock_visibility)
self._was_minimized = False
def closeEvent(self, event):
"""Override the closeEvent to emit additional signal."""
self.controller.maybe_close()
if self.allow_close:
settings_manager.save_ui_settings(self)
event.accept()
else:
event.ignore()
def load_settings(self):
"""Load the settings from the QSettings object."""
settings = QSettings("petab", "petab_gui")
# Load the visibility of the dock widgets
for dock, _ in self.dock_visibility.items():
dock.setVisible(
settings.value(f"docks/{dock.objectName()}", True, type=bool)
)
# Load the geometry of the main window
self.restoreGeometry(settings.value("main_window/geometry"))
# Restore the positions of the dock widgets
self.restoreState(settings.value("main_window/state"))
# restore the settings of the data tab
self.data_tab.restoreGeometry(settings.value("data_tab/geometry"))
self.data_tab.restoreState(settings.value("data_tab/state"))
def save_settings(self):
"""Save the settings to the QSettings object."""
settings = QSettings("petab", "petab_gui")
# Save the visibility of the dock widgets
for dock, _ in self.dock_visibility.items():
settings.setValue(f"docks/{dock.objectName()}", dock.isVisible())
# Save the geometry of the main window
settings.setValue("main_window/geometry", self.saveGeometry())
# Save the positions of the dock widgets
settings.setValue("main_window/state", self.saveState())
# save the settings of the data tab
settings.setValue("data_tab/geometry", self.data_tab.saveGeometry())
settings.setValue("data_tab/state", self.data_tab.saveState())
def create_find_replace_bar(self):
"""Create the find/replace bar.
Add it without replacing the tab widget.
"""
self.find_replace_bar = FindReplaceBar(self.controller, self)
# manually create a copy of the dock visibility
dock_visibility_values = copy.deepcopy(
list(self.dock_visibility.values())
)
# Create a layout to insert Find/Replace above the tabs
container = QWidget()
layout = QVBoxLayout(container)
layout.setContentsMargins(0, 0, 0, 0) # Remove extra spacing
layout.addWidget(self.find_replace_bar)
layout.addWidget(self.tab_widget) # Keep tab_widget in the layout
self.setCentralWidget(container)
# Restore the visibility of the docks
for dock, visible in zip(
self.dock_visibility.keys(), dock_visibility_values, strict=False
):
self.dock_visibility[dock] = visible
dock.setVisible(visible)
def toggle_find(self):
"""Toggles the find-part of the Find.Replace Bar."""
self.find_replace_bar.toggle_find()
def toggle_replace(self):
"""Toggles the replace-part of the Find.Replace Bar."""
self.find_replace_bar.toggle_replace()