DialogSettingCam.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 widgets import create_widget_cam_shecked
  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.widget = create_widget_cam_shecked(mode=select_mode,
  32. name_object=u'widget',
  33. obj=self.tab,
  34. layout=self.verticalLayout)
  35. else:
  36. self.widget = create_widget_cam_shecked(
  37. name_object=U'widget',
  38. obj=self.tab_2,
  39. layout=self.verticalLayout_3)
  40. def __del_widget(self) -> None:
  41. if self.widget is not None:
  42. self.verticalLayout.removeWidget(self.widget)
  43. self.verticalLayout_3.removeWidget(self.widget)
  44. self.widget = None
  45. def __load_cams_from_db(self):
  46. with connect() as conn:
  47. result = conn.execute(cam_setting.select().where(
  48. cam_setting.c.type == self.tabWidget.currentIndex()
  49. )).fetchone()
  50. if result:
  51. self.__mode = result.mode
  52. self.__recreate_widget(self.__mode)
  53. match self.__mode:
  54. case 'video':
  55. self.mode_list.setCurrentIndex(0)
  56. self.widget.line_cam.setCurrentText(
  57. result.selected_cam)
  58. case 'snapshot':
  59. self.mode_list.setCurrentIndex(1)
  60. self.widget.line_cam.setText(result.selected_cam)
  61. case 'document':
  62. self.widget.line_cam.setCurrentText(
  63. result.selected_cam)
  64. case _: ...
  65. else: self.__recreate_widget()
  66. def __save_cams(self) -> None:
  67. query = None
  68. with connect() as conn:
  69. result = conn.execute(cam_setting.select().where(
  70. cam_setting.c.type == self.tabWidget.currentIndex()
  71. )).fetchone()
  72. if result is None:
  73. query = cam_setting.insert().values(
  74. type=self.tabWidget.currentIndex(),
  75. mode=self.__mode,
  76. selected_cam=self.widget.get_value())
  77. else:
  78. query = cam_setting.update().where(
  79. cam_setting.c.type == self.tabWidget.currentIndex()
  80. ).values(mode=self.__mode, selected_cam=self.widget.get_value())
  81. result = conn.execute(query)