DialogSettingCam.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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. print(result)
  55. self.__mode = result.mode
  56. self.__recreate_widget(self.__mode)
  57. match self.__mode:
  58. case 'video':
  59. self.mode_list.setCurrentIndex(0)
  60. self.widget.line_cam.setCurrentText(
  61. result.selected_cam)
  62. case 'snapshot':
  63. self.mode_list.setCurrentIndex(1)
  64. self.widget.line_cam.setText(result.selected_cam)
  65. case 'document':
  66. self.widget.line_cam.setCurrentText(
  67. result.selected_cam)
  68. case _: ...
  69. else: self.__recreate_widget()
  70. def __save_cams(self) -> None:
  71. query = None
  72. with connect() as conn:
  73. try:
  74. result = conn.execute(cam_setting.select().where(
  75. cam_setting.c.type == self.tabWidget.currentIndex()
  76. )).fetchone()
  77. if result is None:
  78. query = cam_setting.insert().values(
  79. type=self.tabWidget.currentIndex(),
  80. mode=self.__mode,
  81. selected_cam=self.widget.get_value())
  82. else:
  83. query = cam_setting.update().where(
  84. cam_setting.c.type == self.tabWidget.currentIndex()
  85. ).values(mode=self.__mode, selected_cam=self.widget.get_value())
  86. result = conn.execute(query)
  87. show_dialog(QMessageBox.Icon.Information, '', 'Сохранено')
  88. logger.info('Камера сохранена')
  89. except OperationalError as er:
  90. logger.error(er)
  91. show_dialog(QMessageBox.Icon.Critical, 'Ошибка', 'Произошла ошибка! \nОбратитеcь к администратору')