UserChangesModel.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from collections import OrderedDict
  5. from PyQt5.QtCore import pyqtSlot, Qt
  6. from UM.Application import Application
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from UM.i18n import i18nCatalog
  9. from UM.Settings.SettingFunction import SettingFunction
  10. from UM.Qt.ListModel import ListModel
  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. application = Application.getInstance()
  33. machine_manager = application.getMachineManager()
  34. cura_formula_functions = application.getCuraFormulaFunctions()
  35. item_dict = OrderedDict()
  36. item_list = []
  37. global_stack = machine_manager.activeMachine
  38. if not global_stack:
  39. return
  40. stacks = [global_stack]
  41. stacks.extend(global_stack.extruderList)
  42. # Check if the definition container has a translation file and ensure it's loaded.
  43. definition = global_stack.getBottom()
  44. definition_suffix = ContainerRegistry.getMimeTypeForContainer(type(definition)).preferredSuffix
  45. catalog = i18nCatalog(os.path.basename(definition.getId() + "." + definition_suffix))
  46. if catalog.hasTranslationLoaded():
  47. self._i18n_catalog = catalog
  48. for file_name in definition.getInheritedFiles():
  49. catalog = i18nCatalog(os.path.basename(file_name))
  50. if catalog.hasTranslationLoaded():
  51. self._i18n_catalog = catalog
  52. for stack in stacks:
  53. # Make a list of all containers in the stack.
  54. containers = []
  55. latest_stack = stack
  56. while latest_stack:
  57. containers.extend(latest_stack.getContainers())
  58. latest_stack = latest_stack.getNextStack()
  59. # Override "getExtruderValue" with "getDefaultExtruderValue" so we can get the default values
  60. user_changes = containers.pop(0)
  61. default_value_resolve_context = cura_formula_functions.createContextForDefaultValueEvaluation(stack)
  62. for setting_key in user_changes.getAllKeys():
  63. original_value = None
  64. # Find the category of the instance by moving up until we find a category.
  65. category = user_changes.getInstance(setting_key).definition
  66. while category.type != "category":
  67. category = category.parent
  68. # Handle translation (and fallback if we weren't able to find any translation files.
  69. if self._i18n_catalog:
  70. category_label = self._i18n_catalog.i18nc(category.key + " label", category.label)
  71. else:
  72. category_label = category.label
  73. if self._i18n_catalog:
  74. label = self._i18n_catalog.i18nc(setting_key + " label", stack.getProperty(setting_key, "label"))
  75. else:
  76. label = stack.getProperty(setting_key, "label")
  77. for container in containers:
  78. if stack == global_stack:
  79. resolve = global_stack.getProperty(setting_key, "resolve", default_value_resolve_context)
  80. if resolve is not None:
  81. original_value = resolve
  82. break
  83. original_value = container.getProperty(setting_key, "value", default_value_resolve_context)
  84. # If a value is a function, ensure it's called with the stack it's in.
  85. if isinstance(original_value, SettingFunction):
  86. original_value = original_value(stack, default_value_resolve_context)
  87. if original_value is not None:
  88. break
  89. item_to_add = {"key": setting_key,
  90. "label": label,
  91. "user_value": str(user_changes.getProperty(setting_key, "value")),
  92. "original_value": str(original_value),
  93. "extruder": "",
  94. "category": category_label}
  95. if stack != global_stack:
  96. item_to_add["extruder"] = stack.getName()
  97. if category_label not in item_dict:
  98. item_dict[category_label] = []
  99. item_dict[category_label].append(item_to_add)
  100. for each_item_list in item_dict.values():
  101. item_list += each_item_list
  102. self.setItems(item_list)