Subversion Repositories basico

Rev

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

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: collections.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Collections 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.core.env import ROOT, USER_DIR, APP, LPATH, GPATH, FILE
from basico.core.service import Service


class CollectionsMgtView(Gtk.VBox, Service):
    def __init__(self, app, sid, popover):
        Gtk.VBox.__init__(self)
        self.app = app
        self.log = app.log
        self.sid = sid
        self.popover = popover
        self.current_cid = None
        self.get_services()
        self.setup()
        self.setup_treeview()
        self.update()


    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')


    def setup(self):
        # Setup Widget properties
        self.set_size_request(400, 680)
        self.set_property('margin', 3)
        self.set_hexpand(True)
        self.set_vexpand(True)

        # CollectionMgt Header
        header = Gtk.VBox()
        hbox = Gtk.HBox()
        icon = self.im.get_new_image_icon('basico-collection')
        title = Gtk.Label()
        if self.sid != '0000000000':
            title.set_markup('<big><b>Collections for SAP Note %s</b></big>' % str(int(self.sid)))
        else:
            title.set_markup('<big><b>Manage collections</b></big>')
        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)
        self.pack_start(header, False, False, 3)

        # Entry Widget /  Delete entry button
        hbox = Gtk.HBox()
        self.entry = self.gui.add_widget('gtk_entry_collection_new', Gtk.Entry())
        self.entry.connect('activate', self.update)
        self.entry.connect('changed', self.filter)
        delete = self.uif.create_button('basico-delete', 24, 24, '')
        delete.connect('clicked', self.delete)
        hbox.pack_start(self.entry, True, True, 6)
        hbox.pack_start(delete, False, False, 0)
        self.pack_start(hbox, False, False, 0)

        # Collection Treeview
        scr = Gtk.ScrolledWindow()
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        scr.set_shadow_type(Gtk.ShadowType.IN)
        viewport = Gtk.Viewport()
        self.treeview = Gtk.TreeView()
        viewport.add(self.treeview)
        scr.add(viewport)
        self.pack_start(scr, True, True, 6)

        # Footer
        separator = Gtk.Separator()
        self.pack_start(separator, False, False, 0)

        ## Buttons
        footer = Gtk.HBox()
        accept = self.uif.create_button('basico-check-ok', 24, 24, '<b>Apply changes</b>')
        accept.connect('clicked', self.accept)
        footer.pack_start(accept, True, False, 0)
        self.pack_start(footer, False, False, 3)


    def setup_treeview(self):
        # Setup model
        self.model = Gtk.ListStore(
            str,        # key
            int,        # checkbox
            str,        # title
        )

        # Setup columns
        # Collection key
        self.renderer_key = Gtk.CellRendererText()
        self.column_key = Gtk.TreeViewColumn('Key', self.renderer_key, text=0)
        self.column_key.set_visible(False)
        self.column_key.set_expand(False)
        self.column_key.set_clickable(False)
        self.column_key.set_sort_indicator(False)
        self.treeview.append_column(self.column_key)

        # Collection Checkbox
        self.renderer_checkbox = Gtk.CellRendererToggle()
        self.renderer_checkbox.connect("toggled", self.toggle_checkbox)
        self.column_checkbox = Gtk.TreeViewColumn('Selected', self.renderer_checkbox, active=1)
        self.column_checkbox.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
        self.column_checkbox.set_visible(True)
        self.column_checkbox.set_expand(False)
        self.column_checkbox.set_clickable(True)
        self.column_checkbox.set_sort_indicator(False)
        self.treeview.append_column(self.column_checkbox)

        # Collection title
        self.renderer_title = Gtk.CellRendererText()
        self.column_title = Gtk.TreeViewColumn('Collection', self.renderer_title, markup=2)
        self.column_title.set_visible(True)
        self.column_title.set_expand(True)
        self.column_title.set_clickable(True)
        self.column_title
        self.column_title.set_sort_indicator(True)
        self.model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
        self.treeview.append_column(self.column_title)

        # TreeView common
        # ~ self.sorted_model = Gtk.TreeModelSort(model=self.model)
        # ~ self.sorted_model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
        # ~ self.treeview.set_model(self.sorted_model)

        self.treeview.set_can_focus(False)
        self.treeview.set_headers_visible(True)
        self.treeview.set_enable_search(True)
        self.treeview.set_hover_selection(False)
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
        # ~ self.treeview.modify_font(Pango.FontDescription('Monospace 10'))

        # Selection
        self.selection = self.treeview.get_selection()
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)
        self.selection.connect('changed', self.row_changed)

        # Filter
        self.visible_filter = self.model.filter_new()
        self.visible_filter.set_visible_func(self.visible_function)

        self.treeview.set_model(self.visible_filter)

        self.show_all()


    def toggle_checkbox(self, cell, path):
        self.model[path][1] = not self.model[path][1]


    def accept(self, button):
        db = self.app.get_service('DB')
        selected = []
        def get_linked_collections(model, path, itr):
            cid = model.get(itr, 0)[0]
            linked = model.get(itr, 1)[0]
            if linked:
                selected.append(cid)

        self.model.foreach(get_linked_collections)
        if self.sid != '0000000000':
            db.set_collections(self.sid, selected)
        else:
            visor = self.gui.get_widget('visor')
            bag = visor.get_bag()
            for sid in bag:
                db.set_collections(sid, selected)

        self.popover.hide()


    def row_changed(self, selection):
        model, treeiter = selection.get_selected() #_rows()
        visor = self.gui.get_widget('visor')
        try:
            self.current_cid = model[treeiter][0]
            title = model[treeiter][2]
            self.entry.set_text(title)
        except TypeError:
            pass
        except Exception as error:
            self.error("ERROR: collections->row_changed->error: %s" % error)
            raise


    def update(self, entry=None):
        db = self.app.get_service('DB')
        linked = db.get_collections(self.sid)

        if entry is not None and isinstance(entry, Gtk.Entry):
            name = entry.get_text()
            self.clts.create(name)

        self.model.clear()
        collections = self.clts.get_all()
        for cid in collections:
            name = collections[cid]
            if cid in linked:
                self.model.append([cid, 1, name])
            else:
                self.model.append([cid, 0, name])
        self.current_cid = None


    def delete(self, button):
        self.debug("Deleting collection with id: %s" % self.current_cid)
        deleted = self.clts.delete(self.current_cid)
        if deleted:
            viewmenu = self.gui.get_widget('viewmenu')
            viewmenu.refresh()
            viewmenu.grab_focus()
        else:
            colname = self.clts.get_name_by_cid(self.current_cid)
            title = "Collection '%s' not deleted" % colname
            message = "Make sure there are not SAP Notes linked to this collection"
            self.uif.dialog_info(title, message)

    def filter(self, *args):
        self.debug("Filtering")
        self.visible_filter.refilter()


    def visible_function(self, model, itr, data):
        entry = self.gui.get_widget('gtk_entry_collection_new')
        text = entry.get_text()
        title = model.get(itr, 2)[0]
        match = text.upper() in title.upper()
        return match