49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
# bridge.py
|
|
import json
|
|
import time
|
|
from PyQt6.QtCore import QObject, QVariant, pyqtSlot, pyqtSignal, QTimer
|
|
from PyQt6.QtWebChannel import QWebChannel
|
|
from PyQt6.QtWebEngineWidgets import QWebEngineView
|
|
|
|
from enums import KompasCommand
|
|
from parser.main import KompasDocumentParser
|
|
|
|
class PythonEventEmitter(QObject):
|
|
onEvent = pyqtSignal(str, "QVariantMap") # событие: тип + данные
|
|
|
|
|
|
class PythonJSBridge(QObject):
|
|
def __init__(self, browser: QWebEngineView):
|
|
super().__init__()
|
|
self.browser = browser
|
|
|
|
self.channel = QWebChannel()
|
|
self.browser.page().setWebChannel(self.channel)
|
|
|
|
self.emitter = PythonEventEmitter()
|
|
|
|
self.channel.registerObject("pyjs", self)
|
|
self.channel.registerObject("pyjs_events", self.emitter)
|
|
|
|
self.kompas = KompasDocumentParser()
|
|
|
|
@pyqtSlot(str, str, result=str)
|
|
def callFromJS(self, command: str, data_json: str):
|
|
print(f"[Python] Получена команда: {command}")
|
|
data = None
|
|
if command == KompasCommand.OPEN_KOMPAS:
|
|
data = self.kompas.get_open_documents()
|
|
if command == KompasCommand.GET_AVAILABLE_ACTIONS:
|
|
data = self.kompas.get_available_actions()
|
|
if command == KompasCommand.IGES:
|
|
data = self.kompas.save_to_iges()
|
|
if command == KompasCommand.EXPORT_RASTER:
|
|
data = self.kompas.export_to_raster()
|
|
if command == KompasCommand.PROJECT_SUPPORT:
|
|
data = self.kompas.create_drawing_for_parts()
|
|
if command == KompasCommand.STATS:
|
|
data = self.kompas.collect_statistics()
|
|
return json.dumps(data, ensure_ascii=False)
|
|
|
|
def send_event_to_js(self, event_type: str, data: dict):
|
|
self.emitter.onEvent.emit(event_type, data) |