Пример #1
0
 def keyReleaseEvent(self, keyevent):
     """ keyReleaseEvent is received after the keypress is registered by editor, so if we
     check cursor position here we receive the situation after normal cursor movement. So
     moving 'up' to first line would also register here as being in first line and moving up.
     Which is one up too many. So instead we store the last cursor pos and use that to decide
     if we are eg. in first line and moving up.
     :param keyevent:
     :return:
     """
     p = self.parent()
     c = self.textCursor()
     p.cursor_position_changed(c)
     next_sel = None
     if p._fresh_focus:
         p._fresh_focus = False
     elif p._last_blockpos:
         first, last, first_line, last_line = p._last_blockpos
         if first and keyevent.matches(QtGui.QKeySequence.MoveToPreviousChar):
             next_sel = ctrl.graph_scene.next_selectable_from_node(p._host, 'left')
         elif last and keyevent.matches(QtGui.QKeySequence.MoveToNextChar):
             next_sel = ctrl.graph_scene.next_selectable_from_node(p._host, 'right')
         elif first_line and keyevent.matches(QtGui.QKeySequence.MoveToPreviousLine):
             next_sel = ctrl.graph_scene.next_selectable_from_node(p._host, 'up')
         elif last_line and keyevent.matches(QtGui.QKeySequence.MoveToNextLine):
             next_sel = ctrl.graph_scene.next_selectable_from_node(p._host, 'down')
         if next_sel and next_sel != p._host:
             self.clearFocus()
             ctrl.select(next_sel)
             next_sel.setFocus()
     p._last_blockpos = (c.atStart(), c.atEnd(), c.blockNumber() == 0,
                         c.blockNumber() == p.editable_doc.blockCount() - 1)
Пример #2
0
 def create_arrow_from_node_to_point(self, start_node, end_point):
     edge = self.create_edge(start=None, end=None, edge_type=g.ARROW)
     edge.connect_start_to(start_node)
     edge.set_end_point(end_point)
     edge.show()
     ctrl.select(edge)
     return edge
Пример #3
0
 def create_arrow_from_node_to_point(self, start_node, end_point):
     edge = self.create_edge(start=None, end=None, edge_type=g.ARROW)
     edge.connect_start_to(start_node)
     edge.set_end_point(end_point)
     edge.show()
     ctrl.select(edge)
     return edge
Пример #4
0
    def create_arrow(self, p1, p2, text=None, fade=True):
        """ Create an arrow (Edge) using the default arrow style

        :param p1: start point
        :param p2: end point
        :param text: explanatory text associated with the arrow
        :param fade: fade in or appear instantly
        :return:
        """
        start_point = None
        end_point = None
        if isinstance(p1, Node):
            start = p1
        elif isinstance(p1, tuple):
            start_point = p1
            start = None
        else:
            start_point = random.randint(-50, 50), random.randint(-50, 50)
            start = None
        if isinstance(p2, Node):
            end = p2
        elif isinstance(p2, tuple):
            end_point = p2
            end = None
        else:
            end_point = random.randint(-50, 50), random.randint(-50, 50)
            end = None
        arrow = Arrow(start=start, end=end, start_point=start_point,
                      end_point=end_point, text=text)
        self.forest.store(arrow)
        self.forest.add_to_scene(arrow)
        if fade and self.forest.in_display:
            arrow.fade_in()
        ctrl.select(arrow)
        return arrow
Пример #5
0
    def move_selection(self, direction, add_to_selection=False):
        """ Move selection to best candidate
        :param direction:
        """
        def edge_of_set(my_selectables):
            if direction == 'left':
                sortable = [(po[0], po[1], it) for it, po in my_selectables]
                x, y, item = min(sortable)
            elif direction == 'right':
                sortable = [(po[0], po[1], it) for it, po in my_selectables]
                x, y, item = max(sortable)
            elif direction == 'up':
                sortable = [(po[1], po[0], it) for it, po in my_selectables]
                y, x, item = min(sortable)
            elif direction == 'down':
                sortable = [(po[1], po[0], it) for it, po in my_selectables]
                y, x, item = max(sortable)
            else:
                raise KeyError
            return item

        # debugging plotter
        # for item, pos in selectables:
        # x,y = pos
        # el = QtGui.QGraphicsEllipseItem(x-2, y-2, 4, 4)
        # el.setBrush(colors.drawing)
        # self.addItem(el)
        #

        # ############### Absolute left/right/up/down ###############################
        # if nothing is selected, select the edgemost item from given direction
        if not ctrl.selected:
            selectables = [(item, to_tuple(item.sceneBoundingRect().center())) for item in
                           self.items() if hasattr(item, 'select') and item.is_visible()]
            best = edge_of_set(selectables)
            ctrl.select(best)
            return best
        # ################ Relative left/right/up/down #############################
        else:
            if len(ctrl.selected) == 1:
                current = ctrl.get_single_selected()
            else:
                # when there are many selected items, extend it to given direction, from the
                # edgemost item in that direction.
                # this behavior may give odd results, but there may be no intuitive ways how such
                #  a blob of selections should behave.
                selectables = [(item, to_tuple(item.sceneBoundingRect().center())) for item in
                               ctrl.selected if item.is_visible()]
                current = edge_of_set(selectables)
            best = None
            if isinstance(current, Node):
                best = self.next_selectable_from_node(current, direction)
            elif isinstance(current, Edge):
                best = self.next_selectable_from_edge(current, direction)
            if best:
                if add_to_selection:
                    ctrl.add_to_selection(best)
                else:
                    ctrl.select(best)
            return best
