SettingVisibilityPresetsModel.py 7.4 KB

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