Exemplo n.º 1
0
class TestTreeView(TestBase):
    def setUp(self):
        self.treeview = TreeView("Title", self.action, None)

    def action(self):
        pass

    def test_populate(self):
        items = ["A", "B", "C"]
        self.treeview.populate(items)

    def test_get_selection(self):
        self.treeview.get_selection()
Exemplo n.º 2
0
class BlockCodeEditor(Gtk.ScrolledWindow):
    """
    This class contains methods related the BlockCodeEditor class
    """

    # ----------------------------------------------------------------------
    def __init__(self, block_editor, block):
        Gtk.ScrolledWindow.__init__(self)

        self.block_editor = block_editor
        self.block = block

        for widget in self.get_children():
            self.remove(widget)

        vbox = Gtk.VBox()
        self.add(vbox)

        # Code Parts
        content_pane = Gtk.HBox()
        vbox.pack_start(content_pane, True, True, 1)

        self.tree_view = TreeView(_("Code Parts"), self.__on_row_activated)
        content_pane.pack_start(self.tree_view, True, True, 1)

        button_bar = ButtonBar()
        button_bar.add_button({
            "icone": Gtk.STOCK_NEW,
            "action": self.__new,
            "data": None
        })
        button_bar.add_button({
            "icone": Gtk.STOCK_DELETE,
            "action": self.__delete,
            "data": None
        })
        vbox.pack_start(button_bar, False, False, 1)

        self.codes = self.block.codes
        self.__populate_list()

        self.side_panel = Gtk.VBox()
        content_pane.pack_start(self.side_panel, True, True, 1)

        self.show_all()

    # ----------------------------------------------------------------------
    def __on_edit(self, widget=None, data=None):
        """
        This method save the block.
            Parameters:
                * **block** (:class:`<>`)
        """
        count = 0
        for code_widget in self.code_widgets:
            self.block.codes[count] = self.code_widgets[count].get_value()
            count = count + 1

    # ----------------------------------------------------------------------
    def __create_menu(self, widget, values, name):
        menu_item = Gtk.MenuItem(name)
        widget.append(menu_item)
        menu = Gtk.Menu()
        menu_item.set_submenu(menu)
        for value in values:
            item = Gtk.MenuItem(value)
            item.connect("activate", self.__on_select, None)
            menu.append(item)

    # ----------------------------------------------------------------------
    def __populate_menu(self, entry, widget):

        # Block Common Properties
        values = [
            "$id$", "$label$", "$x$", "$y$", "$type$", "$language$",
            "$framework$", "$group$", "$color$", "$help$"
        ]
        self.__create_menu(widget, values, "Common Properties")

        # Block Properties
        values = []
        for prop in self.block.get_properties():
            values.append("$prop[" + prop["name"] + "]$")
        values.sort()
        self.__create_menu(widget, values, "Block Properties")

        values = []
        for port in self.block.ports:
            values.append("$port[" + port["name"] + "]$")
        values.sort()
        self.__create_menu(widget, values, "Ports")

        widget.show_all()

    # ----------------------------------------------------------------------
    def __get_current_code_area(self):
        current_tab = None
        for widget in self.side_panel.get_children():
            if widget.get_name() == "code":
                current_tab = widget
                break
        return current_tab

    # ----------------------------------------------------------------------
    def __on_select(self, widget=None, data=None):
        code_area = self.__get_current_code_area()
        if code_area is None:
            return
        value = widget.get_label()
        code_area.insert_at_cursor(value)

    # ----------------------------------------------------------------------
    def __populate_list(self):
        labels = []
        for code_part in self.codes:
            labels.append(code_part)
        self.tree_view.populate(labels)

    # ----------------------------------------------------------------------
    def __on_row_activated(self, tree_view, path, column, data):
        treeselection = tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        name = model.get_value(model.get_iter(path), 0)

        configuration = {}
        configuration["name"] = name
        configuration["code"] = self.codes[name]
        self.__create_side_panel(configuration)

    # ----------------------------------------------------------------------
    def __new(self, widget=None, data=None):
        self.__create_side_panel(None)

    # ----------------------------------------------------------------------
    def __delete(self, widget=None, data=None):
        treeselection = None
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        path = model.get_path(iterac)
        name = model.get_value(model.get_iter(path), 0)

        dialog = Dialog().confirm_dialog(_("Are you sure?"), self)
        result = dialog.run()
        dialog.destroy()
        if result != Gtk.ResponseType.OK:
            return
        path = model.get_path(iterac)

        del self.codes[name]
        self.__populate_list()
        self.__clean_side_panel()

    # ----------------------------------------------------------------------
    def __clean_side_panel(self):
        for widget in self.side_panel.get_children():
            self.side_panel.remove(widget)

    # ----------------------------------------------------------------------
    def __create_side_panel(self, configuration):
        self.__clean_side_panel()

        data = {"label": _("Code Part Name"), "name": "name"}
        field = StringField(data, None)
        self.side_panel.pack_start(field, False, False, 1)
        try:
            field.set_value(configuration["name"])
        except:
            pass

        data = {"label": _("Code"), "name": "code"}
        field = CodeField(data, None)
        self.side_panel.pack_start(field, True, True, 1)
        try:
            field.set_value(configuration["code"])
        except:
            pass

        field.field.connect("populate-popup", self.__populate_menu)

        button = Gtk.Button.new_with_label("Save")
        button.connect("clicked", self.__add_code_part)
        self.side_panel.pack_start(button, False, False, 1)
        self.side_panel.show_all()

    # ----------------------------------------------------------------------
    def __add_code_part(self, widget=None, data=None):
        name = ""
        code = ""
        for widget in self.side_panel.get_children():
            if widget.get_name() == "name":
                name = widget.get_value()
            if widget.get_name() == "code":
                code = widget.get_value()
        if name == "":
            message = "Code Part Name can not be empty"
            Dialog().message_dialog("Error", message, self)
            return
        if code == "":
            message = "Code can not be empty"
            Dialog().message_dialog("Error", message, self)
            return
        contains = False
        i = 0
        self.codes[name] = code
        self.__populate_list()
        self.__clean_side_panel()
