MainWindow.py 7.9 KB

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