Subversion Repositories basico

Rev

Rev 243 | Rev 264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
232 t00m 1
#!/usr/bin/python
246 t00m 2
"""
232 t00m 3
# -*- coding: utf-8 -*-
246 t00m 4
# File: wdg_annot.py
232 t00m 5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: Annotation Widget
246 t00m 8
"""
232 t00m 9
 
10
import gi
11
from gi.repository import Gtk
12
from gi.repository import Pango
246 t00m 13
from basico.core.mod_env import LPATH
232 t00m 14
 
246 t00m 15
gi.require_version('Gtk', '3.0')
16
gi.require_version('Gdk', '3.0')
232 t00m 17
 
243 t00m 18
 
19
class AnnotationWidget(Gtk.VBox):
246 t00m 20
    """
21
    Annotation Widget
22
    """
232 t00m 23
    def __init__(self, app, sid='0000000000'):
24
        Gtk.VBox.__init__(self)
25
        self.app = app
26
        self.sid = sid
27
        self.get_services()
233 t00m 28
        self.__setup()
232 t00m 29
 
30
 
31
    def get_services(self):
32
        """Load services to be used in this class
33
        """
246 t00m 34
        self.srvgui = self.app.get_service("GUI")
35
        self.srvicm = self.app.get_service("IM")
36
        self.srvuif = self.app.get_service("UIF")
37
        self.srvclt = self.app.get_service('Collections')
38
        self.srvclb = self.app.get_service('Callbacks')
39
        self.srvant = self.app.get_service('Annotation')
40
        self.srvutl = self.app.get_service('Utils')
232 t00m 41
 
42
 
233 t00m 43
    def __setup(self):
232 t00m 44
        # Setup Widget properties
45
        self.set_property('margin-left', 3)
46
        self.set_property('margin-right', 3)
47
        self.set_hexpand(True)
48
        self.set_vexpand(True)
49
 
50
        # Annotation container (body)
246 t00m 51
        self.container_body = self.srvgui.add_widget('gtk_vbox_annotation_container', Gtk.VBox())
232 t00m 52
        self.container_body.set_border_width(3)
53
        self.pack_start(self.container_body, True, True, 3)
54
 
233 t00m 55
        self.__setup_header()
56
        self.__setup_body()
57
        self.__setup_footer()
232 t00m 58
 
233 t00m 59
 
60
    def __setup_header(self):
232 t00m 61
        # Annotation Header
246 t00m 62
        container = self.srvgui.get_widget('gtk_vbox_annotation_container')
232 t00m 63
        header = Gtk.VBox()
64
        hbox = Gtk.HBox()
246 t00m 65
        title = self.srvgui.add_widget('gtk_label_annotation_sid', Gtk.Label())
232 t00m 66
        if self.sid == '0000000000':
67
            title.set_markup('<big><b>Annotation</b></big>')
68
        else:
69
            title.set_markup('<big><b>Annotation for SAP Note %s</b>' % str(int(self.sid)))
70
        title.set_xalign(0.0)
233 t00m 71
        a_lbl_aid = Gtk.Label()
72
        a_lbl_aid.set_property('xalign', 1.0)
73
        a_lbl_aid.modify_font(Pango.FontDescription('Monospace 10'))
74
        a_lbl_aid.set_markup('<b>Annotation Id:</b>')
246 t00m 75
        a_aid = self.srvgui.add_widget('gtk_label_aid', Gtk.Label())
233 t00m 76
        a_aid.set_property('xalign', 1.0)
77
        a_aid.set_selectable(True)
78
        a_aid.modify_font(Pango.FontDescription('Monospace 10'))
79
        separator = Gtk.Separator()
232 t00m 80
        hbox.pack_start(title, True, True, 0)
233 t00m 81
        hbox.pack_start(a_lbl_aid, False, False, 0)
82
        hbox.pack_start(a_aid, False, False, 0)
232 t00m 83
        header.pack_start(hbox, False, False, 0)
233 t00m 84
 
85
        # Timestamp
246 t00m 86
        a_wdg_human_timestamp = self.srvgui.add_widget('gtk_label_human_timestamp', Gtk.Label())
233 t00m 87
        a_wdg_human_timestamp.modify_font(Pango.FontDescription('Monospace 10'))
88
        a_wdg_human_timestamp.set_xalign(0.0)
89
        header.pack_start(a_wdg_human_timestamp, True, True, 0)
90
 
232 t00m 91
        header.pack_start(separator, False, False, 3)
92
        header.show_all()
233 t00m 93
        container.pack_start(header, False, False, 0)
232 t00m 94
 
95
 
233 t00m 96
    def __setup_body(self):
232 t00m 97
        vbox = Gtk.VBox()
98
 
99
        # Hidden metadata
