MainWindow.py 10 KB

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