| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  | import base | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  | from base import log | 
					
						
							|  |  |  | import json | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  | import random | 
					
						
							|  |  |  | from sight import Sight | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-13 19:32:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  | class SightFactory(): | 
					
						
							| 
									
										
										
										
											2023-11-13 19:32:09 +00:00
										 |  |  |     __job_jsrun = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.list = {} | 
					
						
							|  |  |  |         self.data = {} #Global data(s) for the sight(s) | 
					
						
							| 
									
										
										
										
											2023-11-13 19:32:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def addJsJob(self, script, page_id): | 
					
						
							|  |  |  |         self.__job_jsrun.addJob(script,page_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-27 13:14:49 +00:00
										 |  |  |     def create(self, id, page): | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |         log.byCode("SF1000E","The page '" + str(page) + "' with '"+str(id)+"' id.") | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |         if str(id) == "main" and self.checkKey(id): | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |             log.warning("SF1000W", "The '"+str(id)+"' window already exist! You can't create more than one 'main' page sight!") | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |             return None | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2023-10-26 15:58:46 +00:00
										 |  |  |         if base.data["sight_mode"] == "unique": | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |             if len(id) == 0: | 
					
						
							|  |  |  |                 print("No ID was given! In single mode need declare an ID to the Sight in advance!") | 
					
						
							|  |  |  |                 return None | 
					
						
							|  |  |  |             if self.checkKey(id): | 
					
						
							| 
									
										
										
										
											2023-10-27 13:14:49 +00:00
										 |  |  |                 print("This ['"+page+"'-'"+str(id)+"'] already exist!") | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2023-10-26 15:58:46 +00:00
										 |  |  |         if base.data["sight_mode"] == "auto": | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |             if len(id) == 0: | 
					
						
							|  |  |  |                 id = self.createUniqueId() | 
					
						
							|  |  |  |             if len(id) > 0 and self.checkKey(id) and str(id) != "main": | 
					
						
							|  |  |  |                 id = self.createUniqueId() | 
					
						
							| 
									
										
										
										
											2023-10-26 15:58:46 +00:00
										 |  |  |         if base.data["sight_mode"] == "normal": | 
					
						
							|  |  |  |             if self.checkKey(id) and len(id) != 0: | 
					
						
							| 
									
										
										
										
											2023-10-27 13:14:49 +00:00
										 |  |  |                 print("This ['"+page+"'-'"+str(id)+"'] already exist!") | 
					
						
							| 
									
										
										
										
											2023-10-26 15:58:46 +00:00
										 |  |  |                 return None | 
					
						
							|  |  |  |             if len(id) == 0: | 
					
						
							|  |  |  |                 id = self.createUniqueId() | 
					
						
							| 
									
										
										
										
											2023-10-27 13:14:49 +00:00
										 |  |  |         self.list[id] = Sight(id, page) | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |         return id   | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |     def show(self, id): | 
					
						
							|  |  |  |         if self.checkKey(id): | 
					
						
							|  |  |  |             self.list[id].show() | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |             log.byCode("SF1000E","The '"+str(id)+"' sight is showed!") | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |             return True | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |         log.byCode("SF1001W", "This '"+str(id)+"' id isn't exist to show!") | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2023-10-26 18:47:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def getSights(self): | 
					
						
							|  |  |  |         return self.list | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |     def get(self, id): | 
					
						
							|  |  |  |         if self.checkKey(id): | 
					
						
							|  |  |  |             return self.list[id] | 
					
						
							| 
									
										
										
										
											2023-10-27 08:58:33 +00:00
										 |  |  |         """print("This '"+str(id)+"' id isn't exist to get!")""" | 
					
						
							| 
									
										
										
										
											2023-10-30 19:43:35 +00:00
										 |  |  |      | 
					
						
							|  |  |  |     def closeChildren(self, id, removeFromParent = True): | 
					
						
							|  |  |  |         """ CLOSE ITS CHILDREN. It doesnt close the children's children just the current parent children
 | 
					
						
							|  |  |  |          will be closed. """
 | 
					
						
							|  |  |  |         for cid in self.get(id).getChildren(): | 
					
						
							|  |  |  |             self.close(cid) | 
					
						
							|  |  |  |             print("CLOSED - " + cid)          | 
					
						
							|  |  |  |         if removeFromParent: | 
					
						
							|  |  |  |             """ If the parent doesn't exist anymore! """ | 
					
						
							|  |  |  |             if self.get(self.get(id).getParent()) is not None: | 
					
						
							|  |  |  |                 children = self.get(self.get(id).getParent()).getChildren() | 
					
						
							|  |  |  |                 if len(children) > 0: | 
					
						
							|  |  |  |                     self.get(self.get(id).getParent()).removeChild(id) | 
					
						
							|  |  |  |                     print("AFTER REMOVED CHILDREN " +  str(self.get(self.get(id).getParent()).getChildren()) ) | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2023-10-30 19:43:35 +00:00
										 |  |  |     def close(self, id, closeChildrenToo=True): | 
					
						
							|  |  |  |         if closeChildrenToo: | 
					
						
							|  |  |  |             self.closeChildren(id) | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |         if self.checkKey(id): | 
					
						
							|  |  |  |             if id == "main": | 
					
						
							|  |  |  |                 print("Program Exit!") | 
					
						
							|  |  |  |                 base.app.exit(0) | 
					
						
							|  |  |  |             self.list[id].close() | 
					
						
							| 
									
										
										
										
											2023-10-30 19:43:35 +00:00
										 |  |  |             if id in self.list: | 
					
						
							|  |  |  |                 self.list[id].deleteLater() | 
					
						
							|  |  |  |                 del self.list[id] | 
					
						
							| 
									
										
										
										
											2023-10-27 08:58:33 +00:00
										 |  |  |             """print("The '"+str(id)+"' is closed now!")""" | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |             return True | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print("The '"+str(id)+"' isn't exist'") | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2023-10-27 08:58:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |     def sendMessage(self, label, sight_list, data): | 
					
						
							|  |  |  |         data = json.dumps(data) | 
					
						
							|  |  |  |         if len(sight_list) == 0: | 
					
						
							|  |  |  |             for sight in self.list.values(): | 
					
						
							|  |  |  |                 sight.browser.page().runJavaScript("message('" + label + "', '"+str(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 + "', '" + str(data) + "')", self.ready) | 
					
						
							| 
									
										
										
										
											2023-10-27 08:58:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def ready(self, returnValue): | 
					
						
							|  |  |  |         if returnValue is not None: | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |             print("RETURN "+returnValue) | 
					
						
							| 
									
										
										
										
											2023-10-27 08:58:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |     def processes(self): | 
					
						
							| 
									
										
										
										
											2023-10-27 08:58:33 +00:00
										 |  |  |         if len(self.list) > 0: | 
					
						
							|  |  |  |             print(self.list) | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |          | 
					
						
							|  |  |  |     def checkKey(self, id): | 
					
						
							|  |  |  |         if id in self.list: | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  |         return False | 
					
						
							| 
									
										
										
										
											2023-10-27 08:58:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |     def addGlobalData(self, key, value): | 
					
						
							|  |  |  |         self.data[key] = value | 
					
						
							| 
									
										
										
										
											2023-10-26 15:58:46 +00:00
										 |  |  |     def getGlobauniquelData(self, key): | 
					
						
							| 
									
										
										
										
											2023-05-17 10:16:15 +00:00
										 |  |  |         if key in self.data: | 
					
						
							|  |  |  |             return self.data[key] | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |     def createUniqueId(self): | 
					
						
							|  |  |  |         new_id = str(random.randint(1000, 9999)) | 
					
						
							|  |  |  |         if self.checkKey(new_id): | 
					
						
							|  |  |  |             self.createUniqueId() | 
					
						
							| 
									
										
										
										
											2023-10-30 19:43:35 +00:00
										 |  |  |         return new_id |