SettingVisibilityPresetsModel.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, List
  4. from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
  5. from UM.Logger import Logger
  6. from UM.Resources import Resources
  7. from UM.i18n import i18nCatalog
  8. from cura.Settings.SettingVisibilityPreset import SettingVisibilityPreset
  9. catalog = i18nCatalog("cura")
  10. class SettingVisibilityPresetsModel(QObject):
  11. onItemsChanged = pyqtSignal()
  12. activePresetChanged = pyqtSignal()
  13. def __init__(self, preferences, parent = None):
  14. super().__init__(parent)
  15. self._items = [] # type: List[SettingVisibilityPreset]
  16. self._populate()
  17. basic_item = self.getVisibilityPresetById("basic")
  18. basic_visibile_settings = ";".join(basic_item.settings)
  19. self._preferences = preferences
  20. # Preference to store which preset is currently selected
  21. self._preferences.addPreference("cura/active_setting_visibility_preset", "basic")
  22. # Preference that stores the "custom" set so it can always be restored (even after a restart)
  23. self._preferences.addPreference("cura/custom_visible_settings", basic_visibile_settings)
  24. self._preferences.preferenceChanged.connect(self._onPreferencesChanged)
  25. self._active_preset_item = self.getVisibilityPresetById(self._preferences.getValue("cura/active_setting_visibility_preset"))
  26. # Initialize visible settings if it is not done yet
  27. visible_settings = self._preferences.getValue("general/visible_settings")
  28. if not visible_settings:
  29. self._preferences.setValue("general/visible_settings", ";".join(self._active_preset_item.settings))
  30. else:
  31. self._onPreferencesChanged("general/visible_settings")
  32. self.activePresetChanged.emit()
  33. def getVisibilityPresetById(self, item_id: str) -> Optional[SettingVisibilityPreset]:
  34. result = None
  35. for item in self._items:
  36. if item.presetId == item_id:
  37. result = item
  38. break
  39. return result
  40. def _populate(self) -> None:
  41. from cura.CuraApplication import CuraApplication
  42. items = [] # type: List[SettingVisibilityPreset]
  43. custom_preset = SettingVisibilityPreset(preset_id="custom", name ="Custom selection", weight = -100)
  44. items.append(custom_preset)
  45. for file_path in Resources.getAllResourcesOfType(CuraApplication.ResourceTypes.SettingVisibilityPreset):
  46. setting_visibility_preset = SettingVisibilityPreset()
  47. try:
  48. setting_visibility_preset.loadFromFile(file_path)
  49. except Exception:
  50. Logger.logException("e", "Failed to load setting preset %s", file_path)
  51. items.append(setting_visibility_preset)
  52. # Sort them on weight (and if that fails, use ID)
  53. items.sort(key = lambda k: (int(k.weight), k.presetId))
  54. self.setItems(items)
  55. @pyqtProperty("QVariantList", notify = onItemsChanged)
  56. def items(self):
  57. return self._items
  58. def setItems(self, items: List[SettingVisibilityPreset]) -> None:
  59. if self._items != items:
  60. self._items = items
  61. self.onItemsChanged.emit()
  62. @pyqtSlot(str)
  63. def setActivePreset(self, preset_id: str) -> None:
  64. if preset_id == self._active_preset_item.presetId:
  65. Logger.log("d", "Same setting visibility preset [%s] selected, do nothing.", preset_id)
  66. return
  67. preset_item = self.getVisibilityPresetById(preset_id)
  68. if preset_item is None:
  69. Logger.log("w", "Tried to set active preset to unknown id [%s]", preset_id)
  70. return
  71. need_to_save_to_custom = self._active_preset_item.presetId == "custom" and preset_id != "custom"
  72. if need_to_save_to_custom:
  73. # Save the current visibility settings to custom
  74. current_visibility_string = self._preferences.getValue("general/visible_settings")
  75. if current_visibility_string:
  76. self._preferences.setValue("cura/custom_visible_settings", current_visibility_string)
  77. new_visibility_string = ";".join(preset_item.settings)
  78. if preset_id == "custom":
  79. # Get settings from the stored custom data
  80. new_visibility_string = self._preferences.getValue("cura/custom_visible_settings")
  81. if new_visibility_string is None:
  82. new_visibility_string = self._preferences.getValue("general/visible_settings")
  83. self._preferences.setValue("general/visible_settings", new_visibility_string)
  84. self._preferences.setValue("cura/active_setting_visibility_preset", preset_id)
  85. self._active_preset_item = preset_item
  86. self.activePresetChanged.emit()
  87. @pyqtProperty(str, notify = activePresetChanged)
  88. def activePreset(self) -> str:
  89. return self._active_preset_item.presetId
  90. def _onPreferencesChanged(self, name: str) -> None:
  91. if name != "general/visible_settings":
  92. return
  93. # Find the preset that matches with the current visible settings setup
  94. visibility_string = self._preferences.getValue("general/visible_settings")
  95. if not visibility_string:
  96. return
  97. visibility_set = set(visibility_string.split(";"))
  98. matching_preset_item = None
  99. for item in self._items:
  100. if item.presetId == "custom":
  101. continue
  102. if set(item.settings) == visibility_set:
  103. matching_preset_item = item
  104. break
  105. item_to_set = self._active_preset_item
  106. if matching_preset_item is None:
  107. # The new visibility setup is "custom" should be custom
  108. if self._active_preset_item is None or self._active_preset_item.presetId == "custom":
  109. # We are already in custom, just save the settings
  110. self._preferences.setValue("cura/custom_visible_settings", visibility_string)
  111. else:
  112. # We need to move to custom preset.
  113. item_to_set = self.getVisibilityPresetById("custom")
  114. else:
  115. item_to_set = matching_preset_item
  116. if self._active_preset_item is None or self._active_preset_item.presetId != item_to_set.presetId:
  117. self._active_preset_item = item_to_set
  118. self._preferences.setValue("cura/active_setting_visibility_preset", self._active_preset_item.presetId)
  119. self.activePresetChanged.emit()