class BlockPropertyEditor(Gtk.ScrolledWindow):
    """
    This class contains methods related the BlockPropertyEditor class
    """

    # ----------------------------------------------------------------------
    def __init__(self, block_editor, block):

        Gtk.ScrolledWindow.__init__(self)

        self.block_editor = block_editor
        self.block = block

        for widget in self.get_children():
            self.remove(widget)

        hbox = Gtk.HBox()
        vbox2 = Gtk.VBox()
        self.add(hbox)

        self.tree_view = TreeView(_("Properties"), self.__on_props_row_activated)
        self.__populate_property()
        vbox2.pack_start(self.tree_view, True, True, 1)

        # Button bar
        button_bar = ButtonBar()
        button_bar.add_button({"icone":Gtk.STOCK_NEW, "action": self.__new, "data":None})
        button_bar.add_button({"icone":Gtk.STOCK_DELETE, "action": self.__delete, "data":None})
        button_bar.add_button({"icone":Gtk.STOCK_GO_UP, "action": self.__up, "data":None})
        button_bar.add_button({"icone":Gtk.STOCK_GO_DOWN, "action": self.__down, "data":None})
        vbox2.pack_start(button_bar, False, False, 1)

        hbox.pack_start(vbox2, True, True, 2)
        vbox = Gtk.VBox()
        hbox.pack_start(vbox, True, True, 2)
        self.side_panel = Gtk.VBox()
        vbox.pack_start(self.side_panel, True, True, 1)

        self.set_shadow_type(Gtk.ShadowType.IN)
        self.show_all()

    # ----------------------------------------------------------------------
    def __populate_property(self):
        block_label = []
        for prop in self.block.get_properties():
            block_label.append(prop.get("label"))
        self.tree_view.populate(block_label)

    # ----------------------------------------------------------------------
    def __new(self, widget=None, data=None):
        for widget in self.side_panel.get_children():
            self.side_panel.remove(widget)
        fieldtypes = []
        for key in component_list:
            fieldtypes.append(key)
        data = {"label": _("Field Type"), "values": fieldtypes}
        self.field_type = ComboField(data, self.__select_property_field_type)
        self.side_panel.pack_start(self.field_type, False, False, 1)

    # ----------------------------------------------------------------------
    def __delete(self, widget=None, data=None):
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        dialog = Dialog().confirm_dialog(_("Are you sure?"),
                self.block_editor)
        result = dialog.run()
        dialog.destroy()
        if result != Gtk.ResponseType.OK:
            return
        path = model.get_path(iterac)
        del self.block.get_properties()[int(str(path))]
        self.__populate_property()
        self.__clean_side_panel()

    # ----------------------------------------------------------------------
    def __up(self, widget=None, data=None):
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        path = model.get_path(iterac)
        if int(str(path)) == 0:
            return
        self.block.get_properties()[int(str(path))], \
            self.block.get_properties()[int(str(path)) - 1] = \
            self.block.get_properties()[int(str(path)) - 1], \
            self.block.get_properties()[int(str(path))]
        self.__populate_property()

    # ----------------------------------------------------------------------
    def __down(self, widget=None, data=None):
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        path = model.get_path(iterac)
        if int(str(path)) == len(self.block.get_properties()) - 1:
            return
        self.block.get_properties()[int(str(path))], \
            self.block.get_properties()[int(str(path)) + 1] = \
            self.block.get_properties()[int(str(path)) + 1], \
            self.block.get_properties()[int(str(path))]
        self.__populate_property()

    # ----------------------------------------------------------------------
    def __clean_side_panel(self):
        for widget in self.side_panel.get_children():
            self.side_panel.remove(widget)

    # ----------------------------------------------------------------------
    def __create_side_panel(self, configuration):
        self.__clean_side_panel()
        for key in configuration:
            data = {"label": _(key),
                    "name":key,
                    "value":str(configuration[key])}
            field = StringField(data, None)
            if key == "type":
                field.field.set_property("editable", False)
            self.side_panel.pack_start(field, False, False, 1)
        button = Gtk.Button.new_with_label("Save")
        button.connect("clicked", self.__on_props_edit_ok, None)
        self.side_panel.pack_start(button, False, False, 1)
        self.side_panel.show_all()

    # ----------------------------------------------------------------------
    def __select_property_field_type(self, widget=None, data=None):
        field_type = self.field_type.get_value()
        configuration = component_list[field_type].get_configuration()
        configuration["type"] = field_type
        self.__create_side_panel(configuration)

    # ----------------------------------------------------------------------
    def __on_props_row_activated(self, tree_view, path, column, data=None):
        configuration = self.block.get_properties()[int(str(path))]
        self.__create_side_panel(configuration)

    # ----------------------------------------------------------------------
    def __on_props_edit_ok(self, widget=None, data=None):
        configuration = {}
        for widget in self.side_panel.get_children():
            try:
                configuration[widget.get_name()] = widget.get_value()
            except:
                pass
        if "label" not in configuration or "name" not in configuration or \
                "value" not in configuration:
            return
        if configuration["label"] == "":
            message = "Label can not be empty"
            Dialog().message_dialog("Error", message, self.block_editor)
            return
        if configuration["name"] == "":
            message = "Name can not be empty"
            Dialog().message_dialog("Error", message, self.block_editor)
            return
        contains = False
        i = 0
        for props in self.block.properties:
            if props["label"] == configuration["label"]:
                self.block.properties[i] = configuration
                contains = True
            i += 1
        if not contains:
            self.block.properties.append(configuration)
        self.__populate_property()
        self.__clean_side_panel()
