Пример #1
0
    def layoutSuite( self, vacantRow, suite,
                     scopeKind = None, cf = None, column = 1 ):
        " Does a single suite layout "
        if scopeKind:
            self.__currentCF = cf
            self.__currentScopeClass = _scopeToClass[ scopeKind ]

        for item in suite:
            if item.kind in [ FUNCTION_FRAGMENT, CLASS_FRAGMENT ]:
                scopeCanvas = VirtualCanvas( self.settings, None, None, self )
                scopeItem = item
                if item.kind == FUNCTION_FRAGMENT:
                    scopeCanvas.layout( item, CellElement.FUNC_SCOPE )
                else:
                    scopeCanvas.layout( item, CellElement.CLASS_SCOPE )

                if item.decorators:
                    for dec in reversed( item.decorators ):
                        # Create a decorator scope virtual canvas
                        decScope = VirtualCanvas( self.settings, None, None, self )
                        decScopeRows = len( decScope.cells )
                        if scopeItem.leadingComment:
                            # Need two rows; one for the comment + one for the scope
                            decScope.layout( dec, CellElement.DECOR_SCOPE, 2 )
                            decScope.__allocateCell( decScopeRows - 3, 2 )
                            decScope.cells[ decScopeRows - 3 ][ 1 ] = ConnectorCell( CONN_N_S, decScope, 1, decScopeRows - 3 )
                            decScope.cells[ decScopeRows - 3 ][ 2 ] = LeadingCommentCell( scopeItem, decScope, 2, decScopeRows - 3 )
                        else:
                            # Need one row for the scope
                            decScope.layout( dec, CellElement.DECOR_SCOPE, 1 )

                        decScope.__allocateCell( decScopeRows - 2, 1 )

                        # Fix the parent
                        scopeCanvas.parent = decScope
                        scopeCanvas.canvas = decScope
                        # Set the decorator content
                        decScope.cells[ decScopeRows - 2 ][ 1 ] = scopeCanvas
                        scopeCanvas.addr = [ 1, decScopeRows - 2 ]
                        # Set the current content scope
                        scopeCanvas = decScope
                        scopeItem = dec

                if scopeItem.leadingComment:
                    self.__allocateCell( vacantRow, column + 1 )
                    self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                       self, column, vacantRow )
                    self.cells[ vacantRow ][ column + 1 ] = LeadingCommentCell( scopeItem, self, column + 1, vacantRow )
                    vacantRow += 1

                # Update the scope canvas parent and address
                scopeCanvas.parent = self
                scopeCanvas.addr = [ column, vacantRow ]
                self.__allocateAndSet( vacantRow, column, scopeCanvas )
                vacantRow += 1
                continue

            if item.kind == WITH_FRAGMENT:
                if item.leadingComment:
                    self.__allocateCell( vacantRow, column + 1 )
                    self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                       self, column, vacantRow )
                    self.cells[ vacantRow ][ column + 1 ] = LeadingCommentCell( item, self, column + 1, vacantRow )
                    vacantRow += 1

                self.__allocateScope( item, CellElement.WITH_SCOPE,
                                      vacantRow, column )
                vacantRow += 1
                continue

            if item.kind in [ WHILE_FRAGMENT, FOR_FRAGMENT ]:
                targetScope = CellElement.WHILE_SCOPE
                if item.kind == FOR_FRAGMENT:
                    targetScope = CellElement.FOR_SCOPE

                loopRegionBegin = vacantRow
                if self.__needLoopCommentRow( item ):
                    if item.leadingComment:
                        comment = AboveCommentCell( item, self, column, vacantRow )
                        comment.needConnector = True
                        self.__allocateAndSet( vacantRow, column, comment )
                    else:
                        self.__allocateCell( vacantRow, column )
                        self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                           self, column, vacantRow )
                    if item.elsePart:
                        if item.elsePart.leadingComment:
                            self.__allocateAndSet( vacantRow, column + 1,
                                                   AboveCommentCell( item.elsePart, self, column + 1, vacantRow ) )
                        self.dependentRegions.append( (loopRegionBegin, vacantRow + 1) )
                    vacantRow += 1

                self.__allocateScope( item, targetScope, vacantRow, column )
                if item.elsePart:
                    self.__allocateScope( item.elsePart, CellElement.ELSE_SCOPE,
                                          vacantRow, column + 1 )
                vacantRow += 1
                continue

            if item.kind == COMMENT_FRAGMENT:
                self.__allocateCell( vacantRow, column + 1 )
                self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                   self, column, vacantRow )
                self.cells[ vacantRow ][ column + 1 ] = IndependentCommentCell( item,
                                                                                self, column + 1, vacantRow )
                vacantRow += 1
                continue

            if item.kind == TRY_FRAGMENT:
                tryRegionBegin = vacantRow
                if self.__needTryCommentRow( item ):
                    commentRow = vacantRow
                    vacantRow += 1
                    if item.leadingComment:
                        comment = AboveCommentCell( item, self, column, commentRow )
                        comment.needConnector = True
                        self.__allocateAndSet( commentRow, column, comment )
                    else:
                        self.__allocateAndSet( commentRow, column,
                                               ConnectorCell( CONN_N_S, self, column, commentRow ) )
                    if item.exceptParts:
                        self.dependentRegions.append( (tryRegionBegin, vacantRow) )

                self.__allocateScope( item, CellElement.TRY_SCOPE,
                                      vacantRow, column )
                nextColumn = column + 1
                for exceptPart in item.exceptParts:
                    if exceptPart.leadingComment:
                        self.__allocateAndSet( commentRow, nextColumn,
                                               AboveCommentCell( exceptPart, self, nextColumn, commentRow ) )
                    self.__allocateScope( exceptPart, CellElement.EXCEPT_SCOPE,
                                          vacantRow, nextColumn )
                    nextColumn += 1
                # The else part goes below
                if item.elsePart:
                    vacantRow += 1
                    vacantRow = self.__allocateLeadingComment( item.elsePart, vacantRow, column )
                    self.__allocateScope( item.elsePart, CellElement.ELSE_SCOPE,
                                          vacantRow, column )
                # The finally part is located below
                if item.finallyPart:
                    vacantRow += 1
                    vacantRow = self.__allocateLeadingComment( item.finallyPart, vacantRow, column )
                    self.__allocateScope( item.finallyPart, CellElement.FINALLY_SCOPE,
                                          vacantRow, column )
                vacantRow += 1
                continue

            if item.kind == IF_FRAGMENT:
                ifRegionBegin = vacantRow
                vacantRow = self.__allocateLeadingComment( item, vacantRow, column )
                self.__allocateAndSet( vacantRow, column, IfCell( item, self, column, vacantRow ) )

                # Memorize the No-branch endpoint
                openEnd = [vacantRow, column + 1]
                vacantRow += 1

                # Allocate Yes-branch
                branchLayout = VirtualCanvas( self.settings, column, vacantRow, self )
                branchLayout.isNoScope = True
                branchLayout.layoutSuite( 0, item.suite, CellElement.NO_SCOPE, None, 0 )

                # Copy the layout cells into the current layout calculating the
                # max width of the layout
