Skip to content

Commit

Permalink
ENH: Refactor HomeWidget to support custom toolbars
Browse files Browse the repository at this point in the history
Improves the `HomeWidget` class by introducing convenient functions
to managing custom toolbars in the application:

- Adds `toolbarNames` property to dynamically manage toolbar names
- Refactors the `modifyWindowUI` method to create and manage multiple
  custom toolbars, introducing the following functions:
  - `insertToolBar`: Helper method to insert a new custom toolbar
    between existing ones.
  - `initializeSettingsToolBar`: Creates the settings toolbar with the gear icon button to toggle
    visibility of the settings dialog.

Co-authored-by: Tom Birdsong <[email protected]>
  • Loading branch information
jcfr and tbirdso committed Aug 21, 2024
1 parent 085453f commit 91c17c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions {{cookiecutter.project_name}}/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ isort.known-first-party = [
"Resources",
]
pydocstyle.convention = "pep257"

[lint.per-file-ignores]
# Disable "RUF012: Mutable class attributes should be annotated"
# to allow convenient declaration of instance variables in the HomeWidget class.
"Modules/Scripted/Home/Home.py" = ["RUF012"]
41 changes: 33 additions & 8 deletions {{cookiecutter.project_name}}/Modules/Scripted/Home/Home.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Mapping
from typing import Optional

import qt
Expand Down Expand Up @@ -39,6 +40,12 @@ class HomeWidget(ScriptedLoadableModuleWidget, VTKObservationMixin):
https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/ScriptedLoadableModule.py
"""

@property
def toolbarNames(self) -> list[str]:
return [str(k) for k in self._toolbars]

_toolbars: Mapping[str, qt.QToolBar] = {}

def __init__(self, parent: Optional[qt.QWidget]):
"""Called when the application opens the module the first time and the widget is initialized."""
ScriptedLoadableModuleWidget.__init__(self, parent)
Expand Down Expand Up @@ -77,7 +84,7 @@ def setSlicerUIVisible(self, visible: bool):
exemptToolbars = [
"MainToolBar",
"ViewToolBar",
"CustomToolBar",
*self.toolbarNames,
]
slicer.util.setDataProbeVisible(visible)
slicer.util.setMenuBarsVisible(visible, ignore=exemptToolbars)
Expand All @@ -89,15 +96,33 @@ def setSlicerUIVisible(self, visible: bool):
slicer.util.setToolbarsVisible(visible, keepToolbars)

def modifyWindowUI(self):
# Custom toolbar
mainToolBar = slicer.util.findChild(slicer.util.mainWindow(), "MainToolBar")
self.CustomToolBar = qt.QToolBar("CustomToolBar")
self.CustomToolBar.name = "CustomToolBar"
slicer.util.mainWindow().insertToolBar(mainToolBar, self.CustomToolBar)
"""Customize the entire user interface to resemble the custom application"""
# Custom toolbars
self.initializeSettingsToolBar()

def insertToolBar(self, beforeToolBarName: str, name: str, title: Optional[str] = None) -> qt.QToolBar:
"""Helper method to insert a new toolbar between existing ones"""
beforeToolBar = slicer.util.findChild(slicer.util.mainWindow(), beforeToolBarName)

if title is None:
title = name

toolBar = qt.QToolBar(title)
toolBar.name = name
slicer.util.mainWindow().insertToolBar(beforeToolBar, toolBar)

self._toolbars[name] = toolBar

return toolBar

def initializeSettingsToolBar(self):
"""Create toolbar and dialog for app settings"""
settingsToolBar = self.insertToolBar("MainToolBar", "SettingsToolBar", title="Settings")

# Settings dialog
gearIcon = qt.QIcon(self.resourcePath("Icons/Gears.png"))
self.settingsAction = self.CustomToolBar.addAction(gearIcon, "")
self.settingsAction = settingsToolBar.addAction(gearIcon, "")

# Settings dialog
self.settingsDialog = slicer.util.loadUI(self.resourcePath("UI/Settings.ui"))
self.settingsUI = slicer.util.childWidgetVariables(self.settingsDialog)
self.settingsUI.CustomUICheckBox.toggled.connect(self.setCustomUIVisible)
Expand Down

0 comments on commit 91c17c7

Please sign in to comment.