UserChangesModel.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (c) 2021 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 typing import Optional
  6. from PyQt5.QtCore import pyqtSlot, Qt
  7. from UM.Application import Application
  8. from UM.Logger import Logger
  9. from UM.Settings.ContainerRegistry import ContainerRegistry
  10. from UM.i18n import i18nCatalog
  11. from UM.Settings.SettingFunction import SettingFunction
  12. from UM.Qt.ListModel import ListModel
  13. class UserChangesModel(ListModel):
  14. KeyRole = Qt.UserRole + 1
  15. LabelRole = Qt.UserRole + 2
  16. ExtruderRole = Qt.UserRole + 3
  17. OriginalValueRole = Qt.UserRole + 4
  18. UserValueRole = Qt.UserRole + 6
  19. CategoryRole = Qt.UserRole + 7
  20. def __init__(self, parent: Optional["QObject"] = None) -> None:
  21. super(UserChangesModel, self).__init__(parent = parent)
  22. self.addRoleName(self.KeyRole, "key")
  23. self.addRoleName(self.LabelRole, "label")
  24. self.addRoleName(self.ExtruderRole, "extruder")
  25. self.addRoleName(self.OriginalValueRole, "original_value")
  26. self.addRoleName(self.UserValueRole, "user_value")
  27. self.addRoleName(self.CategoryRole, "category")
  28. self._i18n_catalog = None
  29. self._update()
  30. @pyqtSlot()
  31. def forceUpdate(self):
  32. self._update()
  33. def _update(self):
  34. application = Application.getInstance()
  35. machine_manager = application.getMachineManager()
  36. cura_formula_functions = application.getCuraFormulaFunctions()
  37. item_dict = OrderedDict()
  38. item_list = []
  39. global_stack = machine_manager.activeMachine
  40. if not global_stack:
  41. return
  42. stacks = [global_stack]
  43. stacks.extend(global_stack.extruderList)
  44. # Check if the definition container has a translation file and ensure it's loaded.
  45. definition = global_stack.getBottom()
  46. definition_suffix = ContainerRegistry.getMimeTypeForContainer(type(definition)).preferredSuffix
  47. catalog = i18nCatalog(os.path.basename(definition.getId() + "." + definition_suffix))
  48. if catalog.hasTranslationLoaded():
  49. self._i18n_catalog = catalog
  50. for file_name in definition.getInheritedFiles():
  51. catalog = i18nCatalog(os.path.basename(file_name))
  52. if catalog.hasTranslationLoaded():
  53. self._i18n_catalog = catalog
  54. for stack in stacks:
  55. # Make a list of all containers in the stack.
  56. containers = []
  57. latest_stack = stack
  58. while latest_stack:
  59. containers.extend(latest_stack.getContainers())
  60. latest_stack = latest_stack.getNextStack()
  61. # Override "getExtruderValue" with "getDefaultExtruderValue" so we can get the default values
  62. user_changes = containers.pop(0)
  63. default_value_resolve_context = cura_formula_functions.createContextForDefaultValueEvaluation(stack)
  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 is not None and category.type != "category":
  69. category = category.parent
  70. # Handle translation (and fallback if we weren't able to find any translation files.
  71. if category is not None:
  72. if self._i18n_catalog:
  73. category_label = self._i18n_catalog.i18nc(category.key + " label", category.label)
  74. else:
  75. category_label = category.label
  76. else: # Setting is not in any category. Shouldn't happen, but it do. See https://sentry.io/share/issue/d735884370154166bc846904d9b812ff/
  77. Logger.error("Setting {key} is not in any setting category.".format(key = setting_key))
  78. category_label = ""
  79. if self._i18n_catalog:
  80. label = self._i18n_catalog.i18nc(setting_key + " label", stack.getProperty(setting_key, "label"))
  81. else:
  82. label = stack.getProperty(setting_key, "label")
  83. for container in containers:
  84. if stack == global_stack:
  85. resolve = global_stack.getProperty(setting_key, "resolve", default_value_resolve_context)
  86. if resolve is not None:
  87. original_value = resolve
  88. break
  89. original_value = container.getProperty(setting_key, "value", default_value_resolve_context)
  90. # If a value is a function, ensure it's called with the stack it's in.
  91. if isinstance(original_value, SettingFunction):
  92. original_value = original_value(stack, default_value_resolve_context)
  93. if original_value is not None:
  94. break
  95. item_to_add = {"key": setting_key,
  96. "label": label,
  97. "user_value": str(user_changes.getProperty(setting_key, "value")),
  98. "original_value": str(original_value),
  99. "extruder": "",
  100. "category": category_label}
  101. if stack != global_stack:
  102. item_to_add["extruder"] = stack.getName()
  103. if category_label not in item_dict:
  104. item_dict[category_label] = []
  105. item_dict[category_label].append(item_to_add)
  106. for each_item_list in item_dict.values():
  107. item_list += each_item_list
  108. self.setItems(item_list)