#                branchWidth, branchHeight = self.__copyLayout( branchLayout, vacantRow, column )
                branchWidth, branchHeight = 1, 1
                self.__allocateAndSet( vacantRow, column, branchLayout )

                # Calculate the number of horizontal connectors left->right
                count = branchWidth - 1
                while count > 0:
                    self.__allocateAndSet( openEnd[ 0 ], openEnd[ 1 ],
                                           ConnectorCell( [ (ConnectorCell.WEST,
                                                             ConnectorCell.EAST) ],
                                                          self, openEnd[ 1 ], openEnd[ 0 ] ) )
                    openEnd[ 1 ] += 1
                    count -= 1

                self.__allocateAndSet( openEnd[ 0 ], openEnd[ 1 ],
                                       ConnectorCell( [ (ConnectorCell.WEST,
                                                         ConnectorCell.SOUTH) ],
                                                      self, openEnd[ 1 ], openEnd[ 0 ] ) )
                if item.sideComment:
                    self.__allocateAndSet( openEnd[ 0 ], openEnd[ 1 ] + 1,
                                           SideCommentCell( item, self, openEnd[ 1 ] + 1, openEnd[ 0 ] ) )
                openEnd[ 0 ] += 1

                branchEndStack = []
                branchEndStack.append( (vacantRow + branchHeight, column) )

                # Handle the elif and else branches
                for elifBranch in item.elifParts:
                    if elifBranch.condition:
                        # This is the elif ...
                        openEnd[ 0 ] = self.__allocateLeadingComment( elifBranch, openEnd[ 0 ], openEnd[ 1 ] )
                        self.__allocateAndSet( openEnd[ 0 ], openEnd[ 1 ], IfCell( elifBranch, self, openEnd[ 1 ], openEnd[ 0 ] ) )

                        # Memorize the new open end
                        newOpenEnd = [openEnd[ 0 ], openEnd[ 1 ] + 1]
                        openEnd[ 0 ] += 1

                        # Allocate Yes-branch
                        branchLayout = VirtualCanvas( self.settings, openEnd[ 1 ], openEnd[ 0 ], self )
                        branchLayout.isNoScope = True
                        branchLayout.layoutSuite( 0, elifBranch.suite, CellElement.NO_SCOPE, None, 0 )

                        # Copy the layout cells into the current layout
                        # calculating the max width of the layout