233 t00m 100
        hhbox = Gtk.HBox()
101
 
246 t00m 102
        a_timestamp = self.srvgui.add_widget('gtk_label_timestamp', Gtk.Label(self.srvutl.timestamp()))
232 t00m 103
        a_timestamp.set_sensitive(False)
246 t00m 104
        self.srvuif.set_widget_visibility('gtk_label_timestamp', False)
233 t00m 105
        hhbox.pack_start(a_timestamp, False, False, 0)
106
        vbox.pack_start(hhbox, False, False, 0)
232 t00m 107
 
233 t00m 108
 
109
        # Main hbox
110
        mhbox = Gtk.HBox()
111
 
112
        vboxl = Gtk.VBox()
113
        vboxl.set_vexpand(True)
114
        vboxr = Gtk.VBox()
115
        vboxr.set_vexpand(True)
116
 
232 t00m 117
        # Title
246 t00m 118
        a_title = self.srvgui.add_widget('gtk_entry_annotation_title', Gtk.Entry())
232 t00m 119
        a_title.set_placeholder_text("Type a title here...")
120
        a_title.modify_font(Pango.FontDescription('Monospace 10'))
233 t00m 121
        vboxl.pack_start(a_title, False, False, 0)
232 t00m 122
 
123
        # Text
233 t00m 124
        # ~ box = Gtk.HBox()
232 t00m 125
        scroller = Gtk.ScrolledWindow()
126
        scroller.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
127
        scroller.set_shadow_type(Gtk.ShadowType.IN)
128
        scroller.set_hexpand(True)
129
        scroller.set_vexpand(True)
246 t00m 130
        a_text = self.srvgui.add_widget('gtk_textview_annotation_text', Gtk.TextView())
233 t00m 131
        a_text.set_wrap_mode(Gtk.WrapMode.WORD)
232 t00m 132
        a_text.modify_font(Pango.FontDescription('Monospace 10'))
133
        a_text.set_vexpand(True)
246 t00m 134
        a_text.set_left_margin(6)
135
        a_text.set_top_margin(6)
136
        a_text.set_right_margin(6)
137
        a_text.set_bottom_margin(6)
232 t00m 138
        a_textbuffer = Gtk.TextBuffer()
139
        a_textbuffer.set_text('')
140
        a_text.set_buffer(a_textbuffer)
141
        scroller.add(a_text)
233 t00m 142
        vboxl.pack_start(scroller, True, True, 3)
232 t00m 143
 
144
 
233 t00m 145
        # Type
146
        hbox_type = Gtk.HBox()
147
        a_type_lbl = Gtk.Label()
148
        a_type_lbl.set_markup('%20s' % '<b>Type </b>')
149
        a_type_lbl.set_xalign(1.0)
150
        # ~ vboxr.pack_start(a_type_lbl, False, False, 0)
151
        a_type_model = Gtk.ListStore(str)
246 t00m 152
        self.srvgui.add_widget('annotation_type_FIXME', a_type_model.append(['FIXME']))
153
        active = self.srvgui.add_widget('annotation_type_Note', a_type_model.append(['Note']))
154
        self.srvgui.add_widget('annotation_type_Procedure', a_type_model.append(['Procedure']))
155
        self.srvgui.add_widget('annotation_type_Snippet', a_type_model.append(['Snippet']))
156
        self.srvgui.add_widget('annotation_type_Template', a_type_model.append(['Template']))
157
        self.srvgui.add_widget('annotation_type_TODO', a_type_model.append(['TODO']))
233 t00m 158
        a_type = Gtk.ComboBox.new_with_model(a_type_model)
159
        a_type.set_active_iter(active)
246 t00m 160
        self.srvgui.add_widget('gtk_combobox_annotation_type', a_type)
233 t00m 161
        renderer_type = Gtk.CellRendererText()
162
        a_type.pack_start(renderer_type, True)
163
        a_type.add_attribute(renderer_type, "text", 0)
164
        hbox_type.pack_start(a_type_lbl, False, False, 3)
165
        hbox_type.pack_start(a_type, True, True, 0)
166
        hbox_type.set_homogeneous(True)
167
        vboxr.pack_start(hbox_type, False, False, 0)
168
 
169
        # ~ vbox.pack_start(hbox, False, False, 0)
170
 
171
 
172
 
246 t00m 173
        # ~ calendar = self.srvgui.add_widget('gtk_calendar_annotation_timestamp', Gtk.Calendar())
233 t00m 174
        # ~ calendar.modify_font(Pango.FontDescription('Monospace 8'))
175
        # ~ hbox.pack_start(calendar, False, False, 0)
176
 
177
 
232 t00m 178
        # Url
179
        hbox = Gtk.HBox()
240 t00m 180
 
181
        ## url entry
