Exemplo n.º 1
0
 def getPosPopup(self):
     pos = self.pos()
     pos1 = self.mapToGlobal(pos)
     pos2 = QPoint()
     cRect = self.cursorRect()
     pos2.setX(cRect.x())
     pos2.setY(cRect.y())
     return pos1 + pos2
Exemplo n.º 2
0
 def getPosPopup(self):
     pos = self.pos()
     pos1 = self.mapToGlobal(pos)
     pos2 = QPoint()
     cRect = self.cursorRect()
     pos2.setX(cRect.x())
     pos2.setY(cRect.y())
     return pos1 + pos2
Exemplo n.º 3
0
 def scrollTo(self):
     l = len(self.returns_stack) 
     self.logger.debug('scrollTo')
     if l and isinstance(self.returns_stack[l-1],QPoint):
         viewport = Jaime.instance.view.page().viewportSize()            
         pos = self.returns_stack.pop()            
         scroll_to_pos = QPoint(0,0)
         if pos.x() >= viewport.width():
             scroll_to_pos.setX(int ( pos.x() - ( viewport.width() / 2)))
         if pos.y() >= viewport.height():
             scroll_to_pos.setY(int ( pos.y() - ( viewport.height() / 2)))
         self.logger.info('scrollTo, scrolling to  %s' % scroll_to_pos )
         Jaime.instance.view.page().mainFrame().setScrollPosition(scroll_to_pos)
