CustomQualityProfilesDropDownMenuModel.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING
  4. from UM.Logger import Logger
  5. import cura.CuraApplication # Imported this way to prevent circular references.
  6. from cura.Machines.ContainerTree import ContainerTree
  7. from cura.Machines.Models.QualityProfilesDropDownMenuModel import QualityProfilesDropDownMenuModel
  8. if TYPE_CHECKING:
  9. from PyQt5.QtCore import QObject
  10. from UM.Settings.Interfaces import ContainerInterface
  11. ## This model is used for the custom profile items in the profile drop down
  12. # menu.
  13. class CustomQualityProfilesDropDownMenuModel(QualityProfilesDropDownMenuModel):
  14. def __init__(self, parent: Optional["QObject"] = None) -> None:
  15. super().__init__(parent)
  16. container_registry = cura.CuraApplication.CuraApplication.getInstance().getContainerRegistry()
  17. container_registry.containerAdded.connect(self._qualityChangesListChanged)
  18. container_registry.containerRemoved.connect(self._qualityChangesListChanged)
  19. container_registry.containerMetaDataChanged.connect(self._qualityChangesListChanged)
  20. def _qualityChangesListChanged(self, container: "ContainerInterface") -> None:
  21. if container.getMetaDataEntry("type") == "quality_changes":
  22. self._update()
  23. def _update(self) -> None:
  24. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  25. active_global_stack = cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMachine
  26. if active_global_stack is None:
  27. self.setItems([])
  28. Logger.log("d", "No active GlobalStack, set %s as empty.", self.__class__.__name__)
  29. return
  30. quality_changes_list = ContainerTree.getInstance().getCurrentQualityChangesGroups()
  31. item_list = []
  32. for quality_changes_group in sorted(quality_changes_list, key = lambda qgc: qgc.name.lower()):
  33. item = {"name": quality_changes_group.name,
  34. "layer_height": "",
  35. "layer_height_without_unit": "",
  36. "available": quality_changes_group.is_available,
  37. "quality_changes_group": quality_changes_group}
  38. item_list.append(item)
  39. self.setItems(item_list)