Subversion Repositories basico

Rev

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