Exemplo n.º 1
0
    def parse(self, model_name, root_node, fields):
        dict_widget = {}
        state_widgets = []
        attrs = node_attributes(root_node)
        on_write = attrs.get('on_write', '')
        editable = attrs.get('editable', False)
        if editable:
            treeview = EditableTreeView(editable)
        else:
            treeview = TreeView()
            treeview.cells = {}
        treeview.sequence = attrs.get('sequence', False)
        treeview.colors = attrs.get('colors', '"black"')
        treeview.keyword_open = attrs.get('keyword_open', False)
        self.treeview = treeview
        treeview.set_property('rules-hint', True)
        if not self.title:
            self.title = attrs.get('string', 'Unknown')
        tooltips = common.Tooltips()
        expandable = False

        for node in root_node.childNodes:
            node_attrs = node_attributes(node)
            if node.localName == 'field':
                fname = str(node_attrs['name'])
                for boolean_fields in ('readonly', 'required', 'expand'):
                    if boolean_fields in node_attrs:
                        node_attrs[boolean_fields] = \
                            bool(int(node_attrs[boolean_fields]))
                if fname not in fields:
                    continue
                for attr_name in ('relation', 'domain', 'selection',
                        'relation_field', 'string', 'views', 'invisible',
                        'add_remove', 'sort', 'context', 'filename',
                        'selection_change_with'):
                    if attr_name in fields[fname].attrs and \
                            not attr_name in node_attrs:
                        node_attrs[attr_name] = fields[fname].attrs[attr_name]
                cell_type = node_attrs.get('widget',
                    fields[fname].attrs['type'])
                cell = CELLTYPES.get(cell_type)(fname, model_name,
                    treeview, node_attrs)
                treeview.cells[fname] = cell
                renderer = cell.renderer

                readonly = not (editable and not node_attrs.get('readonly',
                    fields[fname].attrs.get('readonly', False)))
                if isinstance(renderer, CellRendererToggle):
                    renderer.set_property('activatable', not readonly)
                elif isinstance(renderer,
                        (gtk.CellRendererProgress, CellRendererButton)):
                    pass
                else:
                    renderer.set_property('editable', not readonly)
                if (not readonly
                        and not isinstance(renderer, CellRendererBinary)):
                    renderer.connect_after('editing-started', send_keys,
                            treeview)

                col = gtk.TreeViewColumn(fields[fname].attrs['string'])

                prefixes = []
                suffixes = []
                if cell_type in ('url', 'email', 'callto', 'sip'):
                    prefixes.append(Affix(fname, self.treeview, node_attrs,
                            protocol=cell_type))
                if 'icon' in node_attrs:
                    prefixes.append(Affix(fname, self.treeview, node_attrs))
                for affix in node.childNodes:
                    affix_attrs = node_attributes(affix)
                    if affix.localName == 'prefix':
                        list_ = prefixes
                    else:
                        list_ = suffixes
                    list_.append(Affix(fname, self.treeview, affix_attrs))

                for prefix in prefixes:
                    col.pack_start(prefix.renderer, expand=False)
                    col.set_cell_data_func(prefix.renderer, prefix.setter)

                col.pack_start(renderer, expand=True)
                col.set_cell_data_func(renderer, cell.setter)
                col.name = fname

                for suffix in suffixes:
                    col.pack_start(suffix.renderer, expand=False)
                    col.set_cell_data_func(suffix.renderer, suffix.setter)

                hbox = gtk.HBox(False, 2)
                label = gtk.Label(fields[fname].attrs['string'])
                label.show()
                help = fields[fname].attrs['string']
                if fields[fname].attrs.get('help'):
                    help += '\n' + fields[fname].attrs['help']
                tooltips.set_tip(label, help)
                tooltips.enable()
                arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
                col.arrow = arrow
                col.arrow_show = False
                hbox.pack_start(label, True, True, 0)
                hbox.pack_start(arrow, False, False, 0)
                hbox.show()
                col.set_widget(hbox)

                col._type = fields[fname].attrs['type']
                col.set_clickable(True)
                twidth = {
                    'integer': 60,
                    'biginteger': 60,
                    'float': 80,
                    'numeric': 80,
                    'float_time': 100,
                    'date': 110,
                    'datetime': 160,
                    'selection': 90,
                    'char': 100,
                    'one2many': 50,
                    'many2many': 50,
                    'boolean': 20,
                    'binary': 200,
                }
                width = self.screen.tree_column_width[model_name].get(fname)
                if not width:
                    if 'width' in node_attrs:
                        width = int(node_attrs['width'])
                    else:
                        width = twidth.get(fields[fname].attrs['type'], 100)
                col.width = width
                if width > 0:
                    col.set_fixed_width(width)
                col.set_min_width(1)
                expand = node_attrs.get('expand', False)
                col.set_expand(expand)
                expandable |= expand
                if (not treeview.sequence
                        and not self.children_field
                        and fields[fname].attrs.get('sortable', True)):
                    col.connect('clicked', sort_model, treeview, self.screen)
                col.set_resizable(True)
                col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                col.set_visible(not node_attrs.get('tree_invisible',
                    fields[fname].attrs.get('tree_invisible', False)))
                if fname == self.screen.exclude_field:
                    col.set_visible(False)
                i = treeview.append_column(col)
                if 'sum' in node_attrs and fields[fname].attrs['type'] \
                        in ('integer', 'biginteger', 'float', 'numeric',
                            'float_time'):
                    label = gtk.Label(node_attrs['sum'] + _(': '))
                    label_sum = gtk.Label()
                    if isinstance(fields[fname].attrs.get('digits'),
                            basestring):
                        digits = 2
                    else:
                        digits = fields[fname].attrs.get('digits', (16, 2))[1]
                    dict_widget[i] = (fname, label, label_sum, digits)
            elif node.localName == 'button':
                #TODO add shortcut
                cell = Button(treeview, self.screen, node_attrs)
                state_widgets.append(cell)
                renderer = cell.renderer
                string = node_attrs.get('string', _('Unknown'))
                col = gtk.TreeViewColumn(string, renderer)
                col.name = None

                label = gtk.Label(string)
                label.show()
                help = string
                if node_attrs.get('help'):
                    help += '\n' + node_attrs['help']
                tooltips.set_tip(label, help)
                tooltips.enable()
                arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
                col.arrow = arrow
                col.arrow_show = False
                col.set_widget(label)

                col._type = 'button'
                col.set_cell_data_func(renderer, cell.setter)
                if 'width' in node_attrs:
                    width = int(node_attrs['width'])
                else:
                    width = 80
                col.width = width
                if width > 0:
                    col.set_fixed_width(width)
                col.set_resizable(True)
                col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                i = treeview.append_column(col)
        if not expandable:
            col = gtk.TreeViewColumn()
            col.name = None
            arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
            col.arrow = arrow
            col.arrow_show = False
            col._type = 'fill'
            col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
            treeview.append_column(col)
        treeview.set_fixed_height_mode(True)
        return treeview, dict_widget, state_widgets, on_write, [], None
