PStackedWidget.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from PySide6.QtWidgets import QStackedWidget, QWidget, QLabel, QGridLayout
  2. from PySide6.QtGui import QResizeEvent, QPixmap, QImage
  3. from PySide6.QtCore import QSize
  4. from PySide6.QtMultimediaWidgets import QVideoWidget
  5. from module.cam.USBCam import load_image
  6. import os
  7. class PStackedWidget(QStackedWidget):
  8. def __init__(self, parent=None, mode='video'):
  9. super(PStackedWidget, self).__init__(parent)
  10. self.__mode = mode
  11. self.resize_state = False
  12. self.__setup_image_stacked()
  13. self.__setup_video_stacked()
  14. self.set_default_image()
  15. self.resizeEvent = self.__resize
  16. def __resize(self, event: QResizeEvent) -> None:
  17. self.image.resize(event.size())
  18. self.video.resize(event.size())
  19. self.image.setPixmap(QPixmap.fromImage(
  20. self.__scaled_image(self.image, event)
  21. ))
  22. if self.__mode not in 'video':
  23. self.video.setPixmap(QPixmap.fromImage(
  24. self.__scaled_image(self.video, event)
  25. ))
  26. # print(type(self.video))
  27. def __setup_image_stacked(self) -> None:
  28. self.page_image = QWidget()
  29. self.page_image.setObjectName("page_image")
  30. self.layout_image = QGridLayout(self.page_image)
  31. self.layout_image.setObjectName("layout_image")
  32. self.image = QLabel(self.page_image)
  33. self.image.setObjectName("image")
  34. self.layout_image.addWidget(self.image)
  35. self.addWidget(self.page_image)
  36. def __setup_video_stacked(self) -> None:
  37. self.page_video = QWidget()
  38. self.page_video.setObjectName("page_video")
  39. self.layout_video = QGridLayout(self.page_video)
  40. self.layout_video.setObjectName("layout_video")
  41. if self.__mode == 'video':
  42. self.video = QVideoWidget(self.page_video)
  43. else:
  44. self.video = QLabel(self.page_video)
  45. self.video.setObjectName("video")
  46. self.layout_video.addWidget(self.video)
  47. self.addWidget(self.page_video)
  48. def set_default_image(self) -> None:
  49. load_image(self.image, os.environ.get("NO_MEDIA_IMAGE"))
  50. def to_image(self) -> None:
  51. self.setCurrentIndex(0)
  52. def to_video(self) -> None:
  53. self.setCurrentIndex(1)
  54. def __scaled_image(self, label: QLabel, event: QResizeEvent) -> QImage:
  55. return label.pixmap().toImage().scaled(
  56. event.size().width()-20,
  57. event.size().height()-20
  58. )