MainWindow.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. from PySide6.QtWidgets import QMainWindow, QMessageBox
  2. from PySide6.QtCore import Slot, QDate, QDateTime
  3. from module.WorkWithDB import *
  4. from module.MyMessageBox import show_dialog
  5. from PropuskWidgets.PStackedWidget import PStackedWidget
  6. from module.TemplatePropusk import TemplatePropusk
  7. from module.Printer import Printer
  8. from module.lang.ru import *
  9. from module.ImageTool import rotate_image
  10. from module.cam import IPCam, USBCam
  11. from .ui_py.ui_MainWindow import Ui_MainWindow
  12. from .ListPersonal import ListPersonal
  13. from .ListPlace import ListPlace
  14. from .DialogSettingCam import SettingCam
  15. from .DialogAbout import DialogAbout
  16. from .DialogHistory import DialogHistory
  17. from time import sleep
  18. from datetime import datetime
  19. from logger import logger
  20. from pathlib import Path
  21. import platform
  22. class MainWindow(QMainWindow, Ui_MainWindow):
  23. def __init__(self, parent=None) -> None:
  24. super(MainWindow, self).__init__(parent)
  25. self.setupUi(self)
  26. self.__wwc = None
  27. self.data_propusk = None
  28. self.date_from.setDateTime(QDateTime().currentDateTime())
  29. self.date_to.setDateTime(QDateTime().currentDateTime())
  30. self._init_menu_btn_action()
  31. self._init_push_btn_action()
  32. self._update_list_combobox()
  33. self._check_setting_cam()
  34. self._init_widget_cam()
  35. self.stacked_widget.currentChanged.connect(
  36. self.__change_text_in_btn_start_cam
  37. )
  38. def open_history(self) -> None:
  39. DialogHistory(self).exec_()
  40. def __change_text_in_btn_start_cam(self):
  41. match self.stacked_widget.currentIndex():
  42. case 0: self.btn_start_cam_photo.setText(start_cam)
  43. case 1: self.btn_start_cam_photo.setText(stop_cam)
  44. def _init_menu_btn_action(self) -> None:
  45. self.action_open_history.triggered.connect(self.open_history)
  46. self.btn_show_personal_window.triggered.connect(
  47. self._show_personal_window
  48. )
  49. self.btn_show_place_window.triggered.connect(
  50. self._show_place_window
  51. )
  52. self.setting_cam.triggered.connect(
  53. self._show_setting_cam_window
  54. )
  55. self.update_list.triggered.connect(
  56. self._update_list_combobox
  57. )
  58. self.btn_show_about.triggered.connect(
  59. self._show_about_dialog
  60. )
  61. def _show_about_dialog(self):
  62. DialogAbout(self).exec_()
  63. def _check_setting_cam(self) -> None:
  64. try:
  65. self.__mode, self.__cam = self.__get_selected_cam()
  66. except ValueError:
  67. logger.warning(warring_cams.get("title"))
  68. if show_dialog(
  69. QMessageBox.Warning,
  70. warring_cams.get("title"),
  71. warring_cams.get("body")
  72. ):
  73. self._show_setting_cam_window()
  74. self._check_setting_cam()
  75. else:
  76. self.close()
  77. def _init_push_btn_action(self) -> None:
  78. self.btn_start_cam_photo.clicked.connect(
  79. self._start_cam_photo
  80. )
  81. self.capturePhoto.clicked.connect(
  82. self._take_image_face
  83. )
  84. self.btn_save.clicked.connect(self._save)
  85. self.btn_clear.clicked.connect(self._clear)
  86. self.btn_print.clicked.connect(self._print)
  87. def _show_personal_window(self) -> None:
  88. ListPersonal(self).exec_()
  89. self._update_list_combobox()
  90. def _show_place_window(self) -> None:
  91. ListPlace(self).exec_()
  92. self._update_list_combobox()
  93. def _show_setting_cam_window(self) -> None:
  94. SettingCam(self).exec_()
  95. self._check_setting_cam()
  96. def _update_list_combobox(self) -> None:
  97. self._load_personal()
  98. self._load_place()
  99. def _load_personal(self) -> None:
  100. self.personal_combobox.clear()
  101. with connect() as conn:
  102. for personal in conn.execute(
  103. list_personal.select()
  104. ).all():
  105. fio = F"{personal.lastname} {personal.firstname} {personal.middlename}"
  106. self.personal_combobox.addItem(
  107. fio,
  108. userData=personal.id
  109. )
  110. def _load_place(self) -> None:
  111. self.place_combobox.clear()
  112. with connect() as conn:
  113. for place in conn.execute(
  114. list_place.select()
  115. ).all():
  116. self.place_combobox.addItem(
  117. place.name_place,
  118. userData=place.id
  119. )
  120. def __get_selected_cam(self) -> bool:
  121. with connect() as conn:
  122. cam = conn.execute(
  123. cam_setting.select()
  124. ).fetchone()
  125. try:
  126. return cam.mode, cam.selected_cam
  127. except:
  128. raise ValueError('Нет записи о камерах')
  129. def _init_widget_cam(self) -> None:
  130. self.stacked_widget = PStackedWidget(self.groupBox_2, self.__mode)
  131. self.stacked_widget.setObjectName(u'stacked_widget')
  132. self.verticalLayout_4.addWidget(self.stacked_widget)
  133. def _start_cam_photo(self) -> None:
  134. if self.__wwc is None:
  135. self.stacked_widget.to_video()
  136. self._create_wwc()
  137. else:
  138. self.__stop_cam()
  139. self.stacked_widget.to_image()
  140. def _create_wwc(self) -> None:
  141. match self.__mode:
  142. case 'video':
  143. self.__wwc = USBCam(self.stacked_widget.video, self.__cam)
  144. self.__wwc.start_cam()
  145. case 'snapshot':
  146. self.__wwc = IPCam()
  147. self.__wwc.qLabel = self.stacked_widget.video
  148. self.__wwc.lnk_connect = self.__cam
  149. self.__wwc.start()
  150. case _: return None
  151. def _take_image_face(self) -> None:
  152. self.__file_name = self.__wwc.cupture_image(self.stacked_widget.image)
  153. sleep(1)
  154. self.stacked_widget.to_image()
  155. self.__stop_cam()
  156. def _print(self) -> None:
  157. if self.data_propusk is None:
  158. self._save()
  159. propusk_data = self.data_propusk.copy()
  160. propusk_data["personal"] = self.personal_combobox.currentText()
  161. propusk_data["place"] = self.place_combobox.currentText()
  162. propusk_data["date_from"] = self.date_from.dateTime().toString(
  163. 'dd.MM.yyyy hh:mm')
  164. propusk_data["date_to"] = self.date_to.dateTime().toString(
  165. 'dd.MM.yyyy hh:mm')
  166. propusk_data["face_photo"] = self.__file_name
  167. render_text = TemplatePropusk(
  168. propusk_data,
  169. os.path.join(os.environ.get('ABSOLUTE_PATH'), 'docs')
  170. )
  171. Printer(str(render_text)).print()
  172. def _clear(self) -> None:
  173. if self.data_propusk is not None:
  174. self.data_propusk = None
  175. self._set_default_data()
  176. def _save(self) -> None:
  177. date_from = self.date_from.dateTime().toMSecsSinceEpoch() / 1000
  178. date_to = self.date_to.dateTime().toMSecsSinceEpoch() / 1000
  179. self.data_propusk = {
  180. "id_propusk": int(self.number_propusk.text()),
  181. "date_from": datetime.fromtimestamp(date_from),
  182. "date_to": datetime.fromtimestamp(date_to),
  183. "personal": self.personal_combobox.currentData(),
  184. "place": self.place_combobox.currentData(),
  185. "receiving_man": self.receiving_man.toPlainText(),
  186. "purpose_visite": self.purpose_visite.toPlainText(),
  187. "face_photo": F"{Path(self.__file_name).name}",
  188. "pasport_photo": F"{Path(self.__file_name).name}"
  189. }
  190. with connect() as conn:
  191. conn.execute(
  192. list_propusk.insert().values(**self.data_propusk)
  193. )
  194. def _set_default_data(self) -> None:
  195. self.number_propusk.clear()
  196. self.date_from.setDate(QDate().currentDate())
  197. self.date_to.setDate(QDate().currentDate())
  198. if self.__wwc is not None:
  199. self.__stop_cam()
  200. self.stacked_widget.to_image()
  201. self.receiving_man.clear()
  202. self.purpose_visite.clear()
  203. def __stop_cam(self):
  204. if self.__wwc is not None:
  205. self.__wwc.stop_cam()
  206. self.__wwc = None
  207. self.stacked_widget.to_image()