def textChanged(self, text): from blocks.RenderableBlock import RenderableBlock from blocks.BlockStub import BlockStub if (self.labelType == BlockLabel.Type.NAME_LABEL or self.labelType == BlockLabel.Type.PORT_LABEL) and (Block.getBlock( self.blockID).isLabelEditable()): if (self.labelType == (BlockLabel.Type.NAME_LABEL)): Block.getBlock(self.blockID).setBlockLabel(text) plug = Block.getBlock(self.blockID).getPlug() # Check if we're connected to a block. If we are and the the block we're connected to # has stubs, update them. if (plug != None and plug.blockID != Block.NULL): if (Block.getBlock(plug.blockID) != None): if (Block.getBlock(plug.blockID).isProcedureDeclBlock() and Block.getBlock(plug.blockID).hasStubs()): # Blocks already store their socket names when saved so it is not necessary # nor desired to call the connectors changed event again. if (Block.getRenderableBlock( plug.blockID).isLoading()): BlockStub.parentConnectorsChanged( self.workspace, plug.blockID) rb = RenderableBlock.getRenderableBlock(self.blockID) if (rb != None and not rb.isLoading): rb.reformBlockShape() rb.updateBuffImg() #rb.repaint() pass
def textChanged(self, text): from blocks.RenderableBlock import RenderableBlock from blocks.BlockStub import BlockStub if (self.labelType == BlockLabel.Type.NAME_LABEL or self.labelType == BlockLabel.Type.PORT_LABEL) and (Block.getBlock(self.blockID).isLabelEditable()): if (self.labelType == (BlockLabel.Type.NAME_LABEL)): Block.getBlock(self.blockID).setBlockLabel(text); plug = Block.getBlock(self.blockID).getPlug(); # Check if we're connected to a block. If we are and the the block we're connected to # has stubs, update them. if (plug != None and plug.blockID != Block.NULL): if (Block.getBlock(plug.blockID) != None): if(Block.getBlock(plug.blockID).isProcedureDeclBlock() and Block.getBlock(plug.blockID).hasStubs()): # Blocks already store their socket names when saved so it is not necessary # nor desired to call the connectors changed event again. if (Block.getRenderableBlock(plug.blockID).isLoading()): BlockStub.parentConnectorsChanged(self.workspace, plug.blockID); rb = RenderableBlock.getRenderableBlock(self.blockID); if(rb != None and not rb.isLoading): rb.reformBlockShape() rb.updateBuffImg() #rb.repaint() pass
def appendRightSidePath(self, painterPath, connectedBlock, connectedBlockShape): ''' * Appends the right side path of the stack of blocks connected to the specified connectedBlock. If there are * some empty sockets, this method will append empty placeholders. * @param painterPath the GeneralPath to append the new path to * @param connectedBlock the Block instance whose right side of its stack of connected blocks will be appened to the * specified painterPath * @param connectedBlockShape the BlockShape of the specified connectedBlock ''' from blocks.RenderableBlock import RenderableBlock # int lastBottomPathWidth; # append top side of connected block BlockShapeUtil.appendPath(painterPath, connectedBlockShape.getTopSide(), False) startX = painterPath.currentPosition().x() for socket in connectedBlock.getSockets(): # Sets the current x-coordinate to the start x-coordinate # Makes it so path movements created by previous blocks don't affect # the subsequent blocks. painterPath.lineTo(startX, painterPath.currentPosition().y()) if (socket.blockID == Block.NULL): # just draw an empty socket placeholder # if its the first socket, draw a top side painterPath.lineTo( painterPath.currentPosition().x() + BlockShape.BOTTOM_SOCKET_SIDE_SPACER, painterPath.currentPosition().y()) # now draw the empty right socket side # draw first socket - down right side BlockShape.BCS.addDataSocket(painterPath, socket.getKind(), False) # TODO:lastBottomPathWidth = (int)BOTTOM_SOCKET_SIDE_SPACER; else: # a block is connected to this socket, check if that block has sockets # OR if the block is an infix block - if it is infix, then just wrap around the infix block block = Block.getBlock(socket.blockID) shape = RenderableBlock.getRenderableBlock( socket.blockID).getBlockShape() if (block.getNumSockets() == 0 or block.isInfix()): # append this block's top and right side # TODO instead of just appending the right side...draw line to BlockShapeUtil.appendPath(painterPath, shape.getTopSide(), False) BlockShapeUtil.appendPath(painterPath, shape.getRightSide(), False) else: self.appendRightSidePath(painterPath, block, shape) # Updates the maximum X-coordinate and sets the current point to self.maxX if (self.maxX < painterPath.currentPosition().x()): self.maxX = painterPath.currentPosition().x() painterPath.lineTo(self.maxX, painterPath.currentPosition().y())
def labelChanged(self, label): from blocks.RenderableBlock import RenderableBlock if(self.widget.hasMenu): oldBlock = Block.getBlock(self.blockID); oldBlock.changeLabelTo(label); rb = RenderableBlock.getRenderableBlock(self.blockID); rb.repaintBlock();
def labelChanged(self, label): from blocks.RenderableBlock import RenderableBlock if (self.widget.hasMenu): oldBlock = Block.getBlock(self.blockID) oldBlock.changeLabelTo(label) rb = RenderableBlock.getRenderableBlock(self.blockID) rb.repaintBlock()
def __init__(self, parent): from blocks.RenderableBlock import RenderableBlock ShadowLabel.__init__(self, parent) self.labelWidget = parent self.block = self.labelWidget.getBlock() self.rb = RenderableBlock.getRenderableBlock(self.block.blockID) self.setStyleSheet("border-radius: 3px; border:1px solid rgb(255, 255, 255,150); background-color : rgb(200, 200, 200,150);") self.lastSelectedItem = None self.isVariable = False self.familyMap = {}
def __init__(self, parent): from blocks.RenderableBlock import RenderableBlock ShadowLabel.__init__(self, parent) self.labelWidget = parent self.block = self.labelWidget.getBlock() self.rb = RenderableBlock.getRenderableBlock(self.block.blockID) self.setStyleSheet( "border-radius: 3px; border:1px solid rgb(255, 255, 255,150); background-color : rgb(200, 200, 200,150);" ) self.lastSelectedItem = None self.isVariable = False self.familyMap = {}
def onRenameVariable(self, old_name, new_name): from blocks.RenderableBlock import RenderableBlock from blocks.FactoryRenderableBlock import FactoryRenderableBlock from blocks.BlockGenus import BlockGenus rb = RenderableBlock.getRenderableBlock(self.blockID) block = Block.getBlock(self.blockID) familyMap = block.getCustomerFamily() findVar = False for key in familyMap: if (familyMap[key] == old_name): familyMap[key] = new_name findVar = True if (not findVar): familyMap[new_name] = new_name #self.labelChanged(new_name) #return #factoryBlock = rb.factoryRB #for rb in factoryBlock.child_list: # blockLabel = rb.blockLabel # #blockLabel.widget.setMenu(); # if(blockLabel.getText() == old_name): # blockLabel.labelChanged(new_name) #factoryBlock.blockLabel.widget.setMenu() #if(factoryBlock.blockLabel.getText() == old_name): # factoryBlock.blockLabel.labelChanged(new_name) #print(block.getGenus().familyName) #print(BlockGenus.familyBlocks) if (block.getGenus().familyName in BlockGenus.familyBlocks): for genus in BlockGenus.familyBlocks[block.getGenus().familyName]: genusName = genus.genusName if genusName not in FactoryRenderableBlock.factoryRBs: continue factoryBlock = FactoryRenderableBlock.factoryRBs[genusName] for rb in factoryBlock.child_list: blockLabel = rb.blockLabel #blockLabel.widget.setMenu(); if (blockLabel.getText() == old_name): blockLabel.labelChanged(new_name) #factoryBlock.blockLabel.widget.setMenu(); if (factoryBlock.blockLabel.getText() == old_name): #print(factoryBlock) factoryBlock.blockLabel.labelChanged(new_name)
def onRenameVariable(self, old_name, new_name): from blocks.RenderableBlock import RenderableBlock from blocks.FactoryRenderableBlock import FactoryRenderableBlock from blocks.BlockGenus import BlockGenus rb = RenderableBlock.getRenderableBlock(self.blockID); block = Block.getBlock(self.blockID) familyMap = block.getCustomerFamily(); findVar = False for key in familyMap: if(familyMap[key] == old_name): familyMap[key] = new_name findVar = True if(not findVar): familyMap[new_name] = new_name #self.labelChanged(new_name) #return #factoryBlock = rb.factoryRB #for rb in factoryBlock.child_list: # blockLabel = rb.blockLabel # #blockLabel.widget.setMenu(); # if(blockLabel.getText() == old_name): # blockLabel.labelChanged(new_name) #factoryBlock.blockLabel.widget.setMenu() #if(factoryBlock.blockLabel.getText() == old_name): # factoryBlock.blockLabel.labelChanged(new_name) #print(block.getGenus().familyName) #print(BlockGenus.familyBlocks) if(block.getGenus().familyName in BlockGenus.familyBlocks): for genus in BlockGenus.familyBlocks[block.getGenus().familyName]: genusName = genus.genusName if genusName not in FactoryRenderableBlock.factoryRBs: continue factoryBlock = FactoryRenderableBlock.factoryRBs[genusName] for rb in factoryBlock.child_list: blockLabel = rb.blockLabel #blockLabel.widget.setMenu(); if(blockLabel.getText() == old_name): blockLabel.labelChanged(new_name) #factoryBlock.blockLabel.widget.setMenu(); if(factoryBlock.blockLabel.getText() == old_name): #print(factoryBlock) factoryBlock.blockLabel.labelChanged(new_name)
def __init__(self, parent, initLabelText, prefix, suffix, labelType, isEditable, blockID, hasComboPopup, tooltipBackground): from blocks.RenderableBlock import RenderableBlock from blocks.FactoryRenderableBlock import FactoryRenderableBlock self.widget = LabelWidget(parent, blockID, initLabelText, prefix, suffix, Block.getBlock(blockID).getColor().darker(), tooltipBackground) self.parent = parent self.zoom = 1.0 self.initLabelText = initLabelText self.labelType = labelType self.blockID = blockID self.hasComboPopup = hasComboPopup # Only editable if the isEditable parameter was true, the label is either a Block's name or # socket label, the block can edit labels, and the block is not in the factory. self.widget.setEditable( isEditable and (labelType == BlockLabel.Type.NAME_LABEL or labelType == BlockLabel.Type.PORT_LABEL) and Block.getBlock(blockID).isLabelEditable() and not isinstance(RenderableBlock.getRenderableBlock(blockID), FactoryRenderableBlock)) if (labelType == None or labelType == (BlockLabel.Type.NAME_LABEL)): self.widget.setFont(BlockLabel.blockFontLarge_Bold) elif (labelType == (BlockLabel.Type.PAGE_LABEL)): self.widget.setFont(BlockLabel.blockFontMedium_Bold) elif (labelType == (BlockLabel.Type.PORT_LABEL)): self.widget.setFont(BlockLabel.blockFontMedium_Bold) elif (labelType == (BlockLabel.Type.DATA_LABEL)): self.widget.setFont(BlockLabel.blockFontMedium_Bold) # set initial text self.widget.updateLabelText(initLabelText) # add and show the textLabel initially self.widget.setEditingState(False) #famList = {} #if (Block.getBlock(blockID).hasSiblings()) : # famList = Block.getBlock(blockID).getSiblingsList(); #self.widget.setMenu(); self.widget.fireTextChanged = self.textChanged self.widget.fireGenusChanged = self.labelChanged self.widget.fireMenuChanged = self.onRenameVariable
def update(self): from blocks.RenderableBlock import RenderableBlock rb = RenderableBlock.getRenderableBlock(self.blockID); if (rb != None): x = 0; y = 0; if(rb.getBlock().isCommandBlock()): x+=5; #if(rb.getBlock().isDeclaration()): x+=12; if(rb.getBlock().hasPlug()): x+=4+BlockConnectorShape.getConnectorDimensions(rb.getBlock().getPlug()).width(); if(rb.getBlock().isInfix() and rb.getBlock().getSocketAt(0) != None): if(not rb.getBlock().getSocketAt(0).hasBlock()): x+=30; else: if(rb.getSocketSpaceDimension(rb.getBlock().getSocketAt(0)) != None): x+= rb.getSocketSpaceDimension(rb.getBlock().getSocketAt(0)).width(); if(rb.getBlockWidget()==None): y+=rb.getAbstractBlockArea().controlPointRect().height()/2; else: y+=8; if(rb.getBlock().isCommandBlock()): y-=2; if(rb.getBlock().hasPageLabel() and rb.getBlock().hasAfterConnector()): y-=BlockConnectorShape.CONTROL_PLUG_HEIGHT; if(not rb.getBlock().hasPageLabel()): y-=self.getAbstractHeight()/2; # Comment Label and Collapse Label take up some additional amount of space x += rb.getControlLabelsWidth(); # if block is collapsed keep the name label from moving y += BlockConnectorShape.CONTROL_PLUG_HEIGHT/2 if rb.isCollapsed() else 0; #y+=3 x=self.rescale(x); y=self.rescale(y); self.setPixelLocation(x, y);
def update(self): from blocks.RenderableBlock import RenderableBlock rb = RenderableBlock.getRenderableBlock(self.blockID); if (rb != None): x = 0; y = 0; y += (rb.getBlockHeight()/rb.getZoom() - 22 + (BlockConnectorShape.CONTROL_PLUG_HEIGHT if self.isActive() else 0)); x += 12; x=rb.rescale(x); y=rb.rescale(y); self.move(x, y); self.resize(rb.rescale(14), rb.rescale(14)); if (self.isActive()): self.setText("+"); else: self.setText("-");
def __init__(self,parent, initLabelText, prefix, suffix, labelType, isEditable, blockID, hasComboPopup, tooltipBackground): from blocks.RenderableBlock import RenderableBlock from blocks.FactoryRenderableBlock import FactoryRenderableBlock self.widget= LabelWidget(parent, blockID, initLabelText, prefix, suffix, Block.getBlock(blockID).getColor().darker(), tooltipBackground) self.parent = parent self.zoom = 1.0 self.initLabelText = initLabelText self.labelType = labelType self.blockID = blockID self.hasComboPopup = hasComboPopup # Only editable if the isEditable parameter was true, the label is either a Block's name or # socket label, the block can edit labels, and the block is not in the factory. self.widget.setEditable( isEditable and (labelType == BlockLabel.Type.NAME_LABEL or labelType == BlockLabel.Type.PORT_LABEL) and Block.getBlock(blockID).isLabelEditable() and not isinstance(RenderableBlock.getRenderableBlock(blockID),FactoryRenderableBlock)); if(labelType == None or labelType == (BlockLabel.Type.NAME_LABEL)): self.widget.setFont(BlockLabel.blockFontLarge_Bold); elif(labelType == (BlockLabel.Type.PAGE_LABEL)): self.widget.setFont(BlockLabel.blockFontMedium_Bold); elif(labelType ==(BlockLabel.Type.PORT_LABEL)): self.widget.setFont(BlockLabel.blockFontMedium_Bold); elif(labelType == (BlockLabel.Type.DATA_LABEL)): self.widget.setFont(BlockLabel.blockFontMedium_Bold); # set initial text self.widget.updateLabelText(initLabelText); # add and show the textLabel initially self.widget.setEditingState(False); #famList = {} #if (Block.getBlock(blockID).hasSiblings()) : # famList = Block.getBlock(blockID).getSiblingsList(); #self.widget.setMenu(); self.widget.fireTextChanged = self.textChanged self.widget.fireGenusChanged = self.labelChanged self.widget.fireMenuChanged = self.onRenameVariable
def loadBlocksFrom(self,blockWorkspaceInfo): from blocks.FactoryRenderableBlock import FactoryRenderableBlock loadedBlocks = [] if('rb_list' in blockWorkspaceInfo): rb_info_list = blockWorkspaceInfo['rb_list'] for rb_info in rb_info_list: rb = RenderableBlock.loadBlockNode(rb_info, self); factoryBlock = FactoryRenderableBlock.factoryRBs[rb.getBlock().getGenusName()] factoryBlock.child_list.append(rb) rb.setParent(self.canvas) self.blockDropped(rb) loadedBlocks.append(rb) for rb in loadedBlocks: rb.reformBlockShape() rb.show() for rb in self.getTopLevelBlocks(): rb.redrawFromTop() if('Families' in blockWorkspaceInfo): self.families = blockWorkspaceInfo['Families'] return loadedBlocks
def makeBottomSide(self): ''' * Overrided from BlockShape. * Takes into account the need to resize the dimensions of an infix block for various cases. ''' from blocks.RenderableBlock import RenderableBlock from blocks.BlockShapeUtil import BlockShapeUtil # Reset the maximum X-coordinate so the infix block can resize if you remove blocks within it self.maxX = 0 # start bottom-right self.setEndPoint(self.gpBottom, self.botLeftCorner, self.topLeftCorner, True) #curve down and right BlockShapeUtil.cornerTo(self.gpBottom, self.botLeftCorner, self.botRightCorner, self.blockCornerRadius) # BOTTOM SOCKETS # for each socket in the iterator socketCounter = 0 #need to use this to determine which socket we're on for curSocket in self.block.getSockets(): #if bottom socket if (curSocket.getPositionType() == BlockConnector.PositionType.BOTTOM): # move away from bottom left corner if (socketCounter > 0): self.gpBottom.lineTo( self.gpBottom.currentPosition().x() + BlockShape.BOTTOM_SOCKET_MIDDLE_SPACER, self.gpBottom.currentPosition().y()) else: self.gpBottom.lineTo( self.gpBottom.currentPosition().x() + BlockShape.BOTTOM_SOCKET_SIDE_SPACER, self.gpBottom.currentPosition().y()) # move down so bevel doesn't screw up from connecting infinitely sharp corner # as occurs from a curved port BlockShapeUtil.lineToRelative(self.gpBottom, 0, -0.1) #////////////////////// #//begin drawing socket #////////////////////// if (curSocket.blockID == Block.NULL): # draw first socket - up left side leftSocket = BlockShape.BCS.addDataSocketUp( self.gpBottom, curSocket.type, True) self.rb.updateSocketPoint(curSocket, leftSocket) # System.out.println("socket poitn: "+rb.getSocketPoint(curSocket)); # System.out.println("socket poitn leftsocket: "+leftSocket); # draw left standard empty socket space - top side self.gpBottom.lineTo( self.gpBottom.currentPosition().x() + BlockShape.BOTTOM_SOCKET_SIDE_SPACER, self.gpBottom.currentPosition().y()) #draw first socket - down right side BlockShape.BCS.addDataSocket(self.gpBottom, curSocket.type, False) #rb.updateSocketPoint(curSocket, rightSocket); else: # there is a connected block connectedBlock = Block.getBlock(curSocket.blockID) connectedRBlock = RenderableBlock.getRenderableBlock( curSocket.blockID) if (connectedBlock == None or connectedRBlock == None): continue # calculate and update the new socket point # update the socket point of this cursocket which should now adopt the plug socket point of its # connected block since we're also adopting the left side of its shape # Use coordinates when the zoom level is 1.0 to calculate socket point unzoomX = connectedRBlock.getSocketPixelPoint( connectedBlock.getPlug()).x( ) / connectedRBlock.getZoom() unzoomY = connectedRBlock.getSocketPixelPoint( connectedBlock.getPlug()).y( ) / connectedRBlock.getZoom() connectedBlockSocketPoint = QtCore.QPoint( unzoomX, unzoomY) currentPoint = self.gpBottom.currentPosition() newX = connectedBlockSocketPoint.x() + abs( connectedBlockSocketPoint.x() - currentPoint.x()) newY = connectedBlockSocketPoint.y() + abs( connectedRBlock.getBlockHeight() / connectedRBlock.getZoom() - currentPoint.y()) self.rb.updateSocketPoint(curSocket, QtCore.QPoint(newX, newY)) self.gpBottom.currentPosition().x() connectedBlockShape = RenderableBlock.getRenderableBlock( curSocket.blockID).getBlockShape() #append left side of connected block connectedBlockShape.reformArea() BlockShapeUtil.appendPath( self.gpBottom, connectedBlockShape.getLeftSide(), False) connectedBlockShape.getLeftSide().currentPosition() self.gpBottom.currentPosition().x() # append right side of connected block (more complicated) if (connectedBlock.getNumSockets() == 0 or connectedBlock.isInfix()): # append top side of connected block BlockShapeUtil.appendPath( self.gpBottom, connectedBlockShape.getTopSide(), False) BlockShapeUtil.appendPath( self.gpBottom, connectedBlockShape.getRightSide(), False) else: # iterate through the sockets of the connected block, checking if # it has blocks connected to them self.appendRightSidePath(self.gpBottom, connectedBlock, connectedBlockShape) # Updates the maximum X-coordinate and sets the current point to self.maxX if (self.maxX < self.gpBottom.currentPosition().x()): self.maxX = self.gpBottom.currentPosition().x() self.gpBottom.lineTo(self.maxX, self.gpBottom.currentPosition().y()) # bump down so bevel doesn't screw up BlockShapeUtil.lineToRelative(self.gpBottom, 0, 0.1) # System.out.println("gpbottom starting point: "+gpBottom.currentPosition()); # draw RIGHT to create divider //// if (socketCounter < self.block.getNumSockets() - 1): self.gpBottom.lineTo( #need to add the width of the block label. warning: this assumes that there is only one block label self.gpBottom.currentPosition().x() + BlockShape.BOTTOM_SOCKET_MIDDLE_SPACER + self.rb.accomodateLabelsWidth(), self.gpBottom.currentPosition().y()) else: self.gpBottom.lineTo( self.gpBottom.currentPosition().x() + BlockShape.BOTTOM_SOCKET_SIDE_SPACER, self.gpBottom.currentPosition().y()) socketCounter += 1 #curve right and up BlockShapeUtil.cornerTo(self.gpBottom, self.botRightCorner, self.topRightCorner, self.blockCornerRadius) #end bottom self.setEndPoint(self.gpBottom, self.botRightCorner, self.topRightCorner, False)
def cloneBlock(myblock, canvas): mygenusname = myblock.getGenusName(); label = myblock.getBlockLabel(); # sometimes the factory block will have an assigned label different # from its genus label. if(myblock.getInitialLabel() != (myblock.getBlockLabel())): # acquire prefix and suffix length from myblock label prefixLength = len(myblock.getLabelPrefix()); suffixLength = len(myblock.getLabelSuffix()); # we need to set the block label without the prefix and suffix attached because those # values are automatically concatenated to the string specified in setBlockLabel. I know its # weird, but its the way block labels were designed. if(prefixLength > 0 or suffixLength > 0): # TODO we could do this outside of this method, even in constructor label = myblock.getBlockLabel() # check genus instance counter and if label unique - change label accordingly # also check if label already has a value at the end, if so update counter to have the max value # TODO ria need to make this smarter # some issues to think about: # - what if they throw out an instance, such as setup2? should the next time they take out # a setup block, should it have setup2 on it? but wouldn't that be confusing? # - when we load up a new project with some instances with numbered labels, how do we keep # track of new instances relative to these old ones? # - the old implementation just iterated through all the instances of a particular genus in the # workspace and compared a possible label to the current labels of that genus. if there wasn't # any current label that matched the possible label, it returned that label. do we want to do this? # is there something more efficient? labelWithIndex = label; # labelWithIndex will have the instance value # initialize value that will be appended to the end of the label if(mygenusname in BlockUtilities.instanceCounter): value = BlockUtilities.instanceCounter[mygenusname]; else: value = 0; # begin check for validation of label # iterate until label is valid while(not BlockUtilities.isLabelValid(myblock, labelWithIndex, canvas)): value +=1; labelWithIndex = labelWithIndex + value; # set valid label and save current instance number BlockUtilities.instanceCounter[mygenusname] = value if(labelWithIndex != (label)): # only set it if the label actually changed... label = labelWithIndex; if(isinstance(myblock,BlockStub)): parent = myblock.getParent(); block = BlockStub(parent.blockID, parent.getGenusName(), parent.getBlockLabel(), myblock.getGenusName()); else: block = Block.createBlock(canvas, myblock.getGenusName(), True, label); # TODO - djwendel - create a copy of the RB properties too, using an RB copy constructor. Don't just use the genus. # RenderableBlock renderable = new RenderableBlock(this.getParentWidget(), block.blockID); renderable = RenderableBlock.from_blockID(canvas, block.blockID); #renderable.initFinished = False #renderable.setZoomLevel(BlockUtilities.zoom); #renderable.redrawFromTop(); #renderable.repaint(); return renderable;
def __init__(self, workspaceWidget): RenderableBlock.__init__(self, workspaceWidget)
def connect(self): from blocks.RenderableBlock import RenderableBlock from blocks.BlockLinkChecker import BlockLinkChecker from blocks.BlockConnectorShape import BlockConnectorShape # Make sure to disconnect any connections that are going to be overwritten # by this new connection. For example, if inserting a block between two # others, make sure to break that original link.*/ if (self.socket.hasBlock()): # save the ID of the block previously attached to (in) this # socket. This is used by insertion rules to re-link the replaced # block to the newly-inserted block. self.lastPlugBlockID = self.socket.blockID # break the link between the socket block and the block in that socket plugBlock = Block.getBlock(self.lastPlugBlockID) plugBlockPlug = BlockLinkChecker.getPlugEquivalent(plugBlock) if (plugBlockPlug != None and plugBlockPlug.hasBlock()): socketBlock = Block.getBlock(plugBlockPlug.blockID) link = BlockLink.getBlockLink(plugBlock, socketBlock, plugBlockPlug, self.socket) link.disconnect() # don't tell the block about the disconnect like we would normally do, because # we don't actually want it to have a chance to remove any expandable sockets # since the inserted block will be filling whatever socket was vacated by this # broken link. #NOTIFY WORKSPACE LISTENERS OF DISCONNECTION (not sure if this is great because the connection is immediately replaced) #Workspace.getInstance().notifyListeners(new WorkspaceEvent(RenderableBlock.getRenderableBlock(socketBlock.blockID).getParentWidget(), link, WorkspaceEvent.BLOCKS_DISCONNECTED)); if (self.plug.hasBlock()): # in the case of insertion, breaking the link above will mean that # the plug shouldn't be connected by the time we reach here. This # exception will only be thrown if the plug is connected even # after any insertion-esq links were broken above #throw new RuntimeException("trying to link a plug that's already connected somewhere."); return # actually form the connection self.plug.setConnectorBlockID(self.socketBlockID) self.socket.setConnectorBlockID(self.plugBlockID) # notify renderable block of connection so it can redraw with stretching socketRB = RenderableBlock.getRenderableBlock(self.socketBlockID) socketRB.blockConnected(self.socket, self.plugBlockID) if (self.getLastBlockID() != None and self.getLastBlockID() != Block.NULL and BlockConnectorShape.isCommandConnector(self.getPlug()) and BlockConnectorShape.isCommandConnector(self.getSocket())): top = Block.getBlock(self.getPlugBlockID()) while (top.hasAfterConnector() and top.getAfterConnector().hasBlock()): top = Block.getBlock(top.getAfterBlockID()) bottom = Block.getBlock(self.getLastBlockID()) # For safety: if either the top stack is terminated, or # the bottom stack is not a starter, don't try to force a link if (top == None or bottom == None or not top.hasAfterConnector() or not bottom.hasBeforeConnector()): return link = BlockLink.getBlockLink(top, bottom, top.getAfterConnector(), bottom.getBeforeConnector()) link.connect() if (self.clickSound != None): # System.out.println("playing click sound"); pass
def connect(self): from blocks.RenderableBlock import RenderableBlock from blocks.BlockLinkChecker import BlockLinkChecker from blocks.BlockConnectorShape import BlockConnectorShape # Make sure to disconnect any connections that are going to be overwritten # by this new connection. For example, if inserting a block between two # others, make sure to break that original link.*/ if (self.socket.hasBlock()): # save the ID of the block previously attached to (in) this # socket. This is used by insertion rules to re-link the replaced # block to the newly-inserted block. self.lastPlugBlockID = self.socket.blockID; # break the link between the socket block and the block in that socket plugBlock = Block.getBlock(self.lastPlugBlockID); plugBlockPlug = BlockLinkChecker.getPlugEquivalent(plugBlock); if (plugBlockPlug != None and plugBlockPlug.hasBlock()): socketBlock = Block.getBlock(plugBlockPlug.blockID); link = BlockLink.getBlockLink(plugBlock, socketBlock, plugBlockPlug, self.socket); link.disconnect(); # don't tell the block about the disconnect like we would normally do, because # we don't actually want it to have a chance to remove any expandable sockets # since the inserted block will be filling whatever socket was vacated by this # broken link. #NOTIFY WORKSPACE LISTENERS OF DISCONNECTION (not sure if this is great because the connection is immediately replaced) #Workspace.getInstance().notifyListeners(new WorkspaceEvent(RenderableBlock.getRenderableBlock(socketBlock.blockID).getParentWidget(), link, WorkspaceEvent.BLOCKS_DISCONNECTED)); if (self.plug.hasBlock()): # in the case of insertion, breaking the link above will mean that # the plug shouldn't be connected by the time we reach here. This # exception will only be thrown if the plug is connected even # after any insertion-esq links were broken above #throw new RuntimeException("trying to link a plug that's already connected somewhere."); return # actually form the connection self.plug.setConnectorBlockID(self.socketBlockID); self.socket.setConnectorBlockID(self.plugBlockID); # notify renderable block of connection so it can redraw with stretching socketRB = RenderableBlock.getRenderableBlock(self.socketBlockID); socketRB.blockConnected(self.socket, self.plugBlockID); if (self.getLastBlockID() != None and self.getLastBlockID() != Block.NULL and BlockConnectorShape.isCommandConnector(self.getPlug()) and BlockConnectorShape.isCommandConnector(self.getSocket())): top = Block.getBlock(self.getPlugBlockID()); while (top.hasAfterConnector() and top.getAfterConnector().hasBlock()): top = Block.getBlock(top.getAfterBlockID()); bottom = Block.getBlock(self.getLastBlockID()); # For safety: if either the top stack is terminated, or # the bottom stack is not a starter, don't try to force a link if (top == None or bottom == None or not top.hasAfterConnector() or not bottom.hasBeforeConnector()): return; link = BlockLink.getBlockLink(top, bottom, top.getAfterConnector(), bottom.getBeforeConnector()); link.connect(); if(self.clickSound != None): # System.out.println("playing click sound"); pass
def __init__(self,workspaceWidget ): RenderableBlock.__init__(self, workspaceWidget)