def doubleClicked(self, vp): if not vp.Object.Running: vp.Object.Proxy.cmd_server = com.startServer( vp.Object.Address, vp.Object.Port) if isinstance(vp.Object.Proxy.cmd_server, int): if vp.Object.Proxy.cmd_server == \ com.SERVER_ERROR_INVALID_ADDRESS: QMessageBox.warning( None, 'Error while starting server', "The address was not in supported " + "format.") elif vp.Object.Proxy.cmd_server == \ com.SERVER_ERROR_PORT_OCCUPIED: QMessageBox.warning( None, 'Error while starting server', "The port requested is already " + "occupied.") else: vp.Object.setEditorMode("Address", 1) vp.Object.setEditorMode("Port", 1) vp.Object.Running = True self._icon = path.join(PATH_TO_ICONS, "ServerRunning.png") elif vp.Object.Running: vp.Object.Proxy.cmd_server.close() vp.Object.setEditorMode("Address", 0) vp.Object.setEditorMode("Port", 0) vp.Object.Running = False self._icon = path.join(PATH_TO_ICONS, "Server.png") return True
def setProperties(self, fp): # Check properties are present and create them if not if not hasattr(fp, "Address"): fp.addProperty( "App::PropertyString", "Address", "Server settings", "IP address where the server will listen for " + "connection.\nValid values are IPv4 and " + "IPv6 addresses or 'localhost'string.").Address = "localhost" if not hasattr(fp, "Port"): fp.addProperty( "App::PropertyIntegerConstraint", "Port", "Server settings", "Port where the server will " + "listen for connections.\n" + "Valid port numbers are in range <0 | 65535>,\n" + "but some may be already taken!").Port = (54321, 0, 65535, 1) else: fp.Port = (fp.Port, 0, 65535, 1) if not hasattr(fp, "Running"): fp.addProperty( "App::PropertyBool", "Running", "Server settings", "If Server Running is true, then Server listens " + "for new connections.").Running = False # hide Placement property as there is nothing to display/move fp.setEditorMode("Placement", 2) # make Running property read-only as it's set from context menu/ # by double clicking fp.setEditorMode("Running", 1) # try to start cmd_server, if it was running before closing if fp.Running: self.cmd_server = com.startServer(fp.Address, fp.Port) if self.cmd_server == com.SERVER_ERROR_INVALID_ADDRESS: fp.ViewObject.Proxy._icon = path.join(PATH_TO_ICONS, "Server.png") QMessageBox.warning( None, 'Error while starting server', "The address was not in supported format.") fp.Running = False elif self.cmd_server == com.SERVER_ERROR_PORT_OCCUPIED: fp.ViewObject.Proxy._icon = path.join(PATH_TO_ICONS, "Server.png") QMessageBox.warning(None, 'Error while starting server', "The port requested is already occupied.") fp.Running = False else: fp.setEditorMode("Address", 1) fp.setEditorMode("Port", 1) fp.Running = True fp.ViewObject.Proxy._icon = path.join(PATH_TO_ICONS, "ServerRunning.png") # Make an document observer to be notified when document will be closed import AnimateDocumentObserver AnimateDocumentObserver.addObserver() FreeCAD.animate_observer.addServerToNotify(self, FreeCAD.ActiveDocument.Name)
def doubleClicked(self, vp): """ Method called when `FeaturePython` Server is double-clicked in the Tree View. This methods tries to start ServerProxy's `cmd_server` if it wasn't running and closes it in the opposite case. It shows warning dialogs if something failed. If action is successful, then the icon in the Tree View is changed (You may need to recompute the document to see the change). Args: vp: A double-clicked `Gui.ViewProviderDocumentObject` Server.ViewObject. Returns: True to specify that it was implemented and executed. """ if not vp.Object.Running: vp.Object.Proxy.cmd_server = com.startServer( vp.Object.Address, vp.Object.Port) if isinstance(vp.Object.Proxy.cmd_server, int): if vp.Object.Proxy.cmd_server == \ com.SERVER_ERROR_INVALID_ADDRESS: QMessageBox.warning( None, 'Error while starting server', "The address was not in supported " + "format.") elif vp.Object.Proxy.cmd_server == \ com.SERVER_ERROR_PORT_OCCUPIED: QMessageBox.warning( None, 'Error while starting server', "The port requested is already " + "occupied.") else: vp.Object.setEditorMode("Address", 1) vp.Object.setEditorMode("Port", 1) vp.Object.Running = True self._icon = path.join(PATH_TO_ICONS, "ServerRunning.png") elif vp.Object.Running: vp.Object.Proxy.cmd_server.close() vp.Object.setEditorMode("Address", 0) vp.Object.setEditorMode("Port", 0) vp.Object.Running = False self._icon = path.join(PATH_TO_ICONS, "Server.png") return True