~ubuntu-branches/ubuntu/jaunty/pida/jaunty

« back to all changes in this revision

Viewing changes to pida/pidagtk/gladeview.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*- 
 
2
 
 
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
 
4
#Copyright (c) 2005 Ali Afshar aafshar@gmail.com
 
5
 
 
6
#Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
#of this software and associated documentation files (the "Software"), to deal
 
8
#in the Software without restriction, including without limitation the rights
 
9
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
#copies of the Software, and to permit persons to whom the Software is
 
11
#furnished to do so, subject to the following conditions:
 
12
 
 
13
#The above copyright notice and this permission notice shall be included in
 
14
#all copies or substantial portions of the Software.
 
15
 
 
16
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
19
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
22
#SOFTWARE.
 
23
 
 
24
# pidagtk import(s)
 
25
import pida.pidagtk.contentview as contentview
 
26
 
 
27
# gtk import(s)
 
28
import gtk
 
29
import gtk.glade as glade
 
30
 
 
31
 
 
32
def glade_view_builder(glade_file_name, top_level_name='pida_view'):
 
33
    def __init__(self, svc, prefix, **kw):
 
34
        view = glade_view(svc, prefix,
 
35
                          glade_file_name=glade_file_name,
 
36
                          top_level_name=top_level_name,
 
37
                          **kw)
 
38
        return view
 
39
    return __init__
 
40
    
 
41
class glade_view(contentview.content_view):
 
42
    """A content_view based on a glade file"""
 
43
 
 
44
    glade_file_name = None
 
45
    top_level_name = 'pida_view'
 
46
 
 
47
    def init(self, glade_file_name=None, top_level_name=None):
 
48
        assert (self.glade_file_name or glade_file_name,
 
49
                'must provide a glade file')
 
50
        if glade_file_name is None:
 
51
            glade_file_name = self.glade_file_name
 
52
        else:
 
53
            self.glade_file_name = glade_file_name
 
54
        glade_file = self.__find_gladefile(glade_file_name)
 
55
        if not glade_file:
 
56
            self.service.log.info('glade file not found %s', glade_file)
 
57
            not_found = gtk.Label('this glade file was not found')
 
58
            self.widget.pack_start(not_found)
 
59
            return
 
60
        gtk.glade.set_custom_handler(self.__get_custom_handler)
 
61
        glade_build = glade.XML(glade_file)
 
62
        self.__auto_connect(glade_build)
 
63
        if top_level_name is None:
 
64
            top_level_name = self.top_level_name
 
65
        else:
 
66
            self.top_level_name = top_level_name
 
67
        top_level = glade_build.get_widget(self.top_level_name)
 
68
        if top_level.get_parent() is not None:
 
69
            top_level.unparent()
 
70
        self.widget.pack_start(top_level)
 
71
        top_level.show_all()
 
72
        self.__glade_build = glade_build
 
73
        self.init_glade()
 
74
 
 
75
    def init_glade(self):
 
76
        pass
 
77
 
 
78
    def __auto_connect(self, glade_build):
 
79
        self.__connect_signals(glade_build, self, prefix='')
 
80
        self.__connect_signals(glade_build, self.service, prefix=self.prefix)
 
81
 
 
82
    def __connect_signals(self, glade_build, target, prefix=''):
 
83
        for attr in dir(target):
 
84
            if not attr.startswith('on_'):
 
85
                continue
 
86
            try:
 
87
                widg_name, sig_name = attr[3:].split('__')
 
88
                if prefix:
 
89
                    if widg_name.startswith(prefix):
 
90
                        widg_name = widg_name[len(prefix) + 1:]
 
91
                    else:
 
92
                        continue
 
93
            except ValueError:
 
94
                self.service.log.info('badly named signal handler "%s"',
 
95
                                      attr)
 
96
                continue
 
97
            f = getattr(target, attr)
 
98
            if not callable(f):
 
99
                self.service.log.info('non callable signal handler "%s"',
 
100
                                      attr)
 
101
                continue
 
102
            widget = glade_build.get_widget(widg_name)
 
103
            if widget is None:
 
104
                self.service.log.info('signal handler exists, but there'
 
105
                                      'is no widget "%s"', widg_name)
 
106
                continue
 
107
            try:
 
108
                widget.connect(sig_name, f)
 
109
            except TypeError:
 
110
                self.service.log.info('signal type "%s" does not exist '
 
111
                                      'for widget type "%s"',
 
112
                                       sig_name, widget)
 
113
 
 
114
    def __get_custom_handler(self, glade, function_name, widget_name, 
 
115
            str1, str2, int1, int2):
 
116
        """
 
117
        Generic handler for creating custom widgets, used to
 
118
        enable custom widgets.
 
119
 
 
120
        The custom widgets have a creation function specified in design time.
 
121
        Those creation functions are always called with str1,str2,int1,int2 as
 
122
        arguments, that are values specified in design time.
 
123
 
 
124
        This handler assumes that we have a method for every custom widget
 
125
        creation function specified in glade.
 
126
 
 
127
        If a custom widget has create_foo as creation function, then the
 
128
        method named create_foo is called with str1,str2,int1,int2 as arguments.
 
129
 
 
130
        see <http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq22.007.htp>
 
131
        """
 
132
        handler = getattr(self, function_name)
 
133
        return handler(str1, str2, int1, int2)
 
134
 
 
135
    def __find_gladefile(self, filename):
 
136
        from pkg_resources import Requirement, resource_filename
 
137
        requirement = Requirement.parse('pida')
 
138
        resource = 'glade/%s' % filename
 
139
        try:
 
140
            glade_file = resource_filename(requirement, resource)
 
141
        except KeyError:
 
142
            glade_file = None
 
143
        return glade_file
 
144
 
 
145
    def get_widget(self, name):
 
146
        return self.__glade_build.get_widget(name)
 
147