DialogSettingCam.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from .ui_py.ui_DialogSettingCam import Ui_DialogSettingCam
  2. from PySide6.QtWidgets import QDialog
  3. from PySide6.QtCore import Qt, Slot
  4. from sqlalchemy.exc import OperationalError
  5. from module.WorkWithDB import *
  6. from widgets import create_widget_cam_shecked
  7. from logger import logger
  8. class DialogSettingCam(QDialog, Ui_DialogSettingCam):
  9. def __init__(self, parent=None) -> None:
  10. super(DialogSettingCam, self).__init__(parent)
  11. self.setAttribute(Qt.WA_DeleteOnClose)
  12. self.setupUi(self)
  13. self.widget = None
  14. self.__mode = self.mode_list.currentData()
  15. self.tabWidget.setCurrentIndex(0)
  16. self.__load_cams_from_db()
  17. self.save_setting_cam.clicked.connect(self.__save_cams)
  18. self.mode_list.currentIndexChanged.connect(self.__change_mode)
  19. self.tabWidget.currentChanged.connect(self.__change_type)
  20. # self.__recreate_widget()
  21. def __change_type(self) -> None:
  22. if self.tabWidget.currentIndex() == 1:
  23. self.__mode = 'document'
  24. else:
  25. self.__mode = 'video'
  26. self.__load_cams_from_db()
  27. def __change_mode(self) -> None:
  28. self.__mode = self.mode_list.currentData()
  29. self.__recreate_widget(self.__mode)
  30. logger.info(F'Change cams to {self.__mode}')
  31. @Slot()
  32. def __recreate_widget(self, select_mode: str = 'video') -> None:
  33. self.__del_widget()
  34. if self.tabWidget.currentIndex() == 0:
  35. self.widget = create_widget_cam_shecked(
  36. mode=select_mode,
  37. name_object=u'widget',
  38. obj=self.tab,
  39. layout=self.verticalLayout)
  40. else:
  41. self.widget = create_widget_cam_shecked(
  42. name_object=U'widget',
  43. obj=self.tab_2,
  44. layout=self.verticalLayout_3)
  45. def __del_widget(self) -> None:
  46. if self.widget is not None:
  47. try:
  48. self.widget.stop_cam()
  49. except AttributeError as er:
  50. logger.error(er)
  51. self.verticalLayout.removeWidget(self.widget)
  52. self.verticalLayout_3.removeWidget(self.widget)
  53. self.widget = None
  54. @check_error_sql
  55. def __load_cams_from_db(self):
  56. with connect() as conn:
  57. result = conn.execute(cam_setting.select().where(
  58. cam_setting.c.type == self.tabWidget.currentIndex()
  59. )).fetchone()
  60. if result:
  61. self.__mode = result.mode
  62. self.__recreate_widget(self.__mode)
  63. logger.info(F'select mode cam {self.__mode}')
  64. match self.__mode:
  65. case 'video':
  66. self.mode_list.setCurrentIndex(0)
  67. self.widget.line_cam.setCurrentText(
  68. result.selected_cam)
  69. case 'snapshot':
  70. self.mode_list.setCurrentIndex(1)
  71. self.widget.line_cam.setText(result.selected_cam)
  72. case 'document':
  73. self.widget.line_cam.setCurrentText(
  74. result.selected_cam)
  75. case _: ...
  76. else: self.__recreate_widget()
  77. @check_error_sql
  78. def __save_cams(self) -> None:
  79. query = None
  80. with connect() as conn:
  81. result = conn.execute(cam_setting.select().where(
  82. cam_setting.c.type == self.tabWidget.currentIndex()
  83. )).fetchone()
  84. if result is None:
  85. query = cam_setting.insert().values(
  86. type=self.tabWidget.currentIndex(),
  87. mode=self.__mode,
  88. selected_cam=self.widget.get_value())
  89. else:
  90. query = cam_setting.update().where(
  91. cam_setting.c.type == self.tabWidget.currentIndex()
  92. ).values(mode=self.__mode, selected_cam=self.widget.get_value())
  93. result = conn.execute(query)
  94. show_dialog(QMessageBox.Icon.Information, '', 'Сохранено')
  95. logger.info(F'Камера сохранена {self.widget.get_value()}')