Exemplo n.º 2
0
    def parse(self, model, root_node, fields):
        dict_widget = {}
        attrs = tools.node_attributes(root_node)
        on_write = attrs.get('on_write', '')
        editable = attrs.get('editable', False)
        treeview = EditableTreeView(editable)
        treeview.colors = dict()
        self.treeview = treeview

        for color_spec in attrs.get('colors', '').split(';'):
            if color_spec:
                colour, test = color_spec.split(':')
                treeview.colors.setdefault(colour,[])
                treeview.colors[colour].append(test)
        if not self.title:
            self.title = attrs.get('string', 'Unknown')
        treeview.set_property('rules-hint', True)
        treeview.sequence = False
        treeview.connect("motion-notify-event", treeview.set_tooltip)
        treeview.connect('key-press-event', treeview.on_tree_key_press)

        for node in root_node:
            node_attrs = tools.node_attributes(node)

            if node.tag == 'button':
                cell = Cell('button')(node_attrs['string'], treeview, node_attrs)
                cell.name = node_attrs['name']
                cell.attrs = node_attrs
                cell.type = node_attrs.get('type','object')
                cell.context = node_attrs.get('context',{})
                cell.model = model
                treeview.cells[node_attrs['name']] = cell
                col = gtk.TreeViewColumn(None, cell.renderer)
                col.set_clickable(True)
                col.set_cell_data_func(cell.renderer, cell.setter)
                col.name = node_attrs['name']
                col.attrs = node_attrs
                col._type = 'Button'
                col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                col.tooltip = node_attrs['string']
                if node_attrs.get('help',False):
                    col.tooltip = node_attrs['string'] +':\n' + node_attrs['help']
                col.set_fixed_width(20)
                visval = eval(str(node_attrs.get('invisible', 'False')), {'context':self.screen.context})
                col.set_visible(not visval)
                treeview.append_column(col)

            if node.tag == 'field':
                handler_id = False
                fname = str(node_attrs['name'])
                if fields[fname]['type'] in ('image', 'binary'):
                    continue    # not showed types
                if fname == 'sequence':
                    treeview.sequence = True
                for boolean_fields in ('readonly', 'required'):
                    if boolean_fields in node_attrs:
                        if node_attrs[boolean_fields] in ('True', 'False'):
                            node_attrs[boolean_fields] = eval(node_attrs[boolean_fields])
                        else:
                            node_attrs[boolean_fields] = bool(int(node_attrs[boolean_fields]))

                if fields[fname]['type'] == 'selection':
                    if fields[fname].get('selection',[]):
                        node_attrs['selection'] = fields[fname]['selection']
                fields[fname].update(node_attrs)
                node_attrs.update(fields[fname])
                node_attrs['editable'] = editable
                cell = Cell(fields[fname]['type'])(fname, treeview, node_attrs,
                        self.window)
                treeview.cells[fname] = cell
                renderer = cell.renderer

                write_enable = editable and not node_attrs.get('readonly', False)
                if isinstance(renderer, gtk.CellRendererToggle):
                    renderer.set_property('activatable', write_enable)
                elif isinstance(renderer, (gtk.CellRendererText, gtk.CellRendererCombo, date_renderer.DecoratorRenderer)):
                    renderer.set_property('editable', write_enable)
                if write_enable:
                    handler_id = renderer.connect_after('editing-started', send_keys, treeview)

                col = gtk.TreeViewColumn(None, renderer)
                treeview.handlers[col] = handler_id
                col_label = gtk.Label('')
                if fields[fname].get('required', False):
                    col_label.set_markup('<b>%s</b>' % cgi.escape(fields[fname]['string']))
                else:
                    col_label.set_text(fields[fname]['string'])
                col_label.show()
                col.set_widget(col_label)
                col.name = fname
                col._type = fields[fname]['type']
                col.set_cell_data_func(renderer, cell.setter)
                col.set_clickable(True)
                twidth = {
                    'integer': (60, 170),
                    'float': (80, 300),
                    'float_time': (80,150),
                    'date': (70, False),
                    'datetime': (145, 145),
                    'selection': (90, 250),
                    'char': (100, False),
                    'one2many': (50, False),
                    'many2many': (50, False),
                    'boolean': (20, 80),
                    'progressbar':(150, 200)
                }

                if col._type not in twidth:
                    col.set_expand(True)
                else:
                    if 'width' in fields[fname]:
                        min_width = max_width = int(fields[fname]['width'])
                    else:
                        min_width, max_width = twidth[col._type]

                    col.set_min_width(min_width)
                    if max_width:
                        col.set_max_width(max_width)

                col.connect('clicked', sort_model, self.screen)
                col.set_resizable(True)
                #col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                visval = eval(str(fields[fname].get('invisible', 'False')), {'context':self.screen.context})
                col.set_visible(not visval)
                n = treeview.append_column(col)
                calculate = ''
                if 'sum' in node_attrs.keys():
                    calculate = 'sum'
                elif 'avg' in node_attrs.keys():
                    calculate = 'avg'

                if calculate and fields[fname]['type'] \
                        in ('integer', 'float', 'float_time'):
                    label = gtk.Label()
                    label.set_use_markup(True)
                    label_str = fields[fname][calculate] + ': '
                    label_bold = bool(int(fields[fname].get('bold', 0)))
                    if label_bold:
                        label.set_markup('<b>%s</b>' % label_str)
                    else:
                        label.set_markup(label_str)
                    label_sum = gtk.Label()
                    label_sum.set_use_markup(True)
                    dict_widget[n] = (fname, label, label_sum,
                            fields[fname].get('digits', (16,2))[1], label_bold, calculate)
        return treeview, dict_widget, [], on_write
