Skip to content

Commit

Permalink
Use SpyderWidgetMixin API to build toolbars and menus
Browse files Browse the repository at this point in the history
* Consistently use add_item_to_menu() and add_item_to_toolbar() to add
  items to menus and toolbars.
* Use sections to add separators to menus and toolbars.
* Define constants for the names of actions, widgets, etc in enum-like
  classes.
* A few minor improvements to code style.
  • Loading branch information
jitseniesen committed Feb 14, 2024
1 parent 2a61b45 commit 4e2c559
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 216 deletions.
10 changes: 7 additions & 3 deletions spyder/api/widgets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ def create_stretcher(self, id_=None):
stretcher.ID = id_
return stretcher

def create_toolbar(self, name: str,
register: bool = True) -> SpyderToolbar:
def create_toolbar(
self,
name: str,
register: bool = True
) -> SpyderToolbar:
"""
Create a Spyder toolbar.
Expand All @@ -184,7 +187,8 @@ def create_toolbar(self, name: str,
toolbar.setStyleSheet(str(PANES_TOOLBAR_STYLESHEET))
if register:
TOOLBAR_REGISTRY.register_reference(
toolbar, name, self.PLUGIN_NAME, self.CONTEXT_NAME)
toolbar, name, self.PLUGIN_NAME, self.CONTEXT_NAME
)
return toolbar

def get_toolbar(self, name: str, context: Optional[str] = None,
Expand Down
52 changes: 38 additions & 14 deletions spyder/plugins/variableexplorer/widgets/arrayeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@
from spyder.py3compat import (is_binary_string, is_string, is_text_string,
to_binary_string, to_text_string)
from spyder.utils.icon_manager import ima
from spyder.utils.qthelpers import add_actions, keybinding, safe_disconnect
from spyder.utils.qthelpers import keybinding, safe_disconnect
from spyder.utils.stylesheet import AppStyle, MAC

# =============================================================================
# ---- Constants
# =============================================================================

class ArrayEditorActions:
Copy = 'copy_action'
Expand All @@ -56,6 +59,16 @@ class ArrayEditorActions:
ToggleBackgroundColor = 'toggle_background_color_action'


class ArrayEditorMenus:
Options = 'options_menu'


class ArrayEditorWidgets:
OptionsToolButton = 'options_button_widget'
Toolbar = 'toolbar'
ToolbarStretcher = 'toolbar_stretcher'


# Note: string and unicode data types will be formatted with 's' (see below)
SUPPORTED_FORMATS = {
'single': '.6g',
Expand Down Expand Up @@ -105,10 +118,10 @@ class ArrayEditorActions:
LARGE_NROWS = 1e5
LARGE_COLS = 60


#==============================================================================
# Utility functions
# ---- Utility functions
#==============================================================================

def is_float(dtype):
"""Return True if datatype dtype is a float kind"""
return ('float' in dtype.name) or dtype.name in ['single', 'double']
Expand All @@ -127,8 +140,9 @@ def get_idx_rect(index_list):


#==============================================================================
# Main classes
# ---- Main classes
#==============================================================================

class ArrayModel(QAbstractTableModel, SpyderFontsMixin):
"""Array Editor Table Model"""

Expand Down Expand Up @@ -539,7 +553,7 @@ def resize_to_contents(self):
def setup_menu(self):
"""Setup context menu"""
self.copy_action = self.create_action(
name=None,
name=ArrayEditorActions.Copy,
text=_('Copy'),
icon=ima.icon('editcopy'),
triggered=self.copy,
Expand All @@ -549,14 +563,17 @@ def setup_menu(self):
self.copy_action.setShortcutContext(Qt.WidgetShortcut)

edit_action = self.create_action(
name=None,
name=ArrayEditorActions.Edit,
text=_('Edit'),
icon=ima.icon('edit'),
triggered=self.edit_item,
register_action=False
)

menu = self.create_menu('Editor menu', register=False)
add_actions(menu, [self.copy_action, edit_action])
for action in [self.copy_action, edit_action]:
self.add_item_to_menu(action, menu)

return menu

def contextMenuEvent(self, event):
Expand Down Expand Up @@ -766,24 +783,31 @@ def do_nothing():

# ---- Toolbar and options menu

options_menu = self.create_menu('Options menu', register=False)
options_menu = self.create_menu(
ArrayEditorMenus.Options,
register=False
)
options_menu.add_action(self.toggle_bgcolor_action)
options_menu.add_action(self.format_action)

options_button = self.create_toolbutton(
name='Options toolbutton',
name=ArrayEditorWidgets.OptionsToolButton,
text=_('Options'),
icon=ima.icon('tooloptions'),
register=False
)
options_button.setPopupMode(QToolButton.InstantPopup)
options_button.setMenu(options_menu)

toolbar = self.create_toolbar('Editor toolbar', register=False)
toolbar.add_item(self.create_stretcher(id_='stretcher'))
toolbar.add_item(self.resize_action)
toolbar.add_item(self.refresh_action)
toolbar.add_item(options_button)
toolbar = self.create_toolbar(
ArrayEditorWidgets.Toolbar,
register=False
)
stretcher = self.create_stretcher(ArrayEditorWidgets.ToolbarStretcher)
for item in [stretcher, self.resize_action, self.refresh_action,
options_button]:
self.add_item_to_toolbar(item, toolbar)

toolbar._render()

# ---- Stack widget (empty)
Expand Down
Loading

0 comments on commit 4e2c559

Please sign in to comment.