246 t00m 182
        a_link = self.srvgui.add_widget('gtk_entry_annotation_link', Gtk.Entry())
232 t00m 183
        a_link.set_placeholder_text("Type a url here...")
184
        hbox.pack_start(a_link, True, True, 3)
240 t00m 185
 
186
        ## url button
246 t00m 187
        a_link_button = self.srvgui.add_widget('gtk_link_button_annotation_link', Gtk.LinkButton())
240 t00m 188
        a_link_button.set_relief(Gtk.ReliefStyle.NORMAL)
189
        a_link_button.set_label('Visit')
190
        hbox.pack_start(a_link_button, False, False, 3)
191
 
192
        # url type
232 t00m 193
        a_link_type_model = Gtk.ListStore(str)
246 t00m 194
        self.srvgui.add_widget('annotation_link_type_Intranet', a_link_type_model.append(['Intranet']))
195
        self.srvgui.add_widget('annotation_link_type_SAP Blog', a_link_type_model.append(['SAP Blog']))
196
        self.srvgui.add_widget('annotation_link_type_SAP Document', a_link_type_model.append(['SAP Document']))
197
        self.srvgui.add_widget('annotation_link_type_SAP Help', a_link_type_model.append(['SAP Help']))
198
        self.srvgui.add_widget('annotation_link_type_SAP Incident', a_link_type_model.append(['SAP Incident']))
199
        self.srvgui.add_widget('annotation_link_type_SAP Questions and Answers', a_link_type_model.append(['SAP Questions and Answers']))
200
        self.srvgui.add_widget('annotation_link_type_SAP Wiki', a_link_type_model.append(['SAP Wiki']))
240 t00m 201
        # ~ 'Knowledge Base Search'
246 t00m 202
        active = self.srvgui.add_widget('annotation_link_type_Website', a_link_type_model.append(['Website']))
232 t00m 203
        a_link_type = Gtk.ComboBox.new_with_model(a_link_type_model)
204
        a_link_type.set_active_iter(active)
246 t00m 205
        self.srvgui.add_widget('gtk_combobox_annotation_link_type', a_link_type)
232 t00m 206
        renderer_text = Gtk.CellRendererText()
207
        a_link_type.pack_start(renderer_text, True)
208
        a_link_type.add_attribute(renderer_text, "text", 0)
209
        hbox.pack_start(a_link_type, False, False, 0)
233 t00m 210
        vboxl.pack_start(hbox, False, False, 3)
232 t00m 211
 
233 t00m 212
        mhbox.pack_start(vboxl, True, True, 6)
213
        mhbox.pack_start(vboxr, False, False, 0)
214
        vbox.pack_start(mhbox, True, True, 3)
232 t00m 215
        self.container_body.add(vbox)
216
 
217
 
233 t00m 218
    def __setup_footer(self):
232 t00m 219
        # Buttons Accept/Cancel
220
        hbox = Gtk.HBox()
246 t00m 221
        accept = self.srvgui.add_widget('gtk_button_accept_annotation', Gtk.Button('Accept'))
222
        accept.connect('clicked', self.srvclb.action_annotation_accept, self.sid)
232 t00m 223
        accept.set_property('always-show-image', True)
246 t00m 224
        icon = self.srvicm.get_new_image_icon('basico-check-accept', 24, 24)
232 t00m 225
        accept.set_image(icon)
246 t00m 226
        cancel = self.srvgui.add_widget('gtk_button_cancel_annotation', Gtk.Button('Cancel'))
227
        cancel.connect('clicked', self.srvclb.action_annotation_cancel)
232 t00m 228
        cancel.set_property('always-show-image', True)
246 t00m 229
        icon = self.srvicm.get_new_image_icon('basico-check-cancel', 24, 24)
232 t00m 230
        cancel.set_image(icon)
231
        hbox.pack_start(accept, True, False, 3)
232
        hbox.pack_start(cancel, True, False, 3)
233
        self.pack_start(hbox, False, False, 3)
234
 
235
 
233 t00m 236
    def set_aid_to_widget(self, aid):
246 t00m 237
        """
238
        C:237, 4: Missing method docstring (missing-docstring)
239
        """
240
        a_aid = self.srvgui.get_widget('gtk_label_aid')
232 t00m 241
        a_aid.set_text(aid)
242
 
243
 
233 t00m 244
    def get_aid_from_widget(self):
246 t00m 245
        """
246
        C:237, 4: Missing method docstring (missing-docstring)
247
        """
248
        a_aid = self.srvgui.get_widget('gtk_label_aid')
232 t00m 249
        return a_aid.get_text()
250
 
251
 
233 t00m 252
    def set_metadata_to_widget(self, aid, action):
246 t00m 253
        """
254
        C:237, 4: Missing method docstring (missing-docstring)
255
        """
