MainWindow.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.date_from.setDateTime(QDateTime().currentDateTime())
  27. self.date_to.setDateTime(QDateTime().currentDateTime())
  28. # self.stacked_widget_photo.currentChanged.connect(
  29. # self.current_change_photo
  30. # )
  31. self._init_menu_btn_action()
  32. self._init_push_btn_action()
  33. self._update_list_combobox()
  34. self._check_setting_cam()
  35. self._init_widget_cam()
  36. def open_history(self) -> None:
  37. DialogHistory(self).exec_()
  38. # def current_change_photo(self):
  39. # if self.stacked_widget_photo.currentIndex() == os.environ.get('INDEX_CAMERA'):
  40. # self.btn_start_cam_photo.setText(stop_cam)
  41. # else:
  42. # self.btn_start_cam_photo.setText(start_cam)
  43. def _init_menu_btn_action(self) -> None:
  44. self.action_open_history.triggered.connect(self.open_history)
  45. self.btn_show_personal_window.triggered.connect(
  46. self._show_personal_window
  47. )
  48. self.btn_show_place_window.triggered.connect(
  49. self._show_place_window
  50. )
  51. self.setting_cam.triggered.connect(
  52. self._show_setting_cam_window
  53. )
  54. self.update_list.triggered.connect(
  55. self._update_list_combobox
  56. )
  57. self.btn_show_about.triggered.connect(
  58. self._show_about_dialog
  59. )
  60. def _show_about_dialog(self):
  61. DialogAbout(self).exec_()
  62. def _check_setting_cam(self) -> None:
  63. try:
  64. self.__mode, self.__cam = self.__get_selected_cam()
  65. except ValueError:
  66. logger.warning(warring_cams.get("title"))
  67. if show_dialog(
  68. QMessageBox.Warning,
  69. warring_cams.get("title"),
  70. warring_cams.get("body")
  71. ):
  72. self._show_setting_cam_window()
  73. self._check_setting_cam()
  74. else:
  75. self.close()
  76. def _init_push_btn_action(self) -> None:
  77. self.btn_start_cam_photo.clicked.connect(
  78. self._start_cam_photo
  79. )
  80. self.capturePhoto.clicked.connect(
  81. self._take_image_face
  82. )
  83. self.btn_save.clicked.connect(self._save)
  84. self.btn_clear.clicked.connect(self._clear)
  85. self.btn_print.clicked.connect(self._print)
  86. def _show_personal_window(self) -> None:
  87. ListPersonal(self).exec_()
  88. self._update_list_combobox()
  89. @Slot()
  90. def _show_place_window(self) -> None:
  91. ListPlace(self).exec_()
  92. self._update_list_combobox()
  93. @Slot()
  94. def _show_setting_cam_window(self) -> None:
  95. SettingCam(self).exec_()
  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={"id": 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={"id": 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. @Slot()
  134. def _start_cam_photo(self) -> None:
  135. if not hasattr(self, '__wwc'):
  136. if self.stacked_widget.currentIndex() == int(os.environ.get('INDEX_CAMERA')):
  137. self.__stop_cam()
  138. self.stacked_widget.to_image()
  139. self.stacked_widget.to_video()
  140. self._create_wwc()
  141. def _create_wwc(self) -> None:
  142. match self.__mode:
  143. case 'video':
  144. self.__wwc = USBCam(self.stacked_widget.video, self.__cam)
  145. self.__wwc.start_cam()
  146. case 'snapshot':
  147. self.__wwc = IPCam()
  148. self.__wwc.qLabel = self.stacked_widget.video
  149. self.__wwc.lnk_connect = self.__cam
  150. self.__wwc.start()
  151. print("Started...")
  152. case _: return None
  153. @Slot()
  154. def _take_image_face(self) -> None:
  155. self.__file_name = self.__wwc.cupture_image(self.stacked_widget.image)
  156. sleep(1)
  157. self.stacked_widget.to_image()
  158. self.__stop_cam()
  159. @Slot()
  160. def _print(self) -> None:
  161. if not hasattr(self, "data_propusk"):
  162. self._save()
  163. propusk_data = self.data_propusk.copy()
  164. propusk_data["personal"] = self.personal_combobox.currentText()
  165. propusk_data["place"] = self.place_combobox.currentText()
  166. propusk_data["date_from"] = self.date_from.dateTime().toString(
  167. 'dd.MM.yyyy hh:mm')
  168. propusk_data["date_to"] = self.date_to.dateTime().toString(
  169. 'dd.MM.yyyy hh:mm')
  170. rotate_img = rotate_image(self.__file_name, -90)
  171. if platform.system() in 'Linux':
  172. rotate_img = F"file://{rotate_img}"
  173. propusk_data["face_photo"] = rotate_img
  174. render_text = TemplatePropusk(
  175. propusk_data,
  176. os.path.join(os.environ.get('ABSOLUTE_PATH'), 'docs')
  177. )
  178. Printer(str(render_text)).print()
  179. @Slot()
  180. def _clear(self) -> None:
  181. if hasattr(self, "data_propusk"):
  182. del self.data_propusk
  183. self._set_default_data()
  184. @Slot()
  185. def _save(self) -> None:
  186. date_from = self.date_from.dateTime().toMSecsSinceEpoch() / 1000
  187. date_to = self.date_to.dateTime().toMSecsSinceEpoch() / 1000
  188. self.data_propusk = {
  189. "id_propusk": int(self.number_propusk.text()),
  190. "date_from": datetime.fromtimestamp(date_from),
  191. "date_to": datetime.fromtimestamp(date_to),
  192. "personal": self.personal_combobox.currentData()['id'],
  193. "place": self.place_combobox.currentData()['id'],
  194. "receiving_man": self.receiving_man.toPlainText(),
  195. "purpose_visite": self.purpose_visite.toPlainText(),
  196. "face_photo": F"{Path(self.__file_name).name}",
  197. "pasport_photo": F"{Path(self.__file_name).name}"
  198. }
  199. with connect() as conn:
  200. conn.execute(
  201. list_propusk.insert().values(**self.data_propusk)
  202. )
  203. def _set_default_data(self) -> None:
  204. self.number_propusk.clear()
  205. self.date_from.setDate(QDate().currentDate())
  206. self.date_to.setDate(QDate().currentDate())
  207. self._init_widget_cam()
  208. self.receiving_man.clear()
  209. self.purpose_visite.clear()
  210. self.stacked_widget.to_image()
  211. if hasattr(self, "face_wwc"):
  212. delattr(self, "face_wwc")
  213. def __stop_cam(self):
  214. self.__wwc.stop_cam()
  215. del self.__wwc
  216. self.stacked_widget.to_image()