DialogSettingCam.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from .ui_py.ui_DialogSettingCam import Ui_DialogSettingCam
  2. from PySide6.QtWidgets import QDialog
  3. from PySide6.QtCore import Qt
  4. from module.WorkWithDB import *
  5. from PropuskWidgets.PCamChecked import PCamChecked
  6. from logger import logger
  7. class SettingCam(QDialog, Ui_DialogSettingCam):
  8. def __init__(self, parent=None) -> None:
  9. super(SettingCam, self).__init__(parent)
  10. self.setAttribute(Qt.WA_DeleteOnClose)
  11. self.setupUi(self)
  12. self.save_setting_cam.clicked.connect(self.__save_cams)
  13. self.mode_list.currentTextChanged.connect(self.__change_mode)
  14. # self.__recreate_widget()
  15. self.__load_cams_from_db()
  16. self.__mode = self.mode_list.currentData()
  17. def __change_mode(self) -> None:
  18. self.__mode = self.mode_list.currentData()
  19. self.__recreate_widget(self.__mode)
  20. logger.info(F'Change cams to {self.__mode}')
  21. def __recreate_widget(self, select_mode: str = 'video') -> None:
  22. if hasattr(self, 'widget'):
  23. delattr(self, 'widget')
  24. self.widget = PCamChecked(self.groupBox, mode=select_mode)
  25. self.widget.setObjectName('widget')
  26. self.gridLayout_3.addWidget(self.widget, 1, 0, 1, 1)
  27. def __load_cams_from_db(self):
  28. with connect() as conn:
  29. result = conn.execute(cam_setting.select()).fetchone()
  30. if result:
  31. self.__recreate_widget(result.mode)
  32. match result.mode:
  33. case 'video': self.widget.line_cam.setCurrentText(result.mode)
  34. case 'snapshot': self.widget.line_cam.setText(result.selected_cam)
  35. else: self.__recreate_widget()
  36. def __save_cams(self) -> None:
  37. query = None
  38. with connect() as conn:
  39. result = conn.execute(cam_setting.select()).fetchone()
  40. if result is None:
  41. query = cam_setting.insert().values(
  42. mode=self.__mode, selected_cam=self.widget.get_value())
  43. else:
  44. query = cam_setting.update().where(
  45. cam_setting.c.id == 1).values(mode=self.__mode, selected_cam=self.widget.get_value())
  46. result = conn.execute(query)