Exemplo n.º 4
0
class Decision(VerticalSymbol):
    ''' SDL DECISION Symbol '''
    _unique_followers = ['Comment']
    _insertable_followers = [
        'DecisionAnswer', 'Task', 'ProcedureCall', 'Output', 'Decision',
        'Label'
    ]
    _terminal_followers = ['Join', 'State', 'ProcedureStop']
    common_name = 'decision'
    # Define reserved keywords for the syntax highlighter
    blackbold = SDL_BLACKBOLD + [
        '\\b{}\\b'.format(word) for word in ('AND', 'OR')
    ]
    redbold = SDL_REDBOLD
    completion_list = {'length', 'present'}

    def __init__(self, parent=None, ast=None):
        ast = ast or ogAST.Decision()
        # Define the point where all branches of the decision can join again
        self.connectionPoint = QPoint(ast.width / 2, ast.height + 30)
        super(Decision, self).__init__(parent,
                                       text=ast.inputString,
                                       x=ast.pos_x,
                                       y=ast.pos_y,
                                       hyperlink=ast.hyperlink)
        self.set_shape(ast.width, ast.height)
        self.setBrush(QColor(255, 255, 202))
        self.minDistanceToSymbolAbove = 0
        self.parser = ogParser
        self.text_alignment = Qt.AlignHCenter
        if ast.comment:
            Comment(parent=self, ast=ast.comment)

    @property
    def terminal_symbol(self):
        '''
            Compute dynamically if the item is terminal by checking
            if all its branches end with a terminator
        '''
        for branch in self.branches():
            if not branch.last_branch_item.terminal_symbol:
                return False
        return True

    def branches(self):
        ''' Return the list of decision answers (as a generator) '''
        return (branch for branch in self.childSymbols()
                if isinstance(branch, DecisionAnswer))

    def check_syntax(self):
        ''' Redefined function, to check only the symbol, not recursively '''
        _, syntax_errors, _, _, _ = self.parser.parseSingleElement(
            self.common_name, self.PR(recursive=False))
        try:
            # LOG.error('\n'.join(syntax_errors))
            self.scene().raise_syntax_errors(syntax_errors)
        except AttributeError:
            LOG.debug('raise_syntax_error raised an exception')

    def set_shape(self, width, height):
        ''' Define polygon points to draw the symbol '''
        path = QPainterPath()
        path.moveTo(width / 2, 0)
        path.lineTo(width, height / 2)
        path.lineTo(width / 2, height)
        path.lineTo(0, height / 2)
        path.lineTo(width / 2, 0)
        self.setPath(path)
        super(Decision, self).set_shape(width, height)

    def resize_item(self, rect):
        ''' On resize event, make sure connection points are updated '''
        delta_y = self.boundingRect().height() - rect.height()
        super(Decision, self).resize_item(rect)
        self.connectionPoint.setX(self.boundingRect().center().x())
        self.connectionPoint.setY(self.connectionPoint.y() - delta_y)
        self.update_connections()

    def update_connections(self):
        ''' Redefined - update arrows shape below connection point '''
        super(Decision, self).update_connections()
        for branch in self.branches():
            for cnx in branch.last_branch_item.connections():
                cnx.reshape()

    def updateConnectionPointPosition(self):
        ''' Compute the joining point of decision branches '''
        new_y = 0
        new_x = self.boundingRect().width() / 2.0
        answers = False
        for branch in self.branches():
            answers = True
            last_cnx = None
            last = branch.last_branch_item
            try:
                # To compute the branch length, we must keep only the symbols,
                # so we must remove the last connection (if any)
                last_cnx, = (
                    c for c in last.childItems() if isinstance(c, Connection)
                    and not isinstance(c.child, (Comment, HorizontalSymbol)))
                # Don't set parent item to None to avoid Qt segfault
                last_cnx.setParentItem(self)
            except ValueError:
                pass
            branch_len = branch.y() + (branch.boundingRect() |
                                       branch.childrenBoundingRect()).height()
            try:
                last_cnx.setParentItem(last)
            except AttributeError:
                pass
            # If last item was a decision, use its connection point
            # position to get the length of the branch:
            try:
                branch_len = (last.connectionPoint.y() +
                              self.mapFromScene(0,
                                                last.scenePos().y()).y())
            except AttributeError:
                pass
            # Rounded with int() -> mandatory when view scale has changed
            new_y = int(max(new_y, branch_len))
        if not answers:
            new_y = int(self.boundingRect().height())
        new_y += 15
        delta = new_y - self.connectionPoint.y()
        self.connectionPoint.setY(new_y)
        self.connectionPoint.setX(new_x)
        if delta != 0:
            child = self.next_aligned_symbol()
            try:
                child.moveBy(0, delta)
            except AttributeError:
                pass
        self.update_connections()

    def PR(self, recursive=True):
        ''' Get PR notation of a decision (possibly recursively) '''
        comment = self.comment.PR() if self.comment else u';'
        pos = self.scenePos()
        result = [
            u'/* CIF DECISION ({x}, {y}), ({w}, {h}) */\n'
            '{hlink}'
            'DECISION {d}{comment}'.format(hlink=self.text.PR(),
                                           d=unicode(self),
                                           x=int(pos.x()),
                                           y=int(pos.y()),
                                           w=int(self.boundingRect().width()),
                                           h=int(self.boundingRect().height()),
                                           comment=comment)
        ]
        if recursive:
            for answer in self.branches():
                if unicode(answer).lower().strip() == 'else':
                    result.append(answer.PR())
                else:
                    result.insert(1, answer.PR())
        result.append('ENDDECISION;')
        return u'\n'.join(result)
