Subversion Repositories basico

Rev

Rev 234 | Details | Compare with Previous | Last modification | View Log | RSS feed

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