MainWindow.py 11 KB

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