#                        branchWidth, branchHeight = self.__copyLayout( branchLayout, openEnd[ 0 ], openEnd[ 1 ] )
                        branchWidth, branchHeight = 1, 1
                        self.__allocateAndSet( openEnd[ 0 ], openEnd[ 1 ], branchLayout )

                        # Calculate the number of horizontal connectors left->right
                        count = branchWidth - 1
                        while count > 0:
                            self.__allocateAndSet( newOpenEnd[ 0 ], newOpenEnd[ 1 ],
                                                   ConnectorCell( CONN_W_E,
                                                                  self, newOpenEnd[ 1 ], newOpenEnd[ 0 ] ) )
                            newOpenEnd[ 1 ] += 1
                            count -= 1

                        self.__allocateAndSet( newOpenEnd[ 0 ], newOpenEnd[ 1 ],
                                               ConnectorCell( CONN_W_S,
                                                              self, newOpenEnd[ 1 ], newOpenEnd[ 0 ] ) )
                        if elifBranch.sideComment:
                            self.__allocateAndSet( newOpenEnd[ 0 ], newOpenEnd[ 1 ] + 1,
                                                   SideCommentCell( elifBranch, self, newOpenEnd[ 1 ] + 1, newOpenEnd[ 0 ] ) )
                        newOpenEnd[ 0 ] += 1

                        branchEndStack.append( (openEnd[ 0 ] + branchHeight, openEnd[ 1 ]) )
                        openEnd = newOpenEnd
                    else:
                        # This is the else which is always the last
                        if elifBranch.leadingComment:
                            # Draw it as an independent comment
                            self.__allocateCell( openEnd[ 0 ], openEnd[ 1 ] + 1 )
                            self.cells[ openEnd[ 0 ] ][ openEnd[ 1 ] ] = ConnectorCell( CONN_N_S,
                                                                                        self, openEnd[ 1 ],
                                                                                        openEnd[ 0 ] )
                            cItem = IndependentCommentCell( elifBranch.leadingComment,
                                                            self, openEnd[ 1 ] + 1, openEnd[ 0 ] )
                            cItem.leadingForElse = True
                            self.cells[ openEnd[ 0 ] ][ openEnd[ 1 ] + 1 ] = cItem
                            openEnd[ 0 ] += 1
                        if elifBranch.sideComment:
                            # Draw it as an independent comment
                            self.__allocateCell( openEnd[ 0 ], openEnd[ 1 ] + 1 )
                            self.cells[ openEnd[ 0 ] ][ openEnd[ 1 ] ] = ConnectorCell( CONN_N_S,
                                                                                        self, openEnd[ 1 ],
                                                                                        openEnd[ 0 ] )
                            cItem = IndependentCommentCell( elifBranch.sideComment,
                                                            self, openEnd[ 1 ] + 1, openEnd[ 0 ] )
                            cItem.sideForElse = True
                            self.cells[ openEnd[ 0 ] ][ openEnd[ 1 ] + 1 ] = cItem
                            openEnd[ 0 ] += 1

                        branchLayout = VirtualCanvas( self.settings, openEnd[ 1 ], openEnd[ 0 ], self )
                        branchLayout.isNoScope = True
                        branchLayout.layoutSuite( 0, elifBranch.suite, CellElement.NO_SCOPE, None, 0 )
