SpecificSettingsModel.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (c) 2024 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt6.QtCore import Qt, pyqtSignal
  4. from UM import i18nCatalog
  5. from UM.Logger import Logger
  6. from UM.Settings.SettingDefinition import SettingDefinition
  7. from UM.Qt.ListModel import ListModel
  8. class SpecificSettingsModel(ListModel):
  9. CategoryRole = Qt.ItemDataRole.UserRole + 1
  10. LabelRole = Qt.ItemDataRole.UserRole + 2
  11. ValueRole = Qt.ItemDataRole.UserRole + 3
  12. def __init__(self, parent = None):
  13. super().__init__(parent = parent)
  14. self.addRoleName(self.CategoryRole, "category")
  15. self.addRoleName(self.LabelRole, "label")
  16. self.addRoleName(self.ValueRole, "value")
  17. self._settings_catalog = i18nCatalog("fdmprinter.def.json")
  18. self._update()
  19. modelChanged = pyqtSignal()
  20. def addSettingsFromStack(self, stack, category, settings):
  21. for setting, value in settings.items():
  22. unit = stack.getProperty(setting, "unit")
  23. setting_type = stack.getProperty(setting, "type")
  24. if setting_type is not None:
  25. # This is not very good looking, but will do for now
  26. value = str(SettingDefinition.settingValueToString(setting_type, value))
  27. if unit:
  28. value += " " + str(unit)
  29. if setting_type == "enum":
  30. options = stack.getProperty(setting, "options")
  31. value_msgctxt = f"{str(setting)} option {str(value)}"
  32. value_msgid = options[stack.getProperty(setting, "value")]
  33. value = self._settings_catalog.i18nc(value_msgctxt, value_msgid)
  34. else:
  35. value = str(value)
  36. label_msgctxt = f"{str(setting)} label"
  37. label_msgid = stack.getProperty(setting, "label")
  38. label = self._settings_catalog.i18nc(label_msgctxt, label_msgid)
  39. self.appendItem({
  40. "category": category,
  41. "label": label,
  42. "value": value
  43. })
  44. self.modelChanged.emit()
  45. def _update(self):
  46. Logger.debug(f"Updating {self.__class__.__name__}")
  47. self.setItems([])
  48. self.modelChanged.emit()
  49. return