SettingVisibilityPresetsModel.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # Sort them on weight (and if that fails, use ID)
  59. items.sort(key = lambda k: (int(k.weight), k.presetId))
  60. self.setItems(items)
  61. @pyqtProperty("QVariantList", notify = onItemsChanged)
  62. def items(self) -> List[SettingVisibilityPreset]:
  63. return self._items
  64. def setItems(self, items: List[SettingVisibilityPreset]) -> None:
  65. if self._items != items:
  66. self._items = items
  67. self.onItemsChanged.emit()
  68. @pyqtSlot(str)
  69. def setActivePreset(self, preset_id: str) -> None:
  70. if self._active_preset_item is not None and preset_id == self._active_preset_item.presetId:
  71. Logger.log("d", "Same setting visibility preset [%s] selected, do nothing.", preset_id)
  72. return
  73. preset_item = self.getVisibilityPresetById(preset_id)
  74. if preset_item is None:
  75. Logger.log("w", "Tried to set active preset to unknown id [%s]", preset_id)
  76. return
  77. need_to_save_to_custom = self._active_preset_item is None or (self._active_preset_item.presetId == "custom" and preset_id != "custom")
  78. if need_to_save_to_custom:
  79. # Save the current visibility settings to custom
  80. current_visibility_string = self._preferences.getValue("general/visible_settings")
  81. if current_visibility_string:
  82. self._preferences.setValue("cura/custom_visible_settings", current_visibility_string)
  83. new_visibility_string = ";".join(preset_item.settings)
  84. if preset_id == "custom":
  85. # Get settings from the stored custom data
  86. new_visibility_string = self._preferences.getValue("cura/custom_visible_settings")
  87. if new_visibility_string is None:
  88. new_visibility_string = self._preferences.getValue("general/visible_settings")
  89. self._preferences.setValue("general/visible_settings", new_visibility_string)
  90. self._preferences.setValue("cura/active_setting_visibility_preset", preset_id)
  91. self._active_preset_item = preset_item
  92. self.activePresetChanged.emit()
  93. @pyqtProperty(str, notify = activePresetChanged)
  94. def activePreset(self) -> str:
  95. if self._active_preset_item is not None:
  96. return self._active_preset_item.presetId
  97. return ""
  98. def _onPreferencesChanged(self, name: str) -> None:
  99. if name != "general/visible_settings":
  100. return
  101. # Find the preset that matches with the current visible settings setup
  102. visibility_string = self._preferences.getValue("general/visible_settings")
  103. if not visibility_string:
  104. return
  105. visibility_set = set(visibility_string.split(";"))
  106. matching_preset_item = None
  107. for item in self._items:
  108. if item.presetId == "custom":
  109. continue
  110. if set(item.settings) == visibility_set:
  111. matching_preset_item = item
  112. break
  113. item_to_set = self._active_preset_item
  114. if matching_preset_item is None:
  115. # The new visibility setup is "custom" should be custom
  116. if self._active_preset_item is None or self._active_preset_item.presetId == "custom":
  117. # We are already in custom, just save the settings
  118. self._preferences.setValue("cura/custom_visible_settings", visibility_string)
  119. else:
  120. # We need to move to custom preset.
  121. item_to_set = self.getVisibilityPresetById("custom")
  122. else:
  123. item_to_set = matching_preset_item
  124. # If we didn't find a matching preset, fallback to custom.
  125. if item_to_set is None:
  126. item_to_set = self._custom_preset
  127. if self._active_preset_item is None or self._active_preset_item.presetId != item_to_set.presetId:
  128. self._active_preset_item = item_to_set
  129. if self._active_preset_item is not None:
  130. self._preferences.setValue("cura/active_setting_visibility_preset", self._active_preset_item.presetId)
  131. self.activePresetChanged.emit()