mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-05-29 09:38:21 +08:00
Get meta data per sidebar view in sidebar view model
This commit is contained in:
parent
f2b4cbe182
commit
713055e320
@ -1,10 +1,19 @@
|
||||
# Copyright (c) 2017 Ultimaker B.V.
|
||||
|
||||
from PyQt5.QtCore import QObject
|
||||
|
||||
from UM.i18n import i18nCatalog
|
||||
from cura.Sidebar.SidebarView import SidebarView
|
||||
i18n_catalog = i18nCatalog("cura")
|
||||
|
||||
class SettingsSidebarView(QObject, SidebarView):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
## As the default sidebar is not a plugin, we have a get meta data method here to allow the sidebar view model to get the needed data.
|
||||
def getMetaData(self):
|
||||
return {
|
||||
"sidebar_view": {
|
||||
"name": i18n_catalog.i18nc("", "Print settings"),
|
||||
"weight": 1
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ from UM.Logger import Logger
|
||||
from UM.PluginRegistry import PluginRegistry
|
||||
from UM.Signal import Signal
|
||||
from .SidebarView import SidebarView
|
||||
from typing import Optional
|
||||
from typing import Optional, Dict
|
||||
|
||||
# The sidebar controller manages available sidebar components and decides which one to display.
|
||||
# The cura.qml file uses this controller to repeat over the sidebars and show the active index.
|
||||
@ -46,3 +46,7 @@ class SidebarController:
|
||||
## Change the active sidebar view to one of the registered views.
|
||||
def setActiveSidebarView(self, name: str):
|
||||
print("setting active sidebar view")
|
||||
|
||||
## Get all sidebar views registered in this controller.
|
||||
def getAllSidebarViews(self) -> Dict[SidebarView]:
|
||||
return self._sidebar_views
|
||||
|
@ -13,4 +13,42 @@ class SidebarViewModel(ListModel):
|
||||
def __init__(self, parent = None):
|
||||
super().__init__(parent)
|
||||
|
||||
|
||||
self._controller = Application.getInstance().getSidebarController()
|
||||
|
||||
# register Qt list roles
|
||||
self.addRoleName(self.IdRole, "id")
|
||||
self.addRoleName(self.NameRole, "name")
|
||||
self.addRoleName(self.ActiveRole, "active")
|
||||
|
||||
## Update the model when new views are added or another view is made the active view.
|
||||
def _onSidebarViewsChanged(self):
|
||||
items = []
|
||||
sidebar_views = self._controller.getAllSidebarViews()
|
||||
current_view = self._controller.getActiveSidebarView()
|
||||
|
||||
for sidebar_view_id, sidebar_view in sidebar_views.items():
|
||||
plugin_metadata = PluginRegistry.getInstance().getMetaData(sidebar_view_id)
|
||||
if plugin_metadata:
|
||||
# Check if the registered view came from a plugin and extract the metadata if so.
|
||||
sidebar_view_metadata = plugin_metadata.get("sidebar_view", {})
|
||||
else:
|
||||
# Get the meta data directly from the plugin
|
||||
sidebar_view_metadata = sidebar_view.getMetaData()
|
||||
|
||||
# Skip view modes that are marked as not visible
|
||||
if "visible" in sidebar_view_metadata and not sidebar_view_metadata["visible"]:
|
||||
continue
|
||||
|
||||
name = sidebar_view_metadata.get("name", id)
|
||||
weight = sidebar_view_metadata.get("weight", 0)
|
||||
|
||||
items.append({
|
||||
"id": sidebar_view_id,
|
||||
"name": name,
|
||||
"active": sidebar_view_id == current_view.getPluginId(),
|
||||
"weight": weight
|
||||
})
|
||||
|
||||
# Sort the views by weight
|
||||
items.sort(key=lambda t: t["weight"])
|
||||
self.setItems(items)
|
||||
|
Loading…
x
Reference in New Issue
Block a user