Subversion Repositories basico

Rev

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

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

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 .service import Service

class Column(IntEnum):
    rowtype = 0
    checkbox = 1
    icon = 2
    component = 3
    category = 4
    type = 5
    id = 6
    title = 7
    priority = 8
    language = 9
    released = 10


class SAPNoteView(Gtk.TreeView, Service):
    def __init__(self, app):
        Gtk.TreeView.__init__(self)
        self.app = app

        self.get_services()
        self.toggled = 0
        self.selected = set()
        self.count = 0


        # Setup treeview and model
        Gtk.TreeView.__init__(self)
        self.model = Gtk.TreeStore(
            str,            # RowType@RowId
            bool,           # CheckBox
            Pixbuf,         # Icon
            str,            # Component key
            str,            # Category
            str,            # Type
            str,            # SAP Note Id
            str,            # Title
            str,            # Priority
            str,            # Language
            str             # Release date
        )
        self.set_model(self.model)

        # Setup columns
        # RowType
        self.renderer_rowtype = Gtk.CellRendererText()
        self.column_rowtype = Gtk.TreeViewColumn('', self.renderer_rowtype, text=Column.rowtype.value)
        self.column_rowtype.set_visible(False)
        self.column_rowtype.set_expand(False)
        self.column_rowtype.set_clickable(False)
        self.column_rowtype.set_sort_indicator(False)
        self.append_column(self.column_rowtype)

        # Checkbox
        self.renderer_checkbox = Gtk.CellRendererToggle()
        #~ self.renderer_checkbox.connect("toggled", self.on_cell_toggled)
        self.column_checkbox = Gtk.TreeViewColumn('', self.renderer_checkbox, active=1)
        self.column_checkbox.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_checkbox.set_visible(False)
        self.column_checkbox.set_expand(False)
        self.column_checkbox.set_clickable(False)
        self.column_checkbox.set_sort_indicator(False)
        self.append_column(self.column_checkbox)
        #~ self.column_checkbox.set_cell_data_func(self.renderer_checkbox, self.cell_data_func)

        # Icon
        self.renderer_icon = Gtk.CellRendererPixbuf()
        self.renderer_icon.set_alignment(0.0, 0.5)
        self.column_icon = Gtk.TreeViewColumn('', self.renderer_icon, pixbuf=2)
        self.column_icon.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_icon.set_visible(False)
        self.column_icon.set_expand(False)
        self.column_icon.set_clickable(False)
        self.column_icon.set_sort_indicator(False)
        self.append_column(self.column_icon)

        # Component key
        self.renderer_compkey = Gtk.CellRendererText()
        # ~ self.renderer_compkey.set_editable(True)
        #~ self.renderer_compkey.set_property('background', 'Lemon Chiffon')
        self.column_component = Gtk.TreeViewColumn('SAP Notes', self.renderer_compkey, markup=3)
        self.column_component.set_visible(True)
        self.column_component.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_component.set_clickable(True)
        self.column_component.set_sort_indicator(True)
        self.column_component.set_sort_column_id(3)
        self.column_component.set_sort_order(Gtk.SortType.ASCENDING)
        self.column_component.set_expand(True)
        self.append_column(self.column_component)
        expander_column = self.column_component

        # Category
        self.renderer_cat = Gtk.CellRendererText()
        self.column_cat = Gtk.TreeViewColumn('Category', self.renderer_cat, markup=4)
        self.column_cat.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_cat.set_visible(False)
        self.column_cat.set_expand(False)
        self.column_cat.set_clickable(True)
        self.column_cat.set_sort_indicator(True)
        self.column_cat.set_sort_column_id(4)
        self.column_cat.set_sort_order(Gtk.SortType.ASCENDING)
        self.append_column(self.column_cat)

        # Type
        self.renderer_sntype = Gtk.CellRendererText()
        self.column_sntype = Gtk.TreeViewColumn('Type', self.renderer_sntype, markup=5)
        self.column_sntype.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_sntype.set_visible(False)
        self.column_sntype.set_expand(False)
        self.column_sntype.set_clickable(True)
        self.column_sntype.set_sort_indicator(True)
        self.column_sntype.set_sort_column_id(5)
        self.column_sntype.set_sort_order(Gtk.SortType.ASCENDING)
        self.append_column(self.column_sntype)

        # SAP Note Id
        self.renderer_sid = Gtk.CellRendererText()
        self.renderer_sid.set_alignment(1.0, 0.5)
        self.column_sid = Gtk.TreeViewColumn('SAP Note', self.renderer_sid, markup=6)
        self.column_sid.set_visible(False)
        self.column_sid.set_expand(False)
        self.column_sid.set_clickable(True)
        self.column_sid.set_sort_indicator(True)
        self.column_sid.set_sort_column_id(6)
        self.column_sid.set_sort_order(Gtk.SortType.ASCENDING)
        self.append_column(self.column_sid)

        # Title
        self.renderer_title = Gtk.CellRendererText()
        self.renderer_title.set_property('ellipsize', Pango.EllipsizeMode.MIDDLE)
        self.column_title = Gtk.TreeViewColumn('Title', self.renderer_title, markup=7)
        self.column_title.set_visible(False)
        self.column_title.set_expand(True)
        self.column_title.set_clickable(True)
        self.column_title.set_sort_indicator(True)
        self.column_title.set_sort_column_id(7)
        self.column_title.set_sort_order(Gtk.SortType.ASCENDING)
        self.append_column(self.column_title)

        # Priority
        self.renderer_priority = Gtk.CellRendererText()
        self.column_priority = Gtk.TreeViewColumn('Priority', self.renderer_priority, markup=8)
        self.column_priority.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_priority.set_visible(True)
        self.column_priority.set_expand(False)
        self.column_priority.set_clickable(True)
        self.column_priority.set_sort_indicator(True)
        self.column_priority.set_sort_column_id(8)
        self.column_priority.set_sort_order(Gtk.SortType.ASCENDING)
        self.append_column(self.column_priority)

        # Language
        self.renderer_lang = Gtk.CellRendererText()
        self.column_lang = Gtk.TreeViewColumn('Language', self.renderer_lang, markup=9)
        self.column_lang.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_lang.set_visible(False)
        self.column_lang.set_expand(False)
        self.column_lang.set_clickable(True)
        self.column_lang.set_sort_indicator(True)
        self.column_lang.set_sort_column_id(9)
        self.column_lang.set_sort_order(Gtk.SortType.ASCENDING)
        self.append_column(self.column_lang)

        # Release date
        self.renderer_reldate = Gtk.CellRendererText()
        self.column_rel = Gtk.TreeViewColumn('Released on', self.renderer_reldate, markup=10)
        self.column_rel.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_rel.set_visible(False)
        self.column_rel.set_expand(False)
        self.column_rel.set_clickable(True)
        self.column_rel.set_sort_indicator(True)
        self.column_rel.set_sort_column_id(10)
        self.column_rel.set_sort_order(Gtk.SortType.ASCENDING)
        self.append_column(self.column_rel)

        # TreeView common
        #~ self.set_level_indentation(6)
        self.set_can_focus(True)
        self.set_headers_visible(False)
        self.set_enable_search(True)
        self.set_hover_selection(False)
        self.set_grid_lines(Gtk.TreeViewGridLines.NONE)
        self.set_search_entry(self.gui.get_widget('stySearchInfo'))
        self.set_search_column(3)
        #~ self.set_row_separator_func(self.row_separator_func)

        # Selection
        self.selection = self.get_selection()
        #~ self.selection.set_select_function(self.select_function)
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)

        # Font
        font_desc = Pango.FontDescription('Monospace 10')
        if font_desc:
            self.modify_font(font_desc)

        # Go live
        # FIXME: right click not working
        #~ self.connect('button_press_event', self.right_click)
        #~ self.connect('row-activated', self.double_click)
        #~ self.connect('cursor-changed', self.row_changed)
        #~ self.sig_selection_changed = self.selection.connect('changed', self.row_changed) #self.selection_changed)

        # Custom method to be implemented.
        self.prepare()
        self.show_all()


    def get_renderer_compkey(self):
        return self.renderer_compkey


    def get_services(self):
        self.gui = self.app.get_service("GUI")
        self.cb = self.app.get_service('Callbacks')
        self.sap = self.app.get_service('SAP')
        self.im = self.app.get_service('IM')
        self.settings = self.app.get_service('Settings')
        self.db = self.app.get_service('DB')
        self.annot = self.app.get_service('Annotation')


    def get_node(self, key, checkbox, icon, component, cat='', sntype='', sid='', title='', priority='', lang='', rel=''):
        node = []
        node.append(key)
        node.append(checkbox)
        node.append(icon)
        node.append(component)
        node.append(cat)
        node.append(sntype)
        node.append(sid)
        node.append(title)
        node.append(priority)
        node.append(lang)
        node.append(rel)
        return node


    def get_node_date_year(self, date, token_date):
        title = "%s" % token_date
        return self.get_node('date-year@%s' % token_date, 0, None, title)


    def get_node_date_month(self, date, token_date):
        title = "%s" % date.strftime("%B")
        return self.get_node('date-month@%s' % token_date, 0, None, title)


    def get_node_date_day(self, date, token_date):
        title = "%s" % date.strftime("%d - %A")
        return self.get_node('date-day@%s' % token_date, 0, None, title)


    def get_node_priority(self, priority):
        icon = None
        if len(priority) == 0:
            title = "No priority assigned"
        else:
            title = "%s" % priority

        return self.get_node('priority@%s' % priority, 0, icon, title)


    def get_node_collection(self, collection_id, collection_name, icon=None):
        icon = None
        return self.get_node('collection@%s' % collection_id, 0, icon, collection_name)


    def get_node_type(self, sntype):
        icon = self.im.get_icon('basico-type', 32, 32)
        if len(sntype) == 0:
            title = "SAP Note type not found"
        else:
            title = "%s" % sntype
        return self.get_node('type@%s' % sntype, 0, icon, title)


    def get_node_category(self, category=''):
        icon = None
        if len(category) == 0:
            catname = "No category assigned"
        else:
            catname = "%s" % category
        return self.get_node('category@%s' % catname, 0, icon, catname)


    def get_node_component(self, compkey, comptxt):
        icon = self.im.get_icon('basico-component', 32, 32)
        node = []
        count = len(self.db.get_notes_by_node("componentkey", compkey))
        component = "%s" % (compkey)
        return self.get_node('componentkey@%s' % compkey, 0, icon, component)


    def get_node_component_desc(self, compkey, comptxt):
        icon = self.im.get_icon('basico-description', 32, 32)
        node = []
        count = len(self.db.get_notes_by_node("componentkey", compkey))
        if len(comptxt) == 0:
            component = "%s" % (compkey)
        else:
            component = "%s" % (comptxt)
        return self.get_node('componentkey@%s' % compkey, 0, icon, component)