def layoutIfBranch( self, yBranch, nBranch ): " Used in 'if' statements processing " # It is always called when a layout is empty vacantRow = self.__allocateLeadingComment( yBranch, 0, 0 ) self.__allocateAndSet( vacantRow, 0, IfCell( yBranch, self, 0, vacantRow ) ) self.__allocateAndSet( vacantRow, 1, ConnectorCell( CONN_W_S, self, 1, vacantRow ) ) if yBranch.sideComment: self.__allocateAndSet( vacantRow, 2, SideCommentCell( yBranch, self, 2, vacantRow ) ) vacantRow += 1 # Test if there is a switch of the branches yBelow = CMLVersion.find( yBranch.leadingCMLComments, CMLsw ) is not None # Allocate the YES branch if yBelow: branchLayout = VirtualCanvas( self.settings, 0, vacantRow, self ) else: branchLayout = VirtualCanvas( self.settings, 1, vacantRow, self ) branchLayout.isNoScope = True branchLayout.layoutSuite( 0, yBranch.suite, CellElement.NO_SCOPE, None, 0 ) if yBelow: self.__allocateAndSet( vacantRow, 0, branchLayout ) else: self.__allocateAndSet( vacantRow, 1, branchLayout ) # nBranch could be: None: for absent of else # ifPart: present else # vcanvas: other elif if nBranch is None: if yBelow: self.__allocateAndSet( vacantRow, 1, ConnectorCell( CONN_N_S, self, 1, vacantRow ) ) vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell( CONN_N_W, self, 1, vacantRow ) ) if self.__isTerminalCell( vacantRow - 1, 0 ) or \ self.__isVacantCell( vacantRow - 1, 0 ): self.__allocateAndSet( vacantRow, 0, ConnectorCell( CONN_E_S, self, 0, vacantRow ) ) else: self.__allocateAndSet( vacantRow, 0, ConnectorCell( [ (ConnectorCell.NORTH, ConnectorCell.SOUTH), (ConnectorCell.EAST, ConnectorCell.CENTER) ], self, 0, vacantRow ) ) else: self.__allocateAndSet( vacantRow, 0, ConnectorCell( CONN_N_S, self, 0, vacantRow ) ) if not self.__isTerminalCell( vacantRow, 1 ) and \ not self.__isVacantCell( vacantRow, 1 ): vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell( CONN_N_W, self, 1, vacantRow ) ) self.__allocateAndSet( vacantRow, 0, ConnectorCell( [ (ConnectorCell.NORTH, ConnectorCell.SOUTH), (ConnectorCell.EAST, ConnectorCell.CENTER) ], self, 0, vacantRow ) ) else: if nBranch.kind == CellElement.VCANVAS: if yBelow: self.__allocateAndSet( vacantRow, 1, nBranch ) else: self.__allocateAndSet( vacantRow, 0, nBranch ) else: # This is 'else' suite scopeCommentRows = 0 if nBranch.leadingComment: scopeCommentRows += 1 if nBranch.sideComment: scopeCommentRows += 1 if yBelow: branchLayout = VirtualCanvas( self.settings, 1, vacantRow, self ) else: branchLayout = VirtualCanvas( self.settings, 0, vacantRow, self ) if nBranch.leadingComment: # Draw as an independent comment: insert into the layout conn = ConnectorCell( CONN_N_S, branchLayout, 0, 0 ) cItem = IndependentCommentCell( nBranch.leadingComment, branchLayout, 1, 0 ) branchLayout.cells.append( [] ) branchLayout.cells[ 0 ].append( conn ) branchLayout.cells[ 0 ].append( cItem ) if nBranch.sideComment: # Draw as an independent comment: insert into the layout rowIndex = scopeCommentRows - 1 conn = ConnectorCell( CONN_N_S, branchLayout, 0, rowIndex ) cItem = IndependentCommentCell( nBranch.sideComment, branchLayout, 1, rowIndex ) cItem.sideForElse = True branchLayout.cells.append( [] ) branchLayout.cells[ rowIndex ].append( conn ) branchLayout.cells[ rowIndex ].append( cItem ) branchLayout.isNoScope = True branchLayout.layoutSuite( scopeCommentRows, nBranch.suite, CellElement.NO_SCOPE, None, 0 ) if yBelow: self.__allocateAndSet( vacantRow, 1, branchLayout ) else: self.__allocateAndSet( vacantRow, 0, branchLayout ) # Finilizing connectors leftTerminal = self.__isTerminalCell( vacantRow, 0 ) or self.__isVacantCell( vacantRow, 0 ) rightTerminal = self.__isTerminalCell( vacantRow, 1 ) or self.__isVacantCell( vacantRow, 1 ) if leftTerminal and rightTerminal: pass # No need to do anything elif leftTerminal: vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell( CONN_N_W, self, 1, vacantRow ) ) self.__allocateAndSet( vacantRow, 0, ConnectorCell( CONN_E_S, self, 0, vacantRow ) ) elif rightTerminal: pass # No need to do anything else: # Both are non terminal vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell( CONN_N_W, self, 1, vacantRow ) ) self.__allocateAndSet( vacantRow, 0, ConnectorCell( [ (ConnectorCell.NORTH, ConnectorCell.SOUTH), (ConnectorCell.EAST, ConnectorCell.CENTER) ], self, 0, vacantRow ) ) self.dependentRegions.append( (0, vacantRow) ) return
def layoutIfBranch(self, yBranch, nBranch): " Used in 'if' statements processing " # It is always called when a layout is empty vacantRow = self.__allocateLeadingComment(yBranch, 0, 0) self.__allocateAndSet(vacantRow, 0, IfCell(yBranch, self, 0, vacantRow)) self.__allocateAndSet(vacantRow, 1, ConnectorCell(CONN_W_S, self, 1, vacantRow)) if yBranch.sideComment: self.__allocateAndSet(vacantRow, 2, SideCommentCell(yBranch, self, 2, vacantRow)) vacantRow += 1 # Test if there is a switch of the branches yBelow = CMLVersion.find(yBranch.leadingCMLComments, CMLsw) is not None # Allocate the YES branch if yBelow: branchLayout = VirtualCanvas(self.settings, 0, vacantRow, self) else: branchLayout = VirtualCanvas(self.settings, 1, vacantRow, self) branchLayout.isNoScope = True branchLayout.layoutSuite(0, yBranch.suite, CellElement.NO_SCOPE, None, 0) if yBelow: self.__allocateAndSet(vacantRow, 0, branchLayout) else: self.__allocateAndSet(vacantRow, 1, branchLayout) # nBranch could be: None: for absent of else # ifPart: present else # vcanvas: other elif if nBranch is None: if yBelow: self.__allocateAndSet( vacantRow, 1, ConnectorCell(CONN_N_S, self, 1, vacantRow)) vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell(CONN_N_W, self, 1, vacantRow)) if self.__isTerminalCell( vacantRow - 1, 0 ) or \ self.__isVacantCell( vacantRow - 1, 0 ): self.__allocateAndSet( vacantRow, 0, ConnectorCell(CONN_E_S, self, 0, vacantRow)) else: self.__allocateAndSet( vacantRow, 0, ConnectorCell( [(ConnectorCell.NORTH, ConnectorCell.SOUTH), (ConnectorCell.EAST, ConnectorCell.CENTER)], self, 0, vacantRow)) else: self.__allocateAndSet( vacantRow, 0, ConnectorCell(CONN_N_S, self, 0, vacantRow)) if not self.__isTerminalCell( vacantRow, 1 ) and \ not self.__isVacantCell( vacantRow, 1 ): vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell(CONN_N_W, self, 1, vacantRow)) self.__allocateAndSet( vacantRow, 0, ConnectorCell( [(ConnectorCell.NORTH, ConnectorCell.SOUTH), (ConnectorCell.EAST, ConnectorCell.CENTER)], self, 0, vacantRow)) else: if nBranch.kind == CellElement.VCANVAS: if yBelow: self.__allocateAndSet(vacantRow, 1, nBranch) else: self.__allocateAndSet(vacantRow, 0, nBranch) else: # This is 'else' suite scopeCommentRows = 0 if nBranch.leadingComment: scopeCommentRows += 1 if nBranch.sideComment: scopeCommentRows += 1 if yBelow: branchLayout = VirtualCanvas(self.settings, 1, vacantRow, self) else: branchLayout = VirtualCanvas(self.settings, 0, vacantRow, self) if nBranch.leadingComment: # Draw as an independent comment: insert into the layout conn = ConnectorCell(CONN_N_S, branchLayout, 0, 0) cItem = IndependentCommentCell(nBranch.leadingComment, branchLayout, 1, 0) branchLayout.cells.append([]) branchLayout.cells[0].append(conn) branchLayout.cells[0].append(cItem) if nBranch.sideComment: # Draw as an independent comment: insert into the layout rowIndex = scopeCommentRows - 1 conn = ConnectorCell(CONN_N_S, branchLayout, 0, rowIndex) cItem = IndependentCommentCell(nBranch.sideComment, branchLayout, 1, rowIndex) cItem.sideForElse = True branchLayout.cells.append([]) branchLayout.cells[rowIndex].append(conn) branchLayout.cells[rowIndex].append(cItem) branchLayout.isNoScope = True branchLayout.layoutSuite(scopeCommentRows, nBranch.suite, CellElement.NO_SCOPE, None, 0) if yBelow: self.__allocateAndSet(vacantRow, 1, branchLayout) else: self.__allocateAndSet(vacantRow, 0, branchLayout) # Finilizing connectors leftTerminal = self.__isTerminalCell( vacantRow, 0) or self.__isVacantCell(vacantRow, 0) rightTerminal = self.__isTerminalCell( vacantRow, 1) or self.__isVacantCell(vacantRow, 1) if leftTerminal and rightTerminal: pass # No need to do anything elif leftTerminal: vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell(CONN_N_W, self, 1, vacantRow)) self.__allocateAndSet( vacantRow, 0, ConnectorCell(CONN_E_S, self, 0, vacantRow)) elif rightTerminal: pass # No need to do anything else: # Both are non terminal vacantRow += 1 self.__allocateAndSet( vacantRow, 1, ConnectorCell(CONN_N_W, self, 1, vacantRow)) self.__allocateAndSet( vacantRow, 0, ConnectorCell([(ConnectorCell.NORTH, ConnectorCell.SOUTH), (ConnectorCell.EAST, ConnectorCell.CENTER)], self, 0, vacantRow)) self.dependentRegions.append((0, vacantRow)) return
def _paint(self, painter, option, widget): " Draws the corresponding scope element " s = self.canvas.settings if self.subKind == ScopeCellElement.TOP_LEFT: bgColor, _, borderColor = self.getCustomColors( painter.brush().color(), painter.brush().color()) brush = QBrush(bgColor) painter.setBrush(brush) if self.isSelected(): selectPen = QPen(s.selectColor) selectPen.setWidth(s.selectPenWidth) selectPen.setJoinStyle(Qt.RoundJoin) painter.setPen(selectPen) else: pen = QPen(borderColor) pen.setWidth(s.lineWidth) painter.setPen(pen) painter.drawRoundedRect(self.baseX + s.hCellPadding, self.baseY + s.vCellPadding, self.canvas.minWidth - 2 * s.hCellPadding, self.canvas.minHeight - 2 * s.vCellPadding, s.rectRadius, s.rectRadius) elif self.subKind == ScopeCellElement.DECLARATION: bgColor, fgColor, borderColor = self.getCustomColors( painter.brush().color(), s.boxFGColor) brush = QBrush(bgColor) painter.setBrush(brush) pen = QPen(fgColor) painter.setFont(s.monoFont) painter.setPen(pen) canvasLeft = self.baseX - s.rectRadius canvasTop = self.baseY - s.rectRadius textHeight = self._headerRect.height() yShift = 0 if hasattr(self.ref, "sideComment"): yShift = s.vTextPadding painter.drawText(canvasLeft + s.hHeaderPadding, canvasTop + s.vHeaderPadding + yShift, self._headerRect.width(), textHeight, Qt.AlignLeft, self._getText()) pen = QPen(borderColor) pen.setWidth(s.lineWidth) painter.setPen(pen) # If the scope is selected then the line may need to be shorter # to avoid covering the outline row = self.addr[1] - 1 column = self.addr[0] - 1 correction = 0.0 if self.canvas.cells[row][column].isSelected(): correction = s.selectPenWidth - 1 painter.drawLine( canvasLeft + correction, self.baseY + self.height, canvasLeft + self.canvas.minWidth - 2 * s.hCellPadding - correction, self.baseY + self.height) elif self.subKind == ScopeCellElement.SIDE_COMMENT: brush = QBrush(s.commentBGColor) painter.setBrush(brush) if self.isSelected(): selectPen = QPen(s.selectColor) selectPen.setWidth(s.selectPenWidth) selectPen.setJoinStyle(Qt.RoundJoin) painter.setPen(selectPen) else: pen = QPen(s.commentLineColor) pen.setWidth(s.commentLineWidth) pen.setJoinStyle(Qt.RoundJoin) painter.setPen(pen) canvasTop = self.baseY - s.rectRadius # s.vHeaderPadding below is used intentionally: to have the same # spacing on top, bottom and right for the comment box movedBaseX = self.canvas.baseX + self.canvas.minWidth - self.width - s.rectRadius - s.vHeaderPadding painter.drawPath(self.__sideCommentPath) pen = QPen(s.boxFGColor) painter.setFont(s.monoFont) painter.setPen(pen) painter.drawText(movedBaseX + s.hHeaderPadding + s.hTextPadding, canvasTop + s.vHeaderPadding + s.vTextPadding, self._sideCommentRect.width(), self._sideCommentRect.height(), Qt.AlignLeft, self._getSideComment()) elif self.subKind == ScopeCellElement.DOCSTRING: bgColor, fgColor, borderColor = self.getCustomColors( painter.brush().color(), s.boxFGColor) if self.ref.docstring.leadingCMLComments: colorSpec = CMLVersion.find( self.ref.docstring.leadingCMLComments, CMLcc) if colorSpec: if colorSpec.bg: bgColor = colorSpec.bg if colorSpec.fg: fgColor = colorSpec.fg brush = QBrush(bgColor) painter.setBrush(brush) canvasLeft = self.baseX - s.rectRadius if self.isSelected(): selectPen = QPen(s.selectColor) selectPen.setWidth(s.selectPenWidth) selectPen.setJoinStyle(Qt.RoundJoin) painter.setPen(selectPen) painter.drawRect(canvasLeft, self.baseY, self.canvas.minWidth - 2 * s.hCellPadding, self.height) else: # If the scope is selected then the line may need to be shorter # to avoid covering the outline row = self.addr[1] - 2 column = self.addr[0] - 1 correction = 0.0 if self.canvas.cells[row][column].isSelected(): correction = s.selectPenWidth - 1 # The background could also be custom pen = QPen(bgColor) pen.setWidth(s.lineWidth) pen.setJoinStyle(Qt.MiterJoin) painter.setPen(pen) dsCorr = float(s.lineWidth) if self.canvas.cells[row][column].isSelected(): dsCorr = float(s.selectPenWidth) / 2.0 + float( s.lineWidth) / 2.0 painter.drawRect( float(canvasLeft) + dsCorr, self.baseY + s.lineWidth, float(self.canvas.minWidth) - 2.0 * float(s.hCellPadding) - 2.0 * dsCorr, self.height - 2 * s.lineWidth) pen = QPen(borderColor) pen.setWidth(s.lineWidth) painter.setPen(pen) painter.drawLine( canvasLeft + correction, self.baseY + self.height, canvasLeft + self.canvas.minWidth - 2 * s.hCellPadding - correction, self.baseY + self.height) pen = QPen(fgColor) painter.setFont(s.monoFont) painter.setPen(pen) painter.drawText(canvasLeft + s.hHeaderPadding, self.baseY + s.vHeaderPadding, self.canvas.width - 2 * s.hHeaderPadding, self.height - 2 * s.vHeaderPadding, Qt.AlignLeft, self.getDocstringText()) return
def _paint( self, painter, option, widget ): " Draws the corresponding scope element " s = self.canvas.settings if self.subKind == ScopeCellElement.TOP_LEFT: bgColor, _, borderColor = self.getCustomColors( painter.brush().color(), painter.brush().color() ) brush = QBrush( bgColor ) painter.setBrush( brush ) if self.isSelected(): selectPen = QPen( s.selectColor ) selectPen.setWidth( s.selectPenWidth ) selectPen.setJoinStyle( Qt.RoundJoin ) painter.setPen( selectPen ) else: pen = QPen( borderColor ) pen.setWidth( s.lineWidth ) painter.setPen( pen ) painter.drawRoundedRect( self.baseX + s.hCellPadding, self.baseY + s.vCellPadding, self.canvas.minWidth - 2 * s.hCellPadding, self.canvas.minHeight - 2 * s.vCellPadding, s.rectRadius, s.rectRadius ) elif self.subKind == ScopeCellElement.DECLARATION: bgColor, fgColor, borderColor = self.getCustomColors( painter.brush().color(), s.boxFGColor ) brush = QBrush( bgColor ) painter.setBrush( brush ) pen = QPen( fgColor ) painter.setFont( s.monoFont ) painter.setPen( pen ) canvasLeft = self.baseX - s.rectRadius canvasTop = self.baseY - s.rectRadius textHeight = self._headerRect.height() yShift = 0 if hasattr( self.ref, "sideComment" ): yShift = s.vTextPadding painter.drawText( canvasLeft + s.hHeaderPadding, canvasTop + s.vHeaderPadding + yShift, self._headerRect.width(), textHeight, Qt.AlignLeft, self._getText() ) pen = QPen( borderColor ) pen.setWidth( s.lineWidth ) painter.setPen( pen ) # If the scope is selected then the line may need to be shorter # to avoid covering the outline row = self.addr[ 1 ] - 1 column = self.addr[ 0 ] - 1 correction = 0.0 if self.canvas.cells[ row ][ column ].isSelected(): correction = s.selectPenWidth - 1 painter.drawLine( canvasLeft + correction, self.baseY + self.height, canvasLeft + self.canvas.minWidth - 2 * s.hCellPadding - correction, self.baseY + self.height ) elif self.subKind == ScopeCellElement.SIDE_COMMENT: brush = QBrush( s.commentBGColor ) painter.setBrush( brush ) if self.isSelected(): selectPen = QPen( s.selectColor ) selectPen.setWidth( s.selectPenWidth ) selectPen.setJoinStyle( Qt.RoundJoin ) painter.setPen( selectPen ) else: pen = QPen( s.commentLineColor ) pen.setWidth( s.commentLineWidth ) pen.setJoinStyle( Qt.RoundJoin ) painter.setPen( pen ) canvasTop = self.baseY - s.rectRadius # s.vHeaderPadding below is used intentionally: to have the same # spacing on top, bottom and right for the comment box movedBaseX = self.canvas.baseX + self.canvas.minWidth - self.width - s.rectRadius - s.vHeaderPadding painter.drawPath( self.__sideCommentPath ) pen = QPen( s.boxFGColor ) painter.setFont( s.monoFont ) painter.setPen( pen ) painter.drawText( movedBaseX + s.hHeaderPadding + s.hTextPadding, canvasTop + s.vHeaderPadding + s.vTextPadding, self._sideCommentRect.width(), self._sideCommentRect.height(), Qt.AlignLeft, self._getSideComment() ) elif self.subKind == ScopeCellElement.DOCSTRING: bgColor, fgColor, borderColor = self.getCustomColors( painter.brush().color(), s.boxFGColor ) if self.ref.docstring.leadingCMLComments: colorSpec = CMLVersion.find( self.ref.docstring.leadingCMLComments, CMLcc ) if colorSpec: if colorSpec.bg: bgColor = colorSpec.bg if colorSpec.fg: fgColor = colorSpec.fg brush = QBrush( bgColor ) painter.setBrush( brush ) canvasLeft = self.baseX - s.rectRadius if self.isSelected(): selectPen = QPen( s.selectColor ) selectPen.setWidth( s.selectPenWidth ) selectPen.setJoinStyle( Qt.RoundJoin ) painter.setPen( selectPen ) painter.drawRect( canvasLeft, self.baseY, self.canvas.minWidth - 2 * s.hCellPadding, self.height ) else: # If the scope is selected then the line may need to be shorter # to avoid covering the outline row = self.addr[ 1 ] - 2 column = self.addr[ 0 ] - 1 correction = 0.0 if self.canvas.cells[ row ][ column ].isSelected(): correction = s.selectPenWidth - 1 # The background could also be custom pen = QPen( bgColor ) pen.setWidth( s.lineWidth ) pen.setJoinStyle( Qt.MiterJoin ) painter.setPen( pen ) dsCorr = float( s.lineWidth ) if self.canvas.cells[ row ][ column ].isSelected(): dsCorr = float( s.selectPenWidth ) / 2.0 + float( s.lineWidth ) / 2.0 painter.drawRect( float( canvasLeft ) + dsCorr, self.baseY + s.lineWidth, float( self.canvas.minWidth ) - 2.0 * float( s.hCellPadding ) - 2.0 * dsCorr, self.height - 2 * s.lineWidth ) pen = QPen( borderColor ) pen.setWidth( s.lineWidth ) painter.setPen( pen ) painter.drawLine( canvasLeft + correction, self.baseY + self.height, canvasLeft + self.canvas.minWidth - 2 * s.hCellPadding - correction, self.baseY + self.height ) pen = QPen( fgColor ) painter.setFont( s.monoFont ) painter.setPen( pen ) painter.drawText( canvasLeft + s.hHeaderPadding, self.baseY + s.vHeaderPadding, self.canvas.width - 2 * s.hHeaderPadding, self.height - 2 * s.vHeaderPadding, Qt.AlignLeft, self.getDocstringText() ) return