MainWindow.py 10.0 KB

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