MainWindow.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 module.WorkWithCam import WorkWithCam, load_image
  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 .ui_py.ui_MainWindow import Ui_MainWindow
  11. from .ListPersonal import ListPersonal
  12. from .ListPlace import ListPlace
  13. from .DialogSettingCam import SettingCam
  14. from .DialogAbout import DialogAbout
  15. from .DialogHistory import DialogHistory
  16. from time import sleep
  17. from datetime import datetime
  18. from logger import logger
  19. from pathlib import Path
  20. import platform
  21. class MainWindow(QMainWindow, Ui_MainWindow):
  22. def __init__(self, parent=None) -> None:
  23. super(MainWindow, self).__init__(parent)
  24. self.setupUi(self)
  25. self.date_from.setDateTime(QDateTime().currentDateTime())
  26. self.date_to.setDateTime(QDateTime().currentDateTime())
  27. self.stacked_widget_photo.currentChanged.connect(
  28. self.current_change_photo
  29. )
  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_photo()
  35. def open_history(self) -> None:
  36. DialogHistory(self).exec_()
  37. def current_change_photo(self):
  38. if self.stacked_widget_photo.currentIndex() == os.environ.get('INDEX_CAMERA'):
  39. self.btn_start_cam_photo.setText(stop_cam)
  40. else:
  41. self.btn_start_cam_photo.setText(start_cam)
  42. def _init_menu_btn_action(self) -> None:
  43. self.action_open_history.triggered.connect(self.open_history)
  44. self.btn_show_personal_window.triggered.connect(
  45. self._show_personal_window
  46. )
  47. self.btn_show_place_window.triggered.connect(
  48. self._show_place_window
  49. )
  50. self.setting_cam.triggered.connect(
  51. self._show_setting_cam_window
  52. )
  53. self.update_list.triggered.connect(
  54. self._update_list_combobox
  55. )
  56. self.btn_show_about.triggered.connect(
  57. self._show_about_dialog
  58. )
  59. def _show_about_dialog(self):
  60. DialogAbout(self).exec_()
  61. def _check_setting_cam(self) -> None:
  62. if not self._get_selected_cam():
  63. logger.warning(warring_cams.get("title"))
  64. show_dialog(
  65. QMessageBox.Warning,
  66. warring_cams.get("title"),
  67. warring_cams.get("body")
  68. )
  69. def _init_push_btn_action(self) -> None:
  70. self.btn_start_cam_photo.clicked.connect(
  71. self._start_cam_photo
  72. )
  73. self.capturePhoto.clicked.connect(
  74. self._take_image_face
  75. )
  76. self.btn_save.clicked.connect(self._save)
  77. self.btn_clear.clicked.connect(self._clear)
  78. self.btn_print.clicked.connect(self._print)
  79. def _show_personal_window(self) -> None:
  80. ListPersonal(self).exec_()
  81. self._update_list_combobox()
  82. @Slot()
  83. def _show_place_window(self) -> None:
  84. ListPlace(self).exec_()
  85. self._update_list_combobox()
  86. @Slot()
  87. def _show_setting_cam_window(self) -> None:
  88. SettingCam(self).exec_()
  89. def _update_list_combobox(self) -> None:
  90. self._load_personal()
  91. self._load_place()
  92. def _load_personal(self) -> None:
  93. self.personal_combobox.clear()
  94. with connect() as conn:
  95. for personal in conn.execute(
  96. list_personal.select()
  97. ).all():
  98. fio = F"{personal.lastname} {personal.firstname} {personal.middlename}"
  99. self.personal_combobox.addItem(
  100. fio,
  101. userData={"id": personal.id}
  102. )
  103. def _load_place(self) -> None:
  104. self.place_combobox.clear()
  105. with connect() as conn:
  106. for place in conn.execute(
  107. list_place.select()
  108. ).all():
  109. self.place_combobox.addItem(
  110. place.name_place,
  111. userData={"id": place.id}
  112. )
  113. def _get_selected_cam(self) -> bool | str:
  114. with connect() as conn:
  115. cam = conn.execute(
  116. cam_setting.select()
  117. ).fetchone()
  118. try:
  119. return cam.selected_cam
  120. except:
  121. return False
  122. def _init_photo(self) -> None:
  123. load_image(self.imagePhoto, os.environ.get('NO_MEDIA_IMAGE'))
  124. @Slot()
  125. def _start_cam_photo(self) -> None:
  126. if self._get_selected_cam():
  127. if self.stacked_widget_photo.currentIndex() == int(os.environ.get('INDEX_PHOTO')):
  128. self.stacked_widget_photo.setCurrentIndex(
  129. int(os.environ.get('INDEX_CAMERA')))
  130. self._create_wwc()
  131. self.face_wwc.start_cam()
  132. else:
  133. self.face_wwc.stop_cam()
  134. self.stacked_widget_photo.setCurrentIndex(
  135. int(os.environ.get('INDEX_PHOTO')))
  136. def _create_wwc(self) -> None:
  137. if not hasattr(self, "face_wwc"):
  138. self.face_wwc = WorkWithCam(
  139. q_Video_Widget=self.face_video_widget,
  140. name_cam=self._get_selected_cam())
  141. @Slot()
  142. def _take_image_face(self) -> None:
  143. if hasattr(self, "face_wwc"):
  144. self.face_file_name = self.face_wwc.cupture_image(self.imagePhoto)
  145. sleep(1)
  146. self.stacked_widget_photo.setCurrentIndex(
  147. int(os.environ.get('INDEX_PHOTO')))
  148. self.face_wwc.stop_cam()
  149. @Slot()
  150. def _print(self) -> None:
  151. if not hasattr(self, "data_propusk"):
  152. self._save()
  153. propusk_data = self.data_propusk.copy()
  154. propusk_data["personal"] = self.personal_combobox.currentText()
  155. propusk_data["place"] = self.place_combobox.currentText()
  156. propusk_data["date_from"] = self.date_from.dateTime().toString(
  157. 'dd.MM.yyyy hh:mm')
  158. propusk_data["date_to"] = self.date_to.dateTime().toString(
  159. 'dd.MM.yyyy hh:mm')
  160. rotate_img = rotate_image(self.face_file_name, -90)
  161. if platform.system() in 'Linux':
  162. rotate_img = F"file://{rotate_img}"
  163. propusk_data["face_photo"] = rotate_img
  164. render_text = TemplatePropusk(
  165. propusk_data,
  166. os.path.join(os.environ.get('ABSOLUTE_PATH'), 'docs')
  167. )
  168. Printer(str(render_text)).print()
  169. @Slot()
  170. def _clear(self) -> None:
  171. if hasattr(self, "data_propusk"):
  172. del self.data_propusk
  173. self._set_default_data()
  174. @Slot()
  175. def _save(self) -> None:
  176. date_from = self.date_from.dateTime().toMSecsSinceEpoch() / 1000
  177. date_to = self.date_to.dateTime().toMSecsSinceEpoch() / 1000
  178. self.data_propusk = {
  179. "id_propusk": int(self.number_propusk.text()),
  180. "date_from": datetime.fromtimestamp(date_from),
  181. "date_to": datetime.fromtimestamp(date_to),
  182. "personal": self.personal_combobox.currentData()['id'],
  183. "place": self.place_combobox.currentData()['id'],
  184. "receiving_man": self.receiving_man.toPlainText(),
  185. "purpose_visite": self.purpose_visite.toPlainText(),
  186. "face_photo": F"{Path(self.face_file_name).name}",
  187. "pasport_photo": F"{Path(self.face_file_name).name}"
  188. }
  189. with connect() as conn:
  190. conn.execute(
  191. list_propusk.insert().values(**self.data_propusk)
  192. )
  193. def _set_default_data(self) -> None:
  194. self.number_propusk.clear()
  195. self.date_from.setDate(QDate().currentDate())
  196. self.date_to.setDate(QDate().currentDate())
  197. self._init_photo()
  198. self.receiving_man.clear()
  199. self.purpose_visite.clear()
  200. self.stacked_widget_photo.setCurrentIndex(
  201. int(os.environ.get('INDEX_PHOTO')))
  202. if hasattr(self, "face_wwc"):
  203. delattr(self, "face_wwc")