SettingVisibilityPresetsModel.py 7.4 KB

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