QualityManagementModel.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, pyqtSlot
  4. from UM.Qt.ListModel import ListModel
  5. from UM.Logger import Logger
  6. #
  7. # This the QML model for the quality management page.
  8. #
  9. class QualityManagementModel(ListModel):
  10. NameRole = Qt.UserRole + 1
  11. IsReadOnlyRole = Qt.UserRole + 2
  12. QualityGroupRole = Qt.UserRole + 3
  13. QualityChangesGroupRole = Qt.UserRole + 4
  14. def __init__(self, parent = None):
  15. super().__init__(parent)
  16. self.addRoleName(self.NameRole, "name")
  17. self.addRoleName(self.IsReadOnlyRole, "is_read_only")
  18. self.addRoleName(self.QualityGroupRole, "quality_group")
  19. self.addRoleName(self.QualityChangesGroupRole, "quality_changes_group")
  20. from cura.CuraApplication import CuraApplication
  21. self._container_registry = CuraApplication.getInstance().getContainerRegistry()
  22. self._machine_manager = CuraApplication.getInstance().getMachineManager()
  23. self._extruder_manager = CuraApplication.getInstance().getExtruderManager()
  24. self._quality_manager = CuraApplication.getInstance().getQualityManager()
  25. self._machine_manager.globalContainerChanged.connect(self._update)
  26. self._quality_manager.qualitiesUpdated.connect(self._update)
  27. self._update()
  28. def _update(self):
  29. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  30. global_stack = self._machine_manager.activeMachine
  31. if not global_stack:
  32. self.setItems([])
  33. return
  34. quality_group_dict = self._quality_manager.getQualityGroups(global_stack)
  35. quality_changes_group_dict = self._quality_manager.getQualityChangesGroups(global_stack)
  36. available_quality_types = set(quality_type for quality_type, quality_group in quality_group_dict.items()
  37. if quality_group.is_available)
  38. if not available_quality_types and not quality_changes_group_dict:
  39. # Nothing to show
  40. self.setItems([])
  41. return
  42. item_list = []
  43. # Create quality group items
  44. for quality_group in quality_group_dict.values():
  45. if not quality_group.is_available:
  46. continue
  47. item = {"name": quality_group.name,
  48. "is_read_only": True,
  49. "quality_group": quality_group,
  50. "quality_changes_group": None}
  51. item_list.append(item)
  52. # Sort by quality names
  53. item_list = sorted(item_list, key = lambda x: x["name"].upper())
  54. # Create quality_changes group items
  55. quality_changes_item_list = []
  56. for quality_changes_group in quality_changes_group_dict.values():
  57. quality_group = quality_group_dict.get(quality_changes_group.quality_type)
  58. item = {"name": quality_changes_group.name,
  59. "is_read_only": False,
  60. "quality_group": quality_group,
  61. "quality_changes_group": quality_changes_group}
  62. quality_changes_item_list.append(item)
  63. # Sort quality_changes items by names and append to the item list
  64. quality_changes_item_list = sorted(quality_changes_item_list, key = lambda x: x["name"].upper())
  65. item_list += quality_changes_item_list
  66. self.setItems(item_list)
  67. # TODO: Duplicated code here from InstanceContainersModel. Refactor and remove this later.
  68. #
  69. ## Gets a list of the possible file filters that the plugins have
  70. # registered they can read or write. The convenience meta-filters
  71. # "All Supported Types" and "All Files" are added when listing
  72. # readers, but not when listing writers.
  73. #
  74. # \param io_type \type{str} name of the needed IO type
  75. # \return A list of strings indicating file name filters for a file
  76. # dialog.
  77. @pyqtSlot(str, result = "QVariantList")
  78. def getFileNameFilters(self, io_type):
  79. from UM.i18n import i18nCatalog
  80. catalog = i18nCatalog("uranium")
  81. #TODO: This function should be in UM.Resources!
  82. filters = []
  83. all_types = []
  84. for plugin_id, meta_data in self._getIOPlugins(io_type):
  85. for io_plugin in meta_data[io_type]:
  86. filters.append(io_plugin["description"] + " (*." + io_plugin["extension"] + ")")
  87. all_types.append("*.{0}".format(io_plugin["extension"]))
  88. if "_reader" in io_type:
  89. # if we're listing readers, add the option to show all supported files as the default option
  90. filters.insert(0, catalog.i18nc("@item:inlistbox", "All Supported Types ({0})", " ".join(all_types)))
  91. filters.append(catalog.i18nc("@item:inlistbox", "All Files (*)")) # Also allow arbitrary files, if the user so prefers.
  92. return filters
  93. ## Gets a list of profile reader or writer plugins
  94. # \return List of tuples of (plugin_id, meta_data).
  95. def _getIOPlugins(self, io_type):
  96. from UM.PluginRegistry import PluginRegistry
  97. pr = PluginRegistry.getInstance()
  98. active_plugin_ids = pr.getActivePlugins()
  99. result = []
  100. for plugin_id in active_plugin_ids:
  101. meta_data = pr.getMetaData(plugin_id)
  102. if io_type in meta_data:
  103. result.append( (plugin_id, meta_data) )
  104. return result