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 json
import random
from PyQt6.QtCore import QObject, pyqtSlot
class CallHandler(QObject):
def __init__(self, sight):
super().__init__()
@ -14,7 +16,7 @@ class CallHandler(QObject):
@pyqtSlot(str, result=str)
def test2(self, t):
print(t)
return t+"_return"
return t + "_return"
@pyqtSlot(result=str)
def getDefaultTemplate(self):
@ -28,7 +30,7 @@ class CallHandler(QObject):
def setHeight(self, height):
self.sight.resize(self.sight.width(), height);
@pyqtSlot(str, str, result = str)
@pyqtSlot(str, str, result=str)
def create(self, id, type):
new_id = base.sights.create(id, type)
if new_id is not None:
@ -40,14 +42,14 @@ class CallHandler(QObject):
return new_id;
@pyqtSlot(str, str, bool)
def addData(self, key, value, is_global = False):
def addData(self, key, value, is_global=False):
if is_global:
base.sights.addGlobalData(key,value)
base.sights.addGlobalData(key, value)
else:
self.sight.addData(key,value)
self.sight.addData(key, value)
@pyqtSlot(str, bool, result=str)
def getData(self, key, is_global = False):
def getData(self, key, is_global=False):
if is_global:
return base.sights.getGlobalData(key)
return self.sight.getData(key)
@ -58,24 +60,25 @@ class CallHandler(QObject):
print("Active - " + self.sight.getId())
base.data["active_sight"] = self.sight.getId()
@pyqtSlot()
def close(self):
""" CLOSE ITS CHILDREN. It doesnt close the children's children just the current parent children
will be closed. """
for cid in self.sight.getChildren():
base.sights.close(cid)
print("CLOSED - "+cid)
print("CLOSED - " + cid)
""" If the parent doesn't exist anymore! """
if base.sights.get(self.sight.getParent()) is not None:
""" The child will be removed from his parent. So the parent wont be know about that because its child closed. """
base.sights.get(self.sight.getParent()).removeChild(self.sight.getId())
base.sights.close(self.sight.getId())
@pyqtSlot()
def setMaximized(self):
self.sight.showMaximized()
self.sight.setLayoutContentMargins(0,0,0,0) #because there will be a transparented line as a border
self.sight.setLayoutContentMargins(0, 0, 0, 0) # because there will be a transparented line as a border
@pyqtSlot()
def setMinimize(self):
@ -84,7 +87,8 @@ class CallHandler(QObject):
@pyqtSlot()
def restoreDown(self):
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()
def setFullscreen(self):
@ -94,6 +98,7 @@ class CallHandler(QObject):
def getTitle(self):
return self.sight.getTitle();
"""
@pyqtSlot()
def move(self):
base.data["move"] = True
@ -104,19 +109,27 @@ class CallHandler(QObject):
@pyqtSlot()
def resize(self):
new_height = base.mousectrl.position[1]-self.sight.pos().y()
new_width = base.mousectrl.position[0]-self.sight.pos().x()
if(new_height<250):
new_height = base.mousectrl.position[1] - self.sight.pos().y()
new_width = base.mousectrl.position[0] - self.sight.pos().x()
if (new_height < 250):
new_height = 250
if(new_width<250):
if (new_width < 250):
new_width = 250
self.sight.resize( new_width, new_height)
self.sight.resize(new_width, new_height)
@pyqtSlot(str)
def message(self, label):
print("label - " + label)
@pyqtSlot(str, str, str)
def message(self, label, sight_id_json, data):
sight_list = json.loads(sight_id_json)
print("Message - " + label)
if len(sight_list) == 0:
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):
if returnValue is not None:
print(returnValue)

View File

@ -120,6 +120,20 @@ if(navigator.userAgent.indexOf("QtWebEngine") > 0) {
function addData(key, value, is_global = false) {
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) {
window.sight.getData(key, is_global, function(result) {
alert(result)

View File

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

View File

@ -9,12 +9,21 @@
<script>
$(document).ready(function(){
//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) {
if(label === "changed_now") {
return "everything alright - " + label
function message(label,data) {
if(label === "test") {
data_dict = JSON.parse(data)
return "DATA - "+data_dict["account_name"] + " - " + data_dict["details"]["place"]
}
}
</script>

View File

@ -51,29 +51,41 @@ class SightFactory():
def get(self, id):
if self.checkKey(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):
if self.checkKey(id):
self.eventMessage("CLOSING", "sight", id)
if id == "main":
print("Program Exit!")
base.app.exit(0)
self.list[id].close()
self.list[id].deleteLater()
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
else:
print("The '"+str(id)+"' isn't exist'")
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):
if len(self.list) > 0:
print(self.list)
def checkKey(self, id):
if id in self.list:
return True
return False
def addGlobalData(self, key, value):
self.data[key] = value
def getGlobauniquelData(self, key):