WhatsNewPagesModel.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from .WelcomePagesModel import WelcomePagesModel
  4. import os
  5. from typing import Optional, Dict, List, Tuple
  6. from PyQt6.QtCore import pyqtProperty, pyqtSlot
  7. from UM.Logger import Logger
  8. from UM.Resources import Resources
  9. #
  10. # This Qt ListModel is more or less the same the WelcomePagesModel, except that this model is only for showing the
  11. # "what's new" page. This is also used in the "Help" menu to show the changes log.
  12. #
  13. class WhatsNewPagesModel(WelcomePagesModel):
  14. image_formats = [".png", ".jpg", ".jpeg", ".gif", ".svg"]
  15. text_formats = [".txt", ".htm", ".html"]
  16. image_key = "image"
  17. text_key = "text"
  18. @staticmethod
  19. def _collectOrdinalFiles(resource_type: int, include: List[str]) -> Tuple[Dict[int, str], int]:
  20. result = {} #type: Dict[int, str]
  21. highest = -1
  22. try:
  23. folder_path = Resources.getPath(resource_type, "whats_new")
  24. for _, _, files in os.walk(folder_path):
  25. for filename in files:
  26. basename = os.path.basename(filename)
  27. base, ext = os.path.splitext(basename)
  28. if ext.lower() not in include or not base.isdigit():
  29. continue
  30. page_no = int(base)
  31. highest = max(highest, page_no)
  32. result[page_no] = os.path.join(folder_path, filename)
  33. except FileNotFoundError:
  34. Logger.logException("w", "Could not find 'whats_new' folder for resource-type {0}".format(resource_type))
  35. return result, highest
  36. @staticmethod
  37. def _loadText(filename: str) -> str:
  38. result = ""
  39. try:
  40. with open(filename, "r", encoding="utf-8") as file:
  41. result = file.read()
  42. except OSError:
  43. Logger.logException("w", "Could not open {0}".format(filename))
  44. return result
  45. def initialize(self) -> None:
  46. self._pages = []
  47. self._pages.append({"id": "whats_new",
  48. "page_url": self._getBuiltinWelcomePagePath("WhatsNewContent.qml"),
  49. "next_page_button_text": self._catalog.i18nc("@action:button", "Skip"),
  50. "next_page_id": "changelog"
  51. })
  52. self._pages.append({"id": "changelog",
  53. "page_url": self._getBuiltinWelcomePagePath("ChangelogContent.qml"),
  54. "next_page_button_text": self._catalog.i18nc("@action:button", "Close"),
  55. })
  56. self.setItems(self._pages)
  57. images, max_image = WhatsNewPagesModel._collectOrdinalFiles(Resources.Images, WhatsNewPagesModel.image_formats)
  58. texts, max_text = WhatsNewPagesModel._collectOrdinalFiles(Resources.Texts, WhatsNewPagesModel.text_formats)
  59. highest = max(max_image, max_text)
  60. self._subpages = [] #type: List[Dict[str, Optional[str]]]
  61. for n in range(0, highest + 1):
  62. self._subpages.append({
  63. WhatsNewPagesModel.image_key: None if n not in images else images[n],
  64. WhatsNewPagesModel.text_key: None if n not in texts else self._loadText(texts[n])
  65. })
  66. if len(self._subpages) == 0:
  67. self._subpages.append({WhatsNewPagesModel.text_key: "~ There Is Nothing New Under The Sun ~"})
  68. def _getSubpageItem(self, page: int, item: str) -> Optional[str]:
  69. if 0 <= page < self.subpageCount and item in self._subpages[page]:
  70. return self._subpages[page][item]
  71. else:
  72. return None
  73. @pyqtProperty(int, constant = True)
  74. def subpageCount(self) -> int:
  75. return len(self._subpages)
  76. @pyqtSlot(int, result = str)
  77. def getSubpageImageSource(self, page: int) -> str:
  78. result = self._getSubpageItem(page, WhatsNewPagesModel.image_key)
  79. return "file:///" + (result if result else Resources.getPath(Resources.Images, "cura-icon.png"))
  80. @pyqtSlot(int, result = str)
  81. def getSubpageText(self, page: int) -> str:
  82. result = self._getSubpageItem(page, WhatsNewPagesModel.text_key)
  83. return result if result else "* * *"
  84. __all__ = ["WhatsNewPagesModel"]