Ejemplo n.º 1
0
class GroupLabelEmbed(UIEmbed):
    def __init__(self, parent, edge):
        UIEmbed.__init__(self, parent, edge, 'Highlight a group of nodes')
        self.marker = None
        ui = self.ui_manager
        layout = QtWidgets.QVBoxLayout()
        layout.addLayout(self.top_row_layout)  # close-button from UIEmbed
        smaller_font = qt_prefs.get_font(g.ITALIC_FONT)
        big_font = QtGui.QFont(smaller_font)
        big_font.setPointSize(big_font.pointSize() * 2)
        self.input_line_edit = ExpandingLineEdit(self,
                                                 big_font=big_font,
                                                 smaller_font=smaller_font,
                                                 prefill='label')
        hlayout = QtWidgets.QHBoxLayout()
        hlayout.addWidget(self.input_line_edit)
        label = make_label('Name for the group (optional)',
                           parent=self,
                           layout=hlayout,
                           tooltip='Group of nodes can be singled out and named, e.g. as phases',
                           buddy=self.input_line_edit)
        layout.addLayout(hlayout)
        hlayout = QtWidgets.QHBoxLayout()
        self.color_select = ColorSelector(self)
        ui.connect_element_to_action(self.color_select, 'change_group_color')
        hlayout.addWidget(self.color_select, 1, QtCore.Qt.AlignRight)
        label = make_label('Color',
                           parent=self,
                           layout=hlayout,
                           tooltip='Select color for highlight',
                           buddy=self.color_select,
                           align=QtCore.Qt.AlignLeft)
        self.fill_checkbox = QtWidgets.QCheckBox()
        ui.connect_element_to_action(self.fill_checkbox, 'change_group_fill')
        hlayout.addWidget(self.fill_checkbox, 1, QtCore.Qt.AlignRight)
        label = make_label('Fill',
                           parent=self,
                           layout=hlayout,
                           tooltip="Group area is marked with translucent color",
                           buddy=self.fill_checkbox,
                           align=QtCore.Qt.AlignLeft)
        self.outline_checkbox = QtWidgets.QCheckBox()
        ui.connect_element_to_action(self.outline_checkbox, 'change_group_outline')
        hlayout.addWidget(self.outline_checkbox, 1, QtCore.Qt.AlignRight)
        label = make_label('Outline',
                           parent=self,
                           layout=hlayout,
                           tooltip="Group is marked by line drawn around it",
                           buddy=self.outline_checkbox,
                           align=QtCore.Qt.AlignLeft)

        self.include_children_checkbox = QtWidgets.QCheckBox()
        ui.connect_element_to_action(self.include_children_checkbox, 'change_group_children')
        hlayout.addWidget(self.include_children_checkbox, 1, QtCore.Qt.AlignRight)
        label = make_label('Include children',
                           parent=self,
                           layout=hlayout,
                           tooltip="Automatically add child nodes to group's scope",
                           buddy=self.include_children_checkbox,
                           align=QtCore.Qt.AlignLeft)
        self.allow_overlap_checkbox = QtWidgets.QCheckBox()
        ui.connect_element_to_action(self.allow_overlap_checkbox, 'change_group_overlaps')
        hlayout.addWidget(self.allow_overlap_checkbox, 1, QtCore.Qt.AlignRight)
        label = make_label('Allow groups to overlap',
                           parent=self,
                           layout=hlayout,
                           tooltip="Can group include members of other group. If not, lower group "
                                   "has priority",
                           buddy=self.allow_overlap_checkbox,
                           align=QtCore.Qt.AlignLeft)

        layout.addLayout(hlayout)
        hlayout = QtWidgets.QHBoxLayout()
        self.delete_button = QtWidgets.QPushButton("Delete")  # U+21A9 ↩
        self.delete_button.setMaximumWidth(60)
        ui.connect_element_to_action(self.delete_button, 'delete_group')

        hlayout.addWidget(self.delete_button)
        self.enter_button = QtWidgets.QPushButton("Keep ↩")  # U+21A9 ↩
        self.enter_button.setMaximumWidth(60)
        ui.connect_element_to_action(self.enter_button, 'save_group_changes')
        hlayout.addStretch(0)
        hlayout.addWidget(self.enter_button)
        layout.addLayout(hlayout)
        self.setLayout(layout)
        #self.assumed_width = 200
        #self.assumed_height = 37
        self.update_position()
        self.setEnabled(True)

    def update_fields(self):
        """

        :return:
        """
        a = self.host
        self.allow_overlap_checkbox.setChecked(a.allow_overlap)
        self.include_children_checkbox.setChecked(a.include_children)
        self.input_line_edit.setText(a.get_label_text())
        self.outline_checkbox.setChecked(a.outline)
        self.fill_checkbox.setChecked(a.fill)
        s = self.color_select
        s.model().selected_color = a.color_key
        s.select_by_data(a.color_key)
        s.update()
        super().update_fields()

    def focus_to_main(self):
        self.input_line_edit.setFocus()

    def update_embed(self, focus_point=None):
        if self.host:
            p = QtGui.QPalette()
            p.setColor(QtGui.QPalette.Text, self.host.color)
            self.input_line_edit.setPalette(p)
        super().update_embed(focus_point=focus_point)

    def update_position(self, focus_point=None):
        super().update_position(focus_point=focus_point)
