SettingVisibilityPresetsModel.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. import urllib
  5. from configparser import ConfigParser
  6. from PyQt5.QtCore import pyqtProperty, Qt, pyqtSignal, pyqtSlot, QUrl
  7. from UM.Logger import Logger
  8. from UM.Qt.ListModel import ListModel
  9. from UM.Preferences import Preferences
  10. from UM.Resources import Resources
  11. from UM.MimeTypeDatabase import MimeTypeDatabase, MimeTypeNotFoundError
  12. import cura.CuraApplication
  13. class SettingVisibilityPresetsModel(ListModel):
  14. IdRole = Qt.UserRole + 1
  15. NameRole = Qt.UserRole + 2
  16. SettingsRole = Qt.UserRole + 4
  17. def __init__(self, parent = None):
  18. super().__init__(parent)
  19. self.addRoleName(self.IdRole, "id")
  20. self.addRoleName(self.NameRole, "name")
  21. self.addRoleName(self.SettingsRole, "settings")
  22. self._populate()
  23. self._preferences = Preferences.getInstance()
  24. self._preferences.addPreference("cura/active_setting_visibility_preset", "custom") # Preference to store which preset is currently selected
  25. self._preferences.addPreference("cura/custom_visible_settings", "") # Preference that stores the "custom" set so it can always be restored (even after a restart)
  26. self._preferences.preferenceChanged.connect(self._onPreferencesChanged)
  27. self._active_preset = self._preferences.getValue("cura/active_setting_visibility_preset")
  28. if self.find("id", self._active_preset) < 0:
  29. self._active_preset = "custom"
  30. self.activePresetChanged.emit()
  31. def _populate(self):
  32. items = []
  33. for item in Resources.getAllResourcesOfType(cura.CuraApplication.CuraApplication.ResourceTypes.SettingVisibilityPreset):
  34. try:
  35. mime_type = MimeTypeDatabase.getMimeTypeForFile(item)
  36. except MimeTypeNotFoundError:
  37. Logger.log("e", "Could not determine mime type of file %s", item)
  38. continue
  39. id = urllib.parse.unquote_plus(mime_type.stripExtension(os.path.basename(item)))
  40. if not os.path.isfile(item):
  41. continue
  42. parser = ConfigParser(allow_no_value=True) # accept options without any value,
  43. try:
  44. parser.read([item])
  45. if not parser.has_option("general", "name") and not parser.has_option("general", "weight"):
  46. continue
  47. settings = []
  48. for section in parser.sections():
  49. if section == 'general':
  50. continue
  51. settings.append(section)
  52. for option in parser[section].keys():
  53. settings.append(option)
  54. items.append({
  55. "id": id,
  56. "name": parser["general"]["name"],
  57. "weight": parser["general"]["weight"],
  58. "settings": settings
  59. })
  60. except Exception as e:
  61. Logger.log("e", "Failed to load setting preset %s: %s", file_path, str(e))
  62. items.sort(key = lambda k: (k["weight"], k["id"]))
  63. self.setItems(items)
  64. @pyqtSlot(str)
  65. def setActivePreset(self, preset_id):
  66. if preset_id != "custom" and self.find("id", preset_id) == -1:
  67. Logger.log("w", "Tried to set active preset to unknown id %s", preset_id)
  68. return
  69. if preset_id == "custom" and self._active_preset == "custom":
  70. # Copy current visibility set to custom visibility set preference so it can be restored later
  71. visibility_string = self._preferences.getValue("general/visible_settings")
  72. self._preferences.setValue("cura/custom_visible_settings", visibility_string)
  73. self._preferences.setValue("cura/active_setting_visibility_preset", preset_id)
  74. self._active_preset = preset_id
  75. self.activePresetChanged.emit()
  76. activePresetChanged = pyqtSignal()
  77. @pyqtProperty(str, notify = activePresetChanged)
  78. def activePreset(self):
  79. return self._active_preset
  80. def _onPreferencesChanged(self, name):
  81. if name != "general/visible_settings":
  82. return
  83. if self._active_preset != "custom":
  84. return
  85. # Copy current visibility set to custom visibility set preference so it can be restored later
  86. visibility_string = self._preferences.getValue("general/visible_settings")
  87. self._preferences.setValue("cura/custom_visible_settings", visibility_string)
  88. # Factory function, used by QML
  89. @staticmethod
  90. def createSettingVisibilityPresetsModel(engine, js_engine):
  91. return SettingVisibilityPresetsModel.getInstance()
  92. ## Get the singleton instance for this class.
  93. @classmethod
  94. def getInstance(cls) -> "SettingVisibilityPresetsModel":
  95. # Note: Explicit use of class name to prevent issues with inheritance.
  96. if not SettingVisibilityPresetsModel.__instance:
  97. SettingVisibilityPresetsModel.__instance = cls()
  98. return SettingVisibilityPresetsModel.__instance
  99. __instance = None # type: "SettingVisibilityPresetsModel"