MainWindow.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. from PySide6.QtWidgets import QMainWindow, QMessageBox, QApplication
  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 widgets import PStackedWidget, create_widget_stacked
  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 sys
  22. vecrot_cam_from_db = [str, str, str]
  23. class MainWindow(QMainWindow, Ui_MainWindow):
  24. def __init__(self, parent=None) -> None:
  25. super(MainWindow, self).__init__(parent)
  26. self.setupUi(self)
  27. self.__wwc = None
  28. self.data_propusk = None
  29. self.stacked_face = None
  30. self.date_from.setDateTime(QDateTime().currentDateTime())
  31. self.date_to.setDateTime(QDateTime().currentDateTime())
  32. self.__init_menu_action()
  33. self.__init_btn_action()
  34. self.__update_list_combobox()
  35. self.__check_setting_cam()
  36. self.__create_widget_face_cam()
  37. self.tabWidget.currentChanged.connect(
  38. self.change_tab
  39. )
  40. def __init_menu_action(self) -> None:
  41. self.action_open_history.triggered.connect(self.open_history)
  42. self.btn_show_personal_window.triggered.connect(
  43. self.__show_personal_window
  44. )
  45. self.btn_show_place_window.triggered.connect(
  46. self.__show_place_window
  47. )
  48. self.setting_cam.triggered.connect(
  49. self.__show_setting_cam_window
  50. )
  51. self.update_list.triggered.connect(
  52. self.__update_list_combobox
  53. )
  54. self.btn_show_about.triggered.connect(
  55. self.__show_about_dialog
  56. )
  57. def __init_btn_action(self) -> None:
  58. self.btn_start_cam.clicked.connect(
  59. self.__press_btn_start_cam
  60. )
  61. self.capturePhoto.clicked.connect(
  62. self.__take_image
  63. )
  64. self.btn_save.clicked.connect(self.__save)
  65. self.btn_clear.clicked.connect(self.__clear)
  66. self.btn_print.clicked.connect(self.__print)
  67. def __show_about_dialog(self):
  68. DialogAbout(self).exec_()
  69. def __show_personal_window(self) -> None:
  70. ListPersonal(self).exec_()
  71. self.__update_list_combobox()
  72. def __show_place_window(self) -> None:
  73. ListPlace(self).exec_()
  74. self.__update_list_combobox()
  75. def __show_setting_cam_window(self) -> None:
  76. self.__stop_cam()
  77. SettingCam(self).exec_()
  78. self.__check_setting_cam()
  79. self.__create_widget_face_cam()
  80. def open_history(self) -> None:
  81. DialogHistory(self).exec_()
  82. def change_tab(self) -> None:
  83. self.__stop_cam()
  84. self.__check_setting_cam()
  85. if self.tabWidget.currentIndex() == 1:
  86. self.__mode = 'video'
  87. def __check_setting_cam(self) -> None:
  88. try:
  89. self.__mode, self.__cam_face, self.__cam_document = self.__get_selected_cam()
  90. except ValueError:
  91. logger.warning(warring_cams.get("title"))
  92. if show_dialog(
  93. QMessageBox.Warning,
  94. warring_cams.get("title"),
  95. warring_cams.get("body")
  96. ):
  97. self.__show_setting_cam_window()
  98. self.__check_setting_cam()
  99. else:
  100. self.close()
  101. except TypeError:
  102. logger.warning(warring_cams.get("title"))
  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. if show_dialog(
  143. QMessageBox.Icon.Critical,
  144. 'Камера',
  145. 'Отсутствуют записи о камерах'
  146. ):
  147. self.__show_setting_cam_window()
  148. self.__check_setting_cam()
  149. else:
  150. sys.exit(0)
  151. def __press_btn_start_cam(self):
  152. match self.tabWidget.currentIndex():
  153. case 0: self.__start_cam(self.stacked_face, self.__cam_face)
  154. case 1: self.__start_cam(self.stacked_document, self.__cam_document)
  155. def __start_cam(self, widget: PStackedWidget, cam: str) -> None:
  156. if self.__wwc is None:
  157. widget.to_video()
  158. self.__create_wwc(widget, cam)
  159. self.btn_start_cam.setText(stop_cam)
  160. else:
  161. self.__stop_cam()
  162. widget.to_image()
  163. self.btn_start_cam.setText(start_cam)
  164. def __create_wwc(self, widget: PStackedWidget, cam: str) -> None:
  165. if self.__mode in 'snapshot':
  166. self.__wwc = IPCam()
  167. self.__wwc.qLabel = widget.video
  168. self.__wwc.lnk_connect = cam
  169. self.__wwc.start()
  170. else:
  171. self.__wwc = USBCam(widget.video, cam)
  172. self.__wwc.start_cam()
  173. def __take_image(self) -> None:
  174. if self.tabWidget.currentIndex() == 1:
  175. self.__file_name_document = self.__wwc.cupture_image(
  176. self.stacked_document.image)
  177. self.stacked_document.to_image()
  178. else:
  179. self.__file_name_face = self.__wwc.cupture_image(
  180. self.stacked_face.image)
  181. self.stacked_face.to_image()
  182. # self.__stop_cam()
  183. def __print(self) -> None:
  184. if self.data_propusk is None:
  185. self.__save()
  186. propusk_data = self.data_propusk.copy()
  187. propusk_data.update({
  188. "personal": self.personal_combobox.currentText(),
  189. "place": self.place_combobox.currentText(),
  190. "date_from": self.date_from.dateTime().toString('dd.MM.yyyy hh:mm'),
  191. "date_to": self.date_to.dateTime().toString('dd.MM.yyyy hh:mm'),
  192. "face": self.__file_name_face,
  193. "document": self.__file_name_face
  194. })
  195. render_text = TemplatePropusk(
  196. propusk_data,
  197. os.path.join(os.environ.get('ABSOLUTE_PATH'), 'docs')
  198. )
  199. Printer(str(render_text)).print()
  200. def __clear(self) -> None:
  201. if self.data_propusk is not None:
  202. self.data_propusk = None
  203. self.__set_default_data()
  204. def __save(self) -> None:
  205. date_from = self.date_from.dateTime().toMSecsSinceEpoch() / 1000
  206. date_to = self.date_to.dateTime().toMSecsSinceEpoch() / 1000
  207. self.data_propusk = {
  208. "id_propusk": int(self.number_propusk.text()),
  209. "date_from": datetime.fromtimestamp(date_from),
  210. "date_to": datetime.fromtimestamp(date_to),
  211. "personal": self.personal_combobox.currentData(),
  212. "place": self.place_combobox.currentData(),
  213. "receiving_man": self.receiving_man.toPlainText(),
  214. "purpose_visite": self.purpose_visite.toPlainText(),
  215. "face": F"{Path(self.__file_name_face).name}",
  216. "document": F"{Path(self.__file_name_document).name}"
  217. }
  218. with connect() as conn:
  219. try:
  220. conn.execute(
  221. list_propusk.insert().values(**self.data_propusk)
  222. )
  223. show_dialog(QMessageBox.Icon.Information, '', 'Сохранено')
  224. except OperationalError as er:
  225. logger.error(er)
  226. show_dialog(QMessageBox.Icon.Critical, 'Ошибка',
  227. 'Произошла ошибка при сохранении! \n Обратитесь к администратору')
  228. def __set_default_data(self) -> None:
  229. self.number_propusk.clear()
  230. self.date_from.setDate(QDate().currentDate())
  231. self.date_to.setDate(QDate().currentDate())
  232. if self.__wwc is not None:
  233. self.__stop_cam()
  234. self.stacked_document.to_image()
  235. self.stacked_face.to_image()
  236. load_image(self.stacked_document, os.environ.get('NO_MEDIA_IMAGE'))
  237. load_image(self.stacked_face, os.environ.get('NO_MEDIA_IMAGE'))
  238. self.receiving_man.clear()
  239. self.purpose_visite.clear()
  240. def __stop_cam(self):
  241. if self.__wwc is not None:
  242. self.__wwc.stop_cam()
  243. self.__wwc = None
  244. self.btn_start_cam.setText(start_cam)
  245. def __create_widget_face_cam(self) -> None:
  246. self.gridLayout.removeWidget(self.stacked_face)
  247. self.stacked_face = create_widget_stacked(
  248. name_object=u'stacked_face',
  249. obj=self.tab,
  250. layout=self.gridLayout,
  251. mode=self.__mode)
  252. self.stacked_face.currentChanged.connect(
  253. self.__change_text_btn
  254. )
  255. def __change_text_btn(self) -> None:
  256. if self.stacked_face.currentIndex() == 0:
  257. self.btn_start_cam.setText(stop_cam)
  258. else:
  259. self.btn_start_cam.setText(start_cam)