DialogSettingCam.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 SettingCam(QDialog, Ui_DialogSettingCam):
  9. def __init__(self, parent=None) -> None:
  10. super(SettingCam, 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.__load_cams_from_db()
  16. self.save_setting_cam.clicked.connect(self.__save_cams)
  17. self.mode_list.currentIndexChanged.connect(self.__change_mode)
  18. self.tabWidget.currentChanged.connect(self.__change_type)
  19. # self.__recreate_widget()
  20. def __change_type(self) -> None:
  21. if self.tabWidget.currentIndex() == 1:
  22. self.__mode = 'document'
  23. # else:
  24. # self.__mode = 'video'
  25. self.__load_cams_from_db()
  26. def __change_mode(self) -> None:
  27. self.__mode = self.mode_list.currentData()
  28. self.__recreate_widget(self.__mode)
  29. logger.info(F'Change cams to {self.__mode}')
  30. @Slot()
  31. def __recreate_widget(self, select_mode: str = 'video') -> None:
  32. self.__del_widget()
  33. if self.tabWidget.currentIndex() == 0:
  34. self.widget = create_widget_cam_shecked(mode=select_mode,
  35. name_object=u'widget',
  36. obj=self.tab,
  37. layout=self.verticalLayout)
  38. else:
  39. self.widget = create_widget_cam_shecked(
  40. name_object=U'widget',
  41. obj=self.tab_2,
  42. layout=self.verticalLayout_3)
  43. def __del_widget(self) -> None:
  44. try:
  45. self.widget.stop_cam()
  46. except AttributeError as er:
  47. logger.error(er)
  48. if self.widget is not None:
  49. self.verticalLayout.removeWidget(self.widget)
  50. self.verticalLayout_3.removeWidget(self.widget)
  51. self.widget = None
  52. def __load_cams_from_db(self):
  53. with connect() as conn:
  54. result = conn.execute(cam_setting.select().where(
  55. cam_setting.c.type == self.tabWidget.currentIndex()
  56. )).fetchone()
  57. if result:
  58. self.__mode = result.mode
  59. self.__recreate_widget(self.__mode)
  60. match self.__mode:
  61. case 'video':
  62. self.mode_list.setCurrentIndex(0)
  63. self.widget.line_cam.setCurrentText(
  64. result.selected_cam)
  65. case 'snapshot':
  66. self.mode_list.setCurrentIndex(1)
  67. self.widget.line_cam.setText(result.selected_cam)
  68. case 'document':
  69. self.widget.line_cam.setCurrentText(
  70. result.selected_cam)
  71. case _: ...
  72. else: self.__recreate_widget()
  73. def __save_cams(self) -> None:
  74. query = None
  75. with connect() as conn:
  76. try:
  77. result = conn.execute(cam_setting.select().where(
  78. cam_setting.c.type == self.tabWidget.currentIndex()
  79. )).fetchone()
  80. if result is None:
  81. query = cam_setting.insert().values(
  82. type=self.tabWidget.currentIndex(),
  83. mode=self.__mode,
  84. selected_cam=self.widget.get_value())
  85. else:
  86. query = cam_setting.update().where(
  87. cam_setting.c.type == self.tabWidget.currentIndex()
  88. ).values(mode=self.__mode, selected_cam=self.widget.get_value())
  89. result = conn.execute(query)
  90. show_dialog(QMessageBox.Icon.Information, '', 'Сохранено')
  91. logger.info('Камера сохранена')
  92. except OperationalError as er:
  93. logger.error(er)
  94. show_dialog(QMessageBox.Icon.Critical, 'Ошибка', 'Произошла ошибка! \nОбратитеcь к администратору')