Exemplo n.º 4
0
class BlockPortEditor(Gtk.ScrolledWindow):
    """
    This class contains methods related the BlockPortEditor class
    """

    # ----------------------------------------------------------------------
    def __init__(self, block):
        Gtk.ScrolledWindow.__init__(self)
        self.block = block

        self.selected_port = None

        for widget in self.get_children():
            self.remove(widget)
        hbox = Gtk.HBox()
        vbox2 = Gtk.VBox()
        self.add(hbox)

        self.tree_view = TreeView(_("Ports"), self.__on_row_activated)
        vbox2.pack_start(self.tree_view, True, True, 1)

        # Button bar
        button_bar = ButtonBar()
        button_bar.add_button({"icone":Gtk.STOCK_NEW, "action": self.__new, "data":None})
        button_bar.add_button({"icone":Gtk.STOCK_DELETE, "action": self.__delete, "data":None})
        button_bar.add_button({"icone":Gtk.STOCK_GO_UP, "action": self.__up, "data":None})
        button_bar.add_button({"icone":Gtk.STOCK_GO_DOWN, "action": self.__down, "data":None})
        vbox2.pack_start(button_bar, False, False, 1)

        hbox.pack_start(vbox2, True, True, 2)
        vbox = Gtk.VBox()
        hbox.pack_start(vbox, True, True, 2)
        self.side_panel = Gtk.VBox()
        vbox.pack_start(self.side_panel, True, True, 1)
        self.__populate_list()

        self.set_shadow_type(Gtk.ShadowType.IN)
        self.show_all()

    # ----------------------------------------------------------------------
    def __populate_list(self):
        labels = []
        for port in self.block.ports:
            labels.append(port.label)
        self.tree_view.populate(labels)

    # ----------------------------------------------------------------------
    def __new(self, widget=None, data=None):
        self.selected_port = data
        configuration = None
        self.__create_side_panel(configuration)

    # ----------------------------------------------------------------------
    def __delete(self, widget=None, data=None):
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return None
        result = ConfirmDialog(_("Are you sure?"), self.get_toplevel()).run()
        if result != Gtk.ResponseType.OK:
            return
        path = model.get_path(iterac)

        del self.block.ports[int(str(path))]
        self.__populate_list()
        self.__clean_side_panel()

    # ----------------------------------------------------------------------
    def __up(self, widget=None, data=None):
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return
        path = model.get_path(iterac)
        if int(str(path)) == 0:
            return

        self.block.ports[int(str(path))], \
                self.block.ports[int(str(path)) - 1] = \
                self.block.ports[int(str(path)) - 1], \
                self.block.ports[int(str(path))]
        self.__populate_list()

    # ----------------------------------------------------------------------
    def __down(self, widget=None, data=None):
        treeselection = self.tree_view.get_selection()
        model, iterac = treeselection.get_selected()
        if iterac is None:
            return
        path = model.get_path(iterac)

        if int(str(path)) == len(self.block.ports) - 1:
            return
        self.block.ports[int(str(path))], \
                self.block.ports[int(str(path)) + 1] = \
                self.block.ports[int(str(path)) + 1], \
                self.block.ports[int(str(path))]
        self.__populate_list()

    # ----------------------------------------------------------------------
    def __clean_side_panel(self):
        for widget in self.side_panel.get_children():
            self.side_panel.remove(widget)

    # ----------------------------------------------------------------------
    def __create_side_panel(self, configuration):
        self.__clean_side_panel()

        connectors = []
        ports = System.get_ports()
        for key in ports:
            if ports[key].language == self.block.language:
                connectors.append(key)

        data = {"label": _("Port Type"), "name":"type", "values": connectors}
        field = ComboField(data, None)
        self.side_panel.pack_start(field, False, False, 1)
        if configuration is not None: field.set_value(configuration.type)

        data = {"label": _("Connection Type"), "name":"conn_type", "values": [Port.INPUT, Port.OUTPUT]}
        field = ComboField(data, None)
        self.side_panel.pack_start(field, False, False, 1)
        if configuration is not None: field.set_value(configuration.conn_type)

        data = {"label": _("Label"), "name":"label"}
        field = StringField(data, None)
        self.side_panel.pack_start(field, False, False, 1)
        if configuration is not None: field.set_value(configuration.label)

        data = {"label": _("Name"), "name":"name"}
        field = StringField(data, None)
        self.side_panel.pack_start(field, False, False, 1)
        if configuration is not None: field.set_value(configuration.name)

        button = Gtk.Button.new_with_label("Save")
        button.connect("clicked", self.__on_save, None)
        self.side_panel.pack_start(button, False, False, 1)
        self.side_panel.show_all()

    # ----------------------------------------------------------------------
    def __on_row_activated(self, tree_view, path, column, data):
        self.selected_port = data
        configuration = self.block.ports[int(str(path))]
        self.__create_side_panel(configuration)

    # ----------------------------------------------------------------------
    def __on_save(self, widget=None, data=None):
        new_port = Port()
        for widget in self.side_panel.get_children():
            try:
                new_port.__dict__[widget.get_name()] = widget.get_value()
            except:
                pass

        if new_port.type == "":
            message = "Type can not be empty"
            MessageDialog("Error", message, self.get_toplevel()).run()
            return
        if new_port.label == "":
            message = "Label can not be empty"
            MessageDialog("Error", message, self.get_toplevel()).run()
            return
        if new_port.name == "":
            message = "Name can not be empty"
            MessageDialog("Error", message, self.get_toplevel()).run()
            return
        contains = False
        i = 0
        for port in self.block.ports:
            if port.label == new_port.label:
                self.block.ports[i] = new_port
                contains = True
            i += 1
        if not contains:
            self.block.ports.append(new_port)
        self.__populate_list()
        self.__clean_side_panel()