#                        branchWidth, branchHeight = self.__copyLayout( branchLayout, openEnd[ 0 ], openEnd[ 1 ] )
                        branchWidth, branchHeight = 1, 1
                        self.__allocateAndSet( openEnd[ 0 ], openEnd[ 1 ], branchLayout )

                        # replace the open end
                        openEnd[ 0 ] += branchHeight

                branchEndStack.append( openEnd )
                mainBranch = branchEndStack.pop( 0 )
                mainRow = mainBranch[ 0 ]
                mainCol = mainBranch[ 1 ]

                while branchEndStack:
                    srcRow, srcCol = branchEndStack.pop( 0 )

                    # Adjust the main branch
                    if self.__isTerminalCell( mainRow - 1, mainCol ) or \
                       self.__isVacantCell( mainRow - 1, mainCol ):
                        if mainRow < srcRow:
                            mainRow = srcRow
                    else:
                        while mainRow < srcRow:
                            self.__allocateAndSet( mainRow, mainCol,
                                                   ConnectorCell( CONN_N_S, self, mainCol, mainRow ) )
                            mainRow += 1

                    if self.__isTerminalCell( srcRow - 1, srcCol ):
                        # No need to make any connections from a terminated branch
                        continue

                    # Do the source branch adjustment
                    while srcRow < mainRow:
                        self.__allocateAndSet( srcRow, srcCol,
                                               ConnectorCell( CONN_N_S, self, srcCol, srcRow ) )
                        srcRow += 1

                    # Do the horizontal connection
                    self.__allocateAndSet( srcRow, srcCol,
                                           ConnectorCell( CONN_N_W,
                                                          self, srcCol, srcRow ) )
                    srcCol -= 1
                    while mainCol < srcCol:
                        self.__allocateAndSet( srcRow, srcCol,
                                               ConnectorCell( CONN_E_W, self, srcCol, srcRow ) )
                        srcCol -= 1

                    # Do the proper main branch connection
                    if self.__isTerminalCell( mainRow - 1, mainCol ) or \
                       self.__isVacantCell( mainRow - 1, mainCol ):
                        self.__allocateAndSet( mainRow, mainCol,
                                               ConnectorCell( CONN_E_S, self, mainCol, mainRow ) )
                    else:
                        self.__allocateAndSet( mainRow, mainCol,
                                               ConnectorCell( [ (ConnectorCell.NORTH,
                                                                 ConnectorCell.SOUTH),
                                                                (ConnectorCell.EAST,
                                                                 ConnectorCell.CENTER) ], self, mainCol, mainRow ) )
                    mainRow += 1

                vacantRow = mainRow

                # Memorize the inclusive if-statement region. It is used at the
                # further rendering stage
                self.dependentRegions.append( (ifRegionBegin, vacantRow - 1) )
                continue

            # Below are the single cell fragments possibly with comments
            cellClass = _fragmentKindToCellClass[ item.kind ]
            vacantRow = self.__allocateLeadingComment( item, vacantRow, column )
            self.__allocateAndSet( vacantRow, column,
                                   cellClass( item, self, column, vacantRow ) )

            if item.sideComment:
                self.__allocateAndSet( vacantRow, column + 1,
                                       SideCommentCell( item, self, column + 1, vacantRow ) )
            vacantRow += 1

            # end of for loop

        return vacantRow
