1
# -*- coding: utf-8 -*-
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
4
#Copyright (c) 2005 Ali Afshar aafshar@gmail.com
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:
13
#The above copyright notice and this permission notice shall be included in
14
#all copies or substantial portions of the Software.
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
25
import pida.pidagtk.contentview as contentview
29
import gtk.glade as glade
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,
41
class glade_view(contentview.content_view):
42
"""A content_view based on a glade file"""
44
glade_file_name = None
45
top_level_name = 'pida_view'
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
53
self.glade_file_name = glade_file_name
54
glade_file = self.__find_gladefile(glade_file_name)
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)
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
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:
70
self.widget.pack_start(top_level)
72
self.__glade_build = glade_build
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)
82
def __connect_signals(self, glade_build, target, prefix=''):
83
for attr in dir(target):
84
if not attr.startswith('on_'):
87
widg_name, sig_name = attr[3:].split('__')
89
if widg_name.startswith(prefix):
90
widg_name = widg_name[len(prefix) + 1:]
94
self.service.log.info('badly named signal handler "%s"',
97
f = getattr(target, attr)
99
self.service.log.info('non callable signal handler "%s"',
102
widget = glade_build.get_widget(widg_name)
104
self.service.log.info('signal handler exists, but there'
105
'is no widget "%s"', widg_name)
108
widget.connect(sig_name, f)
110
self.service.log.info('signal type "%s" does not exist '
111
'for widget type "%s"',
114
def __get_custom_handler(self, glade, function_name, widget_name,
115
str1, str2, int1, int2):
117
Generic handler for creating custom widgets, used to
118
enable custom widgets.
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.
124
This handler assumes that we have a method for every custom widget
125
creation function specified in glade.
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.
130
see <http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq22.007.htp>
132
handler = getattr(self, function_name)
133
return handler(str1, str2, int1, int2)
135
def __find_gladefile(self, filename):
136
from pkg_resources import Requirement, resource_filename
137
requirement = Requirement.parse('pida')
138
resource = 'glade/%s' % filename
140
glade_file = resource_filename(requirement, resource)
145
def get_widget(self, name):
146
return self.__glade_build.get_widget(name)