Exemplo n.º 5
0
class Decision(VerticalSymbol):
    ''' SDL DECISION Symbol '''
    _unique_followers = ['Comment']
    _insertable_followers = ['DecisionAnswer', 'Task', 'ProcedureCall',
                             'Output', 'Decision', 'Label']
    _terminal_followers = ['Join', 'State', 'ProcedureStop']
    common_name = 'decision'
    # Define reserved keywords for the syntax highlighter
    blackbold = SDL_BLACKBOLD + ['\\b{}\\b'.format(word)
                                   for word in ('AND', 'OR')]
    redbold = SDL_REDBOLD

    def __init__(self, parent=None, ast=None):
        ast = ast or ogAST.Decision()
        self.ast = ast
        # Define the point where all branches of the decision can join again
        self.connectionPoint = QPoint(ast.width / 2, ast.height + 30)
        super(Decision, self).__init__(parent, text=ast.inputString,
                x=ast.pos_x or 0, y=ast.pos_y or 0, hyperlink=ast.hyperlink)
        self.set_shape(ast.width, ast.height)
        self.setBrush(QColor(255, 255, 202))
        self.minDistanceToSymbolAbove = 0
        self.parser = ogParser
        self.text_alignment = Qt.AlignHCenter
        if ast.comment:
            Comment(parent=self, ast=ast.comment)

    @property
    def terminal_symbol(self):
        '''
            Compute dynamically if the item is terminal by checking
            if all its branches end with a terminator
        '''
        for branch in self.branches():
            if not branch.last_branch_item.terminal_symbol:
                return False
        return True

    @property
    def completion_list(self):
        ''' Set auto-completion list '''
        return chain(variables_autocompletion(self), ('length', 'present'))

    def branches(self):
        ''' Return the list of decision answers (as a generator) '''
        return (branch for branch in self.childSymbols()
                if isinstance(branch, DecisionAnswer))

    def set_shape(self, width, height):
        ''' Define polygon points to draw the symbol '''
        path = QPainterPath()
        path.moveTo(width / 2, 0)
        path.lineTo(width, height / 2)
        path.lineTo(width / 2, height)
        path.lineTo(0, height / 2)
        path.lineTo(width / 2, 0)
        self.setPath(path)
        super(Decision, self).set_shape(width, height)

    def resize_item(self, rect):
        ''' On resize event, make sure connection points are updated '''
        delta_y = self.boundingRect().height() - rect.height()
        super(Decision, self).resize_item(rect)
        self.connectionPoint.setX(self.boundingRect().center().x())
        self.connectionPoint.setY(self.connectionPoint.y() - delta_y)
        self.update_connections()

    def update_connections(self):
        ''' Redefined - update arrows shape below connection point '''
        super(Decision, self).update_connections()
        for branch in self.branches():
            for cnx in branch.last_branch_item.connections():
                cnx.reshape()

    def updateConnectionPointPosition(self):
        ''' Compute the joining point of decision branches '''
        new_y = 0
        new_x = self.boundingRect().width() / 2.0
        answers = False
        for branch in self.branches():
            answers = True
            last_cnx = None
            last = branch.last_branch_item
            try:
                # To compute the branch length, we must keep only the symbols,
                # so we must remove the last connection (if any)
                last_cnx, = (c for c in last.childItems() if
                    isinstance(c, Connection) and not
                    isinstance(c.child, (Comment, HorizontalSymbol)))
                # Don't set parent item to None to avoid Qt segfault
                last_cnx.setParentItem(self)
            except ValueError:
                pass
            branch_len = branch.y() + (
                    branch.boundingRect() |
                    branch.childrenBoundingRect()).height()
            try:
                last_cnx.setParentItem(last)
            except AttributeError:
                pass
            # If last item was a decision, use its connection point
            # position to get the length of the branch:
            try:
                branch_len = (last.connectionPoint.y() +
                        self.mapFromScene(0, last.scenePos().y()).y())
            except AttributeError:
                pass
            # Rounded with int() -> mandatory when view scale has changed
            new_y = int(max(new_y, branch_len))
        if not answers:
            new_y = int(self.boundingRect().height())
        new_y += 15
        delta = new_y - self.connectionPoint.y()
        self.connectionPoint.setY(new_y)
        self.connectionPoint.setX(new_x)
        if delta != 0:
            child = self.next_aligned_symbol()
            try:
                child.pos_y += delta
            except AttributeError:
                pass
        self.update_connections()
