forked from SpikeInterface/spikeinterface-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_base.py
More file actions
418 lines (333 loc) · 14 KB
/
view_base.py
File metadata and controls
418 lines (333 loc) · 14 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import time
from contextlib import contextmanager
class ViewBase:
_supported_backend = []
_need_compute = False
_settings = None
_gui_help_txt = "The help for this view is not done yet"
_depend_on = None
def __init__(self, controller=None, parent=None, backend="qt"):
self.backend = backend
self.controller = controller
# this is used for panel
self._panel_view_is_visible = True
self._panel_view_is_active = False
self._panel_warning_active = False
if self.backend == "qt":
# For QT the parent is the **widget**
from .backend_qt import SignalNotifier, create_settings, listen_setting_changes
make_layout = self._qt_make_layout
self.qt_widget = parent
if self._settings is not None:
create_settings(self, parent=parent)
self.notifier = SignalNotifier(parent=self.qt_widget, view=self)
elif self.backend == "panel":
import panel as pn
from .backend_panel import SignalNotifier, create_settings, listen_setting_changes
make_layout = self._panel_make_layout
if self._settings is not None:
create_settings(self)
self.notifier = SignalNotifier(view=self)
self.busy = pn.indicators.LoadingSpinner(value=True, size=20, name='busy...')
make_layout()
if self._settings is not None:
listen_setting_changes(self)
self.controller.declare_a_view(self)
def notify_spike_selection_changed(self):
self.notifier.notify_spike_selection_changed()
def notify_unit_visibility_changed(self):
if self.controller.main_settings["color_mode"] in ("color_by_visibility", "color_only_visible"):
# in the mode color change dynamically but without notify to avoid double refresh
self.controller.refresh_colors()
self.controller.update_visible_spikes()
self.notifier.notify_unit_visibility_changed()
def notify_channel_visibility_changed(self):
self.notifier.notify_channel_visibility_changed()
def notify_manual_curation_updated(self):
self.controller.current_curation_saved = False
self.notifier.notify_manual_curation_updated()
def notify_time_info_updated(self):
self.notifier.notify_time_info_updated()
def notify_use_times_updated(self):
self.notifier.notify_use_times_updated()
def notify_active_view_updated(self):
# this is used for panel
if self.backend == "panel":
self.notifier.notify_active_view_updated()
def notify_unit_color_changed(self):
self.notifier.notify_unit_color_changed()
def on_settings_changed(self, *params):
# what to do when one settings is changed
# optionally views can implement custom method
# but the general case is to refresh
if self.backend == "qt" and hasattr(self, "_qt_on_settings_changed"):
return self._qt_on_settings_changed()
elif self.backend == "panel" and hasattr(self, "_panel_on_settings_changed"):
return self._panel_on_settings_changed()
elif hasattr(self, "_on_settings_changed"):
self._on_settings_changed()
else:
self.refresh()
def is_view_visible(self):
if self.backend == "qt":
# a widget is visible even is it is hidden under another tab!! TODO fix this
return self.qt_widget.isVisible()
elif self.backend == "panel":
return self._panel_view_is_visible
def is_view_active(self):
if self.backend == "qt":
return True
elif self.backend == "panel":
return self._panel_view_is_active
def refresh(self, **kwargs):
if self.controller.verbose:
t0 = time.perf_counter()
if not self.is_view_visible():
return
self._refresh(**kwargs)
if self.controller.verbose:
t1 = time.perf_counter()
print(f"Refresh {self.__class__.__name__} took {t1 - t0:.3f} seconds", flush=True)
def compute(self, event=None):
with self.busy_cursor():
self._compute()
self.refresh()
def _compute(self):
pass
def _refresh(self, **kwargs):
if self.backend == "qt":
self._qt_refresh(**kwargs)
elif self.backend == "panel":
self._panel_refresh(**kwargs)
def warning(self, warning_msg):
if self.backend == "qt":
self._qt_insert_warning(warning_msg)
elif self.backend == "panel":
self._panel_insert_warning(warning_msg)
def continue_from_user(self, warning_msg, action, *args):
"""Display a warning and execute risky action only if user continues.
Params
-------
warning_msg : str
The warning message to display
action : function
Function to execute if user chooses to continue
*args : tuple
Arguments to pass to action
For Qt: Shows modal dialog, executes action if Continue is clicked
For Panel: Shows dialog with callback, executes action in callback
"""
if self.backend == "qt":
# Qt: synchronous approach
if self._qt_insert_warning_with_choice(warning_msg):
action(*args)
elif self.backend == "panel":
# Panel: asynchronous approach with callback
self._panel_insert_warning_with_choice(warning_msg, action, *args)
def get_unit_color(self, unit_id):
if self.backend == "qt":
from .myqt import QT
# cache qcolors in the controller
if not hasattr(self.controller, "_cached_qcolors"):
self.controller._cached_qcolors = {}
if unit_id not in self.controller._cached_qcolors:
color = self.controller.get_unit_color(unit_id)
r, g, b, a = color
qcolor = QT.QColor(int(r * 255), int(g * 255), int(b * 255))
self.controller._cached_qcolors[unit_id] = qcolor
return self.controller._cached_qcolors[unit_id]
elif self.backend == "panel":
import matplotlib
color = self.controller.get_unit_color(unit_id)
html_color = matplotlib.colors.rgb2hex(color, keep_alpha=True)
return html_color
# Default behavior for all views : this can be changed view by view for perfs reasons
def on_spike_selection_changed(self):
if not self.is_view_visible():
return
if self.backend == "qt":
self._qt_on_spike_selection_changed()
elif self.backend == "panel":
self._panel_on_spike_selection_changed()
def on_unit_visibility_changed(self):
# print(f"on_unit_visibility_changed {self.__class__.__name__} visible{self.is_view_visible()}", flush=True)
if not self.is_view_visible():
return
if self.backend == "qt":
self._qt_on_unit_visibility_changed()
elif self.backend == "panel":
self._panel_on_unit_visibility_changed()
def on_channel_visibility_changed(self):
# print(f"on_channel_visibility_changed {self.__class__.__name__} visible{self.is_view_visible()}", flush=True)
if not self.is_view_visible():
return
if self.backend == "qt":
self._qt_on_channel_visibility_changed()
elif self.backend == "panel":
self._panel_on_channel_visibility_changed()
def on_manual_curation_updated(self):
if not self.is_view_visible():
return
if self.backend == "qt":
self._qt_on_manual_curation_updated()
elif self.backend == "panel":
self._panel_on_manual_curation_updated()
def on_time_info_updated(self):
if self.backend == "qt":
self._qt_on_time_info_updated()
elif self.backend == "panel":
self._panel_on_time_info_updated()
def on_use_times_updated(self):
if self.backend == "qt":
self._qt_on_use_times_updated()
elif self.backend == "panel":
self._panel_on_use_times_updated()
def on_unit_color_changed(self):
if self.backend == "qt":
self._qt_on_unit_color_changed()
elif self.backend == "panel":
self._panel_on_unit_color_changed()
def busy_cursor(self):
if self.backend == "qt":
return self._qt_busy_cursor()
elif self.backend == "panel":
return self._panel_busy_cursor()
else:
raise ValueError(f"Unknown backend: {self.backend}")
## Zone to be done per layout ##
## QT ##
def _qt_make_layout(self):
raise NotImplementedError
def _qt_refresh(self):
raise (NotImplementedError)
def _qt_on_spike_selection_changed(self):
pass
def _qt_on_unit_visibility_changed(self):
# most view need a refresh
self.refresh()
def _qt_on_channel_visibility_changed(self):
pass
def _qt_on_manual_curation_updated(self):
pass
def _qt_on_time_info_updated(self):
pass
def _qt_on_use_times_updated(self):
pass
def _qt_on_unit_color_changed(self):
self.refresh()
def _qt_insert_warning(self, warning_msg):
from .myqt import QT
alert = QT.QMessageBox(QT.QMessageBox.Warning, "Warning", warning_msg, parent=self.qt_widget)
alert.setStandardButtons(QT.QMessageBox.Ok)
alert.exec_()
def _qt_insert_warning_with_choice(self, warning_msg):
from .myqt import QT
alert = QT.QMessageBox(QT.QMessageBox.Warning, "Warning", warning_msg, parent=self.qt_widget)
alert.setStandardButtons(QT.QMessageBox.Cancel | QT.QMessageBox.Yes)
alert.setDefaultButton(QT.QMessageBox.Cancel)
# Set button text
alert.button(QT.QMessageBox.Yes).setText("Continue")
alert.button(QT.QMessageBox.Cancel).setText("Cancel")
result = alert.exec_()
return result == QT.QMessageBox.Yes
@contextmanager
def _qt_busy_cursor(self):
from .myqt import QT, QtWidgets
QtWidgets.QApplication.setOverrideCursor(QT.WaitCursor)
try:
yield
finally:
QtWidgets.QApplication.restoreOverrideCursor()
## PANEL ##
def _panel_make_layout(self):
raise NotImplementedError
def _panel_refresh(self):
raise (NotImplementedError)
def _panel_on_spike_selection_changed(self):
pass
def _panel_on_unit_visibility_changed(self):
# most view need a refresh
self.refresh()
def _panel_on_channel_visibility_changed(self):
pass
def _panel_on_manual_curation_updated(self):
pass
def _panel_on_time_info_updated(self):
pass
def _panel_on_use_times_updated(self):
pass
def _panel_on_unit_color_changed(self):
self.refresh()
def _panel_insert_warning(self, warning_msg):
import panel as pn
alert_markdown = pn.pane.Markdown(
f"""⚠️⚠️⚠️
{warning_msg}""",
hard_line_break=True,
styles={"color": "red", "font-size": "16px"},
)
close_button = pn.widgets.Button(name="Close", button_type="light")
close_button.on_click(self._panel_clear_warning)
row = pn.Column(alert_markdown, close_button, sizing_mode="stretch_width")
if not self._panel_warning_active:
self.layout.insert(0, row)
else:
self.layout[0] = row
self._panel_warning_active = True
def _panel_insert_warning_with_choice(self, warning_msg, continue_callback, *args):
"""Callback-based approach for Panel (recommended)"""
import panel as pn
# Store callback and args for later use
self._panel_continue_callback = continue_callback
self._panel_continue_args = args
alert_markdown = pn.pane.Markdown(
f"""⚠️⚠️⚠️
{warning_msg}
""",
hard_line_break=True,
styles={"color": "red", "font-size": "16px", "text-align": "center"},
)
continue_button = pn.widgets.Button(name="Continue", button_type="primary", width=100)
cancel_button = pn.widgets.Button(name="Cancel", button_type="light", width=100)
continue_button.on_click(self._panel_continue_choice_callback)
cancel_button.on_click(self._panel_cancel_choice_callback)
buttons_row = pn.Row(pn.Spacer(), continue_button, cancel_button, pn.Spacer(), sizing_mode="stretch_width")
warning_row = pn.Column(
alert_markdown,
buttons_row,
styles={"background": "#fff3cd", "border": "1px solid #ffeaa7", "padding": "15px", "border-radius": "5px"},
sizing_mode="stretch_width",
)
self.layout.insert(0, warning_row)
def _panel_continue_choice_callback(self, event):
"""Handler for callback-based approach"""
# Remove the warning dialog
try:
self.layout.pop(0)
except:
pass
# Execute the risky action callback with args
if hasattr(self, "_panel_continue_callback") and self._panel_continue_callback is not None:
try:
args = getattr(self, "_panel_continue_args", ())
self._panel_continue_callback(*args)
except Exception as e:
print(f"Error executing continue callback: {e}")
def _panel_cancel_choice_callback(self, event):
"""Handler for callback-based approach"""
# Remove the warning dialog
try:
self.layout.pop(0)
except:
pass
# Do nothing on cancel - just remove the dialog
def _panel_clear_warning(self, event):
self.layout.pop(0)
self._panel_warning_active = False
@contextmanager
def _panel_busy_cursor(self):
self.layout.insert(0, self.busy)
try:
yield
finally:
self.layout.pop(0)