Пример #2
0
    def layoutSuite( self, vacantRow, suite,
                     scopeKind = None, cf = None, column = 1 ):
        " Does a single suite layout "
        if scopeKind:
            self.__currentCF = cf
            self.__currentScopeClass = _scopeToClass[ scopeKind ]

        for item in suite:
            if item.kind == CML_COMMENT_FRAGMENT:
                # CML comments are not shown on the diagram
                continue

            if item.kind in [ FUNCTION_FRAGMENT, CLASS_FRAGMENT ]:
                scopeCanvas = VirtualCanvas( self.settings, None, None, self )
                scopeItem = item
                if item.kind == FUNCTION_FRAGMENT:
                    scopeCanvas.layout( item, CellElement.FUNC_SCOPE )
                else:
                    scopeCanvas.layout( item, CellElement.CLASS_SCOPE )

                if item.decorators:
                    for dec in reversed( item.decorators ):
                        # Create a decorator scope virtual canvas
                        decScope = VirtualCanvas( self.settings, None, None, self )
                        decScopeRows = len( decScope.cells )
                        if scopeItem.leadingComment:
                            # Need two rows; one for the comment + one for the scope
                            decScope.layout( dec, CellElement.DECOR_SCOPE, 2 )
                            decScope.__allocateCell( decScopeRows - 3, 2 )
                            decScope.cells[ decScopeRows - 3 ][ 1 ] = ConnectorCell( CONN_N_S, decScope, 1, decScopeRows - 3 )
                            decScope.cells[ decScopeRows - 3 ][ 2 ] = LeadingCommentCell( scopeItem, decScope, 2, decScopeRows - 3 )
                        else:
                            # Need one row for the scope
                            decScope.layout( dec, CellElement.DECOR_SCOPE, 1 )

                        decScope.__allocateCell( decScopeRows - 2, 1 )

                        # Fix the parent
                        scopeCanvas.parent = decScope
                        scopeCanvas.canvas = decScope
                        # Set the decorator content
                        decScope.cells[ decScopeRows - 2 ][ 1 ] = scopeCanvas
                        scopeCanvas.addr = [ 1, decScopeRows - 2 ]
                        # Set the current content scope
                        scopeCanvas = decScope
                        scopeItem = dec

                if scopeItem.leadingComment:
                    self.__allocateCell( vacantRow, column + 1 )
                    self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                       self, column, vacantRow )
                    self.cells[ vacantRow ][ column + 1 ] = LeadingCommentCell( scopeItem, self, column + 1, vacantRow )
                    vacantRow += 1

                # Update the scope canvas parent and address
                scopeCanvas.parent = self
                scopeCanvas.addr = [ column, vacantRow ]
                self.__allocateAndSet( vacantRow, column, scopeCanvas )
                vacantRow += 1
                continue

            if item.kind == WITH_FRAGMENT:
                if item.leadingComment:
                    self.__allocateCell( vacantRow, column + 1 )
                    self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                       self, column, vacantRow )
                    self.cells[ vacantRow ][ column + 1 ] = LeadingCommentCell( item, self, column + 1, vacantRow )
                    vacantRow += 1

                self.__allocateScope( item, CellElement.WITH_SCOPE,
                                      vacantRow, column )
                vacantRow += 1
                continue

            if item.kind in [ WHILE_FRAGMENT, FOR_FRAGMENT ]:
                targetScope = CellElement.WHILE_SCOPE
                if item.kind == FOR_FRAGMENT:
                    targetScope = CellElement.FOR_SCOPE

                loopRegionBegin = vacantRow
                if self.__needLoopCommentRow( item ):
                    if item.leadingComment:
                        comment = AboveCommentCell( item, self, column, vacantRow )
                        comment.needConnector = True
                        self.__allocateAndSet( vacantRow, column, comment )
                    else:
                        self.__allocateCell( vacantRow, column )
                        self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                           self, column, vacantRow )
                    if item.elsePart:
                        if item.elsePart.leadingComment:
                            self.__allocateAndSet( vacantRow, column + 1,
                                                   AboveCommentCell( item.elsePart, self, column + 1, vacantRow ) )
                        self.dependentRegions.append( (loopRegionBegin, vacantRow + 1) )
                    vacantRow += 1

                self.__allocateScope( item, targetScope, vacantRow, column )
                if item.elsePart:
                    self.__allocateScope( item.elsePart, CellElement.ELSE_SCOPE,
                                          vacantRow, column + 1 )
                vacantRow += 1
                continue

            if item.kind == COMMENT_FRAGMENT:
                self.__allocateCell( vacantRow, column + 1 )
                self.cells[ vacantRow ][ column ] = ConnectorCell( CONN_N_S,
                                                                   self, column, vacantRow )
                self.cells[ vacantRow ][ column + 1 ] = IndependentCommentCell( item,
                                                                                self, column + 1, vacantRow )
                vacantRow += 1
                continue

            if item.kind == TRY_FRAGMENT:
                tryRegionBegin = vacantRow
                if self.__needTryCommentRow( item ):
                    commentRow = vacantRow
                    vacantRow += 1
                    if item.leadingComment:
                        comment = AboveCommentCell( item, self, column, commentRow )
                        comment.needConnector = True
                        self.__allocateAndSet( commentRow, column, comment )
                    else:
                        self.__allocateAndSet( commentRow, column,
                                               ConnectorCell( CONN_N_S, self, column, commentRow ) )
                    if item.exceptParts:
                        self.dependentRegions.append( (tryRegionBegin, vacantRow) )

                self.__allocateScope( item, CellElement.TRY_SCOPE,
                                      vacantRow, column )
                nextColumn = column + 1
                for exceptPart in item.exceptParts:
                    if exceptPart.leadingComment:
                        self.__allocateAndSet( commentRow, nextColumn,
                                               AboveCommentCell( exceptPart, self, nextColumn, commentRow ) )
                    self.__allocateScope( exceptPart, CellElement.EXCEPT_SCOPE,
                                          vacantRow, nextColumn )
                    nextColumn += 1
                # The else part goes below
                if item.elsePart:
                    vacantRow += 1
                    vacantRow = self.__allocateLeadingComment( item.elsePart, vacantRow, column )
                    self.__allocateScope( item.elsePart, CellElement.ELSE_SCOPE,
                                          vacantRow, column )
                # The finally part is located below
                if item.finallyPart:
                    vacantRow += 1
                    vacantRow = self.__allocateLeadingComment( item.finallyPart, vacantRow, column )
                    self.__allocateScope( item.finallyPart, CellElement.FINALLY_SCOPE,
                                          vacantRow, column )
                vacantRow += 1
                continue

            if item.kind == IF_FRAGMENT:

                lastNonElseIndex = len( item.parts ) - 1
                for index in xrange( len( item.parts ) ):
                    if item.parts[ index ].condition is None:
                        lastNonElseIndex = index - 1
                        break

                canvas = VirtualCanvas( self.settings, 0, 0, self )
                canvas.isNoScope = True

                if lastNonElseIndex == len( item.parts ) - 1:
                    # There is no else
                    canvas.layoutIfBranch( item.parts[ lastNonElseIndex ], None )
                else:
                    canvas.layoutIfBranch( item.parts[ lastNonElseIndex ],
                                           item.parts[ lastNonElseIndex + 1 ] )

                index = lastNonElseIndex - 1
                while index >= 0:
                    tempCanvas = VirtualCanvas( self.settings, 0, 0, self )
                    tempCanvas.isNoScope = True
                    tempCanvas.layoutIfBranch( item.parts[ index ], canvas )
                    canvas = tempCanvas
                    index -= 1

                self.__allocateAndSet( vacantRow, 1, canvas )
                vacantRow += 1
                continue

            # Below are the single cell fragments possibly with comments
            cellClass = _fragmentKindToCellClass[ item.kind ]
            vacantRow = self.__allocateLeadingComment( item, vacantRow, column )
            self.__allocateAndSet( vacantRow, column,
                                   cellClass( item, self, column, vacantRow ) )

            if item.sideComment:
                self.__allocateAndSet( vacantRow, column + 1,
                                       SideCommentCell( item, self, column + 1, vacantRow ) )
            vacantRow += 1

            # end of for loop

        return vacantRow
