~ubuntu-branches/ubuntu/trusty/openerp-client/trusty

« back to all changes in this revision

Viewing changes to bin/widget/view/form_gtk/interface.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-12-07 20:17:00 UTC
  • Revision ID: james.westby@ubuntu.com-20081207201700-a875pic3sd7xkoru
Tags: upstream-5.0.0~alpha
ImportĀ upstreamĀ versionĀ 5.0.0~alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
import copy
 
24
import gtk
 
25
import wid_common
 
26
 
 
27
import rpc
 
28
import common
 
29
 
 
30
_attrs_boolean = {
 
31
    'required': False,
 
32
    'readonly': False
 
33
}
 
34
 
 
35
class widget_interface(object):
 
36
    def __init__(self, window, parent=None, view=None, attrs=None):
 
37
        if attrs is None:
 
38
            attrs = {}
 
39
        self.parent = parent
 
40
        self._window = window
 
41
        self._view = None
 
42
        self.attrs = attrs
 
43
        for key,val in _attrs_boolean.items():
 
44
            self.attrs[key] = attrs.get(key, False) not in ('False', '0', False)
 
45
        self.default_readonly = self.attrs.get('readonly', False)
 
46
        self._menu_entries = [
 
47
            (_('Set to default value'), lambda x: self._menu_sig_default_get(), 1),
 
48
            (_('Set as default'), lambda x: self._menu_sig_default_set(), 1),
 
49
        ]
 
50
 
 
51
    def destroy(self):
 
52
        pass
 
53
 
 
54
    def _menu_sig_default_get(self):
 
55
        try:
 
56
            if self._view.modelfield.get_state_attrs(self._view.model).get('readonly', False):
 
57
                return False
 
58
            model = self._view.modelfield.parent.resource
 
59
            res = rpc.session.rpc_exec_auth_try('/object', 'execute', model, 'default_get', [self.attrs['name']])
 
60
            self._view.modelfield.set(self._view.model, res.get(self.attrs['name'], False))
 
61
            self.display(self._view.model, self._view.modelfield)
 
62
        except:
 
63
            common.warning('You can not set to the default value here !', 'Operation not permited')
 
64
            return False
 
65
 
 
66
    def sig_activate(self, widget=None):
 
67
        # emulate a focus_out so that the onchange is called if needed
 
68
        self._focus_out()
 
69
 
 
70
    def _readonly_set(self, ro):
 
71
        pass
 
72
 
 
73
    def _color_widget(self):
 
74
        return self.widget
 
75
 
 
76
    def color_set(self, name):
 
77
        widget = self._color_widget()
 
78
        map = widget.get_colormap()
 
79
        colour = map.alloc_color(common.colors.get(name,'white'))
 
80
        widget.modify_bg(gtk.STATE_ACTIVE, colour)
 
81
        widget.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
 
82
        widget.modify_base(gtk.STATE_NORMAL, colour)
 
83
        widget.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
 
84
        widget.modify_text(gtk.STATE_INSENSITIVE, gtk.gdk.color_parse("black"))
 
85
 
 
86
    def _menu_sig_default_set(self):
 
87
        deps = []
 
88
        wid = self._view.view_form.widgets
 
89
        for wname, wview in self._view.view_form.widgets.items():
 
90
            if wview.modelfield.attrs.get('change_default', False):
 
91
                value = wview.modelfield.get(self._view.model)
 
92
                deps.append((wname, wname, value, value))
 
93
        value = self._view.modelfield.get_default(self._view.model)
 
94
        model = self._view.modelfield.parent.resource
 
95
        wid_common.field_pref_set(self._view.widget_name,
 
96
                self.attrs.get('string', self._view.widget_name), model,
 
97
                value, deps, window=self._window)
 
98
 
 
99
    def _menu_open(self, obj, event):
 
100
        if event.button == 3:
 
101
            menu = gtk.Menu()
 
102
            for stock_id,callback,sensitivity in self._menu_entries:
 
103
                if stock_id:
 
104
                    item = gtk.ImageMenuItem(stock_id)
 
105
                    if callback:
 
106
                        item.connect("activate",callback)
 
107
                    item.set_sensitive(sensitivity)
 
108
                else:
 
109
                    item=gtk.SeparatorMenuItem()
 
110
                item.show()
 
111
                menu.append(item)
 
112
            menu.popup(None,None,None,event.button,event.time)
 
113
            return True
 
114
 
 
115
    def _focus_in(self):
 
116
        pass
 
117
 
 
118
    def _focus_out(self):
 
119
        if not self._view.modelfield:
 
120
            return False
 
121
        self.set_value(self._view.model, self._view.modelfield)
 
122
 
 
123
    def display(self, model, modelfield):
 
124
        if not modelfield:
 
125
            self._readonly_set(self.attrs.get('readonly', False))
 
126
            return
 
127
        self._readonly_set(modelfield.get_state_attrs(model).get('readonly', False))
 
128
        if modelfield.get_state_attrs(model).get('readonly', False):
 
129
            self.color_set('readonly')
 
130
        elif not modelfield.get_state_attrs(model).get('valid', True):
 
131
            self.color_set('invalid')
 
132
        elif modelfield.get_state_attrs(model).get('required', False):
 
133
            self.color_set('required')
 
134
        else:
 
135
            self.color_set('normal')
 
136
 
 
137
    def sig_changed(self):
 
138
        if self.attrs.get('on_change',False):
 
139
            self._view.view_form.screen.on_change(self.attrs['on_change'])
 
140
 
 
141
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
142