mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-22 22:23:57 -06:00
WIP: Refactor Materials management page to use new Managers
This commit is contained in:
parent
aed0c124e9
commit
1e0a078af8
6 changed files with 1264 additions and 332 deletions
|
@ -1,17 +1,16 @@
|
|||
# Copyright (c) 2017 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
|
||||
from typing import Any, List
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
from typing import Any, List, Optional
|
||||
from PyQt5.QtCore import Qt, QObject
|
||||
|
||||
from UM.Logger import Logger
|
||||
from UM.Qt.ListModel import ListModel
|
||||
from UM.Settings.ContainerRegistry import ContainerRegistry #To listen for changes to the materials.
|
||||
from UM.Settings.Models.InstanceContainersModel import InstanceContainersModel #We're extending this class.
|
||||
from UM.Settings.ContainerRegistry import ContainerRegistry
|
||||
from UM.Settings.Models.InstanceContainersModel import InstanceContainersModel
|
||||
|
||||
|
||||
def getAvailableMaterials():
|
||||
def getAvailableMaterials(extruder_position: Optional[int] = None):
|
||||
from cura.CuraApplication import CuraApplication
|
||||
machine_manager = CuraApplication.getInstance().getMachineManager()
|
||||
extruder_manager = CuraApplication.getInstance().getExtruderManager()
|
||||
|
@ -19,18 +18,21 @@ def getAvailableMaterials():
|
|||
material_manager = CuraApplication.getInstance()._material_manager
|
||||
|
||||
active_global_stack = machine_manager._global_container_stack
|
||||
active_extruder_stack = extruder_manager.getActiveExtruderStack()
|
||||
extruder_stack = extruder_manager.getActiveExtruderStack()
|
||||
if extruder_position is not None:
|
||||
if active_global_stack is not None:
|
||||
extruder_stack = active_global_stack.extruders.get(str(extruder_position))
|
||||
|
||||
if active_global_stack is None or active_extruder_stack is None:
|
||||
if active_global_stack is None or extruder_stack is None:
|
||||
Logger.log("d", "Active global stack [%s] or extruder stack [%s] is None, setting material list to empty.",
|
||||
active_global_stack, active_extruder_stack)
|
||||
active_global_stack, extruder_stack)
|
||||
return
|
||||
|
||||
machine_definition_id = active_global_stack.definition.getId()
|
||||
variant_name = None
|
||||
if active_extruder_stack.variant.getId() != "empty_variant":
|
||||
variant_name = active_extruder_stack.variant.getName()
|
||||
diameter = active_extruder_stack.getProperty("material_diameter", "value")
|
||||
if extruder_stack.variant.getId() != "empty_variant":
|
||||
variant_name = extruder_stack.variant.getName()
|
||||
diameter = extruder_stack.getProperty("material_diameter", "value")
|
||||
|
||||
# Fetch the available materials (ContainerNode) for the current active machine and extruder setup.
|
||||
result_dict = material_manager.getAvailableMaterials(machine_definition_id, variant_name, diameter)
|
||||
|
@ -44,7 +46,7 @@ class BaseMaterialsModel(ListModel):
|
|||
BrandRole = Qt.UserRole + 4
|
||||
MaterialRole = Qt.UserRole + 5
|
||||
ColorRole = Qt.UserRole + 6
|
||||
ContainerNodeRole = Qt.UserRole + 6
|
||||
ContainerNodeRole = Qt.UserRole + 7
|
||||
|
||||
def __init__(self, parent = None):
|
||||
super().__init__(parent)
|
||||
|
@ -184,6 +186,90 @@ class BrandMaterialsModel(ListModel):
|
|||
self.setItems(brand_item_list)
|
||||
|
||||
|
||||
#
|
||||
# This model is for the Material management page.
|
||||
#
|
||||
class NewMaterialsModel(ListModel):
|
||||
RootMaterialIdRole = Qt.UserRole + 1
|
||||
DisplayNameRole = Qt.UserRole + 2
|
||||
BrandRole = Qt.UserRole + 3
|
||||
MaterialTypeRole = Qt.UserRole + 4
|
||||
ColorNameRole = Qt.UserRole + 5
|
||||
ColorCodeRole = Qt.UserRole + 6
|
||||
ContainerNodeRole = Qt.UserRole + 7
|
||||
ContainerIdRole = Qt.UserRole + 8
|
||||
|
||||
DescriptionRole = Qt.UserRole + 9
|
||||
AdhesionInfoRole = Qt.UserRole + 10
|
||||
ApproximateDiameterRole = Qt.UserRole + 11
|
||||
GuidRole = Qt.UserRole + 12
|
||||
DensityRole = Qt.UserRole + 13
|
||||
DiameterRole = Qt.UserRole + 14
|
||||
|
||||
def __init__(self, parent = None):
|
||||
super().__init__(parent)
|
||||
|
||||
self.addRoleName(self.RootMaterialIdRole, "root_material_id")
|
||||
self.addRoleName(self.DisplayNameRole, "name")
|
||||
self.addRoleName(self.BrandRole, "brand")
|
||||
self.addRoleName(self.MaterialTypeRole, "material")
|
||||
self.addRoleName(self.ColorNameRole, "color_name")
|
||||
self.addRoleName(self.ColorCodeRole, "color_code")
|
||||
self.addRoleName(self.ContainerNodeRole, "container_node")
|
||||
self.addRoleName(self.ContainerIdRole, "container_id")
|
||||
|
||||
self.addRoleName(self.DescriptionRole, "description")
|
||||
self.addRoleName(self.AdhesionInfoRole, "adhesion_info")
|
||||
self.addRoleName(self.ApproximateDiameterRole, "approximate_diameter")
|
||||
self.addRoleName(self.GuidRole, "guid")
|
||||
self.addRoleName(self.DensityRole, "density")
|
||||
self.addRoleName(self.DiameterRole, "diameter")
|
||||
|
||||
from cura.CuraApplication import CuraApplication
|
||||
machine_manager = CuraApplication.getInstance().getMachineManager()
|
||||
extruder_manager = CuraApplication.getInstance().getExtruderManager()
|
||||
material_manager = CuraApplication.getInstance()._material_manager
|
||||
|
||||
machine_manager.globalContainerChanged.connect(self._update)
|
||||
extruder_manager.activeExtruderChanged.connect(self._update)
|
||||
material_manager.materialsUpdated.connect(self._update)
|
||||
|
||||
self._update()
|
||||
|
||||
def _update(self):
|
||||
result_dict = getAvailableMaterials()
|
||||
if result_dict is None:
|
||||
self.setItems([])
|
||||
return
|
||||
|
||||
material_list = []
|
||||
for root_material_id, container_node in result_dict.items():
|
||||
keys_to_fetch = ("name",
|
||||
"brand",
|
||||
"material",
|
||||
"color_name",
|
||||
"color_code",
|
||||
"description",
|
||||
"adhesion_info",
|
||||
"approximate_diameter",)
|
||||
|
||||
item = {"root_material_id": container_node.metadata["base_file"],
|
||||
"container_node": container_node,
|
||||
"guid": container_node.metadata["GUID"],
|
||||
"container_id": container_node.metadata["id"],
|
||||
"density": container_node.metadata.get("properties", {}).get("density", ""),
|
||||
"diameter": container_node.metadata.get("properties", {}).get("diameter", ""),
|
||||
}
|
||||
|
||||
for key in keys_to_fetch:
|
||||
item[key] = container_node.metadata.get(key, "")
|
||||
|
||||
material_list.append(item)
|
||||
|
||||
material_list = sorted(material_list, key = lambda k: (k["brand"], k["name"]))
|
||||
self.setItems(material_list)
|
||||
|
||||
|
||||
## A model that shows a list of currently valid materials. Used by management page.
|
||||
class MaterialsModel(InstanceContainersModel):
|
||||
def __init__(self, parent = None):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue