CuraSplashScreen.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Uranium is released under the terms of the LGPLv3 or higher.
  3. import math
  4. from PyQt6.QtCore import Qt, QCoreApplication, QTimer
  5. from PyQt6.QtGui import QPixmap, QColor, QFont, QPen, QPainter
  6. from PyQt6.QtWidgets import QSplashScreen
  7. from UM.Resources import Resources
  8. from UM.Application import Application
  9. from cura import ApplicationMetadata
  10. import time
  11. class CuraSplashScreen(QSplashScreen):
  12. def __init__(self):
  13. super().__init__()
  14. self._scale = 1
  15. self._version_y_offset = 0 # when extra visual elements are in the background image, move version text down
  16. if ApplicationMetadata.IsAlternateVersion:
  17. splash_image = QPixmap(Resources.getPath(Resources.Images, "cura_wip.png"))
  18. elif ApplicationMetadata.IsEnterpriseVersion:
  19. splash_image = QPixmap(Resources.getPath(Resources.Images, "cura_enterprise.png"))
  20. self._version_y_offset = 26
  21. else:
  22. splash_image = QPixmap(Resources.getPath(Resources.Images, "cura.png"))
  23. self.setPixmap(splash_image)
  24. self._current_message = ""
  25. self._loading_image_rotation_angle = 0
  26. self._to_stop = False
  27. self._change_timer = QTimer()
  28. self._change_timer.setInterval(50)
  29. self._change_timer.setSingleShot(False)
  30. self._change_timer.timeout.connect(self.updateLoadingImage)
  31. self._last_update_time = None
  32. def show(self):
  33. super().show()
  34. self._last_update_time = time.time()
  35. self._change_timer.start()
  36. def updateLoadingImage(self):
  37. if self._to_stop:
  38. return
  39. time_since_last_update = time.time() - self._last_update_time
  40. self._last_update_time = time.time()
  41. # Since we don't know how much time actually passed, check how many intervals of 50 we had.
  42. self._loading_image_rotation_angle -= 10 * (time_since_last_update * 1000 / 50)
  43. self._loading_image_rotation_angle = math.fmod(self._loading_image_rotation_angle, 360)
  44. self.repaint()
  45. # Override the mousePressEvent so the splashscreen doesn't disappear when clicked
  46. def mousePressEvent(self, mouse_event):
  47. pass
  48. def drawContents(self, painter):
  49. if self._to_stop:
  50. return
  51. painter.save()
  52. painter.setPen(QColor(255, 255, 255, 255))
  53. painter.setRenderHint(QPainter.RenderHint.Antialiasing)
  54. painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
  55. version = Application.getInstance().getVersion().split("-")
  56. # Draw version text
  57. font = QFont()
  58. font.setPixelSize(24)
  59. painter.setFont(font)
  60. if len(version) == 1:
  61. painter.drawText(40, 104 + self._version_y_offset, round(330 * self._scale), round(230 * self._scale), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop, version[0] if not ApplicationMetadata.IsAlternateVersion else ApplicationMetadata.CuraBuildType)
  62. elif len(version) > 1:
  63. painter.drawText(40, 104 + self._version_y_offset, round(330 * self._scale), round(230 * self._scale), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop, f"{version[0]}-{version[1]}" if not ApplicationMetadata.IsAlternateVersion else ApplicationMetadata.CuraBuildType)
  64. # Draw the loading image
  65. pen = QPen()
  66. pen.setWidthF(2 * self._scale)
  67. pen.setColor(QColor(255, 255, 255, 255))
  68. painter.setPen(pen)
  69. painter.drawArc(38, 324, round(20 * self._scale), round(20 * self._scale), round(self._loading_image_rotation_angle * 16), 300 * 16)
  70. # Draw message text
  71. if self._current_message:
  72. font = QFont() # Using system-default font here
  73. font.setPixelSize(13)
  74. pen = QPen()
  75. pen.setColor(QColor(255, 255, 255, 255))
  76. painter.setPen(pen)
  77. painter.setFont(font)
  78. painter.drawText(70, 308, 170, 48,
  79. Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextWordWrap,
  80. self._current_message)
  81. painter.restore()
  82. super().drawContents(painter)
  83. def showMessage(self, message, *args, **kwargs):
  84. if self._to_stop:
  85. return
  86. self._current_message = message
  87. self.messageChanged.emit(message)
  88. QCoreApplication.processEvents() # Used to be .flush() -- this might be the closest alternative, but uncertain.
  89. self.repaint()
  90. def close(self):
  91. # set stop flags
  92. self._to_stop = True
  93. self._change_timer.stop()
  94. super().close()