UserChangesModel.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from UM.Qt.ListModel import ListModel
  2. from PyQt5.QtCore import pyqtSlot, Qt
  3. from UM.Application import Application
  4. from cura.Settings.ExtruderManager import ExtruderManager
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.i18n import i18nCatalog
  7. from UM.Settings.SettingFunction import SettingFunction
  8. from UM.Settings.PropertyEvaluationContext import PropertyEvaluationContext
  9. from collections import OrderedDict
  10. import os
  11. class UserChangesModel(ListModel):
  12. KeyRole = Qt.UserRole + 1
  13. LabelRole = Qt.UserRole + 2
  14. ExtruderRole = Qt.UserRole + 3
  15. OriginalValueRole = Qt.UserRole + 4
  16. UserValueRole = Qt.UserRole + 6
  17. CategoryRole = Qt.UserRole + 7
  18. def __init__(self, parent = None):
  19. super().__init__(parent = parent)
  20. self.addRoleName(self.KeyRole, "key")
  21. self.addRoleName(self.LabelRole, "label")
  22. self.addRoleName(self.ExtruderRole, "extruder")
  23. self.addRoleName(self.OriginalValueRole, "original_value")
  24. self.addRoleName(self.UserValueRole, "user_value")
  25. self.addRoleName(self.CategoryRole, "category")
  26. self._i18n_catalog = None
  27. self._update()
  28. @pyqtSlot()
  29. def forceUpdate(self):
  30. self._update()
  31. def _update(self):
  32. item_dict = OrderedDict()
  33. item_list = []
  34. global_stack = Application.getInstance().getGlobalContainerStack()
  35. if not global_stack:
  36. return
  37. stacks = ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks()
  38. # Check if the definition container has a translation file and ensure it's loaded.
  39. definition = global_stack.getBottom()
  40. definition_suffix = ContainerRegistry.getMimeTypeForContainer(type(definition)).preferredSuffix
  41. catalog = i18nCatalog(os.path.basename(definition.getId() + "." + definition_suffix))
  42. if catalog.hasTranslationLoaded():
  43. self._i18n_catalog = catalog
  44. for file_name in definition.getInheritedFiles():
  45. catalog = i18nCatalog(os.path.basename(file_name))
  46. if catalog.hasTranslationLoaded():
  47. self._i18n_catalog = catalog
  48. for stack in stacks:
  49. # Make a list of all containers in the stack.
  50. containers = []
  51. latest_stack = stack
  52. while latest_stack:
  53. containers.extend(latest_stack.getContainers())
  54. latest_stack = latest_stack.getNextStack()
  55. # Override "getExtruderValue" with "getDefaultExtruderValue" so we can get the default values
  56. user_changes = containers.pop(0)
  57. default_value_resolve_context = PropertyEvaluationContext(stack)
  58. default_value_resolve_context.context["evaluate_from_container_index"] = 1 # skip the user settings container
  59. default_value_resolve_context.context["override_operators"] = {
  60. "extruderValue": ExtruderManager.getDefaultExtruderValue,
  61. "extruderValues": ExtruderManager.getDefaultExtruderValues,
  62. "resolveOrValue": ExtruderManager.getDefaultResolveOrValue
  63. }
  64. for setting_key in user_changes.getAllKeys():
  65. original_value = None
  66. # Find the category of the instance by moving up until we find a category.
  67. category = user_changes.getInstance(setting_key).definition
  68. while category.type != "category":
  69. category = category.parent
  70. # Handle translation (and fallback if we weren't able to find any translation files.
  71. if self._i18n_catalog:
  72. category_label = self._i18n_catalog.i18nc(category.key + " label", category.label)
  73. else:
  74. category_label = category.label
  75. if self._i18n_catalog:
  76. label = self._i18n_catalog.i18nc(setting_key + " label", stack.getProperty(setting_key, "label"))
  77. else:
  78. label = stack.getProperty(setting_key, "label")
  79. for container in containers:
  80. if stack == global_stack:
  81. resolve = global_stack.getProperty(setting_key, "resolve", default_value_resolve_context)
  82. if resolve is not None:
  83. original_value = resolve
  84. break
  85. original_value = container.getProperty(setting_key, "value", default_value_resolve_context)
  86. # If a value is a function, ensure it's called with the stack it's in.
  87. if isinstance(original_value, SettingFunction):
  88. original_value = original_value(stack, default_value_resolve_context)
  89. if original_value is not None:
  90. break
  91. item_to_add = {"key": setting_key,
  92. "label": label,
  93. "user_value": str(user_changes.getProperty(setting_key, "value")),
  94. "original_value": str(original_value),
  95. "extruder": "",
  96. "category": category_label}
  97. if stack != global_stack:
  98. item_to_add["extruder"] = stack.getName()
  99. if category_label not in item_dict:
  100. item_dict[category_label] = []
  101. item_dict[category_label].append(item_to_add)
  102. for each_item_list in item_dict.values():
  103. item_list += each_item_list
  104. self.setItems(item_list)