File: Entry.py

package info (click to toggle)
pybliographer 1.2.12-4squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,560 kB
  • ctags: 1,413
  • sloc: python: 9,817; xml: 2,560; sh: 813; makefile: 621
file content (190 lines) | stat: -rw-r--r-- 5,841 bytes parent folder | download | duplicates (4)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# This file is part of pybliographer
# 
# Copyright (C) 1998-2004 Frederic GOBRY
# Email : gobry@pybliographer.org
# 	   
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 
# of the License, or (at your option) any later version.
#   
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# 

from gtk import *
from gnome import ui

import pango, gnome, gobject

import string

from Pyblio.GnomeUI import Utils
from Pyblio import Config, recode, Fields


class Entry:
    ''' Displays a bibliographic entry as simple text '''

    def __init__ (self):
        ''' Create the graphical widget '''
        
        self.text = TextView ()
        self.buff = self.text.get_buffer ()

        self.tag = {}
        
        self.tag ['title'] = \
                 self.buff.create_tag ('title',
                                       weight = pango.WEIGHT_BOLD)
        self.tag ['field'] = \
                 self.buff.create_tag ('field',
                                       indent = -20,
                                       style = pango.STYLE_OBLIQUE)
        self.tag ['body'] = \
                 self.buff.create_tag ('body',
                                       left_margin = 20)

        self.w = ScrolledWindow ()
        self.w.set_policy (POLICY_AUTOMATIC, POLICY_AUTOMATIC)
        self.w.add (self.text)

        self.text.set_editable (False)
        self.text.set_cursor_visible (False)
        self.text.set_wrap_mode (WRAP_WORD)
        self.text.set_left_margin (5)
        self.text.set_right_margin (5)
        
        # currently, nothing to display
        self.entry = None
        return

    def display (self, entry):
        self.entry = entry

        if not self.entry:
            self.buff.set_text ('')
            return
        
        # Display this entry
        self.buff.delete (self.buff.get_start_iter (),
                          self.buff.get_end_iter ())
        
        iter = self.buff.get_start_iter ()
        
        self.buff.insert_with_tags (iter, entry.type.name,
                                    self.tag ['title'])
        self.buff.insert_with_tags (iter, ' {'+ str (entry.key.key) + '}\n\n',
                                    self.tag ['title'])

        dico = entry.keys ()


        def maybe_insert_button (field):
            """ Create a button that opens the URL if the field is of type URL """
            
            if not isinstance (field, Fields.URL): return
            
            # Add a button to open the URL
            self.buff.insert (iter, ' ')

            anchor = self.buff.create_child_anchor (iter)
            
            button = Button ('...')
            button.show ()

            def url_open (w, url):
                try:
                    gnome.url_show (url)
                    
                except gobject.GError, msg:

                    d = MessageDialog (None,
                                       DIALOG_MODAL |
                                       DIALOG_DESTROY_WITH_PARENT,
                                       MESSAGE_ERROR,
                                       BUTTONS_CLOSE,
                                       _('Cannot open URL:\n%s') % msg)
                    d.run ()
                    d.destroy ()
                    
                return
                
            button.connect ('clicked', url_open, field.get_url ())
            
            self.text.add_child_at_anchor (button, anchor)
            return
        
        for f in entry.type.fields:
            
            field = string.lower (f.name)
            
            if entry.has_key (field):

                n = f.name + ': '
                t = str (entry [field]).decode ('latin-1')
                
                si = iter.get_offset ()

                self.buff.insert (iter, n)
                mi = iter.get_offset ()
                
                self.buff.insert (iter, t)

                si = self.buff.get_iter_at_offset (si)
                mi = self.buff.get_iter_at_offset (mi)
                
                self.buff.apply_tag (self.tag ['body'],
                                     si, iter)
                self.buff.apply_tag (self.tag ['field'],
                                     si, mi)

                maybe_insert_button (entry [field])
                
                self.buff.insert (iter, '\n')
                
                dico.remove (field)


        self.buff.insert (iter, '\n')

        dico.sort ()
        
        for f in dico:
            n = f + ': '
            t = str (entry [f]).decode ('latin-1')
            
            si = iter.get_offset ()
            
            self.buff.insert (iter, n)
            mi = iter.get_offset ()
            
            self.buff.insert (iter, t)
                
            si = self.buff.get_iter_at_offset (si)
            mi = self.buff.get_iter_at_offset (mi)
                
            self.buff.apply_tag (self.tag ['body'],
                                 si, iter)
            self.buff.apply_tag (self.tag ['field'],
                                 si, mi)

            maybe_insert_button (entry [f])
            
            self.buff.insert (iter, '\n')

        return

    def clear (self):
        self.buff.set_text ('')
        return