MainWindow.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. from PySide6.QtWidgets import QMainWindow, QMessageBox
  2. from PySide6.QtCore import QDate, QDateTime
  3. from module.WorkWithDB import *
  4. from module.MyMessageBox import show_dialog
  5. from module.TemplatePropusk import TemplatePropusk
  6. from module.Printer import Printer
  7. from module.lang.ru import *
  8. from module.cam import IPCam, USBCam, load_image
  9. from widgets import PStackedWidget, create_widget_stacked
  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. vecrot_cam_from_db = [str, str, str]
  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.__wwc = None
  26. self.data_propusk = None
  27. self.date_from.setDateTime(QDateTime().currentDateTime())
  28. self.date_to.setDateTime(QDateTime().currentDateTime())
  29. self.__init_menu_action()
  30. self.__init_btn_action()
  31. self.__update_list_combobox()
  32. self.__check_setting_cam()
  33. self.__create_widget_face_cam()
  34. self.stacked_document.currentChanged.connect(
  35. self.__change_text_in_btn_start_cam
  36. )
  37. self.tabWidget.currentChanged.connect(
  38. self.change_tab
  39. )
  40. def __change_text_in_btn_start_cam(self):
  41. match self.tabWidget.currentIndex():
  42. case 0: self.btn_start_cam.setText(start_cam)
  43. case 1: self.btn_start_cam.setText(stop_cam)
  44. def __init_menu_action(self) -> None:
  45. self.action_open_history.triggered.connect(self.open_history)
  46. self.btn_show_personal_window.triggered.connect(
  47. self.__show_personal_window
  48. )
  49. self.btn_show_place_window.triggered.connect(
  50. self.__show_place_window
  51. )
  52. self.setting_cam.triggered.connect(
  53. self.__show_setting_cam_window
  54. )
  55. self.update_list.triggered.connect(
  56. self.__update_list_combobox
  57. )
  58. self.btn_show_about.triggered.connect(
  59. self.__show_about_dialog
  60. )
  61. def __init_btn_action(self) -> None:
  62. self.btn_start_cam.clicked.connect(
  63. self.__press_btn_start_cam
  64. )
  65. self.capturePhoto.clicked.connect(
  66. self.__take_image_face
  67. )
  68. self.btn_save.clicked.connect(self.__save)
  69. self.btn_clear.clicked.connect(self.__clear)
  70. self.btn_print.clicked.connect(self.__print)
  71. def __show_about_dialog(self):
  72. DialogAbout(self).exec_()
  73. def __show_personal_window(self) -> None:
  74. ListPersonal(self).exec_()
  75. self.__update_list_combobox()
  76. def __show_place_window(self) -> None:
  77. ListPlace(self).exec_()
  78. self.__update_list_combobox()
  79. def __show_setting_cam_window(self) -> None:
  80. SettingCam(self).exec_()
  81. self.__check_setting_cam()
  82. self.__create_widget_face_cam()
  83. def open_history(self) -> None:
  84. DialogHistory(self).exec_()
  85. def change_tab(self) -> None:
  86. self.__check_setting_cam()
  87. if self.tabWidget.currentIndex() == 1:
  88. self.__mode = 'video'
  89. def __check_setting_cam(self) -> None:
  90. try:
  91. self.__mode, self.__cam_face, self.__cam_document = self.__get_selected_cam()
  92. except ValueError:
  93. logger.warning(warring_cams.get("title"))
  94. if show_dialog(
  95. QMessageBox.Warning,
  96. warring_cams.get("title"),
  97. warring_cams.get("body")
  98. ):
  99. self.__show_setting_cam_window()
  100. self.__check_setting_cam()
  101. else:
  102. self.close()
  103. def __update_list_combobox(self) -> None:
  104. self.__load_personal()
  105. self.__load_place()
  106. def __load_personal(self) -> None:
  107. self.personal_combobox.clear()
  108. with connect() as conn:
  109. for personal in conn.execute(
  110. list_personal.select()
  111. ).all():
  112. fio = F"{personal.lastname} {personal.firstname} {personal.middlename}"
  113. self.personal_combobox.addItem(
  114. fio,
  115. userData=personal.id
  116. )
  117. def __load_place(self) -> None:
  118. self.place_combobox.clear()
  119. with connect() as conn:
  120. for place in conn.execute(
  121. list_place.select()
  122. ).all():
  123. self.place_combobox.addItem(
  124. place.name_place,
  125. userData=place.id
  126. )
  127. def __get_selected_cam(self) -> vecrot_cam_from_db:
  128. '''
  129. return:
  130. str -> video, snapshot
  131. str -> ссылка для подключения к ip камеры или названия камеры (для лица)
  132. str -> названия камеры (для документа)
  133. '''
  134. with connect() as conn:
  135. cam = conn.execute(
  136. cam_setting.select()
  137. ).all()
  138. try:
  139. return cam[0].mode, cam[0].selected_cam, cam[1].selected_cam
  140. except:
  141. logger.error('Нет записи о камерах')
  142. show_dialog(
  143. QMessageBox.Icon.Critical,
  144. 'Отсутствуют записи о камерах',
  145. 'Отсутствуют записи о камерах'
  146. )
  147. def __press_btn_start_cam(self):
  148. match self.tabWidget.currentIndex():
  149. case 0: self.__start_cam(self.stacked_face, self.__cam_face)
  150. case 1: self.__start_cam(self.stacked_document, self.__cam_document)
  151. def __start_cam(self, widget: PStackedWidget, cam: str) -> None:
  152. if self.__wwc is None:
  153. widget.to_video()
  154. self.__create_wwc(widget, cam)
  155. else:
  156. self.__stop_cam()
  157. widget.to_image()
  158. def __create_wwc(self, widget: PStackedWidget, cam: str) -> None:
  159. if self.__mode in 'snapshot':
  160. self.__wwc = IPCam()
  161. self.__wwc.qLabel = widget.video
  162. self.__wwc.lnk_connect = cam
  163. self.__wwc.start()
  164. else:
  165. self.__wwc = USBCam(widget.video, cam)
  166. self.__wwc.start_cam()
  167. def __take_image_face(self) -> None:
  168. if self.tabWidget.currentIndex() == 1:
  169. self.__file_name_document = self.__wwc.cupture_image(self.stacked_document.image)
  170. sleep(1)
  171. load_image(self.stacked_document.image, self.__file_name_document)
  172. else:
  173. self.__file_name_face = self.__wwc.cupture_image(self.stacked_face.image)
  174. sleep(1)
  175. load_image(self.stacked_face.image, self.__file_name_face)
  176. self.__stop_cam()
  177. def __print(self) -> None:
  178. if self.data_propusk is None:
  179. self.__save()
  180. propusk_data = self.data_propusk.copy()
  181. propusk_data.update({
  182. "personal": self.personal_combobox.currentText(),
  183. "place": self.place_combobox.currentText(),
  184. "date_from": self.date_from.dateTime().toString('dd.MM.yyyy hh:mm'),
  185. "date_to": self.date_to.dateTime().toString('dd.MM.yyyy hh:mm'),
  186. "face": self.__file_name_face,
  187. "document": self.__file_name_face
  188. })
  189. render_text = TemplatePropusk(
  190. propusk_data,
  191. os.path.join(os.environ.get('ABSOLUTE_PATH'), 'docs')
  192. )
  193. Printer(str(render_text)).print()
  194. def __clear(self) -> None:
  195. if self.data_propusk is not None:
  196. self.data_propusk = None
  197. self.__set_default_data()
  198. def __save(self) -> None:
  199. date_from = self.date_from.dateTime().toMSecsSinceEpoch() / 1000
  200. date_to = self.date_to.dateTime().toMSecsSinceEpoch() / 1000
  201. self.data_propusk = {
  202. "id_propusk": int(self.number_propusk.text()),
  203. "date_from": datetime.fromtimestamp(date_from),
  204. "date_to": datetime.fromtimestamp(date_to),
  205. "personal": self.personal_combobox.currentData(),
  206. "place": self.place_combobox.currentData(),
  207. "receiving_man": self.receiving_man.toPlainText(),
  208. "purpose_visite": self.purpose_visite.toPlainText(),
  209. "face": F"{Path(self.__file_name_face).name}",
  210. "document": F"{Path(self.__file_name_document).name}"
  211. }
  212. with connect() as conn:
  213. conn.execute(
  214. list_propusk.insert().values(**self.data_propusk)
  215. )
  216. def __set_default_data(self) -> None:
  217. self.number_propusk.clear()
  218. self.date_from.setDate(QDate().currentDate())
  219. self.date_to.setDate(QDate().currentDate())
  220. if self.__wwc is not None:
  221. self.__stop_cam()
  222. self.receiving_man.clear()
  223. self.purpose_visite.clear()
  224. def __stop_cam(self):
  225. if self.__wwc is not None:
  226. self.__wwc.stop_cam()
  227. self.__wwc = None
  228. if self.tabWidget.currentIndex() == 1:
  229. self.stacked_document.to_image()
  230. else: self.stacked_face.to_image()
  231. def __create_widget_face_cam(self) -> None:
  232. if hasattr(self, 'stacked_face'):
  233. self.gridLayout.removeWidget(self.stacked_face)
  234. self.stacked_face = create_widget_stacked(
  235. name_object=u'stacked_face',
  236. obj=self.tab,
  237. layout=self.gridLayout,
  238. mode=self.__mode)
  239. self.stacked_face.currentChanged.connect(
  240. self.__change_text_in_btn_start_cam
  241. )