KikkaShell.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import asyncio
  2. from PySide6.QtCore import Qt, Signal, QPoint
  3. from PySide6.QtWidgets import QWidget, QVBoxLayout, QSplashScreen
  4. from qframelesswindow import FramelessWindow
  5. from Kikka.KikkaConst import *
  6. from Kikka.Utils.Singleton import singleton
  7. @singleton
  8. class KikkaShell():
  9. def __init__(self):
  10. self.init()
  11. self.shellWindow = None
  12. def init(self):
  13. pass
  14. def loadShell(self, data):
  15. async def _async_load_start(data):
  16. future = asyncio.ensure_future(async_load(data))
  17. future.add_done_callback(_loaded)
  18. await asyncio.gather(future)
  19. async def async_load(data):
  20. await asyncio.sleep(1)
  21. shell_type = data.get("shell_type")
  22. if shell_type is None:
  23. return None
  24. elif shell_type == "Image":
  25. from .ImageShellWindow import ImageShellWindow
  26. shellWindow = ImageShellWindow()
  27. shellWindow.loadData(data)
  28. return shellWindow
  29. return None
  30. def _loaded(future):
  31. shellWindow = future.result()
  32. self.shellWindow = shellWindow
  33. self.show()
  34. asyncio.run(_async_load_start(data))
  35. def onKikkaOperation(self, operation, param):
  36. pass
  37. def switchShell(self, new_shell):
  38. if self.shellWindow is not None:
  39. self.shellWindow.deleteLater()
  40. self.shellWindow = new_shell
  41. def show(self):
  42. self.shellWindow.show()
  43. class ShellBase(QSplashScreen):
  44. SIGNAL_SHELL_OPERATION = Signal(EShellOperation, dict)
  45. def __init__(self, parent=None):
  46. super().__init__(parent)
  47. self._isMoving = False
  48. self.init()
  49. def init(self):
  50. self.setWindowFlags(self.windowFlags() | Qt.WindowType.WindowStaysOnTopHint)
  51. self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
  52. self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
  53. self.setMouseTracking(True)
  54. self.setAcceptDrops(True)
  55. def loadData(self, data):
  56. raise NotImplemented
  57. def onKikkaOperation(self, operation, param):
  58. raise NotImplemented
  59. def mousePressEvent(self, event):
  60. self._movepos = event.globalPos() - self.pos()
  61. if event.buttons() == Qt.MouseButton.LeftButton:
  62. self._isMoving = True
  63. event.accept()
  64. def mouseMoveEvent(self, event):
  65. # self._mouseLogging("mouseMoveEvent", event.buttons(), event.globalPos().x(), event.globalPos().y())
  66. self._mousepos = event.pos()
  67. if self._isMoving and event.buttons() == Qt.MouseButton.LeftButton:
  68. self.move(event.globalPos() - self._movepos)
  69. event.accept()
  70. else:
  71. self._isMoving = False
  72. def mouseReleaseEvent(self, event):
  73. self._isMoving = False
  74. def mouseDoubleClickEvent(self, event):
  75. if event.buttons() == Qt.MouseButton.LeftButton:
  76. self._isMoving = False
  77. def wheelEvent(self, event):
  78. pass
  79. def dragEnterEvent(self, event):
  80. event.accept()
  81. def dropEvent(self, event):
  82. urls = event.mimeData().urls()
  83. def move(self, *__args):
  84. if len(__args) == 1 and isinstance(__args[0], QPoint):
  85. x = __args[0].x()
  86. y = __args[0].y()
  87. elif len(__args) == 2 and isinstance(__args[0], int) and isinstance(__args[1], int):
  88. x = __args[0]
  89. y = __args[1]
  90. else:
  91. super().move(*__args)
  92. return
  93. super().move(x, y)