Exemplo n.º 3
0
    def parse(self, model, root_node, fields):
        dict_widget = {}
        attrs = tools.node_attributes(root_node)
        on_write = attrs.get("on_write", "")
        editable = attrs.get("editable", False)
        treeview = EditableTreeView(editable)
        treeview.colors = dict()
        self.treeview = treeview

        for color_spec in attrs.get("colors", "").split(";"):
            if color_spec:
                colour, test = color_spec.split(":")
                treeview.colors.setdefault(colour, [])
                treeview.colors[colour].append(test)
        if not self.title:
            self.title = attrs.get("string", "Unknown")
        treeview.set_property("rules-hint", True)
        treeview.sequence = False
        treeview.connect("motion-notify-event", treeview.set_tooltip)
        treeview.connect("key-press-event", treeview.on_tree_key_press)

        for node in root_node:
            node_attrs = tools.node_attributes(node)

            if node.tag == "button":
                cell = Cell("button")(node_attrs["string"], treeview, node_attrs)
                cell.name = node_attrs["name"]
                cell.attrs = node_attrs
                cell.type = node_attrs.get("type", "object")
                cell.context = node_attrs.get("context", {})
                cell.model = model
                treeview.cells[node_attrs["name"]] = cell
                col = gtk.TreeViewColumn(None, cell.renderer)
                col.set_clickable(True)
                col.set_cell_data_func(cell.renderer, cell.setter)
                col.name = node_attrs["name"]
                col.attrs = node_attrs
                col._type = "Button"
                col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                col.tooltip = node_attrs["string"]
                if node_attrs.get("help", False):
                    col.tooltip = node_attrs["string"] + ":\n" + node_attrs["help"]
                col.set_fixed_width(20)
                visval = eval(str(node_attrs.get("invisible", "False")), {"context": self.screen.context})
                col.set_visible(not visval)
                treeview.append_column(col)

            if node.tag == "field":
                handler_id = False
                fname = str(node_attrs["name"])
                self.field_list.setdefault(fname, 0)
                self.field_list[fname] += 1
                if self.field_list[fname] > 1:
                    continue
                if fields[fname]["type"] in ("image", "binary"):
                    continue  # not showed types
                if fname == "sequence":
                    treeview.sequence = True
                for boolean_fields in ("readonly", "required"):
                    if boolean_fields in node_attrs:
                        if node_attrs[boolean_fields] in ("True", "False"):
                            node_attrs[boolean_fields] = eval(node_attrs[boolean_fields])
                        else:
                            node_attrs[boolean_fields] = bool(int(node_attrs[boolean_fields]))
                if fields[fname]["type"] == "selection":
                    if fields[fname].get("selection", []):
                        node_attrs["selection"] = fields[fname]["selection"]
                fields[fname].update(node_attrs)
                node_attrs.update(fields[fname])
                node_attrs["editable"] = editable
                cell = Cell(fields[fname]["type"])(fname, treeview, node_attrs, self.window)
                treeview.cells[fname] = cell
                renderer = cell.renderer

                write_enable = editable and not node_attrs.get("readonly", False)
                if isinstance(renderer, gtk.CellRendererToggle):
                    renderer.set_property("activatable", write_enable)
                elif isinstance(
                    renderer, (gtk.CellRendererText, gtk.CellRendererCombo, date_renderer.DecoratorRenderer)
                ):
                    renderer.set_property("editable", write_enable)
                if write_enable:
                    handler_id = renderer.connect_after("editing-started", send_keys, treeview)

                col = gtk.TreeViewColumn(None, renderer)
                treeview.handlers[col] = handler_id
                col_label = gtk.Label("")
                if fields[fname].get("required", False):
                    col_label.set_markup("<b>%s</b>" % cgi.escape(fields[fname]["string"]))
                else:
                    col_label.set_text(fields[fname]["string"])
                col_label.show()
                col.set_widget(col_label)
                col.name = fname
                col._type = fields[fname]["type"]
                col.set_cell_data_func(renderer, cell.setter)
                col.set_clickable(True)
                twidth = {
                    "integer": (60, 170),
                    "float": (80, 300),
                    "float_time": (80, 150),
                    "date": (70, False),
                    "datetime": (145, False),
                    "selection": (90, 250),
                    "char": (100, False),
                    "one2many": (50, False),
                    "many2many": (50, False),
                    "boolean": (20, False),
                    "progressbar": (150, 200),
                }

                if col._type not in twidth:
                    col.set_expand(True)
                else:
                    if "width" in fields[fname]:
                        min_width = max_width = int(fields[fname]["width"])
                    else:
                        min_width, max_width = twidth[col._type]

                    col.set_min_width(min_width)
                    if max_width:
                        col.set_max_width(max_width)

                col.connect("clicked", sort_model, self.screen)
                col.set_resizable(True)
                # col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                visval = eval(str(fields[fname].get("invisible", "False")), {"context": self.screen.context})
                col.set_visible(not visval)
                n = treeview.append_column(col)
                calculate = ""
                if "sum" in node_attrs.keys():
                    calculate = "sum"
                elif "avg" in node_attrs.keys():
                    calculate = "avg"

                if calculate and fields[fname]["type"] in ("integer", "float", "float_time"):
                    label = gtk.Label()
                    label.set_use_markup(True)
                    label_str = fields[fname][calculate] + ": "
                    label_bold = bool(int(fields[fname].get("bold", 0)))
                    if label_bold:
                        label.set_markup("<b>%s</b>" % label_str)
                    else:
                        label.set_markup(label_str)
                    label_sum = gtk.Label()
                    label_sum.set_use_markup(True)
                    dict_widget[n] = (
                        fname,
                        label,
                        label_sum,
                        fields[fname].get("digits", (16, 2))[1],
                        label_bold,
                        calculate,
                        fields[fname]["type"],
                    )
        return treeview, dict_widget, [], on_write
