UserChangesModel.py 4.7 KB

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