MainWindow.py 8.3 KB

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