Exemplo n.º 4
0
    def parse(self, model, root_node, fields):
        dict_widget = {}
        btn_list = []
        attrs = tools.node_attributes(root_node)
        on_write = attrs.get('on_write', '')
        editable = attrs.get('editable', False)
        if editable:
            treeview = EditableTreeView(editable)
        else:
            treeview = DecoratedTreeView(editable)
        treeview.colors = dict()
        self.treeview = treeview
        for color_spec in attrs.get('colors', '').split(';'):
            if color_spec:
                colour, test = color_spec.split(':')
                treeview.colors[colour] = test
        treeview.set_property('rules-hint', True)
        if not self.title:
            self.title = attrs.get('string', 'Unknown')

        treeview.sequence = False
        for node in root_node.childNodes:
            node_attrs = tools.node_attributes(node)
            if node.localName == 'button':
                cell = Cell('button')(node_attrs['string'])
                cell.attrs = node_attrs
                cell.name = node_attrs['name']
                cell.type = node_attrs.get('type', 'object')
                cell.context = node_attrs.get('context', {})
                cell.model = model
                treeview.cells[node_attrs['name']] = cell
                col = gtk.TreeViewColumn(None, cell)
                btn_list.append(col)
                cell.set_property('editable', False)
                col._type = 'Button'
                col.name = node_attrs['name']

            if node.localName == 'field':
                fname = str(node_attrs['name'])
                if fields[fname]['type'] in ('image', 'binary'):
                    continue  # not showed types
                if fname == 'sequence':
                    treeview.sequence = True
                for boolean_fields in ('readonly', 'required'):
                    if boolean_fields in node_attrs:
                        if node_attrs[boolean_fields] in ('True', 'False'):
                            node_attrs[boolean_fields] = eval(
                                node_attrs[boolean_fields])
                        else:
                            node_attrs[boolean_fields] = bool(
                                int(node_attrs[boolean_fields]))
                fields[fname].update(node_attrs)
                node_attrs.update(fields[fname])
                cell = Cell(fields[fname]['type'])(fname, treeview, node_attrs,
                                                   self.window)
                treeview.cells[fname] = cell
                renderer = cell.renderer

                write_enable = editable and not node_attrs.get(
                    'readonly', False)
                if isinstance(renderer, gtk.CellRendererToggle):
                    renderer.set_property('activatable', write_enable)
                elif isinstance(renderer,
                                (gtk.CellRendererText, gtk.CellRendererCombo,
                                 date_renderer.DecoratorRenderer)):
                    renderer.set_property('editable', write_enable)
                if write_enable:
                    renderer.connect_after('editing-started', send_keys,
                                           treeview)

                col = gtk.TreeViewColumn(None, renderer)
                col_label = gtk.Label('')
                if fields[fname].get('required', False):
                    col_label.set_markup('<b>%s</b>' %
                                         cgi.escape(fields[fname]['string']))
                else:
                    col_label.set_text(fields[fname]['string'])
                col_label.show()
                col.set_widget(col_label)

                col.name = fname
                col._type = fields[fname]['type']
                col.set_cell_data_func(renderer, cell.setter)
                col.set_clickable(True)
                twidth = {
                    'integer': 60,
                    'float': 80,
                    'float_time': 80,
                    'date': 70,
                    'datetime': 120,
                    'selection': 90,
                    'char': 100,
                    'one2many': 50,
                    'many2many': 50,
                    'boolean': 20,
                }
                if 'width' in fields[fname]:
                    width = int(fields[fname]['width'])
                else:
                    width = twidth.get(fields[fname]['type'], 100)
                col.set_min_width(width)
                if not treeview.sequence:
                    col.connect('clicked', sort_model, treeview)
                col.set_resizable(True)
                #col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                visval = eval(str(fields[fname].get('invisible', 'False')),
                              {'context': self.screen.context})
                col.set_visible(not visval)
                n = treeview.append_column(col)
                for tree_op in TREE_OPS:
                    if tree_op in fields[fname]:
                        if tree_op in ('sum', 'avg') \
                            and fields[fname]['type'] not in ('integer',
                                                              'float',
                                                              'float_time'):
                            continue
                        label = gtk.Label()
                        label.set_use_markup(True)
                        label_str = fields[fname][tree_op] + ': '
                        label_bold = bool(
                            int(fields[fname].get('%s_bold' % tree_op, 0)))
                        if label_bold:
                            label.set_markup('<b>%s</b>' % label_str)
                        else:
                            label.set_markup(label_str)
                        label_wid = gtk.Label()
                        label_wid.set_use_markup(True)
                        dict_widget[n] = (fname, label, label_wid,
                                          fields[fname].get('digits',
                                                            (16, 2))[1],
                                          label_bold, tree_op)
        for btn in btn_list:
            treeview.append_column(btn)
        return treeview, dict_widget, [], on_write
