MainWindow.py 11 KB

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