Пример #6
0
    def create_arrow(self, p1, p2, text=None):
        """ Create an arrow (Edge) using the default arrow style

        :param p1: start point
        :param p2: end point
        :param text: explanatory text associated with the arrow
        :return:
        """
        edge = self.create_edge(start=None, end=None, edge_type=g.ARROW)
        edge.set_start_point(p1)
        edge.set_end_point(p2)
        if text:
            edge.set_label_text(text)
        edge.show()
        ctrl.select(edge)
        return edge
Пример #7
0
    def create_arrow(self, p1, p2, text=None):
        """ Create an arrow (Edge) using the default arrow style

        :param p1: start point
        :param p2: end point
        :param text: explanatory text associated with the arrow
        :return:
        """
        edge = self.create_edge(start=None, end=None, edge_type=g.ARROW)
        edge.set_start_point(p1)
        edge.set_end_point(p2)
        if text:
            edge.set_label_text(text)
        edge.show()
        ctrl.select(edge)
        return edge
Пример #8
0
 def method(self, object_uid):
     """ Select node, edge or other forest object
     :param object_uid: int or str uid of item to select, or list of uids for selecting
      multiple objects, or None to empty the current selection.
     :return: None
     """
     if object_uid is None:
         ctrl.deselect_objects()
     elif isinstance(object_uid, (list, tuple)):
         objs = []
         for uid in object_uid:
             obj = ctrl.forest.get_object_by_uid(uid)
             if obj:
                 objs.append(obj)
         ctrl.select(objs)
     else:
         obj = ctrl.forest.get_object_by_uid(object_uid)
         ctrl.select(obj)
Пример #9
0
 def keyReleaseEvent(self, keyevent):
     """ keyReleaseEvent is received after the keypress is registered by editor, so if we
     check cursor position here we receive the situation after normal cursor movement. So
     moving 'up' to first line would also register here as being in first line and moving up.
     Which is one up too many. So instead we store the last cursor pos and use that to decide
     if we are eg. in first line and moving up.
     :param keyevent:
     :return:
     """
     p = self.parent()
     c = self.textCursor()
     p.cursor_position_changed(c)
     next_sel = None
     if p._fresh_focus:
         p._fresh_focus = False
     elif p._last_blockpos:
         first, last, first_line, last_line = p._last_blockpos
         if first and keyevent.matches(
                 QtGui.QKeySequence.MoveToPreviousChar):
             next_sel = ctrl.graph_scene.next_selectable_from_node(
                 p._host, 'left')
         elif last and keyevent.matches(QtGui.QKeySequence.MoveToNextChar):
             next_sel = ctrl.graph_scene.next_selectable_from_node(
                 p._host, 'right')
         elif first_line and keyevent.matches(
                 QtGui.QKeySequence.MoveToPreviousLine):
             next_sel = ctrl.graph_scene.next_selectable_from_node(
                 p._host, 'up')
         elif last_line and keyevent.matches(
                 QtGui.QKeySequence.MoveToNextLine):
             next_sel = ctrl.graph_scene.next_selectable_from_node(
                 p._host, 'down')
         if next_sel and next_sel != p._host:
             self.clearFocus()
             ctrl.select(next_sel)
             next_sel.setFocus()
     p._last_blockpos = (c.atStart(), c.atEnd(), c.blockNumber() == 0,
                         c.blockNumber() == p.editable_doc.blockCount() - 1)
Пример #10
0
    def move_selection(self, direction, add_to_selection=False):
        """ Move selection to best candidate
        :param direction:
        """
        def edge_of_set(my_selectables):
            if direction == 'left':
                sortable = [(po[0], po[1], it) for it, po in my_selectables]
                x, y, item = min(sortable)
            elif direction == 'right':
                sortable = [(po[0], po[1], it) for it, po in my_selectables]
                x, y, item = max(sortable)
            elif direction == 'up':
                sortable = [(po[1], po[0], it) for it, po in my_selectables]
                y, x, item = min(sortable)
            elif direction == 'down':
                sortable = [(po[1], po[0], it) for it, po in my_selectables]
                y, x, item = max(sortable)
            else:
                raise KeyError
            return item

        # debugging plotter
        # for item, pos in selectables:
        # x,y = pos
        # el = QtGui.QGraphicsEllipseItem(x-2, y-2, 4, 4)
        # el.setBrush(colors.drawing)
        # self.addItem(el)
        #

        # ############### Absolute left/right/up/down ###############################
        # if nothing is selected, select the edgemost item from given direction
        if not ctrl.selected:
            selectables = [(item, to_tuple(item.sceneBoundingRect().center()))
                           for item in self.items()
                           if hasattr(item, 'select') and item.is_visible()]
            best = edge_of_set(selectables)
            ctrl.select(best)
            return best
        # ################ Relative left/right/up/down #############################
        else:
            if len(ctrl.selected) == 1:
                current = ctrl.get_single_selected()
            else:
                # when there are many selected items, extend it to given direction, from the
                # edgemost item in that direction.
                # this behavior may give odd results, but there may be no intuitive ways how such
                #  a blob of selections should behave.
                selectables = [(item,
                                to_tuple(item.sceneBoundingRect().center()))
                               for item in ctrl.selected if item.is_visible()]
                current = edge_of_set(selectables)
            best = None
            if isinstance(current, Node):
                best = self.next_selectable_from_node(current, direction)
            elif isinstance(current, Edge):
                best = self.next_selectable_from_edge(current, direction)
            if best:
                if add_to_selection:
                    ctrl.add_to_selection(best)
                else:
                    ctrl.select(best)
            return best