Subversion Repositories basico

Rev

Rev 232 | 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()
184
        a_link = self.gui.add_widget('gtk_entry_annotation_link', Gtk.Entry())
185
        a_link.set_placeholder_text("Type a url here...")
186
        hbox.pack_start(a_link, True, True, 3)
187
        a_link_type_model = Gtk.ListStore(str)
188
        self.gui.add_widget('annotation_link_type_Intranet', a_link_type_model.append(['Intranet']))
189
        self.gui.add_widget('annotation_link_type_SAP Blog', a_link_type_model.append(['SAP Blog']))
190
        self.gui.add_widget('annotation_link_type_SAP Document', a_link_type_model.append(['SAP Document']))
191
        self.gui.add_widget('annotation_link_SAP Help', a_link_type_model.append(['SAP Help']))
192
        self.gui.add_widget('annotation_link_type_SAP Incident', a_link_type_model.append(['SAP Incident']))
193
        self.gui.add_widget('annotation_link_SAP Questions and Answers', a_link_type_model.append(['SAP Questions and Answers']))
194
        self.gui.add_widget('annotation_link_type_SAP Wiki', a_link_type_model.append(['SAP Wiki']))
195
        active = self.gui.add_widget('annotation_link_type_Website', a_link_type_model.append(['Website']))
196
        a_link_type = Gtk.ComboBox.new_with_model(a_link_type_model)
197
        a_link_type.set_active_iter(active)
198
        self.gui.add_widget('gtk_combobox_annotation_link_type', a_link_type)
199
        renderer_text = Gtk.CellRendererText()
200
        a_link_type.pack_start(renderer_text, True)
201
        a_link_type.add_attribute(renderer_text, "text", 0)
202
        hbox.pack_start(a_link_type, False, False, 0)
233 t00m 203
        vboxl.pack_start(hbox, False, False, 3)
232 t00m 204
 
233 t00m 205
        mhbox.pack_start(vboxl, True, True, 6)
206
        mhbox.pack_start(vboxr, False, False, 0)
207
        vbox.pack_start(mhbox, True, True, 3)
232 t00m 208
        self.container_body.add(vbox)
209
 
210
 
233 t00m 211
    def __setup_footer(self):
232 t00m 212
        # Buttons Accept/Cancel
213
        hbox = Gtk.HBox()
214
        accept = self.gui.add_widget('gtk_button_accept_annotation', Gtk.Button('Accept'))
215
        accept.connect('clicked', self.cb.action_annotation_accept, self.sid)
216
        accept.set_property('always-show-image', True)
217
        icon = self.im.get_new_image_icon('basico-check-accept', 24, 24)
218
        accept.set_image(icon)
219
        cancel = self.gui.add_widget('gtk_button_cancel_annotation', Gtk.Button('Cancel'))
220
        cancel.connect('clicked', self.cb.action_annotation_cancel)
221
        cancel.set_property('always-show-image', True)
222
        icon = self.im.get_new_image_icon('basico-check-cancel', 24, 24)
223
        cancel.set_image(icon)
224
        hbox.pack_start(accept, True, False, 3)
225
        hbox.pack_start(cancel, True, False, 3)
226
        self.pack_start(hbox, False, False, 3)
227
 
228
 
233 t00m 229
    def set_aid_to_widget(self, aid):
232 t00m 230
        a_aid = self.gui.get_widget('gtk_label_aid')
231
        a_aid.set_text(aid)
232
 
233
 
233 t00m 234
    def get_aid_from_widget(self):
232 t00m 235
        a_aid = self.gui.get_widget('gtk_label_aid')
236
        return a_aid.get_text()
237
 
238
 
233 t00m 239
    def set_metadata_to_widget(self, aid, action):
240
        self.set_aid_to_widget(aid)
232 t00m 241
        sid = self.annot.get_sid(aid)
242
        title = self.gui.get_widget('gtk_label_annotation_sid')
243
        if sid == '0000000000':
244
            title.set_markup('<big><b>Annotation</b></big>')