Exemplo n.º 6
0
    def __decompose(self, poly):
        lower_poly = []
        upper_poly = []

        upper_int = QPoint(0, 0)
        lower_int = QPoint(0, 0)

        upper_index = 0
        lower_index = 0
        closest_index = 0

        p = QPoint(0, 0)

        for pi in poly:
            if is_reflex(poly, pi):

                pim1 = poly[-1 if pi is poly[0] else poly.index(pi) - 1]
                pip1 = poly[0 if pi is poly[-1] else poly.index(pi) + 1]

                self.reflex_vertices.append(pi)
                upper_dist = sys.maxint
                lower_dist = sys.maxint

                for pj in poly:
                    pjm1 = poly[-1 if pj is poly[0] else poly.index(pj) - 1]
                    pjp1 = poly[0 if pj is poly[-1] else poly.index(pj) + 1]

                    if left(pim1, pi, pj) and right_on(pim1, pi, pjm1):
                        p = intersection(pim1, pi, pj, pjm1)
                        if right(pip1, pi, p):
                            d = sqdist(pi, p)
                            if d < lower_dist:
                                lower_dist = d
                                lower_int = p
                                lower_index = poly.index(pj)

                    if left(pip1, pi, pjp1) and right_on(pip1, pi, pj):
                        p = intersection(pip1, pi, pj, pjp1)
                        if left(pim1, pi, p):
                            d = sqdist(pi, p)
                            if d < upper_dist:
                                upper_dist = d
                                upper_int = p
                                upper_index = poly.index(pj)

                i = poly.index(pi)
                if lower_index == ((upper_index + 1) % len(poly)):
                    p.setX((lower_int.x() + upper_int.x()) / 2)
                    p.setY((lower_int.y() + upper_int.y()) / 2)

                    self.steiner_points.append(p)

                    if i < upper_index:
                        lower_poly += poly[i:upper_index + 1]

                        lower_poly.append(p)
                        upper_poly.append(p)
                        if lower_index != 0:
                            upper_poly += poly[lower_index:]

                        upper_poly += poly[:i + 1]
                    else:
                        if i != 0:
                            lower_poly += poly[i:]

                        lower_poly += poly[:upper_index + 1]
                        lower_poly.append(p)
                        upper_poly.append(p)

                        upper_poly += poly[lower_index:i + 1]
                else:
                    if lower_index > upper_index:
                        upper_index += len(poly)

                    closest_dist = sys.float_info.max
                    for j in xrange(lower_index, upper_index + 1):

                        pj = poly[j % len(poly)]

                        if left_on(pim1, pi, pj) and right_on(pip1, pi, pj):
                            d = sqdist(pi, pj)
                            if d < closest_dist:
                                closest_dist = d
                                closest_index = j % len(poly)

                    if i < closest_index:
                        lower_poly += poly[i:closest_index + 1]
                        if closest_index != 0:
                            upper_poly += poly[closest_index:]

                        upper_poly += poly[:i + 1]
                    else:
                        if i != 0:
                            lower_poly += poly[i:]

                        lower_poly += poly[:closest_index + 1]
                        upper_poly += poly[closest_index:i + 1]

                if len(lower_poly) < len(upper_poly):
                    self.decompose(lower_poly)
                    self.decompose(upper_poly)
                else:
                    self.decompose(upper_poly)
                    self.decompose(lower_poly)
                return

        self.polys.append(poly)
    def __decompose(self, poly):
        lower_poly = []
        upper_poly = []

        upper_int = QPoint(0, 0)
        lower_int = QPoint(0, 0)

        upper_index = 0
        lower_index = 0
        closest_index = 0

        p = QPoint(0, 0)

        for pi in poly:
            if is_reflex(poly, pi):

                pim1 = poly[-1 if pi is poly[0] else poly.index(pi) - 1]
                pip1 = poly[0 if pi is poly[-1] else poly.index(pi) + 1]

                self.reflex_vertices.append(pi)
                upper_dist = sys.maxint
                lower_dist = sys.maxint

                for pj in poly:
                    pjm1 = poly[-1 if pj is poly[0] else poly.index(pj) - 1]
                    pjp1 = poly[0 if pj is poly[-1] else poly.index(pj) + 1]

                    if left(pim1, pi, pj) and right_on(pim1, pi, pjm1):
                        p = intersection(pim1, pi, pj, pjm1)
                        if right(pip1, pi, p):
                            d = sqdist(pi, p)
                            if d < lower_dist:
                                lower_dist = d
                                lower_int = p
                                lower_index = poly.index(pj)

                    if left(pip1, pi, pjp1) and right_on(pip1, pi, pj):
                        p = intersection(pip1, pi, pj, pjp1)
                        if left(pim1, pi, p):
                            d = sqdist(pi, p)
                            if d < upper_dist:
                                upper_dist = d
                                upper_int = p
                                upper_index = poly.index(pj)

                i = poly.index(pi)
                if lower_index == ((upper_index + 1) % len(poly)):
                    p.setX((lower_int.x() + upper_int.x()) / 2)
                    p.setY((lower_int.y() + upper_int.y()) / 2)

                    self.steiner_points.append(p)

                    if i < upper_index:
                        lower_poly += poly[i : upper_index + 1]

                        lower_poly.append(p)
                        upper_poly.append(p)
                        if lower_index != 0:
                            upper_poly += poly[lower_index:]

                        upper_poly += poly[: i + 1]
                    else:
                        if i != 0:
                            lower_poly += poly[i:]

                        lower_poly += poly[: upper_index + 1]
                        lower_poly.append(p)
                        upper_poly.append(p)

                        upper_poly += poly[lower_index : i + 1]
                else:
                    if lower_index > upper_index:
                        upper_index += len(poly)

                    closest_dist = sys.float_info.max
                    for j in xrange(lower_index, upper_index + 1):

                        pj = poly[j % len(poly)]

                        if left_on(pim1, pi, pj) and right_on(pip1, pi, pj):
                            d = sqdist(pi, pj)
                            if d < closest_dist:
                                closest_dist = d
                                closest_index = j % len(poly)

                    if i < closest_index:
                        lower_poly += poly[i : closest_index + 1]
                        if closest_index != 0:
                            upper_poly += poly[closest_index:]

                        upper_poly += poly[: i + 1]
                    else:
                        if i != 0:
                            lower_poly += poly[i:]

                        lower_poly += poly[: closest_index + 1]
                        upper_poly += poly[closest_index : i + 1]

                if len(lower_poly) < len(upper_poly):
                    self.decompose(lower_poly)
                    self.decompose(upper_poly)
                else:
                    self.decompose(upper_poly)
                    self.decompose(lower_poly)
                return

        self.polys.append(poly)