Exemplo n.º 5
0
    def parse(self, model, root_node, fields):
        dict_widget = {}
        attrs = tools.node_attributes(root_node)
        on_write = attrs.get('on_write', '')
        editable = attrs.get('editable', False)
        treeview = EditableTreeView(editable)
        treeview.colors = dict()
        self.treeview = treeview

        for color_spec in attrs.get('colors', '').split(';'):
            if color_spec:
                colour, test = color_spec.split(':')
                treeview.colors.setdefault(colour, [])
                treeview.colors[colour].append(test)
        if not self.title:
            self.title = attrs.get('string', 'Unknown')
        treeview.set_property('rules-hint', True)
        treeview.sequence = False
        treeview.connect("motion-notify-event", treeview.set_tooltip)
        treeview.connect('key-press-event', treeview.on_tree_key_press)

        for node in root_node:
            node_attrs = tools.node_attributes(node)

            if node.tag == 'button':
                cell = Cell('button')(node_attrs['string'], treeview,
                                      node_attrs)
                cell.name = node_attrs['name']
                cell.attrs = node_attrs
                cell.type = node_attrs.get('type', 'object')
                cell.context = node_attrs.get('context', {})
                cell.model = model
                treeview.cells[node_attrs['name']] = cell
                col = gtk.TreeViewColumn(None, cell.renderer)
                col.set_clickable(True)
                col.set_cell_data_func(cell.renderer, cell.setter)
                col.name = node_attrs['name']
                col.attrs = node_attrs
                col._type = 'Button'
                col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                col.tooltip = node_attrs['string']
                if node_attrs.get('help', False):
                    col.tooltip = node_attrs['string'] + ':\n' + node_attrs[
                        'help']
                col.set_fixed_width(20)
                visval = eval(str(node_attrs.get('invisible', 'False')),
                              {'context': self.screen.context})
                col.set_visible(not visval)
                treeview.append_column(col)

            if node.tag == 'field':
                handler_id = False
                fname = str(node_attrs['name'])
                if fields[fname]['type'] in ('image', 'binary'):
                    continue  # not showed types
                if fname == 'sequence':
                    treeview.sequence = True
                for boolean_fields in ('readonly', 'required'):
                    if boolean_fields in node_attrs:
                        if node_attrs[boolean_fields] in ('True', 'False'):
                            node_attrs[boolean_fields] = eval(
                                node_attrs[boolean_fields])
                        else:
                            node_attrs[boolean_fields] = bool(
                                int(node_attrs[boolean_fields]))

                if fields[fname]['type'] == 'selection':
                    if fields[fname].get('selection', []):
                        node_attrs['selection'] = fields[fname]['selection']
                fields[fname].update(node_attrs)
                node_attrs.update(fields[fname])
                node_attrs['editable'] = editable
                cell = Cell(fields[fname]['type'])(fname, treeview, node_attrs,
                                                   self.window)
                treeview.cells[fname] = cell
                renderer = cell.renderer

                write_enable = editable and not node_attrs.get(
                    'readonly', False)
                if isinstance(renderer, gtk.CellRendererToggle):
                    renderer.set_property('activatable', write_enable)
                elif isinstance(renderer,
                                (gtk.CellRendererText, gtk.CellRendererCombo,
                                 date_renderer.DecoratorRenderer)):
                    renderer.set_property('editable', write_enable)
                if write_enable:
                    handler_id = renderer.connect_after(
                        'editing-started', send_keys, treeview)

                col = gtk.TreeViewColumn(None, renderer)
                treeview.handlers[col] = handler_id
                col_label = gtk.Label('')
                if fields[fname].get('required', False):
                    col_label.set_markup('<b>%s</b>' %
                                         cgi.escape(fields[fname]['string']))
                else:
                    col_label.set_text(fields[fname]['string'])
                col_label.show()
                col.set_widget(col_label)
                col.name = fname
                col._type = fields[fname]['type']
                col.set_cell_data_func(renderer, cell.setter)
                col.set_clickable(True)
                twidth = {
                    'integer': (60, 170),
                    'float': (80, 300),
                    'float_time': (80, 150),
                    'date': (70, False),
                    'datetime': (145, 145),
                    'selection': (90, 250),
                    'char': (100, False),
                    'one2many': (50, False),
                    'many2many': (50, False),
                    'boolean': (20, 80),
                    'progressbar': (150, 200)
                }

                if col._type not in twidth:
                    col.set_expand(True)
                else:
                    if 'width' in fields[fname]:
                        min_width = max_width = int(fields[fname]['width'])
                    else:
                        min_width, max_width = twidth[col._type]

                    col.set_min_width(min_width)
                    if max_width:
                        col.set_max_width(max_width)

                col.connect('clicked', sort_model, self.screen)
                col.set_resizable(True)
                #col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                visval = eval(str(fields[fname].get('invisible', 'False')),
                              {'context': self.screen.context})
                col.set_visible(not visval)
                n = treeview.append_column(col)
                calculate = ''
                if 'sum' in node_attrs.keys():
                    calculate = 'sum'
                elif 'avg' in node_attrs.keys():
                    calculate = 'avg'

                if calculate and fields[fname]['type'] \
                        in ('integer', 'float', 'float_time'):
                    label = gtk.Label()
                    label.set_use_markup(True)
                    label_str = fields[fname][calculate] + ': '
                    label_bold = bool(int(fields[fname].get('bold', 0)))
                    if label_bold:
                        label.set_markup('<b>%s</b>' % label_str)
                    else:
                        label.set_markup(label_str)
                    label_sum = gtk.Label()
                    label_sum.set_use_markup(True)
                    dict_widget[n] = (fname, label, label_sum,
                                      fields[fname].get('digits', (16, 2))[1],
                                      label_bold, calculate)
        return treeview, dict_widget, [], on_write
