DigitalFactoryProjectModel.py 2.5 KB

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