DialogSettingCam.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from .ui_py.ui_DialogSettingCam import Ui_DialogSettingCam
  2. from PySide6.QtWidgets import QDialog
  3. from PySide6.QtCore import Qt, Slot
  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.widget = None
  13. self.__mode = self.mode_list.currentData()
  14. self.__load_cams_from_db()
  15. self.save_setting_cam.clicked.connect(self.__save_cams)
  16. self.mode_list.currentIndexChanged.connect(self.__change_mode)
  17. self.tabWidget.currentChanged.connect(self.__change_type)
  18. # self.__recreate_widget()
  19. def __change_type(self) -> None:
  20. if self.tabWidget.currentIndex() == 1:
  21. self.__mode = 'document'
  22. self.__load_cams_from_db()
  23. def __change_mode(self) -> None:
  24. self.__mode = self.mode_list.currentData()
  25. self.__recreate_widget(self.__mode)
  26. logger.info(F'Change cams to {self.__mode}')
  27. @Slot()
  28. def __recreate_widget(self, select_mode: str = 'video') -> None:
  29. self.__del_widget()
  30. if self.tabWidget.currentIndex() == 0:
  31. self.__create_widget(
  32. mode=select_mode,
  33. tab=self.tab,
  34. layout=self.verticalLayout)
  35. else:
  36. self.__create_widget(
  37. tab=self.tab_2,
  38. layout=self.verticalLayout_3)
  39. def __create_widget(self, tab, layout, mode: str = 'video') -> None:
  40. self.widget = PCamChecked(tab, mode=mode)
  41. self.widget.setObjectName('widget')
  42. layout.addWidget(self.widget)
  43. def __del_widget(self) -> None:
  44. if self.widget is not None:
  45. self.verticalLayout.removeWidget(self.widget)
  46. self.verticalLayout_3.removeWidget(self.widget)
  47. self.widget = None
  48. def __load_cams_from_db(self):
  49. with connect() as conn:
  50. result = conn.execute(cam_setting.select().where(
  51. cam_setting.c.type == self.tabWidget.currentIndex()
  52. )).fetchone()
  53. if result:
  54. self.__mode = result.mode
  55. self.__recreate_widget(self.__mode)
  56. match self.__mode:
  57. case 'video':
  58. self.mode_list.setCurrentIndex(0)
  59. self.widget.line_cam.setCurrentText(
  60. result.selected_cam)
  61. case 'snapshot':
  62. self.mode_list.setCurrentIndex(1)
  63. self.widget.line_cam.setText(result.selected_cam)
  64. case 'document':
  65. self.widget.line_cam.setCurrentText(
  66. result.selected_cam)
  67. case _: ...
  68. else: self.__recreate_widget()
  69. def __save_cams(self) -> None:
  70. query = None
  71. with connect() as conn:
  72. result = conn.execute(cam_setting.select().where(
  73. cam_setting.c.type == self.tabWidget.currentIndex()
  74. )).fetchone()
  75. if result is None:
  76. query = cam_setting.insert().values(
  77. type=self.tabWidget.currentIndex(),
  78. mode=self.__mode,
  79. selected_cam=self.widget.get_value())
  80. else:
  81. query = cam_setting.update().where(
  82. cam_setting.c.type == self.tabWidget.currentIndex()
  83. ).values(mode=self.__mode, selected_cam=self.widget.get_value())
  84. result = conn.execute(query)