245
        else:
246
            title.set_markup('<big><b>Annotation for SAP Note %s</b></big>' % str(int(sid)))
247
 
248
        if action == 'create':
249
            pass
250
        elif action == 'edit':
233 t00m 251
            annotation = self.annot.get_metadata_from_file(aid)
232 t00m 252
            if annotation is not None:
253
                ANNOTATION_FILE_CONTENT = LPATH['ANNOTATIONS'] + aid + '.adoc'
254
                timestamp = self.utils.get_datetime(annotation['timestamp'])
255
                a_wdg_aid = self.gui.get_widget('gtk_label_aid')
256
                a_wdg_timestamp = self.gui.get_widget('gtk_label_timestamp')
257
                a_wdg_title = self.gui.get_widget('gtk_entry_annotation_title')
258
                a_wdg_type = self.gui.get_widget('gtk_combobox_annotation_type')
233 t00m 259
                a_wdg_human_timestamp = self.gui.get_widget('gtk_label_human_timestamp')
232 t00m 260
                a_wdg_text = self.gui.get_widget('gtk_textview_annotation_text')
261
                a_wdg_link = self.gui.get_widget('gtk_entry_annotation_link')
262
                a_wdg_link_type = self.gui.get_widget('gtk_combobox_annotation_link_type')
233 t00m 263
                # ~ a_wdg_cal = self.gui.get_widget('gtk_calendar_annotation_timestamp')
232 t00m 264
 
265
                a_wdg_aid.set_text(annotation['aid'])
233 t00m 266
                # ~ a_wdg_timestamp.set_text(self.utils.timestamp())
267
                a_wdg_timestamp.set_text(annotation['timestamp'])
232 t00m 268
                a_wdg_title.set_text(annotation['title'])
269
                a_wdg_type.set_active_iter(self.gui.get_widget('annotation_type_%s' % annotation['type']))
270
                self.uif.set_textview_text(a_wdg_text, open(ANNOTATION_FILE_CONTENT).read())
271
                a_wdg_link.set_text(annotation['link'])
272
                a_wdg_link_type.set_active_iter(self.gui.get_widget('annotation_link_type_%s' % annotation['link_type']))
233 t00m 273
                human_timestamp = self.utils.get_human_date_from_timestamp(annotation['timestamp'])
274
                a_wdg_human_timestamp.set_text(human_timestamp)
275
                # ~ a_wdg_cal.select_month(timestamp.month-1, timestamp.year)
276
                # ~ a_wdg_cal.select_day(timestamp.day)
232 t00m 277
 
278
 
233 t00m 279
    def get_metadata_from_widget(self, aid):
232 t00m 280
        annotation = {}
281
        a_wdg_aid = self.gui.get_widget('gtk_label_aid')
282
        a_wdg_timestamp = self.gui.get_widget('gtk_label_timestamp')
283
        a_wdg_title = self.gui.get_widget('gtk_entry_annotation_title')
284
        a_wdg_type = self.gui.get_widget('gtk_combobox_annotation_type')
285
        a_wdg_text = self.gui.get_widget('gtk_textview_annotation_text')
286
        a_wdg_link = self.gui.get_widget('gtk_entry_annotation_link')
287
        a_wdg_link_type = self.gui.get_widget('gtk_combobox_annotation_link_type')
288
 
289
        annotation['aid'] = a_wdg_aid.get_text()
290
        annotation['timestamp'] = a_wdg_timestamp.get_text()
291
        annotation['title'] = a_wdg_title.get_text()
292
        annotation['component'] = 'Annotation'
293
        annotation['category'] = ''
294
        annotation['type'] = self.uif.get_combobox_text(a_wdg_type, 0)
295
        annotation['content'] =  self.uif.get_textview_text(a_wdg_text)
296
        annotation['link'] = a_wdg_link.get_text()
297
        annotation['link_type'] = self.uif.get_combobox_text(a_wdg_link_type, 0)
298
 
299
        return annotation