Subversion Repositories basico

Rev

Rev 232 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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