Exemplo n.º 6
0
    def parse(self, model_name, root_node, fields):
        dict_widget = {}
        button_list = []
        attrs = node_attributes(root_node)
        on_write = attrs.get('on_write', '')
        editable = attrs.get('editable', False)
        if editable:
            treeview = EditableTreeView(editable)
        else:
            treeview = gtk.TreeView()
            treeview.cells = {}
        treeview.sequence = attrs.get('sequence', False)
        treeview.colors = attrs.get('colors', '"black"')
        treeview.keyword_open = attrs.get('keyword_open', False)
        treeview.connect('focus', self.set_selection)
        self.treeview = treeview
        treeview.set_property('rules-hint', True)
        if not self.title:
            self.title = attrs.get('string', 'Unknown')
        tooltips = common.Tooltips()
        expandable = False

        for node in root_node.childNodes:
            node_attrs = node_attributes(node)
            if node.localName == 'field':
                fname = str(node_attrs['name'])
                for boolean_fields in ('readonly', 'required', 'expand'):
                    if boolean_fields in node_attrs:
                        node_attrs[boolean_fields] = \
                                bool(int(node_attrs[boolean_fields]))
                if fname not in fields:
                    continue
                for attr_name in ('relation', 'domain', 'selection',
                        'relation_field', 'string', 'views', 'invisible',
                        'add_remove', 'sort', 'context', 'filename'):
                    if attr_name in fields[fname].attrs and \
                            not attr_name in node_attrs:
                        node_attrs[attr_name] = fields[fname].attrs[attr_name]
                cell = CELLTYPES.get(node_attrs.get('widget',
                    fields[fname].attrs['type']))(fname, model_name,
                    treeview, node_attrs)
                treeview.cells[fname] = cell
                renderer = cell.renderer

                readonly = not (editable and not node_attrs.get('readonly',
                    fields[fname].attrs.get('readonly', False)))
                if isinstance(renderer, CellRendererToggle):
                    renderer.set_property('activatable', not readonly)
                elif isinstance(renderer,
                        (gtk.CellRendererProgress, CellRendererButton)):
                    pass
                else:
                    renderer.set_property('editable', not readonly)
                if (not readonly
                        and not isinstance(renderer, CellRendererBinary)):
                    renderer.connect_after('editing-started', send_keys,
                            treeview)

                col = gtk.TreeViewColumn(fields[fname].attrs['string'])

                if 'icon' in node_attrs:
                    render_pixbuf = gtk.CellRendererPixbuf()
                    col.pack_start(render_pixbuf, expand=False)
                    icon = node_attrs['icon']

                    def setter(column, cell, store, iter):
                        if (hasattr(self.treeview, 'get_realized')
                                and not self.treeview.get_realized()):
                            return
                        record = store.get_value(iter, 0)
                        value = record[icon].get_client(record) or ''
                        common.ICONFACTORY.register_icon(value)
                        pixbuf = treeview.render_icon(stock_id=value,
                                size=gtk.ICON_SIZE_BUTTON, detail=None)
                        cell.set_property('pixbuf', pixbuf)
                    col.set_cell_data_func(render_pixbuf, setter)

                col.pack_start(renderer, expand=True)
                col.name = fname

                hbox = gtk.HBox(False, 2)
                label = gtk.Label(fields[fname].attrs['string'])
                label.show()
                help = fields[fname].attrs['string']
                if fields[fname].attrs.get('help'):
                    help += '\n' + fields[fname].attrs['help']
                tooltips.set_tip(label, help)
                tooltips.enable()
                arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
                col.arrow = arrow
                col.arrow_show = False
                hbox.pack_start(label, True, True, 0)
                hbox.pack_start(arrow, False, False, 0)
                hbox.show()
                col.set_widget(hbox)

                col._type = fields[fname].attrs['type']
                col.set_cell_data_func(renderer, cell.setter)
                col.set_clickable(True)
                twidth = {
                    'integer': 60,
                    'biginteger': 60,
                    'float': 80,
                    'numeric': 80,
                    'float_time': 100,
                    'date': 90,
                    'datetime': 140,
                    'selection': 90,
                    'char': 100,
                    'one2many': 50,
                    'many2many': 50,
                    'boolean': 20,
                    'binary': 200,
                }
                if 'width' in node_attrs:
                    width = int(node_attrs['width'])
                else:
                    width = twidth.get(fields[fname].attrs['type'], 100)
                col.width = width
                if width > 0:
                    col.set_fixed_width(width)
                col.set_min_width(1)
                expand = node_attrs.get('expand', False)
                col.set_expand(expand)
                expandable |= expand
                if (not treeview.sequence
                        and not self.children_field
                        and fields[fname].attrs.get('sortable', True)):
                    col.connect('clicked', sort_model, treeview, self.screen)
                col.set_resizable(True)
                col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                col.set_visible(not node_attrs.get('tree_invisible',
                    fields[fname].attrs.get('tree_invisible', False)))
                if fname == self.screen.exclude_field:
                    col.set_visible(False)
                i = treeview.append_column(col)
                if 'sum' in node_attrs and fields[fname].attrs['type'] \
                        in ('integer', 'biginteger', 'float', 'numeric',
                                'float_time'):
                    label = gtk.Label(node_attrs['sum'] + _(': '))
                    label_sum = gtk.Label()
                    if isinstance(fields[fname].attrs.get('digits'),
                            basestring):
                        digits = 2
                    else:
                        digits = fields[fname].attrs.get('digits', (16, 2))[1]
                    dict_widget[i] = (fname, label, label_sum, digits)
            elif node.localName == 'button':
                #TODO add shortcut
                cell = Button(treeview, self.screen, node_attrs)
                button_list.append(cell)
                renderer = cell.renderer
                string = node_attrs.get('string', _('Unknown'))
                col = gtk.TreeViewColumn(string, renderer)
                col.name = None

                label = gtk.Label(string)
                label.show()
                help = string
                if node_attrs.get('help'):
                    help += '\n' + node_attrs['help']
                tooltips.set_tip(label, help)
                tooltips.enable()
                arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
                col.arrow = arrow
                col.arrow_show = False
                col.set_widget(label)

                col._type = 'button'
                col.set_cell_data_func(renderer, cell.setter)
                if 'width' in node_attrs:
                    width = int(node_attrs['width'])
                else:
                    width = 80
                col.width = width
                if width > 0:
                    col.set_fixed_width(width)
                col.set_resizable(True)
                col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                i = treeview.append_column(col)
        if not expandable:
            col = gtk.TreeViewColumn()
            col.name = None
            arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
            col.arrow = arrow
            col.arrow_show = False
            col._type = 'fill'
            col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
            treeview.append_column(col)
        treeview.set_fixed_height_mode(True)
        return treeview, dict_widget, button_list, on_write, [], None
