UserChangesModel.py 4.4 KB

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