-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyIVLS_GUI.py
More file actions
executable file
·234 lines (190 loc) · 9.47 KB
/
Copy pathpyIVLS_GUI.py
File metadata and controls
executable file
·234 lines (190 loc) · 9.47 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
import logging
import re
from os.path import dirname, sep
from PyQt6 import QtWidgets
from PyQt6.QtCore import QObject, pyqtSignal, pyqtSlot
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QFileDialog
from components.pyIVLS_mainWindow import pyIVLS_mainWindow
# move this to mainwindow?
from components.pyIVLS_mdiWindow import pyIVLS_mdiWindow
from pyIVLS_pluginloader import pyIVLS_pluginloader
from pyIVLS_seqBuilder import pyIVLS_seqBuilder
logger = logging.getLogger(__name__)
class pyIVLS_GUI(QObject):
def __init__(self):
super().__init__()
self.path = dirname(__file__) + sep
self._blocking_plugins = set() # Track plugins that block closure for user visibility
self.window = pyIVLS_mainWindow(self.path)
self.pluginloader = pyIVLS_pluginloader(self.path)
self.seqBuilder = pyIVLS_seqBuilder(self.path)
icon_path = self.path + "components" + sep + "icon.png"
self.window.setWindowIcon(QIcon(icon_path))
self.setSeqBuilder()
self.window.actionPlugins.triggered.connect(self.actionPlugins)
self.window.actionSequence_builder.triggered.connect(self.actionSequence_builder)
self.window.menuShow.aboutToShow.connect(self.action_MDIShow_to_open)
self.window.actionDockWidget.triggered.connect(self.actionDockWidget)
self.window.actionRead_config_file.triggered.connect(self.action_read_config_file)
self.window.actionExport_config_file.triggered.connect(self.action_export_config_file)
self.window.seqBuilder_dockWidget.closeSignal.connect(self.seqBuilderReactClose)
self.window.dockWidget.closeSignal.connect(self.dockWidgetReactClose)
self.initial_widget_state = {}
# signal plugincontainer to read new config file
import_config_signal = pyqtSignal(str)
export_config_signal = pyqtSignal(str) # parameter: path to save to
############################### Slots
@pyqtSlot(str)
def show_message(self, str):
QtWidgets.QMessageBox.information(
self.window,
"Information",
str,
QtWidgets.QMessageBox.StandardButton.Ok,
QtWidgets.QMessageBox.StandardButton.Ok,
)
@pyqtSlot(str)
def addDataLog(self, message: str):
"""
Logs a message to both stdout and a log file, using flags in the message to determine log level.
Args:
message (str): The message to log.
"""
# Define mapping of flags to logging functions
flag_map = {
": verbose :": logger.debug,
": debug :": logger.debug,
": info :": logger.info,
": warn :": logger.warning,
": error :": logger.error,
}
# Search for a flag in the message (case-insensitive)
match = re.search(r": (verbose|debug|info|warn|error) :", message, re.IGNORECASE)
if match:
flag = match.group(0).lower()
log_func = flag_map.get(flag, logger.info)
# Remove the flag from the message for cleaner output
clean_message = re.sub(re.escape(flag), ":", message, flags=re.IGNORECASE)
log_func(clean_message)
else:
# Default to info if no flag is found
logger.info(message)
def setCloseLock(self, value, plugin_name=None):
# Track which plugins are blocking closure
if plugin_name:
if value:
self._blocking_plugins.add(plugin_name)
logger.debug(f"Plugin {plugin_name} is blocking closure")
else:
self._blocking_plugins.discard(plugin_name)
logger.debug(f"Plugin {plugin_name} no longer blocking closure")
else:
logger.debug(f"Close lock signal received without plugin name: {value}")
# reverted closelock, since plugins return True when they are not ready to close
any_blocked = len(self._blocking_plugins) > 0
self.window.setCloseOK(not any_blocked, self._blocking_plugins)
logger.debug(f"Current blocking plugins: {list(self._blocking_plugins)}, Close allowed: {not any_blocked}")
@pyqtSlot()
def seqBuilderReactClose(self):
self.window.actionSequence_builder.setChecked(False)
@pyqtSlot()
def dockWidgetReactClose(self):
self.window.actionDockWidget.setChecked(False)
@pyqtSlot()
def mdi_window_react_close(self):
# check if all mdi windows are hidden
all_hidden = True
for subwindow in self.window.mdiArea.subWindowList():
if subwindow.isVisible():
all_hidden = False
break
if all_hidden:
self.window.actionMDI_windows.setChecked(False)
################ Menu actions
def actionPlugins(self):
self.pluginloader.refresh()
self.pluginloader.window.show()
def actionSequence_builder(self):
self.window.seqBuilder_dockWidget.setVisible(self.window.actionSequence_builder.isChecked())
def actionDockWidget(self):
self.window.dockWidget.setVisible(self.window.actionDockWidget.isChecked())
def action_MDIShow_to_open(self):
self.window.mdiWindowsMenu.clear()
for subwindow in self.window.mdiArea.subWindowList():
checkbox = QtWidgets.QCheckBox(subwindow.windowTitle())
checkbox.setChecked(subwindow.isVisible())
# Connect the checkbox state to the subwindow's visibility
checkbox.stateChanged.connect(lambda state, sw=subwindow: sw.setVisible(state))
# Wrap the checkbox in a QWidgetAction
widget_action = QtWidgets.QWidgetAction(self.window)
widget_action.setDefaultWidget(checkbox)
self.window.mdiWindowsMenu.addAction(widget_action)
def action_read_config_file(self) -> None:
"""Prompts user to select a configuration file through QFileDialog. Path emitted as signal(str)"""
# https://forum.qt.io/topic/143116/qfiledialog-getopenfilename-causing-program-to-crash/14
path, _ = QFileDialog.getOpenFileName(
self.window, "Select Configuration File", self.path, "Configuration Files (*.ini)", options=QFileDialog.Option.DontUseNativeDialog | QFileDialog.Option.ReadOnly
)
if path:
self.import_config_signal.emit(path)
def action_export_config_file(self) -> None:
"""Prompts user to select a configuration file through QFileDialog. Path emitted as signal(str)"""
path, _ = QFileDialog.getSaveFileName(self.window, "Select Configuration File", self.path, "Configuration Files (*.ini)", options=QFileDialog.Option.DontUseNativeDialog)
if path:
# check file type and add if needed
if not path.lower().endswith(".ini"):
path += ".ini"
self.export_config_signal.emit(path)
############### Settings Widget
def setSettingsWidget(self, widgets: dict):
"""
Set a list of widgets in a tabbed QDockWidget.
:param widgets: dict of QtWidgets.QWidget instances to be tabbed
"""
# Create a QTabWidget to hold the widgets
tab_widget = QtWidgets.QTabWidget()
# Add each widget to the QTabWidget as a new tab
for name, widget in widgets.items():
tab_widget.addTab(widget, str(name)) # Ensure name is a string
# Set the QTabWidget as the widget for the QDockWidget
self.window.dockWidget.setWidget(tab_widget)
self.window.dockWidget.show() # Ensure the dock widget is visible
def setMDIArea(self, widgets: dict):
"""
Set a list of widgets in MDI area
:param widgets: dict of QtWidgets.QWidget instances to be added to MDI windows
"""
subwindows = self.window.mdiArea.subWindowList()
subwindow_names = [subwindow.windowTitle() for subwindow in subwindows]
default_width = 400 # Default width for MDI widgets
default_height = 300 # Default height for MDI widgets
vertical_spacing = 30 # Spacing between stacked widgets
for index, (name, widget) in enumerate(widgets.items()):
if name not in subwindow_names:
subwindow = pyIVLS_mdiWindow(self.window.mdiArea)
subwindow.setWidget(widget)
subwindow.setWindowTitle(name)
subwindow.resize(default_width, default_height) # Set default size
# Position the subwindow to stack vertically
subwindow.move(0, index * (vertical_spacing))
subwindow.closeSignal.connect(self.mdi_window_react_close)
subwindow.setVisible(True) # Set window to be visible upon loading.
else:
# Subwindow already exists, do nothing. Widget should be set and correct
pass
# Close subwindows that are not in the widgets dict
for sw in subwindows:
if sw.windowTitle() not in widgets:
self.window.mdiArea.removeSubWindow(sw) # Remove subwindow because the subwindow list is used to iterate over existing windows
sw.close() # Actually close
def setSeqBuilder(self):
self.window.seqBuilder_dockWidget.setWidget(self.seqBuilder.widget)
def clearDockWidget(self):
"""
Clear the dock widget by removing all tabs and setting its widget to None.
"""
dock_widget = self.window.dockWidget.widget()
if isinstance(dock_widget, QtWidgets.QTabWidget):
dock_widget.clear() # Clear all tabs
self.window.dockWidget.setWidget(None)