Exemplo n.º 7
0
    def parse(self, model, root_node, fields):
        dict_widget = {}
        btn_list=[]
        attrs = tools.node_attributes(root_node)
        on_write = attrs.get('on_write', '')
        editable = attrs.get('editable', False)
        if editable:
            treeview = EditableTreeView(editable)
        else:
            treeview = DecoratedTreeView(editable)
        treeview.colors = dict()
        self.treeview = treeview
        for color_spec in attrs.get('colors', '').split(';'):
            if color_spec:
                colour, test = color_spec.split(':')
                treeview.colors[colour] = test
        treeview.set_property('rules-hint', True)
        if not self.title:
            self.title = attrs.get('string', 'Unknown')


        treeview.sequence = False
        for node in root_node.childNodes:
            node_attrs = tools.node_attributes(node)
            if node.localName == 'button':
                cell = Cell('button')(node_attrs['string'])
                cell.attrs=node_attrs
                cell.name=node_attrs['name']
                cell.type=node_attrs.get('type','object')
                cell.context=node_attrs.get('context',{})
                cell.model=model
                treeview.cells[node_attrs['name']] = cell
                col = gtk.TreeViewColumn(None, cell)
                btn_list.append(col)
                cell.set_property('editable',False)
                col._type = 'Button'
                col.name = node_attrs['name']
                
            if node.localName == 'field':
                fname = str(node_attrs['name'])
                if fields[fname]['type'] in ('image', 'binary'):
                    continue    # not showed types
                if fname == 'sequence':
                    treeview.sequence = True
                for boolean_fields in ('readonly', 'required'):
                    if boolean_fields in node_attrs:
                        if node_attrs[boolean_fields] in ('True', 'False'):
                            node_attrs[boolean_fields] = eval(node_attrs[boolean_fields])
                        else:    
                            node_attrs[boolean_fields] = bool(int(node_attrs[boolean_fields]))
                fields[fname].update(node_attrs)
                node_attrs.update(fields[fname])
                cell = Cell(fields[fname]['type'])(fname, treeview, node_attrs,
                        self.window)
                treeview.cells[fname] = cell
                renderer = cell.renderer
                
                write_enable = editable and not node_attrs.get('readonly', False)
                if isinstance(renderer, gtk.CellRendererToggle):
                    renderer.set_property('activatable', write_enable)
                elif isinstance(renderer, (gtk.CellRendererText, gtk.CellRendererCombo, date_renderer.DecoratorRenderer)):
                    renderer.set_property('editable', write_enable)
                if write_enable:
                    renderer.connect_after('editing-started', send_keys, treeview)

                col = gtk.TreeViewColumn(None, renderer)
                col_label = gtk.Label('')
                if fields[fname].get('required', False):
                    col_label.set_markup('<b>%s</b>' % cgi.escape(fields[fname]['string']))
                else:
                    col_label.set_text(fields[fname]['string'])
                col_label.show()
                col.set_widget(col_label)

                col.name = fname
                col._type = fields[fname]['type']
                col.set_cell_data_func(renderer, cell.setter)
                col.set_clickable(True)
                twidth = {
                    'integer': 60,
                    'float': 80,
                    'float_time': 80,
                    'date': 70,
                    'datetime': 120,
                    'selection': 90,
                    'char': 100,
                    'one2many': 50,
                    'many2many': 50,
                    'boolean': 20,
                }
                if 'width' in fields[fname]:
                    width = int(fields[fname]['width'])
                else:
                    width = twidth.get(fields[fname]['type'], 100)
                col.set_min_width(width)
                if not treeview.sequence:
                    col.connect('clicked', sort_model, treeview)
                col.set_resizable(True)
                #col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
                visval = eval(str(fields[fname].get('invisible', 'False')), {'context':self.screen.context})
                col.set_visible(not visval)
                n = treeview.append_column(col)
                for tree_op in TREE_OPS:
                    if tree_op in fields[fname]:
                        if tree_op in ('sum', 'avg') \
                            and fields[fname]['type'] not in ('integer',
                                                              'float',
                                                              'float_time'):
                            continue
                        label = gtk.Label()
                        label.set_use_markup(True)
                        label_str = fields[fname][tree_op] + ': '
                        label_bold = bool(int(fields[fname].get(
                            '%s_bold' % tree_op, 0))
                        )
                        if label_bold:
                            label.set_markup('<b>%s</b>' % label_str)
                        else:
                            label.set_markup(label_str)
                        label_wid = gtk.Label()
                        label_wid.set_use_markup(True)
                        dict_widget[n] = (
                            fname,
                            label,
                            label_wid,
                            fields[fname].get('digits', (16,2))[1],
                            label_bold,
                            tree_op
                        )
        for btn in btn_list:
            treeview.append_column(btn)
        return treeview, dict_widget, [], on_write