PStackedWidget.py 2.6 KB

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