CuraSplashScreen.py 4.4 KB

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