Ejemplo n.º 2
0
class NewElementEmbed(UIEmbed):
    def __init__(self, parent):
        UIEmbed.__init__(self, parent, None, 'Create new node')
        self.marker = None
        self.guess_mode = True
        layout = QtWidgets.QVBoxLayout()
        layout.addLayout(self.top_row_layout)
        hlayout = box_row(layout)
        ui = self.ui_manager
        self.new_arrow_button = icon_text_button(ui, hlayout, self, '', '',
                                                 " &Arrow", 'new_arrow', size=QtCore.QSize(48, 20),
                                                 draw_method=arrow)
        self.divider_button = icon_text_button(ui, hlayout, self, '', '',
                                               " &Divider", 'new_divider',
                                               size=QtCore.QSize(48, 20), draw_method=divider)
        self.new_arrow_button.setFlat(False)
        self.divider_button.setFlat(False)
        self.new_arrow_button.hide()
        self.divider_button.hide()
        tt = 'Text for new node'
        smaller_font = qt_prefs.get_font(g.MAIN_FONT)
        big_font = QtGui.QFont(smaller_font)
        big_font.setPointSize(big_font.pointSize() * 2)
        self.input_line_edit = ExpandingLineEdit(self,
                                                 tip=tt,
                                                 big_font=big_font,
                                                 smaller_font=smaller_font,
                                                 prefill='label',
                                                 on_edit=self.guess_type_for_input)
        layout.addWidget(self.input_line_edit)
        hlayout = QtWidgets.QHBoxLayout()
        self.node_type_selector = SelectionBox(self)
        self.node_type_selector.currentIndexChanged.connect(self.changed_node_type)

        self.node_types = [('Guess from input', g.GUESS_FROM_INPUT)]
        for key in classes.node_types_order:
            # we have dedicated buttons for arrows and dividers
            #if key not in (g.ARROW, g.DIVIDER):
            node_class = classes.nodes.get(key, None)
            if (not node_class) or node_class.is_syntactic and not ctrl.free_drawing_mode:
                continue
            self.node_types.append(('New %s' % node_class.display_name[0].lower(), key))
        self.node_types.append(('New arrow', g.ARROW))
        #self.node_types.append(('New divider', g.DIVIDER))
        self.node_type_selector.add_items(self.node_types)
        hlayout.addWidget(self.node_type_selector)
        hlayout.addStretch(0)
        self.enter_button = QtWidgets.QPushButton("Create ↩")  # U+21A9 ↩
        ui.connect_element_to_action(self.enter_button, 'create_new_node_from_text')

        hlayout.addWidget(self.enter_button)
        layout.addLayout(hlayout)
        self.setLayout(layout)
        self.assumed_width = 200
        self.assumed_height = 117

    @property
    def graphic_item(self):
        return self.marker

    def mouseMoveEvent(self, event):
        self.move(self.mapToParent(event.pos()) - self._drag_diff)
        if self.marker:
            self.marker.update_position()
            self.marker.set_dragged(True)
        QtWidgets.QWidget.mouseMoveEvent(self, event)

    def guess_type_for_input(self, text):
        if not self.guess_mode:
            return
        key = guess_node_type(text)
        if key == 100:
            self.top_title.setText('Create new tree')
        else:
            self.top_title.setText('Create new ' + classes.node_info[key]['name'].lower())

    def changed_node_type(self, index=-1):
        if index == 0:
            self.guess_mode = True
        elif index > 0:
            title_text = self.node_type_selector.itemText(index)
            self.top_title.setText('Create ' + title_text.lower())
            self.guess_mode = False

    def focus_to_main(self):
        self.input_line_edit.setFocus(QtCore.Qt.PopupFocusReason)

    def marker_dragged(self):
        if self.guess_mode:
            self.set_node_type(g.ARROW)
            self.guess_mode = False

    def get_marker_points(self):
        p1 = self.marker.pos()
        p2 = self.marker.mapToScene(self.marker.end_point)
        return p1, p2

    def set_node_type(self, value):
        self.node_type_selector.setCurrentIndex(self.node_type_selector.findData(value, role=256))

    def resizeEvent(self, event):
        self.limited_update_position()
        QtWidgets.QWidget.resizeEvent(self, event)

    def limited_update_position(self):
        """ Updates the position but tries to minimize the movement of left top
        corner. This is called on resize, to ensure that all of the embed is in visible area.
        :return:
        """
        view = self.parent()
        eg = self.geometry()
        ex, ey, ew, eh = eg.getRect()
        top_left = view.mapToScene(ex, ey)
        bottom_right = view.mapToScene(QtCore.QPoint(ex + ew, ey + eh))
        size_in_scene = bottom_right - top_left
        x = top_left.x()
        y = top_left.y()
        w = size_in_scene.x()
        h = size_in_scene.y()
        vr = view.mapToScene(view.geometry()).boundingRect()
        view_left, view_top, view_right, view_bottom = vr.getRect()
        view_right += view_left
        view_bottom += view_top

        w_overlap = (x + w) - view_right
        h_overlap = (y + h) - view_bottom

        move = False
        if w_overlap > 0:
            x = x - w_overlap - 12
            move = True
        if h_overlap > 0:
            y = y - h_overlap - 8
            move = True
        if move:
            new_pos = QtCore.QPoint(x, y)
            self.move(view.mapFromScene(new_pos))
            self.updateGeometry()