Subversion Repositories basico

Rev

Rev 130 | Rev 133 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: sapnoteviewvisor.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: SAPNoteView GTK.TreeView widgets

from enum import IntEnum
import traceback as tb
from cgi import escape

import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import Gtk
from gi.repository import Pango
from gi.repository.GdkPixbuf import Pixbuf
from gi.repository import Pango
from datetime import datetime
from dateutil import parser as dateparser

from .sapnoteviewrow import SAPNoteViewRow
from .log import get_logger


class SAPNoteViewVisor(Gtk.Box):
    def __init__(self, app):
        self.app = app
        LOG_FILE = self.app.get_file('LOG')
        LOG_NAME = self.__class__.__name__
        self.log = get_logger(LOG_NAME, LOG_FILE)
        self.get_services()
        self.toggled = 0
        self.selected = set()
        self.count = 0
        Gtk.Box.__init__(self)


    def get_services(self):
        self.gui = self.app.get_service("GUI")
        self.cb = self.app.get_service('Callbacks')
        self.menu = self.app.get_service("Menus")
        self.sap = self.app.get_service('SAP')
        self.im = self.app.get_service('IM')
        self.settings = self.app.get_service('Settings')
        #~ self.plugins = self.app.get_service('Plugins')
        self.db = self.app.get_service('DB')
        self.uif = self.app.get_service("UIF")



    def prepare(self):
        self.set_hexpand(True)
        self.set_property('fill', True)
        self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("White"))
        for widget_name in ['boxVisor', 'scrVisor', 'vwpVisor']:
            widget = self.gui.get_widget(widget_name)
            widget.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("White"))


    def get_node_sapnote(self, sid):
        sapnotes = self.db.get_notes()
        sapnote = sapnotes[sid]
        #~ compkey = escape(sapnote['componentkey'])
        #~ icon_note = self.im.get_icon('fingerprint', 32, 32)
        #~ icon_fav = self.im.get_icon('bookmark', 32, 32)
        #~ compkey = escape(sapnote['componentkey'])
        #~ compkey = escape(sapnote['componentkey'])
        #~ catname = escape(sapnote['category'])
        #~ sid = "0"*(10 - len(sid)) + sid

        return sapnote


    def show_infobox(self, button, revealer):
        revealed = revealer.get_reveal_child()
        revealer.set_reveal_child(not revealed)


    def get_row(self, sid):
        sapnote = self.get_node_sapnote(sid)

        # Container
        container = Gtk.VBox()

        # SAP Note Infobox
        infobox = Gtk.Revealer()
        infobox.set_reveal_child(False)
        label = Gtk.Label(sid)
        label.set_selectable(True)
        infobox.add(label)

        # row widget
        row = Gtk.HBox()
        row.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("Gold"))
        row.set_hexpand(True)

        # row items widgets
        checkbox = Gtk.CheckButton()
        row.pack_start(checkbox, False, False, 6)

        label = Gtk.Label()
        label.set_markup('<big>❤</big>')
        bookmark = Gtk.ToggleButton()
        bookmark.set_relief(Gtk.ReliefStyle.NONE)
        bookmark.get_style_context().add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION)
        bookmark.add(label)
        row.pack_start(bookmark, False, False, 6)

        lblsid = Gtk.Label()
        title = escape("%s - %s" % (sid, sapnote['title']))
        lblsid.set_markup("%s" % title)
        lblsid.set_justify(Gtk.Justification.LEFT)
        lblsid.set_xalign(0.0)
        lblsid.set_selectable(True)
        lblsid.set_hexpand(False)
        lblsid.set_property('ellipsize', Pango.EllipsizeMode.MIDDLE)
        lblsid.modify_font(Pango.FontDescription('Monospace 10'))
        row.pack_start(lblsid, True, True, 6)

        button = Gtk.Button()
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.set_alignment (0.0, 0.0)
        button.set_hexpand(False)
        button.get_style_context().add_class(Gtk.STYLE_CLASS_SUGGESTED_ACTION)
        label = Gtk.Label()
        label.set_markup('<big>❓</big>')
        button.add(label)
        button.connect('clicked', self.show_infobox, infobox)
        row.pack_start(button, False, False, 6)

        button = Gtk.Button()
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.set_alignment (0.0, 0.0)
        button.set_hexpand(False)
        button.get_style_context().add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION)
        label = Gtk.Label()
        label.set_markup('<big>🔗</big>')
        button.add(label)
        button.connect('clicked', self.show_infobox, infobox)
        row.pack_start(button, False, False, 6)

        button = Gtk.Button()
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.set_alignment (0.0, 0.0)
        button.set_hexpand(False)
        label = Gtk.Label()
        label.set_markup('<big>✍</big>')
        button.add(label)
        button.connect('clicked', self.show_infobox, infobox)
        row.pack_start(button, False, False, 6)

        app = self.gui.get_app()
        actions_menu = Gio.Menu()
        actions_menu.append_item(self.uif.create_item('Import Basico Package', 'app.actions-import-basico', 'document-open'))
        app.add_action(self.uif.create_action("actions-import-basico"))
        actions_menu.append_item(self.uif.create_item('Import JSON file', 'app.actions-import-json', 'document-open'))
        app.add_action(self.uif.create_action("actions-import-json"))
        actions_menu.append_item(self.uif.create_item('Import SAP Notes from SAP Launchpad', 'app.actions-import-launchpad', 'download'))
        app.add_action(self.uif.create_action("actions-import-launchpad"))
        btnactions = Gtk.MenuButton()
        btnactions.set_always_show_image(True)
        btnactions.set_property("use-popover", True)
        btnactions.set_menu_model(actions_menu)
        row.pack_start(btnactions, False, False, 6)

        container.pack_start(row, False, False, 0)
        container.pack_start(infobox, False, False, 0)

        #~ container.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("Red"))

        return container


    def get_toggled(self):
        flowbox = self.gui.get_widget('flowbox')
        mnubtnactions = self.gui.get_widget('mnuBtnActions')

        toggled = set()
        def foreach_row(item):
            row = item.get_children()[0]
            active = row.get_checkbox_is_active()
            if active:
                sid = row.get_sid()
                toggled.add(sid)
                #~ self.log.debug("%s -> %s" % (row, active))

        flowbox.foreach(foreach_row)

        lsid = list(toggled)
        if len(lsid) > 0:
            lsid.sort()
            mnubtnactions.set_no_show_all(False)
            mnubtnactions.show_all()
        else:
            mnubtnactions.set_no_show_all(True)
            mnubtnactions.hide()

        return lsid


    def get_bag(self):
        return self.bag


    def filter_by_bookmark(self, item):
        row = item.get_children()[0]
        return row.is_bookmark()


    def populate(self, bag):
        self.bag = bag
        mnubtnactions = self.gui.get_widget('mnuBtnActions')
        mnubtnactions.set_no_show_all(False)
        mnubtnactions.show_all()
        visor = self.gui.get_widget('visor')
        lblSelectedNotes = self.gui.get_widget('lblSelectedNotes')
        flowbox = Gtk.FlowBox()
        flowbox.set_valign(Gtk.Align.START)
        flowbox.set_max_children_per_line(1)
        flowbox.set_selection_mode(Gtk.SelectionMode.NONE)
        #~ flowbox.set_filter_func(self.filter_by_bookmark)
        self.gui.add_widget('flowbox', flowbox)

        self.hide()
        icon_bookmark = self.im.get_icon('bookmark')

        sapnotes = self.db.get_notes()

        lbag = []
        for sid in bag:
            lbag.append(sid)
        lbag.sort()

        for sid in lbag:
            row = SAPNoteViewRow(self.app, sid)
            flowbox.add(row)

        #~ lblcount = self.gui.get_widget("lblSAPNoteViewCount")
        #~ self.log.debug("lblcount: %s (%s)"% (str(lblcount), str(type(lblcount))))
        #~ lblcount.set_markup("<span size=\"30000\">%d</span>" % len(bag))
        self.gui.swap_widget(self, flowbox)

        #~ toggled = self.get_toggled()
        #~ lblSelectedNotes.set_markup('<b><small>%d</small>/%d of <big>%d</big></b>' % (len(toggled), len(bag), self.db.get_total()))

        self.show_all()