233 t00m 256
        self.set_aid_to_widget(aid)
246 t00m 257
        sid = self.srvant.get_sid(aid)
258
        title = self.srvgui.get_widget('gtk_label_annotation_sid')
232 t00m 259
        if sid == '0000000000':
260
            title.set_markup('<big><b>Annotation</b></big>')
261
        else:
262
            title.set_markup('<big><b>Annotation for SAP Note %s</b></big>' % str(int(sid)))
263
 
264
        if action == 'create':
265
            pass
266
        elif action == 'edit':
246 t00m 267
            annotation = self.srvant.get_metadata_from_file(aid)
232 t00m 268
            if annotation is not None:
269
                ANNOTATION_FILE_CONTENT = LPATH['ANNOTATIONS'] + aid + '.adoc'
246 t00m 270
                a_wdg_aid = self.srvgui.get_widget('gtk_label_aid')
271
                a_wdg_timestamp = self.srvgui.get_widget('gtk_label_timestamp')
272
                a_wdg_title = self.srvgui.get_widget('gtk_entry_annotation_title')
273
                a_wdg_type = self.srvgui.get_widget('gtk_combobox_annotation_type')
274
                a_wdg_human_timestamp = self.srvgui.get_widget('gtk_label_human_timestamp')
275
                a_wdg_text = self.srvgui.get_widget('gtk_textview_annotation_text')
276
                a_wdg_link = self.srvgui.get_widget('gtk_entry_annotation_link')
277
                a_wdg_link_button = self.srvgui.get_widget('gtk_link_button_annotation_link')
278
                a_wdg_link_type = self.srvgui.get_widget('gtk_combobox_annotation_link_type')
279
                # ~ a_wdg_cal = self.srvgui.get_widget('gtk_calendar_annotation_timestamp')
232 t00m 280
 
281
                a_wdg_aid.set_text(annotation['aid'])
246 t00m 282
                # ~ a_wdg_timestamp.set_text(self.srvutl.timestamp())
233 t00m 283
                a_wdg_timestamp.set_text(annotation['timestamp'])
232 t00m 284
                a_wdg_title.set_text(annotation['title'])
246 t00m 285
                a_wdg_type.set_active_iter(self.srvgui.get_widget('annotation_type_%s' % annotation['type']))
286
                self.srvuif.set_textview_text(a_wdg_text, open(ANNOTATION_FILE_CONTENT).read())
232 t00m 287
                a_wdg_link.set_text(annotation['link'])
240 t00m 288
                a_wdg_link_button.set_uri(annotation['link'])
246 t00m 289
                a_wdg_link_type.set_active_iter(self.srvgui.get_widget('annotation_link_type_%s' % annotation['link_type']))
290
                human_timestamp = self.srvutl.get_human_date_from_timestamp(annotation['timestamp'])
233 t00m 291
                a_wdg_human_timestamp.set_text(human_timestamp)
292
                # ~ a_wdg_cal.select_month(timestamp.month-1, timestamp.year)
293
                # ~ a_wdg_cal.select_day(timestamp.day)
232 t00m 294
 
295
 
246 t00m 296
    def get_metadata_from_widget(self):
297
        """
298
        C:237, 4: Missing method docstring (missing-docstring)
299
        """
232 t00m 300
        annotation = {}
246 t00m 301
        a_wdg_aid = self.srvgui.get_widget('gtk_label_aid')
302
        a_wdg_timestamp = self.srvgui.get_widget('gtk_label_timestamp')
303
        a_wdg_title = self.srvgui.get_widget('gtk_entry_annotation_title')
304
        a_wdg_type = self.srvgui.get_widget('gtk_combobox_annotation_type')
305
        a_wdg_text = self.srvgui.get_widget('gtk_textview_annotation_text')
306
        a_wdg_link = self.srvgui.get_widget('gtk_entry_annotation_link')
307
        a_wdg_link_type = self.srvgui.get_widget('gtk_combobox_annotation_link_type')
232 t00m 308
 
309
        annotation['aid'] = a_wdg_aid.get_text()
310
        annotation['timestamp'] = a_wdg_timestamp.get_text()
311
        annotation['title'] = a_wdg_title.get_text()
312
        annotation['component'] = 'Annotation'
313
        annotation['category'] = ''
246 t00m 314
        annotation['type'] = self.srvuif.get_combobox_text(a_wdg_type, 0)
315
        annotation['content'] = self.srvuif.get_textview_text(a_wdg_text)
232 t00m 316
        annotation['link'] = a_wdg_link.get_text()
246 t00m 317
        annotation['link_type'] = self.srvuif.get_combobox_text(a_wdg_link_type, 0)
232 t00m 318
 
319
        return annotation