They can send message to the specific sight using the window.sight.message 2nd argument function. There is a new function called sendMessage() which make more simple to send message.

1st argument of the sendMessage is a label.
2nd argument of the sendMessage is a sight ids array if it is empty [] then get everyone.
3rd argument is the data (javascript dict)
main
Balazs Birtalan 2023-10-27 09:58:33 +01:00
parent 617bbe47cc
commit 976fee0004
5 changed files with 106 additions and 53 deletions

View File

@ -1,7 +1,9 @@
import base import base
import json
import random import random
from PyQt6.QtCore import QObject, pyqtSlot from PyQt6.QtCore import QObject, pyqtSlot
class CallHandler(QObject): class CallHandler(QObject):
def __init__(self, sight): def __init__(self, sight):
super().__init__() super().__init__()
@ -58,6 +60,8 @@ class CallHandler(QObject):
print("Active - " + self.sight.getId()) print("Active - " + self.sight.getId())
base.data["active_sight"] = self.sight.getId() base.data["active_sight"] = self.sight.getId()
@pyqtSlot() @pyqtSlot()
def close(self): def close(self):
""" CLOSE ITS CHILDREN. It doesnt close the children's children just the current parent children """ CLOSE ITS CHILDREN. It doesnt close the children's children just the current parent children
@ -71,7 +75,6 @@ class CallHandler(QObject):
base.sights.get(self.sight.getParent()).removeChild(self.sight.getId()) base.sights.get(self.sight.getParent()).removeChild(self.sight.getId())
base.sights.close(self.sight.getId()) base.sights.close(self.sight.getId())
@pyqtSlot() @pyqtSlot()
def setMaximized(self): def setMaximized(self):
self.sight.showMaximized() self.sight.showMaximized()
@ -84,7 +87,8 @@ class CallHandler(QObject):
@pyqtSlot() @pyqtSlot()
def restoreDown(self): def restoreDown(self):
self.sight.showNormal() self.sight.showNormal()
self.sight.setLayoutContentMargins(1,1,1,1) #because if it is no border then I get setgeometry warning what can't be set self.sight.setLayoutContentMargins(1, 1, 1,
1) # because if it is no border then I get setgeometry warning what can't be set
@pyqtSlot() @pyqtSlot()
def setFullscreen(self): def setFullscreen(self):
@ -94,6 +98,7 @@ class CallHandler(QObject):
def getTitle(self): def getTitle(self):
return self.sight.getTitle(); return self.sight.getTitle();
""" """
@pyqtSlot() @pyqtSlot()
def move(self): def move(self):
base.data["move"] = True base.data["move"] = True
@ -112,11 +117,19 @@ class CallHandler(QObject):
new_width = 250 new_width = 250
self.sight.resize(new_width, new_height) self.sight.resize(new_width, new_height)
@pyqtSlot(str) @pyqtSlot(str, str, str)
def message(self, label): def message(self, label, sight_id_json, data):
print("label - " + label) sight_list = json.loads(sight_id_json)
print("Message - " + label)
if len(sight_list) == 0:
for sight in base.sights.getSights().values(): for sight in base.sights.getSights().values():
sight.browser.page().runJavaScript("message('"+label+"')", self.ready) sight.browser.page().runJavaScript("message('" + label + "', '"+data+"')", self.ready)
else:
for sight_id in sight_list:
if base.sights.checkKey(sight_id):
base.sights.get(sight_id).browser.page().runJavaScript("message('" + label + "', '"+data+"')", self.ready)
def ready(self, returnValue): def ready(self, returnValue):
if returnValue is not None:
print(returnValue) print(returnValue)

View File

@ -120,6 +120,20 @@ if(navigator.userAgent.indexOf("QtWebEngine") > 0) {
function addData(key, value, is_global = false) { function addData(key, value, is_global = false) {
window.sight.addData(key,value, is_global); window.sight.addData(key,value, is_global);
} }
/*
label = any unique short message (one word)
sight_id_array = a id list of those sight where the message will be sent to
data = it is a javascript dict (this can contains any key and value) which will be sent
*/
function sendMessage(label, sight_id_array = [], data = {}) {
window.sight.message(label, JSON.stringify(sight_id_array), JSON.stringify(data));
}
function eventMsg(label, where, data) {
return "["+label+"] - " +where+" ("+data+")";
}
function getData(key, is_global = false) { function getData(key, is_global = false) {
window.sight.getData(key, is_global, function(result) { window.sight.getData(key, is_global, function(result) {
alert(result) alert(result)

View File

@ -24,12 +24,17 @@
}); });
function message(label) { function message(label, data) {
if(label === "price_change") { if(label === "price_change") {
window.sight.message("changed_now") sendMessage("changed_now")
return "I update my price - "+label return "I update my price - "+label+"; DATA - "+data
}
if(label === "test") {
return "MAIN GOT TEST MESSAGE"+"; DATA - "+data
} }
} }
</script> </script>
</head> </head>
<body> <body>

View File

@ -9,12 +9,21 @@
<script> <script>
$(document).ready(function(){ $(document).ready(function(){
//getData("name", true); //getData("name", true);
window.sight.message("price_change") sendMessage("price_change")
dict = {}
dict["account_id"] = 10227;
dict["account_name"] = "Test Bank";
dict["details"] = {};
dict["details"]["phone"] = 123456789
dict["details"]["place"] = "New York"
sendMessage("test", ["other","main"], dict)
}); });
function message(label) { function message(label,data) {
if(label === "changed_now") { if(label === "test") {
return "everything alright - " + label data_dict = JSON.parse(data)
return "DATA - "+data_dict["account_name"] + " - " + data_dict["details"]["place"]
} }
} }
</script> </script>

View File

@ -51,29 +51,41 @@ class SightFactory():
def get(self, id): def get(self, id):
if self.checkKey(id): if self.checkKey(id):
return self.list[id] return self.list[id]
print("This '"+str(id)+"' id isn't exist to get!") """print("This '"+str(id)+"' id isn't exist to get!")"""
def close(self, id): def close(self, id):
if self.checkKey(id): if self.checkKey(id):
self.eventMessage("CLOSING", "sight", id)
if id == "main": if id == "main":
print("Program Exit!") print("Program Exit!")
base.app.exit(0) base.app.exit(0)
self.list[id].close() self.list[id].close()
self.list[id].deleteLater() self.list[id].deleteLater()
del self.list[id] del self.list[id]
print("The '"+str(id)+"' is closed now!") """print("The '"+str(id)+"' is closed now!")"""
self.eventMessage("CLOSED", "sight", id)
return True return True
else: else:
print("The '"+str(id)+"' isn't exist'") print("The '"+str(id)+"' isn't exist'")
return False return False
def eventMessage(self, label, where, data):
for sight in self.list.values():
sight.browser.page().runJavaScript("eventMsg('" + label + "', '" + where + "', '" + data + "')", self.ready)
def ready(self, returnValue):
if returnValue is not None:
print(returnValue)
def processes(self): def processes(self):
if len(self.list) > 0:
print(self.list) print(self.list)
def checkKey(self, id): def checkKey(self, id):
if id in self.list: if id in self.list:
return True return True
return False return False
def addGlobalData(self, key, value): def addGlobalData(self, key, value):
self.data[key] = value self.data[key] = value
def getGlobauniquelData(self, key): def getGlobauniquelData(self, key):