MainWindow.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. from PySide6.QtWidgets import (
  2. QMainWindow, QMessageBox, QDialog)
  3. from PySide6.QtCore import Slot, QDate
  4. from PySide6.QtGui import QImage, QRegularExpressionValidator
  5. from module.WorkWithDB import *
  6. from module.MessageBox import showDialog
  7. from module.WorkWithCam import WorkWithCam, load_image
  8. from module.TemplatePropusk import TemplatePropusk
  9. from module.Printer import Printer
  10. from .ui_py.ui_MainWindow import Ui_MainWindow
  11. from .ListPersonal import ListPersonal
  12. from .ListPlace import ListPlace
  13. from .SettingCam import SettingCam
  14. from time import sleep
  15. from datetime import datetime
  16. NO_MEDIA_IMAGE = "image/no_media_main.jpg"
  17. INDEX_PHOTO = 0
  18. INDEX_CAMERA = 1
  19. ABSOLUTE_PATH = os.path.dirname(os.path.abspath(__package__))
  20. class MainWindow(QMainWindow, Ui_MainWindow):
  21. def __init__(self, parent=None) -> None:
  22. super(MainWindow, self).__init__(parent)
  23. self.setupUi(self)
  24. self.number_propusk.setValidator(QRegularExpressionValidator(
  25. r"\d+", self
  26. ))
  27. self._init_menu_btn_action()
  28. self._init_push_btn_action()
  29. self._update_list_combobox()
  30. self._check_setting_cam()
  31. self._init_photo()
  32. def _init_menu_btn_action(self) -> None:
  33. self.btn_show_personal_window.triggered.connect(
  34. self._show_personal_window
  35. )
  36. self.btn_show_place_window.triggered.connect(
  37. self._show_place_window
  38. )
  39. self.setting_cam.triggered.connect(
  40. self._show_setting_cam_window
  41. )
  42. self.update_list.triggered.connect(
  43. self._update_list_combobox
  44. )
  45. def _check_setting_cam(self) -> None:
  46. if not self._get_setting_cam():
  47. showDialog(
  48. QMessageBox.Warning,
  49. "Камеры не настроины",
  50. "Настройте камеры \nНастройки -> Настройки камеры"
  51. )
  52. def _init_push_btn_action(self) -> None:
  53. self.btn_start_cam_photo.clicked.connect(
  54. self._start_cam_photo
  55. )
  56. self.btn_start_cam_pasport.clicked.connect(
  57. self._start_cam_pasport
  58. )
  59. self.capturePhoto.clicked.connect(
  60. self._take_image_face
  61. )
  62. self.capturePasport.clicked.connect(
  63. self._take_image_pasport
  64. )
  65. self.btn_save.clicked.connect(self._save)
  66. self.btn_clear.clicked.connect(self._clear)
  67. self.btn_print.clicked.connect(self._print)
  68. def _show_personal_window(self) -> None:
  69. ListPersonal(self).exec_()
  70. self._update_list_combobox()
  71. @Slot()
  72. def _show_place_window(self) -> None:
  73. ListPlace(self).exec_()
  74. self._update_list_combobox()
  75. @Slot()
  76. def _show_setting_cam_window(self) -> None:
  77. SettingCam(self).show()
  78. def _update_list_combobox(self) -> None:
  79. self._load_personal()
  80. self._load_place()
  81. def _load_personal(self) -> None:
  82. self.personal_combobox.clear()
  83. with connect() as conn:
  84. for personal in conn.execute(
  85. list_personal.select()
  86. ).all():
  87. fio = F"{personal.lastname} {personal.firstname} {personal.middlename}"
  88. self.personal_combobox.addItem(
  89. fio,
  90. userData={"id": personal.id}
  91. )
  92. def _load_place(self) -> None:
  93. self.place_combobox.clear()
  94. with connect() as conn:
  95. for place in conn.execute(
  96. list_place.select()
  97. ).all():
  98. self.place_combobox.addItem(
  99. place.name_place,
  100. userData={"id": place.id}
  101. )
  102. def _get_setting_cam(self) -> bool | list:
  103. with connect() as conn:
  104. cams = conn.execute(
  105. cam_setting.select()
  106. ).all()
  107. if not cams or len(cams) != 2:
  108. return False
  109. else:
  110. return cams
  111. def _init_photo(self) -> None:
  112. load_image(self.imagePhoto, NO_MEDIA_IMAGE)
  113. load_image(self.imagePasport, NO_MEDIA_IMAGE)
  114. @Slot()
  115. def _start_cam_photo(self) -> None:
  116. cams = self._get_setting_cam()
  117. if cams:
  118. if self.stacked_widget_photo.currentIndex() == INDEX_PHOTO:
  119. self.stacked_widget_photo.setCurrentIndex(INDEX_CAMERA)
  120. self.face_wwc = WorkWithCam(
  121. self.face_video_widget,
  122. cams[0].selected_cam
  123. )
  124. self.face_wwc.start_cam()
  125. else:
  126. self.face_wwc.stop_cam()
  127. del self.face_wwc
  128. self.stacked_widget_photo.setCurrentIndex(INDEX_PHOTO)
  129. @Slot()
  130. def _start_cam_pasport(self) -> None:
  131. cams = self._get_setting_cam()
  132. if cams:
  133. if self.stacked_widget_pasport.currentIndex() == INDEX_PHOTO:
  134. self.stacked_widget_pasport.setCurrentIndex(INDEX_CAMERA)
  135. self.pasport_wwc = WorkWithCam(
  136. self.pasport_video_widget,
  137. cams[1].selected_cam
  138. )
  139. self.pasport_wwc.start_cam()
  140. else:
  141. self.pasport_wwc.stop_cam()
  142. del self.pasport_wwc
  143. self.stacked_widget_pasport.setCurrentIndex(INDEX_PHOTO)
  144. @Slot()
  145. def _take_image_face(self):
  146. if hasattr(self, "face_wwc"):
  147. self.face_file_name = self.face_wwc.get_filename()
  148. self.face_wwc._current_preview = QImage()
  149. self.face_wwc._image_capture.captureToFile(self.face_file_name)
  150. self.stacked_widget_photo.setCurrentIndex(INDEX_PHOTO)
  151. del_wwc(self.face_wwc, 1)
  152. load_image(self.imagePhoto, self.face_file_name)
  153. @Slot()
  154. def _take_image_pasport(self):
  155. if hasattr(self, "pasport_wwc"):
  156. self.pasport_file_name = self.pasport_wwc.get_filename()
  157. self.pasport_wwc._current_preview = QImage()
  158. self.pasport_wwc._image_capture.captureToFile(
  159. self.pasport_file_name)
  160. self.stacked_widget_pasport.setCurrentIndex(INDEX_PHOTO)
  161. del_wwc(self.pasport_wwc, 1)
  162. load_image(self.imagePasport, self.pasport_file_name)
  163. @Slot()
  164. def _print(self) -> None:
  165. if not hasattr(self, "data_propusk"):
  166. self._save()
  167. propusk_data = self.data_propusk.copy()
  168. propusk_data["personal"] = self.personal_combobox.currentText()
  169. propusk_data["place"] = self.place_combobox.currentIndex()
  170. Printer(
  171. str(TemplatePropusk(self.data_propusk,
  172. os.path.join(ABSOLUTE_PATH, 'docs')
  173. ))
  174. ).print()
  175. @Slot()
  176. def _clear(self) -> None:
  177. if hasattr(self, "data_propusk"):
  178. del self.data_propusk
  179. self._set_default_data()
  180. @Slot()
  181. def _save(self) -> None:
  182. date_from = self.date_from.dateTime().toMSecsSinceEpoch() / 1000
  183. date_to = self.date_to.dateTime().toMSecsSinceEpoch() / 1000
  184. self.data_propusk = {
  185. "id_propusk": int(self.number_propusk.text()),
  186. "date_from": datetime.fromtimestamp(date_from),
  187. "date_to": datetime.fromtimestamp(date_to),
  188. "personal": self.personal_combobox.currentData()['id'],
  189. "place": self.place_combobox.currentData()['id'],
  190. "receiving_man": self.receiving_man.toPlainText(),
  191. "purpose_visite": self.purpose_visite.toPlainText(),
  192. "face_photo": F"file://{self.face_file_name}",
  193. "pasport_photo": F"file://{self.pasport_file_name}"
  194. }
  195. with connect() as conn:
  196. conn.execute(
  197. list_propusk.insert().values(**self.data_propusk)
  198. )
  199. def _set_default_data(self):
  200. self.number_propusk.clear()
  201. self.date_from.setDate(QDate().currentDate())
  202. self.date_to.setDate(QDate().currentDate())
  203. self._init_photo()
  204. self.receiving_man.clear()
  205. self.purpose_visite.clear()
  206. if hasattr(self, "pasport_wwc", 1):
  207. del_wwc(self.pasport_wwc)
  208. self.stacked_widget_pasport.setCurrentIndex(INDEX_PHOTO)
  209. if hasattr(self, "face_wwc", 1):
  210. del_wwc(self.face_wwc)
  211. self.stacked_widget_photo.setCurrentIndex(INDEX_PHOTO)
  212. def del_wwc(wwc: WorkWithCam, second: int = 0) -> None:
  213. sleep(second)
  214. wwc.stop_cam()
  215. del wwc