Subversion Repositories basico

Rev

Blame | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: annotation.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Annotation Widget

from os.path import sep as SEP
from cgi import escape
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango

from basico.env import ROOT, USER_DIR, APP, LPATH, GPATH, FILE
from basico.service import Service

class AnnotationWidget(Gtk.VBox, Service):
    def __init__(self, app, sid='0000000000'):
        Gtk.VBox.__init__(self)
        self.app = app
        self.log = app.log
        self.sid = sid
        self.get_services()
        self.setup()
        self.setup_header()
        self.setup_body()
        self.setup_footer()



    def get_services(self):
        """Load services to be used in this class
        """

        self.gui = self.app.get_service("GUI")
        self.im = self.app.get_service("IM")
        self.uif = self.app.get_service("UIF")
        self.clts = self.app.get_service('Collections')
        self.cb = self.app.get_service('Callbacks')
        self.annot = self.app.get_service('Annotation')
        self.utils = self.app.get_service('Utils')


    def setup(self):
        # Setup Widget properties
        self.set_property('margin-left', 3)
        self.set_property('margin-right', 3)
        self.set_hexpand(True)
        self.set_vexpand(True)

        # Annotation container (body)
        self.container_body = self.gui.add_widget('gtk_vbox_annotation_container', Gtk.VBox())
        self.container_body.set_border_width(3)
        self.pack_start(self.container_body, True, True, 3)


    def setup_header(self):
        # Annotation Header
        container = self.gui.get_widget('gtk_vbox_annotation_container')
        header = Gtk.VBox()
        hbox = Gtk.HBox()
        icon = self.im.get_new_image_icon('basico-annotation')
        title = self.gui.add_widget('gtk_label_annotation_sid', Gtk.Label())
        if self.sid == '0000000000':
            title.set_markup('<big><b>Annotation</b></big>')
        else:
            title.set_markup('<big><b>Annotation for SAP Note %s</b>' % str(int(self.sid)))
        title.set_xalign(0.0)
        hbox.pack_start(icon, False, False, 6)
        hbox.pack_start(title, True, True, 0)
        separator = Gtk.Separator()
        header.pack_start(hbox, False, False, 0)
        header.pack_start(separator, False, False, 3)
        header.show_all()
        container.pack_start(header, False, False, 3)


    def setup_body(self):
        vbox = Gtk.VBox()

        # Hidden metadata
        hbox = Gtk.HBox()
        a_aid = self.gui.add_widget('gtk_label_aid', Gtk.Label())
        a_aid.set_property('xalign', 0.0)
        a_aid.set_sensitive(False)
        a_aid.set_selectable(True)
        a_aid.modify_font(Pango.FontDescription('Monospace 10'))
        # ~ a_aid.set_visible(False)
        # ~ a_aid.set_no_show_all(True)
        hbox.pack_start(a_aid, True, True, 3)
        a_timestamp = self.gui.add_widget('gtk_label_timestamp', Gtk.Label(self.utils.timestamp()))
        a_timestamp.set_sensitive(False)
        a_timestamp.set_visible(False)
        a_timestamp.set_no_show_all(True)
        hbox.pack_start(a_timestamp, True, True, 0)
        vbox.pack_start(hbox, False, False, 3)

        hbox = Gtk.HBox()
        # Title
        a_title = self.gui.add_widget('gtk_entry_annotation_title', Gtk.Entry())
        a_title.set_placeholder_text("Type a title here...")
        a_title.modify_font(Pango.FontDescription('Monospace 10'))
        hbox.pack_start(a_title, True, True, 0)

        # Type
        a_type_model = Gtk.ListStore(str)
        self.gui.add_widget('annotation_type_FIXME', a_type_model.append(['FIXME']))
        active = self.gui.add_widget('annotation_type_Note', a_type_model.append(['Note']))
        self.gui.add_widget('annotation_type_Procedure', a_type_model.append(['Procedure']))
        self.gui.add_widget('annotation_type_Snippet', a_type_model.append(['Snippet']))
        self.gui.add_widget('annotation_type_Template', a_type_model.append(['Template']))
        self.gui.add_widget('annotation_type_TODO', a_type_model.append(['TODO']))
        a_type = Gtk.ComboBox.new_with_model(a_type_model)
        a_type.set_active_iter(active)
        self.gui.add_widget('gtk_combobox_annotation_type', a_type)
        renderer_type = Gtk.CellRendererText()
        a_type.pack_start(renderer_type, True)
        a_type.add_attribute(renderer_type, "text", 0)
        hbox.pack_start(a_type, False, False, 3)
        vbox.pack_start(hbox, False, False, 0)

        # Text
        hbox = Gtk.HBox()
        scroller = Gtk.ScrolledWindow()
        scroller.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        scroller.set_shadow_type(Gtk.ShadowType.IN)
        scroller.set_hexpand(True)
        scroller.set_vexpand(True)
        a_text = self.gui.add_widget('gtk_textview_annotation_text', Gtk.TextView())
        a_text.modify_font(Pango.FontDescription('Monospace 10'))
        a_text.set_vexpand(True)
        a_text.set_left_margin (6)
        a_text.set_top_margin (6)
        a_text.set_right_margin (6)
        a_text.set_bottom_margin (6)
        a_textbuffer = Gtk.TextBuffer()
        a_textbuffer.set_text('')
        a_text.set_buffer(a_textbuffer)
        scroller.add(a_text)
        hbox.pack_start(scroller, True, True, 3)

        # Calendar
        calendar = self.gui.add_widget('gtk_calendar_annotation_timestamp', Gtk.Calendar())
        hbox.pack_start(calendar, False, False, 0)
        vbox.pack_start(hbox, True, True, 3)

        # Url
        hbox = Gtk.HBox()
        a_link = self.gui.add_widget('gtk_entry_annotation_link', Gtk.Entry())
        a_link.set_placeholder_text("Type a url here...")
        hbox.pack_start(a_link, True, True, 3)
        a_link_type_model = Gtk.ListStore(str)
        self.gui.add_widget('annotation_link_type_Intranet', a_link_type_model.append(['Intranet']))
        self.gui.add_widget('annotation_link_type_SAP Blog', a_link_type_model.append(['SAP Blog']))
        self.gui.add_widget('annotation_link_type_SAP Document', a_link_type_model.append(['SAP Document']))
        self.gui.add_widget('annotation_link_SAP Help', a_link_type_model.append(['SAP Help']))
        self.gui.add_widget('annotation_link_type_SAP Incident', a_link_type_model.append(['SAP Incident']))
        self.gui.add_widget('annotation_link_SAP Questions and Answers', a_link_type_model.append(['SAP Questions and Answers']))
        self.gui.add_widget('annotation_link_type_SAP Wiki', a_link_type_model.append(['SAP Wiki']))
        active = self.gui.add_widget('annotation_link_type_Website', a_link_type_model.append(['Website']))
        a_link_type = Gtk.ComboBox.new_with_model(a_link_type_model)
        a_link_type.set_active_iter(active)
        self.gui.add_widget('gtk_combobox_annotation_link_type', a_link_type)
        renderer_text = Gtk.CellRendererText()
        a_link_type.pack_start(renderer_text, True)
        a_link_type.add_attribute(renderer_text, "text", 0)
        hbox.pack_start(a_link_type, False, False, 0)
        vbox.pack_start(hbox, False, False, 3)

        self.container_body.add(vbox)


    def setup_footer(self):
        # Buttons Accept/Cancel
        hbox = Gtk.HBox()
        accept = self.gui.add_widget('gtk_button_accept_annotation', Gtk.Button('Accept'))
        accept.connect('clicked', self.cb.action_annotation_accept, self.sid)
        accept.set_property('always-show-image', True)
        icon = self.im.get_new_image_icon('basico-check-accept', 24, 24)
        accept.set_image(icon)
        cancel = self.gui.add_widget('gtk_button_cancel_annotation', Gtk.Button('Cancel'))
        cancel.connect('clicked', self.cb.action_annotation_cancel)
        cancel.set_property('always-show-image', True)
        icon = self.im.get_new_image_icon('basico-check-cancel', 24, 24)
        cancel.set_image(icon)
        hbox.pack_start(accept, True, False, 3)
        hbox.pack_start(cancel, True, False, 3)
        self.pack_start(hbox, False, False, 3)


    def set_aid(self, aid):
        a_aid = self.gui.get_widget('gtk_label_aid')
        a_aid.set_text(aid)


    def get_aid(self):
        a_aid = self.gui.get_widget('gtk_label_aid')
        return a_aid.get_text()


    def set_metadata(self, aid, action):
        self.set_aid(aid)
        sid = self.annot.get_sid(aid)
        title = self.gui.get_widget('gtk_label_annotation_sid')
        if sid == '0000000000':
            title.set_markup('<big><b>Annotation</b></big>')
        else:
            title.set_markup('<big><b>Annotation for SAP Note %s</b></big>' % str(int(sid)))

        if action == 'create':
            pass
        elif action == 'edit':
            annotation = self.annot.get_metadata(aid)
            if annotation is not None:
                ANNOTATION_FILE_CONTENT = LPATH['ANNOTATIONS'] + aid + '.adoc'
                timestamp = self.utils.get_datetime(annotation['timestamp'])
                a_wdg_aid = self.gui.get_widget('gtk_label_aid')
                a_wdg_timestamp = self.gui.get_widget('gtk_label_timestamp')
                a_wdg_title = self.gui.get_widget('gtk_entry_annotation_title')
                a_wdg_type = self.gui.get_widget('gtk_combobox_annotation_type')
                a_wdg_text = self.gui.get_widget('gtk_textview_annotation_text')
                a_wdg_link = self.gui.get_widget('gtk_entry_annotation_link')
                a_wdg_link_type = self.gui.get_widget('gtk_combobox_annotation_link_type')
                a_wdg_cal = self.gui.get_widget('gtk_calendar_annotation_timestamp')

                a_wdg_aid.set_text(annotation['aid'])
                a_wdg_timestamp.set_text(self.utils.timestamp())
                a_wdg_title.set_text(annotation['title'])
                a_wdg_type.set_active_iter(self.gui.get_widget('annotation_type_%s' % annotation['type']))
                self.uif.set_textview_text(a_wdg_text, open(ANNOTATION_FILE_CONTENT).read())
                a_wdg_link.set_text(annotation['link'])
                a_wdg_link_type.set_active_iter(self.gui.get_widget('annotation_link_type_%s' % annotation['link_type']))
                a_wdg_cal.select_month(timestamp.month-1, timestamp.year)
                a_wdg_cal.select_day(timestamp.day)


    def get_metadata(self, aid):
        annotation = {}
        a_wdg_aid = self.gui.get_widget('gtk_label_aid')
        a_wdg_timestamp = self.gui.get_widget('gtk_label_timestamp')
        a_wdg_title = self.gui.get_widget('gtk_entry_annotation_title')
        a_wdg_type = self.gui.get_widget('gtk_combobox_annotation_type')
        a_wdg_text = self.gui.get_widget('gtk_textview_annotation_text')
        a_wdg_link = self.gui.get_widget('gtk_entry_annotation_link')
        a_wdg_link_type = self.gui.get_widget('gtk_combobox_annotation_link_type')

        annotation['aid'] = a_wdg_aid.get_text()
        annotation['timestamp'] = a_wdg_timestamp.get_text()
        annotation['title'] = a_wdg_title.get_text()
        annotation['component'] = 'Annotation'
        annotation['category'] = ''
        annotation['type'] = self.uif.get_combobox_text(a_wdg_type, 0)
        annotation['content'] =  self.uif.get_textview_text(a_wdg_text)
        annotation['link'] = a_wdg_link.get_text()
        annotation['link_type'] = self.uif.get_combobox_text(a_wdg_link_type, 0)

        return annotation