ChangeLog.py 4.6 KB

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