PStackedWidget.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from PySide6.QtWidgets import QStackedWidget, QWidget, QLabel, QGridLayout
  2. from PySide6.QtGui import QResizeEvent, QPixmap
  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. img = self.image.pixmap().toImage().scaled(
  20. event.size().width()-20,
  21. event.size().height()-20
  22. )
  23. self.image.setPixmap(QPixmap.fromImage(img))
  24. if self.video.__class__.__name__ in 'QLabel':
  25. img = self.video.pixmap().toImage().scaled(
  26. event.size().width()-20,
  27. event.size().height()-20
  28. )
  29. self.video.setPixmap(QPixmap.fromImage(img))
  30. # print(type(self.video))
  31. def __setup_image_stacked(self) -> None:
  32. self.page_image = QWidget()
  33. self.page_image.setObjectName("page_image")
  34. self.layout_image = QGridLayout(self.page_image)
  35. self.layout_image.setObjectName("layout_image")
  36. self.image = QLabel(self.page_image)
  37. self.image.setObjectName("image")
  38. self.layout_image.addWidget(self.image)
  39. self.addWidget(self.page_image)
  40. def __setup_video_stacked(self) -> None:
  41. self.page_video = QWidget()
  42. self.page_video.setObjectName("page_video")
  43. self.layout_video = QGridLayout(self.page_video)
  44. self.layout_video.setObjectName("layout_video")
  45. if self.__mode == 'video':
  46. self.video = QVideoWidget(self.page_video)
  47. else:
  48. self.video = QLabel(self.page_video)
  49. self.video.setObjectName("video")
  50. self.layout_video.addWidget(self.video)
  51. self.addWidget(self.page_video)
  52. def set_default_image(self) -> None:
  53. load_image(self.image, os.environ.get("NO_MEDIA_IMAGE"))
  54. def to_image(self) -> None:
  55. self.setCurrentIndex(0)
  56. def to_video(self) -> None:
  57. self.setCurrentIndex(1)
  58. # def cupture_image(self) -> None:
  59. # if self.__mode == 'video':
  60. # self.video = self.video.cupture_image(self.image)
  61. # else:
  62. # self.video = self.video.cupture_image(self.image)