SettingVisibilityPreset.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import os
  2. import urllib.parse
  3. from configparser import ConfigParser
  4. from typing import List
  5. from PyQt6.QtCore import pyqtProperty, QObject, pyqtSignal
  6. from UM.Logger import Logger
  7. from UM.MimeTypeDatabase import MimeTypeDatabase
  8. class SettingVisibilityPreset(QObject):
  9. onSettingsChanged = pyqtSignal()
  10. onNameChanged = pyqtSignal()
  11. onWeightChanged = pyqtSignal()
  12. onIdChanged = pyqtSignal()
  13. def __init__(self, preset_id: str = "", name: str = "", weight: int = 0, parent = None) -> None:
  14. super().__init__(parent)
  15. self._settings = [] # type: List[str]
  16. self._id = preset_id
  17. self._weight = weight
  18. self._name = name
  19. @pyqtProperty("QStringList", notify = onSettingsChanged)
  20. def settings(self) -> List[str]:
  21. return self._settings
  22. @pyqtProperty(str, notify = onIdChanged)
  23. def presetId(self) -> str:
  24. return self._id
  25. @pyqtProperty(int, notify = onWeightChanged)
  26. def weight(self) -> int:
  27. return self._weight
  28. @pyqtProperty(str, notify = onNameChanged)
  29. def name(self) -> str:
  30. return self._name
  31. def setName(self, name: str) -> None:
  32. if name != self._name:
  33. self._name = name
  34. self.onNameChanged.emit()
  35. def setId(self, id: str) -> None:
  36. if id != self._id:
  37. self._id = id
  38. self.onIdChanged.emit()
  39. def setWeight(self, weight: int) -> None:
  40. if weight != self._weight:
  41. self._weight = weight
  42. self.onWeightChanged.emit()
  43. def setSettings(self, settings: List[str]) -> None:
  44. if set(settings) != set(self._settings):
  45. self._settings = list(set(settings)) # filter out non unique
  46. self.onSettingsChanged.emit()
  47. # Load a preset from file. We expect a file that can be parsed by means of the config parser.
  48. # The sections indicate the categories and the parameters placed in it (which don't need values) are the settings
  49. # that should be considered visible.
  50. def loadFromFile(self, file_path: str) -> None:
  51. mime_type = MimeTypeDatabase.getMimeTypeForFile(file_path)
  52. item_id = urllib.parse.unquote_plus(mime_type.stripExtension(os.path.basename(file_path)))
  53. if not os.path.isfile(file_path):
  54. Logger.log("e", "[%s] is not a file", file_path)
  55. return None
  56. parser = ConfigParser(interpolation = None, allow_no_value = True) # Accept options without any value,
  57. parser.read([file_path])
  58. if not parser.has_option("general", "name") or not parser.has_option("general", "weight"):
  59. return None
  60. settings = [] # type: List[str]
  61. for section in parser.sections():
  62. if section == "general":
  63. continue
  64. settings.append(section)
  65. for option in parser[section].keys():
  66. settings.append(option)
  67. self.setSettings(settings)
  68. self.setId(item_id)
  69. self.setName(parser["general"]["name"])
  70. self.setWeight(int(parser["general"]["weight"]))