Exemplo n.º 8
0
class Decision(VerticalSymbol):
    ''' SDL DECISION Symbol '''
    _unique_followers = ['Comment']
    _insertable_followers = ['DecisionAnswer', 'Task', 'ProcedureCall',
                             'Output', 'Decision', 'Label']
    _terminal_followers = ['Join', 'State', 'ProcedureStop']
    common_name = 'decision'
    # Define reserved keywords for the syntax highlighter
    blackbold = SDL_BLACKBOLD + ['\\b{}\\b'.format(word)
                                   for word in ('AND', 'OR')]
    redbold = SDL_REDBOLD
    completion_list = {'length', 'present'}

    def __init__(self, parent=None, ast=None):
        ast = ast or ogAST.Decision()
        # Define the point where all branches of the decision can join again
        self.connectionPoint = QPoint(ast.width / 2, ast.height + 30)
        super(Decision, self).__init__(parent, text=ast.inputString,
                x=ast.pos_x, y=ast.pos_y, hyperlink=ast.hyperlink)
        self.set_shape(ast.width, ast.height)
        self.setBrush(QColor(255, 255, 202))
        self.minDistanceToSymbolAbove = 0
        self.parser = ogParser
        self.text_alignment = Qt.AlignHCenter
        if ast.comment:
            Comment(parent=self, ast=ast.comment)

    @property
    def terminal_symbol(self):
        '''
            Compute dynamically if the item is terminal by checking
            if all its branches end with a terminator
        '''
        for branch in self.branches():
            if not branch.last_branch_item.terminal_symbol:
                return False
        return True

    def branches(self):
        ''' Return the list of decision answers (as a generator) '''
        return (branch for branch in self.childSymbols()
                if isinstance(branch, DecisionAnswer))

    def check_syntax(self):
        ''' Redefined function, to check only the symbol, not recursively '''
        _, syntax_errors, _, _, _ = self.parser.parseSingleElement(
                self.common_name, self.PR(recursive=False))
        try:
            # LOG.error('\n'.join(syntax_errors))
            self.scene().raise_syntax_errors(syntax_errors)
        except AttributeError:
            LOG.debug('raise_syntax_error raised an exception')

    def set_shape(self, width, height):
        ''' Define polygon points to draw the symbol '''
        path = QPainterPath()
        path.moveTo(width / 2, 0)
        path.lineTo(width, height / 2)
        path.lineTo(width / 2, height)
        path.lineTo(0, height / 2)
        path.lineTo(width / 2, 0)
        self.setPath(path)
        super(Decision, self).set_shape(width, height)

    def resize_item(self, rect):
        ''' On resize event, make sure connection points are updated '''
        delta_y = self.boundingRect().height() - rect.height()
        super(Decision, self).resize_item(rect)
        self.connectionPoint.setX(self.boundingRect().center().x())
        self.connectionPoint.setY(self.connectionPoint.y() - delta_y)
        self.update_connections()

    def update_connections(self):
        ''' Redefined - update arrows shape below connection point '''
        super(Decision, self).update_connections()
        for branch in self.branches():
            for cnx in branch.last_branch_item.connections():
                cnx.reshape()

    def updateConnectionPointPosition(self):
        ''' Compute the joining point of decision branches '''
        new_y = 0
        new_x = self.boundingRect().width() / 2.0
        answers = False
        for branch in self.branches():
            answers = True
            last_cnx = None
            last = branch.last_branch_item
            try:
                # To compute the branch length, we must keep only the symbols,
                # so we must remove the last connection (if any)
                last_cnx, = (c for c in last.childItems() if
                    isinstance(c, Connection) and not
                    isinstance(c.child, (Comment, HorizontalSymbol)))
                # Don't set parent item to None to avoid Qt segfault
                last_cnx.setParentItem(self)
            except ValueError:
                pass
            branch_len = branch.y() + (
                    branch.boundingRect() |
                    branch.childrenBoundingRect()).height()
            try:
                last_cnx.setParentItem(last)
            except AttributeError:
                pass
            # If last item was a decision, use its connection point
            # position to get the length of the branch:
            try:
                branch_len = (last.connectionPoint.y() +
                        self.mapFromScene(0, last.scenePos().y()).y())
            except AttributeError:
                pass
            # Rounded with int() -> mandatory when view scale has changed
            new_y = int(max(new_y, branch_len))
        if not answers:
            new_y = int(self.boundingRect().height())
        new_y += 15
        delta = new_y - self.connectionPoint.y()
        self.connectionPoint.setY(new_y)
        self.connectionPoint.setX(new_x)
        if delta != 0:
            child = self.next_aligned_symbol()
            try:
                child.moveBy(0, delta)
            except AttributeError:
                pass
        self.update_connections()

    def PR(self, recursive=True):
        ''' Get PR notation of a decision (possibly recursively) '''
        comment = self.comment.PR() if self.comment else u';'
        pos = self.scenePos()
        result = [u'/* CIF DECISION ({x}, {y}), ({w}, {h}) */\n'
                '{hlink}'
                'DECISION {d}{comment}'.format(
                    hlink=self.text.PR(), d=unicode(self),
                    x=int(pos.x()), y=int(pos.y()),
                    w=int(self.boundingRect().width()),
                    h=int(self.boundingRect().height()), comment=comment)]
        if recursive:
            for answer in self.branches():
                if unicode(answer).lower().strip() == 'else':
                    result.append(answer.PR())
                else:
                    result.insert(1, answer.PR())
        result.append('ENDDECISION;')
        return u'\n'.join(result)