Пример #3
0
    def layoutSuite(self, vacantRow, suite, scopeKind=None, cf=None, column=1):
        " Does a single suite layout "
        if scopeKind:
            self.__currentCF = cf
            self.__currentScopeClass = _scopeToClass[scopeKind]

        for item in suite:
            if item.kind == CML_COMMENT_FRAGMENT:
                # CML comments are not shown on the diagram
                continue

            if item.kind in [FUNCTION_FRAGMENT, CLASS_FRAGMENT]:
                scopeCanvas = VirtualCanvas(self.settings, None, None, self)
                scopeItem = item
                if item.kind == FUNCTION_FRAGMENT:
                    scopeCanvas.layout(item, CellElement.FUNC_SCOPE)
                else:
                    scopeCanvas.layout(item, CellElement.CLASS_SCOPE)

                if item.decorators:
                    for dec in reversed(item.decorators):
                        # Create a decorator scope virtual canvas
                        decScope = VirtualCanvas(self.settings, None, None,
                                                 self)
                        decScopeRows = len(decScope.cells)
                        if scopeItem.leadingComment:
                            # Need two rows; one for the comment + one for the scope
                            decScope.layout(dec, CellElement.DECOR_SCOPE, 2)
                            decScope.__allocateCell(decScopeRows - 3, 2)
                            decScope.cells[decScopeRows -
                                           3][1] = ConnectorCell(
                                               CONN_N_S, decScope, 1,
                                               decScopeRows - 3)
                            decScope.cells[decScopeRows -
                                           3][2] = LeadingCommentCell(
                                               scopeItem, decScope, 2,
                                               decScopeRows - 3)
                        else:
                            # Need one row for the scope
                            decScope.layout(dec, CellElement.DECOR_SCOPE, 1)

                        decScope.__allocateCell(decScopeRows - 2, 1)

                        # Fix the parent
                        scopeCanvas.parent = decScope
                        scopeCanvas.canvas = decScope
                        # Set the decorator content
                        decScope.cells[decScopeRows - 2][1] = scopeCanvas
                        scopeCanvas.addr = [1, decScopeRows - 2]
                        # Set the current content scope
                        scopeCanvas = decScope
                        scopeItem = dec

                if scopeItem.leadingComment:
                    self.__allocateCell(vacantRow, column + 1)
                    self.cells[vacantRow][column] = ConnectorCell(
                        CONN_N_S, self, column, vacantRow)
                    self.cells[vacantRow][column + 1] = LeadingCommentCell(
                        scopeItem, self, column + 1, vacantRow)
                    vacantRow += 1

                # Update the scope canvas parent and address
                scopeCanvas.parent = self
                scopeCanvas.addr = [column, vacantRow]
                self.__allocateAndSet(vacantRow, column, scopeCanvas)
                vacantRow += 1
                continue

            if item.kind == WITH_FRAGMENT:
                if item.leadingComment:
                    self.__allocateCell(vacantRow, column + 1)
                    self.cells[vacantRow][column] = ConnectorCell(
                        CONN_N_S, self, column, vacantRow)
                    self.cells[vacantRow][column + 1] = LeadingCommentCell(
                        item, self, column + 1, vacantRow)
                    vacantRow += 1

                self.__allocateScope(item, CellElement.WITH_SCOPE, vacantRow,
                                     column)
                vacantRow += 1
                continue

            if item.kind in [WHILE_FRAGMENT, FOR_FRAGMENT]:
                targetScope = CellElement.WHILE_SCOPE
                if item.kind == FOR_FRAGMENT:
                    targetScope = CellElement.FOR_SCOPE

                loopRegionBegin = vacantRow
                if self.__needLoopCommentRow(item):
                    if item.leadingComment:
                        comment = AboveCommentCell(item, self, column,
                                                   vacantRow)
                        comment.needConnector = True
                        self.__allocateAndSet(vacantRow, column, comment)
                    else:
                        self.__allocateCell(vacantRow, column)
                        self.cells[vacantRow][column] = ConnectorCell(
                            CONN_N_S, self, column, vacantRow)
                    if item.elsePart:
                        if item.elsePart.leadingComment:
                            self.__allocateAndSet(
                                vacantRow, column + 1,
                                AboveCommentCell(item.elsePart, self,
                                                 column + 1, vacantRow))
                        self.dependentRegions.append(
                            (loopRegionBegin, vacantRow + 1))
                    vacantRow += 1

                self.__allocateScope(item, targetScope, vacantRow, column)
                if item.elsePart:
                    self.__allocateScope(item.elsePart, CellElement.ELSE_SCOPE,
                                         vacantRow, column + 1)
                vacantRow += 1
                continue

            if item.kind == COMMENT_FRAGMENT:
                self.__allocateCell(vacantRow, column + 1)
                self.cells[vacantRow][column] = ConnectorCell(
                    CONN_N_S, self, column, vacantRow)
                self.cells[vacantRow][column + 1] = IndependentCommentCell(
                    item, self, column + 1, vacantRow)
                vacantRow += 1
                continue

            if item.kind == TRY_FRAGMENT:
                tryRegionBegin = vacantRow
                if self.__needTryCommentRow(item):
                    commentRow = vacantRow
                    vacantRow += 1
                    if item.leadingComment:
                        comment = AboveCommentCell(item, self, column,
                                                   commentRow)
                        comment.needConnector = True
                        self.__allocateAndSet(commentRow, column, comment)
                    else:
                        self.__allocateAndSet(
                            commentRow, column,
                            ConnectorCell(CONN_N_S, self, column, commentRow))
                    if item.exceptParts:
                        self.dependentRegions.append(
                            (tryRegionBegin, vacantRow))

                self.__allocateScope(item, CellElement.TRY_SCOPE, vacantRow,
                                     column)
                nextColumn = column + 1
                for exceptPart in item.exceptParts:
                    if exceptPart.leadingComment:
                        self.__allocateAndSet(
                            commentRow, nextColumn,
                            AboveCommentCell(exceptPart, self, nextColumn,
                                             commentRow))
                    self.__allocateScope(exceptPart, CellElement.EXCEPT_SCOPE,
                                         vacantRow, nextColumn)
                    nextColumn += 1
                # The else part goes below
                if item.elsePart:
                    vacantRow += 1
                    vacantRow = self.__allocateLeadingComment(
                        item.elsePart, vacantRow, column)
                    self.__allocateScope(item.elsePart, CellElement.ELSE_SCOPE,
                                         vacantRow, column)
                # The finally part is located below
                if item.finallyPart:
                    vacantRow += 1
                    vacantRow = self.__allocateLeadingComment(
                        item.finallyPart, vacantRow, column)
                    self.__allocateScope(item.finallyPart,
                                         CellElement.FINALLY_SCOPE, vacantRow,
                                         column)
                vacantRow += 1
                continue

            if item.kind == IF_FRAGMENT:

                lastNonElseIndex = len(item.parts) - 1
                for index in xrange(len(item.parts)):
                    if item.parts[index].condition is None:
                        lastNonElseIndex = index - 1
                        break

                canvas = VirtualCanvas(self.settings, 0, 0, self)
                canvas.isNoScope = True

                if lastNonElseIndex == len(item.parts) - 1:
                    # There is no else
                    canvas.layoutIfBranch(item.parts[lastNonElseIndex], None)
                else:
                    canvas.layoutIfBranch(item.parts[lastNonElseIndex],
                                          item.parts[lastNonElseIndex + 1])

                index = lastNonElseIndex - 1
                while index >= 0:
                    tempCanvas = VirtualCanvas(self.settings, 0, 0, self)
                    tempCanvas.isNoScope = True
                    tempCanvas.layoutIfBranch(item.parts[index], canvas)
                    canvas = tempCanvas
                    index -= 1

                self.__allocateAndSet(vacantRow, 1, canvas)
                vacantRow += 1
                continue

            # Below are the single cell fragments possibly with comments
            cellClass = _fragmentKindToCellClass[item.kind]
            vacantRow = self.__allocateLeadingComment(item, vacantRow, column)
            self.__allocateAndSet(vacantRow, column,
                                  cellClass(item, self, column, vacantRow))

            if item.sideComment:
                self.__allocateAndSet(
                    vacantRow, column + 1,
                    SideCommentCell(item, self, column + 1, vacantRow))
            vacantRow += 1

            # end of for loop

        return vacantRow