File: button.py

package info (click to toggle)
tryton-client 7.0.27-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,476 kB
  • sloc: python: 27,180; sh: 37; makefile: 18
file content (62 lines) | stat: -rw-r--r-- 2,048 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import gettext

from gi.repository import Gtk

from tryton.common import IconFactory

_ = gettext.gettext


class Button(Gtk.Button):

    def __init__(self, attrs=None):
        self.attrs = attrs or {}
        self.label = '_' + attrs.get('string', '').replace('_', '__')
        super(Button, self).__init__(label=self.label, stock=None,
            use_underline=True)
        self._set_icon(attrs.get('icon'))

    def _set_icon(self, stock):
        self.set_always_show_image(bool(stock))
        image = self.get_image()
        if not image and not stock:
            return
        if not stock:
            self.set_image(None)
            return
        self.set_image(IconFactory.get_image(stock, Gtk.IconSize.BUTTON))

    def state_set(self, record):
        if record:
            states = record.expr_eval(self.attrs.get('states', {}))
        else:
            states = {}
        if states.get('invisible', False):
            self.hide()
        else:
            self.show()
        self.set_sensitive(not states.get('readonly', False))
        self._set_icon(states.get('icon', self.attrs.get('icon')))

        if self.attrs.get('rule'):
            label = self.label
            tip = self.attrs.get('help', '')
            if record:
                clicks = record.get_button_clicks(self.attrs['name'])
                if clicks:
                    label += ' (%s)' % len(clicks)
                    if tip:
                        tip += '\n'
                    tip += _('By: ') + _(', ').join(iter(clicks.values()))
            self.set_label(label)
            self.set_tooltip_text(tip)

        if self.attrs.get('type', 'class') == 'class':
            parent = record.parent if record else None
            while parent:
                if parent.modified:
                    self.set_sensitive(False)
                    break
                parent = parent.parent