SettingOverrideModel.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Uranium is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, pyqtSlot, QUrl
  4. from UM.Application import Application
  5. from UM.Qt.ListModel import ListModel
  6. #from UM.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  7. class SettingOverrideModel(ListModel):
  8. KeyRole = Qt.UserRole + 1
  9. LabelRole = Qt.UserRole + 2
  10. DescriptionRole = Qt.UserRole + 3
  11. ValueRole = Qt.UserRole + 4
  12. TypeRole = Qt.UserRole + 5
  13. UnitRole = Qt.UserRole + 6
  14. ValidRole = Qt.UserRole + 7
  15. OptionsRole = Qt.UserRole + 8
  16. WarningDescriptionRole = Qt.UserRole + 9
  17. ErrorDescriptionRole = Qt.UserRole + 10
  18. GlobalOnlyRole = Qt.UserRole + 11
  19. def __init__(self, node, parent = None):
  20. super().__init__(parent)
  21. self._ignore_setting_change = None
  22. self._node = node
  23. self._node.decoratorsChanged.connect(self._onDecoratorsChanged)
  24. self._onDecoratorsChanged(None)
  25. #self._activeProfile = Application.getInstance().getMachineManager().getWorkingProfile() #To be able to get notified when a setting changes.
  26. #self._activeProfile.settingValueChanged.connect(self._onProfileSettingValueChanged)
  27. #Application.getInstance().getMachineManager().activeProfileChanged.connect(self._onProfileChanged)
  28. self.addRoleName(self.KeyRole, "key")
  29. self.addRoleName(self.LabelRole, "label")
  30. self.addRoleName(self.DescriptionRole, "description")
  31. self.addRoleName(self.ValueRole,"value")
  32. self.addRoleName(self.TypeRole, "type")
  33. self.addRoleName(self.UnitRole, "unit")
  34. self.addRoleName(self.ValidRole, "valid")
  35. self.addRoleName(self.OptionsRole, "options")
  36. self.addRoleName(self.WarningDescriptionRole, "warning_description")
  37. self.addRoleName(self.ErrorDescriptionRole, "error_description")
  38. self.addRoleName(self.GlobalOnlyRole, "global_only")
  39. @pyqtSlot(str, "QVariant")
  40. def setSettingValue(self, key, value):
  41. if not self._decorator:
  42. return
  43. self._decorator.setSettingValue(key, value)
  44. def _onDecoratorsChanged(self, node):
  45. return
  46. '''if not self._node.getDecorator(SettingOverrideDecorator):
  47. self.clear()
  48. return
  49. self._decorator = self._node.getDecorator(SettingOverrideDecorator)
  50. self._decorator.settingAdded.connect(self._onSettingsChanged)
  51. self._decorator.settingRemoved.connect(self._onSettingsChanged)
  52. self._decorator.settingValueChanged.connect(self._onSettingValueChanged)
  53. self._onSettingsChanged()'''
  54. def _createOptionsModel(self, options):
  55. if not options:
  56. return None
  57. model = ListModel()
  58. model.addRoleName(Qt.UserRole + 1, "value")
  59. model.addRoleName(Qt.UserRole + 2, "name")
  60. for value, name in options.items():
  61. model.appendItem({"value": str(value), "name": str(name)})
  62. return model
  63. ## Updates the active profile in this model if the active profile is
  64. # changed.
  65. #
  66. # This links the settingValueChanged of the new profile to this model's
  67. # _onSettingValueChanged function, so that it properly listens to those
  68. # events again.
  69. def _onProfileChanged(self):
  70. if self._activeProfile: #Unlink from the old profile.
  71. self._activeProfile.settingValueChanged.disconnect(self._onProfileSettingValueChanged)
  72. old_profile = self._activeProfile
  73. self._activeProfile = Application.getInstance().getMachineManager().getWorkingProfile()
  74. self._activeProfile.settingValueChanged.connect(self._onProfileSettingValueChanged) #Re-link to the new profile.
  75. for setting_name in old_profile.getChangedSettings().keys(): #Update all changed settings in the old and new profiles.
  76. self._onProfileSettingValueChanged(setting_name)
  77. for setting_name in self._activeProfile.getChangedSettings().keys():
  78. self._onProfileSettingValueChanged(setting_name)
  79. ## Updates the global_only property of a setting once a setting value
  80. # changes.
  81. #
  82. # This method should only get called on settings that are dependent on the
  83. # changed setting.
  84. #
  85. # \param setting_name The setting that needs to be updated.
  86. def _onProfileSettingValueChanged(self, setting_name):
  87. index = self.find("key", setting_name)
  88. if index != -1:
  89. self.setProperty(index, "global_only", Application.getInstance().getMachineManager().getActiveMachineInstance().getMachineDefinition().getSetting(setting_name).getGlobalOnly())
  90. def _onSettingsChanged(self):
  91. self.clear()
  92. items = []
  93. for key, setting in self._decorator.getAllSettings().items():
  94. value = self._decorator.getSettingValue(key)
  95. items.append({
  96. "key": key,
  97. "label": setting.getLabel(),
  98. "description": setting.getDescription(),
  99. "value": str(value),
  100. "type": setting.getType(),
  101. "unit": setting.getUnit(),
  102. "valid": setting.validate(value),
  103. "options": self._createOptionsModel(setting.getOptions()),
  104. "warning_description": setting.getWarningDescription(),
  105. "error_description": setting.getErrorDescription(),
  106. "global_only": setting.getGlobalOnly()
  107. })
  108. items.sort(key = lambda i: i["key"])
  109. for item in items:
  110. self.appendItem(item)
  111. def _onSettingValueChanged(self, setting):
  112. index = self.find("key", setting.getKey())
  113. value = self._decorator.getSettingValue(setting.getKey())
  114. if index != -1:
  115. self.setProperty(index, "value", str(value))
  116. self.setProperty(index, "valid", setting.validate(value))
  117. self.setProperty(index, "global_only", setting.getGlobalOnly())