DigitalFactoryProjectModel.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import List, Optional
  4. from PyQt6.QtCore import Qt, pyqtSignal
  5. from UM.Qt.ListModel import ListModel
  6. from .DigitalFactoryProjectResponse import DigitalFactoryProjectResponse
  7. PROJECT_UPDATED_AT_DATETIME_FORMAT = "%d-%m-%Y"
  8. class DigitalFactoryProjectModel(ListModel):
  9. DisplayNameRole = Qt.ItemDataRole.UserRole + 1
  10. LibraryProjectIdRole = Qt.ItemDataRole.UserRole + 2
  11. DescriptionRole = Qt.ItemDataRole.UserRole + 3
  12. ThumbnailUrlRole = Qt.ItemDataRole.UserRole + 5
  13. UsernameRole = Qt.ItemDataRole.UserRole + 6
  14. LastUpdatedRole = Qt.ItemDataRole.UserRole + 7
  15. dfProjectModelChanged = pyqtSignal()
  16. def __init__(self, parent = None) -> None:
  17. super().__init__(parent)
  18. self.addRoleName(self.DisplayNameRole, "displayName")
  19. self.addRoleName(self.LibraryProjectIdRole, "libraryProjectId")
  20. self.addRoleName(self.DescriptionRole, "description")
  21. self.addRoleName(self.ThumbnailUrlRole, "thumbnailUrl")
  22. self.addRoleName(self.UsernameRole, "username")
  23. self.addRoleName(self.LastUpdatedRole, "lastUpdated")
  24. self._projects = [] # type: List[DigitalFactoryProjectResponse]
  25. def setProjects(self, df_projects: List[DigitalFactoryProjectResponse]) -> None:
  26. if self._projects == df_projects:
  27. return
  28. self._items.clear()
  29. self._projects = df_projects
  30. # self.sortProjectsBy("display_name")
  31. self._update(df_projects)
  32. def extendProjects(self, df_projects: List[DigitalFactoryProjectResponse]) -> None:
  33. if not df_projects:
  34. return
  35. self._projects.extend(df_projects)
  36. # self.sortProjectsBy("display_name")
  37. self._update(df_projects)
  38. def clearProjects(self) -> None:
  39. self.clear()
  40. self._projects.clear()
  41. self.dfProjectModelChanged.emit()
  42. def _update(self, df_projects: List[DigitalFactoryProjectResponse]) -> None:
  43. for project in df_projects:
  44. self.appendItem({
  45. "displayName" : project.display_name,
  46. "libraryProjectId" : project.library_project_id,
  47. "description": project.description,
  48. "thumbnailUrl": project.thumbnail_url,
  49. "username": project.username,
  50. "lastUpdated": project.last_updated.strftime(PROJECT_UPDATED_AT_DATETIME_FORMAT) if project.last_updated else "",
  51. })
  52. self.dfProjectModelChanged.emit()