/
container.py
439 lines (374 loc) · 15.2 KB
/
container.py
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""
Layout container.
"""
# Standard library imports
from collections import OrderedDict
import sys
# Third party imports
from qtpy.QtCore import Qt, Slot
from qtpy.QtWidgets import QMessageBox
# Local imports
from spyder.api.exceptions import SpyderAPIError
from spyder.api.translations import _
from spyder.api.widgets.main_container import PluginMainContainer
from spyder.plugins.layout.api import BaseGridLayoutType
from spyder.plugins.layout.layouts import DefaultLayouts
from spyder.plugins.layout.widgets.dialog import (
LayoutSaveDialog, LayoutSettingsDialog)
class LayoutContainerActions:
DefaultLayout = 'default_layout_action'
MatlabLayout = 'matlab_layout_action'
RStudio = 'rstudio_layout_action'
HorizontalSplit = 'horizontal_split_layout_action'
VerticalSplit = 'vertical_split_layout_action'
SaveLayoutAction = 'save_layout_action'
ShowLayoutPreferencesAction = 'show_layout_preferences_action'
ResetLayout = 'reset_layout_action'
# Needs to have 'Maximize pane' as name to properly register
# the action shortcut
MaximizeCurrentDockwidget = 'Maximize pane'
# Needs to have 'Fullscreen mode' as name to properly register
# the action shortcut
Fullscreen = 'Fullscreen mode'
# Needs to have 'Use next layout' as name to properly register
# the action shortcut
NextLayout = 'Use next layout'
# Needs to have 'Use previous layout' as name to properly register
# the action shortcut
PreviousLayout = 'Use previous layout'
# Needs to have 'Close pane' as name to properly register
# the action shortcut
CloseCurrentDockwidget = 'Close pane'
# Needs to have 'Lock unlock panes' as name to properly register
# the action shortcut
LockDockwidgetsAndToolbars = 'Lock unlock panes'
class LayoutPluginMenus:
PluginsMenu = "plugins_menu"
LayoutsMenu = 'layouts_menu'
class LayoutContainer(PluginMainContainer):
"""
Plugin container class that handles the Spyder quick layouts functionality.
"""
def setup(self):
# Basic attributes to handle layouts options and dialogs references
self._spyder_layouts = OrderedDict()
self._save_dialog = None
self._settings_dialog = None
self._layouts_menu = None
self._current_quick_layout = None
# Close current dockable plugin
self._close_dockwidget_action = self.create_action(
LayoutContainerActions.CloseCurrentDockwidget,
text=_('Close current pane'),
icon=self.create_icon('close_pane'),
triggered=self._plugin.close_current_dockwidget,
context=Qt.ApplicationShortcut,
register_shortcut=True,
shortcut_context='_'
)
# Maximize current dockable plugin
self._maximize_dockwidget_action = self.create_action(
LayoutContainerActions.MaximizeCurrentDockwidget,
text=_('Maximize current pane'),
icon=self.create_icon('maximize'),
toggled=lambda state: self._plugin.maximize_dockwidget(),
context=Qt.ApplicationShortcut,
register_shortcut=True,
shortcut_context='_')
# Fullscreen mode
self._fullscreen_action = self.create_action(
LayoutContainerActions.Fullscreen,
text=_('Fullscreen mode'),
triggered=self._plugin.toggle_fullscreen,
context=Qt.ApplicationShortcut,
register_shortcut=True,
shortcut_context='_')
if sys.platform == 'darwin':
self._fullscreen_action.setEnabled(False)
self._fullscreen_action.setToolTip(_("For fullscreen mode use the "
"macOS built-in feature"))
# Lock dockwidgets and toolbars
self._lock_interface_action = self.create_action(
LayoutContainerActions.LockDockwidgetsAndToolbars,
text='',
triggered=lambda checked:
self._plugin.toggle_lock(),
context=Qt.ApplicationShortcut,
register_shortcut=True,
shortcut_context='_'
)
self._save_layout_action = self.create_action(
LayoutContainerActions.SaveLayoutAction,
_("Save current layout"),
triggered=self.show_save_layout,
context=Qt.ApplicationShortcut,
register_shortcut=False,
)
self._show_preferences_action = self.create_action(
LayoutContainerActions.ShowLayoutPreferencesAction,
text=_("Layout preferences"),
triggered=self.show_layout_settings,
context=Qt.ApplicationShortcut,
register_shortcut=False,
)
self._reset_action = self.create_action(
LayoutContainerActions.ResetLayout,
text=_('Reset to Spyder default'),
triggered=self.reset_window_layout,
register_shortcut=False,
)
# Layouts shortcuts actions
self._toggle_next_layout_action = self.create_action(
LayoutContainerActions.NextLayout,
_("Use next layout"),
triggered=self.toggle_next_layout,
context=Qt.ApplicationShortcut,
register_shortcut=True,
shortcut_context='_')
self._toggle_previous_layout_action = self.create_action(
LayoutContainerActions.PreviousLayout,
_("Use previous layout"),
triggered=self.toggle_previous_layout,
context=Qt.ApplicationShortcut,
register_shortcut=True,
shortcut_context='_')
# Layouts menu
self._layouts_menu = self.create_menu(
LayoutPluginMenus.LayoutsMenu, _("Window layouts"))
self._plugins_menu = self.create_menu(
LayoutPluginMenus.PluginsMenu, _("Panes"))
self._plugins_menu.setObjectName('checkbox-padding')
def update_actions(self):
pass
def update_layout_menu_actions(self):
"""
Update layouts menu and layouts related actions.
"""
menu = self._layouts_menu
menu.clear_actions()
names = self.get_conf('names')
ui_names = self.get_conf('ui_names')
order = self.get_conf('order')
active = self.get_conf('active')
actions = []
for name in order:
if name in active:
if name in self._spyder_layouts:
index = name
name = self._spyder_layouts[index].get_name()
else:
index = names.index(name)
name = ui_names[index]
# closure required so lambda works with the default parameter
def trigger(i=index, self=self):
return lambda: self.quick_layout_switch(i)
layout_switch_action = self.create_action(
name,
text=name,
triggered=trigger(),
register_shortcut=False,
overwrite=True
)
actions.append(layout_switch_action)
for item in actions:
self.add_item_to_menu(item, menu, section="layouts_section")
for item in [self._save_layout_action, self._show_preferences_action,
self._reset_action]:
self.add_item_to_menu(item, menu, section="layouts_section_2")
self._show_preferences_action.setEnabled(len(order) != 0)
# --- Public API
# ------------------------------------------------------------------------
def critical_message(self, title, message):
"""Expose a QMessageBox.critical dialog to be used from the plugin."""
QMessageBox.critical(self, title, message)
def register_layout(self, parent_plugin, layout_type):
"""
Register a new layout type.
Parameters
----------
parent_plugin: spyder.api.plugins.SpyderPluginV2
Plugin registering the layout type.
layout_type: spyder.plugins.layout.api.BaseGridLayoutType
Layout to register.
"""
if not issubclass(layout_type, BaseGridLayoutType):
raise SpyderAPIError(
"A layout must be a subclass is `BaseGridLayoutType`!")
layout_id = layout_type.ID
if layout_id in self._spyder_layouts:
raise SpyderAPIError(
"Layout with id `{}` already registered!".format(layout_id))
layout = layout_type(parent_plugin)
layout._check_layout_validity()
self._spyder_layouts[layout_id] = layout
names = self.get_conf('names')
ui_names = self.get_conf('ui_names')
order = self.get_conf('order')
active = self.get_conf('active')
if layout_id not in names:
names.append(layout_id)
ui_names.append(layout.get_name())
order.append(layout_id)
active.append(layout_id)
self.set_conf('names', names)
self.set_conf('ui_names', ui_names)
self.set_conf('order', order)
self.set_conf('active', active)
def get_layout(self, layout_id):
"""
Get a registered layout by its ID.
Parameters
----------
layout_id : string
The ID of the layout.
Raises
------
SpyderAPIError
If the given id is not found in the registered layouts.
Returns
-------
Instance of a spyder.plugins.layout.api.BaseGridLayoutType subclass
Layout.
"""
if layout_id not in self._spyder_layouts:
raise SpyderAPIError(
"Layout with id `{}` is not registered!".format(layout_id))
return self._spyder_layouts[layout_id]
@Slot()
def show_save_layout(self):
"""Show the save layout dialog."""
names = self.get_conf('names')
ui_names = self.get_conf('ui_names')
order = self.get_conf('order')
active = self.get_conf('active')
dialog_names = [name for name in names
if name not in self._spyder_layouts.keys()]
dlg = self._save_dialog = LayoutSaveDialog(self, dialog_names)
if dlg.exec_():
name = dlg.combo_box.currentText()
if name in self._spyder_layouts:
QMessageBox.critical(
self,
_("Error"),
_("Layout <b>{0}</b> was defined programatically. "
"It is not possible to overwrite programatically "
"registered layouts.").format(name)
)
return
if name in names:
answer = QMessageBox.warning(
self,
_("Warning"),
_("Layout <b>{0}</b> will be overwritten. "
"Do you want to continue?").format(name),
QMessageBox.Yes | QMessageBox.No,
)
index = order.index(name)
else:
answer = True
if None in names:
index = names.index(None)
names[index] = name
else:
index = len(names)
names.append(name)
order.append(name)
# Always make active a new layout even if it overwrites an
# inactive layout
if name not in active:
active.append(name)
if name not in ui_names:
ui_names.append(name)
if answer:
self._plugin.save_current_window_settings(
'layout_{}/'.format(index), section='quick_layouts')
self.set_conf('names', names)
self.set_conf('ui_names', ui_names)
self.set_conf('order', order)
self.set_conf('active', active)
self.update_layout_menu_actions()
@Slot()
def show_layout_settings(self):
"""Layout settings dialog."""
names = self.get_conf('names')
ui_names = self.get_conf('ui_names')
order = self.get_conf('order')
active = self.get_conf('active')
read_only = list(self._spyder_layouts.keys())
dlg = self._settings_dialog = LayoutSettingsDialog(
self, names, ui_names, order, active, read_only)
if dlg.exec_():
self.set_conf('names', dlg.names)
self.set_conf('ui_names', dlg.ui_names)
self.set_conf('order', dlg.order)
self.set_conf('active', dlg.active)
self.update_layout_menu_actions()
@Slot()
def reset_window_layout(self):
"""Reset window layout to default."""
answer = QMessageBox.warning(
self,
_("Warning"),
_("Window layout will be reset to default settings: "
"this affects window position, size and dockwidgets.\n"
"Do you want to continue?"),
QMessageBox.Yes | QMessageBox.No,
)
if answer == QMessageBox.Yes:
self._plugin.setup_layout(default=True)
@Slot()
def toggle_previous_layout(self):
"""Use the previous layout from the layouts list (default + custom)."""
self.toggle_layout('previous')
@Slot()
def toggle_next_layout(self):
"""Use the next layout from the layouts list (default + custom)."""
self.toggle_layout('next')
def toggle_layout(self, direction='next'):
"""Change current layout."""
names = self.get_conf('names')
order = self.get_conf('order')
active = self.get_conf('active')
if len(active) == 0:
return
layout_index = []
for name in order:
if name in active:
layout_index.append(names.index(name))
current_layout = self._current_quick_layout
dic = {'next': 1, 'previous': -1}
if current_layout is None:
# Start from default
current_layout = names.index(DefaultLayouts.SpyderLayout)
if current_layout in layout_index:
current_index = layout_index.index(current_layout)
else:
current_index = 0
new_index = (current_index + dic[direction]) % len(layout_index)
index_or_layout_id = layout_index[new_index]
is_layout_id = (
names[index_or_layout_id] in self._spyder_layouts)
if is_layout_id:
index_or_layout_id = names[layout_index[new_index]]
self.quick_layout_switch(index_or_layout_id)
def quick_layout_switch(self, index_or_layout_id):
"""
Switch to quick layout number *index* or *layout id*.
Parameters
----------
index: int or str
"""
possible_current_layout = self._plugin.quick_layout_switch(
index_or_layout_id)
if possible_current_layout is not None:
if isinstance(possible_current_layout, int):
self._current_quick_layout = possible_current_layout
else:
names = self.get_conf('names')
self._current_quick_layout = names.index(
possible_current_layout)