ChangeLog.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.i18n import i18nCatalog
  4. from UM.Extension import Extension
  5. from UM.Application import Application
  6. from UM.PluginRegistry import PluginRegistry
  7. from UM.Version import Version
  8. from PyQt5.QtCore import pyqtSlot, QObject
  9. import os.path
  10. import collections
  11. catalog = i18nCatalog("cura")
  12. class ChangeLog(Extension, QObject,):
  13. def __init__(self, parent = None):
  14. QObject.__init__(self, parent)
  15. Extension.__init__(self)
  16. self._changelog_window = None
  17. self._changelog_context = None
  18. version_string = Application.getInstance().getVersion()
  19. if version_string is not "master":
  20. self._current_app_version = Version(version_string)
  21. else:
  22. self._current_app_version = None
  23. self._change_logs = None
  24. Application.getInstance().engineCreatedSignal.connect(self._onEngineCreated)
  25. Application.getInstance().getPreferences().addPreference("general/latest_version_changelog_shown", "2.0.0") #First version of CURA with uranium
  26. self.addMenuItem(catalog.i18nc("@item:inmenu", "Show Changelog"), self.showChangelog)
  27. def getChangeLogs(self):
  28. if not self._change_logs:
  29. self.loadChangeLogs()
  30. return self._change_logs
  31. @pyqtSlot(result = str)
  32. def getChangeLogString(self):
  33. logs = self.getChangeLogs()
  34. result = ""
  35. for version in logs:
  36. result += "<h1>" + str(version) + "</h1><br>"
  37. result += ""
  38. for change in logs[version]:
  39. if str(change) != "":
  40. result += "<b>" + str(change) + "</b><br>"
  41. for line in logs[version][change]:
  42. result += str(line) + "<br>"
  43. result += "<br>"
  44. pass
  45. return result
  46. def loadChangeLogs(self):
  47. self._change_logs = collections.OrderedDict()
  48. with open(os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), "ChangeLog.txt"), "r", encoding = "utf-8") as f:
  49. open_version = None
  50. open_header = "" # Initialise to an empty header in case there is no "*" in the first line of the changelog
  51. for line in f:
  52. line = line.replace("\n","")
  53. if "[" in line and "]" in line:
  54. line = line.replace("[","")
  55. line = line.replace("]","")
  56. open_version = Version(line)
  57. open_header = ""
  58. self._change_logs[open_version] = collections.OrderedDict()
  59. elif line.startswith("*"):
  60. open_header = line.replace("*","")
  61. self._change_logs[open_version][open_header] = []
  62. elif line != "":
  63. if open_header not in self._change_logs[open_version]:
  64. self._change_logs[open_version][open_header] = []
  65. self._change_logs[open_version][open_header].append(line)
  66. def _onEngineCreated(self):
  67. if not self._current_app_version:
  68. return #We're on dev branch.
  69. if Application.getInstance().getPreferences().getValue("general/latest_version_changelog_shown") == "master":
  70. latest_version_shown = Version("0.0.0")
  71. else:
  72. latest_version_shown = Version(Application.getInstance().getPreferences().getValue("general/latest_version_changelog_shown"))
  73. Application.getInstance().getPreferences().setValue("general/latest_version_changelog_shown", Application.getInstance().getVersion())
  74. # Do not show the changelog when there is no global container stack
  75. # This implies we are running Cura for the first time.
  76. if not Application.getInstance().getGlobalContainerStack():
  77. return
  78. if self._current_app_version > latest_version_shown:
  79. self.showChangelog()
  80. def showChangelog(self):
  81. if not self._changelog_window:
  82. self.createChangelogWindow()
  83. self._changelog_window.show()
  84. def hideChangelog(self):
  85. if self._changelog_window:
  86. self._changelog_window.hide()
  87. def createChangelogWindow(self):
  88. path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), "ChangeLog.qml")
  89. self._changelog_window = Application.getInstance().createQmlComponent(path, {"manager": self})