Subversion Repositories basico

Rev

Rev 125 | 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: Workplace service


import traceback as tb

import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import GLib

from .log import get_logger
from .sapnoteviewmenu import SAPNoteViewMenu
from .sapnoteviewvisor import SAPNoteViewVisor
from .service import Service


class DlgImportFromLaunchpad(Gtk.Dialog):
    def __init__(self, parent):
        Gtk.Dialog.__init__(self, "Import SAP Notes from Launchpad", parent, 0,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OK, Gtk.ResponseType.OK))

        self.set_default_size(600, 480)

        scrolledwindow = Gtk.ScrolledWindow()
        scrolledwindow.set_hexpand(True)
        scrolledwindow.set_vexpand(True)

        textview = Gtk.TextView()
        self.textbuffer = textview.get_buffer()
        self.textbuffer.set_text("")
        scrolledwindow.add(textview)

        box = self.get_content_area()
        box.add(scrolledwindow)
        self.show_all()

    def get_buffer(self):
        istart, iend = self.textbuffer.get_bounds()
        return self.textbuffer.get_text(istart, iend, False)



class Workplace(Service):
    def initialize(self):
        '''
        Setup Workplace Service
        '''

        self.get_services()
        self.setup_widgets()
        self.setup_menus()


    def show_import_dialog_launchpad(self, *args):
        self.log.debug("Import from Launchpad")
        parent_window = self.gui.get_widget('mainwindow')
        dialog = DlgImportFromLaunchpad(parent_window)
        response = dialog.run()

        if response == Gtk.ResponseType.OK:
            self.log.debug("The OK button was clicked")
            lines = dialog.get_buffer()
            self.cb.import_notes_from_sapnet(lines)
        elif response == Gtk.ResponseType.CANCEL:
            self.log.debug("The Cancel button was clicked")

        dialog.destroy()


    def setup_widgets(self):
        # Toolbar
        #~ btnwpunfull = self.gui.get_widget('btnWPUnfullscreen')
        #~ btnwpunfull.connect('clicked', self.uif.do_unfullscreen)
        #~ imgwpunfull = self.gui.get_widget('imgWPUnfullscreen')
        #~ icon = self.im.get_pixbuf_icon('unfullscreen', 24, 24)
        #~ imgwpunfull.set_from_pixbuf(icon)

        #~ icon = self.im.get_pixbuf_icon('addnote', 24, 24)
        #~ imgwpaddnote = self.gui.get_widget('imgWPAddNote')
        #~ imgwpaddnote.set_from_pixbuf(icon)


        tgbshowmenu = self.gui.get_widget('tgbShowMenuView')
        tgbshowmenu.connect('toggled', self.show_menu_view)
        self.lblSelectedNotes = self.gui.get_widget('lblSelectedNotes')

        # Views
        viewsbox = self.gui.get_widget('viewsbox')
        viewmenu = self.gui.add_widget('viewmenu', SAPNoteViewMenu(self.app))
        viewmenu.set_view('components')
        self.gui.swap_widget(viewsbox, viewmenu)

        # SAP Notes Visor
        visorbox = self.gui.get_widget('notesbox')
        visor = self.gui.add_widget('visor', SAPNoteViewVisor(self.app))
        self.gui.swap_widget(visorbox, visor)

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

    def get_services(self):
        self.gui = self.app.get_service("GUI")
        self.uif = self.app.get_service("UIF")
        self.cb = self.app.get_service('Callbacks')
        self.im = self.app.get_service('IM')


    def show_menu_view(self, *args):
        paned = self.gui.get_widget('boxMenuView')
        button = self.gui.get_widget('tgbShowMenuView')
        if button.get_active():
            paned.show_all()
        else:
            paned.hide()

    def setup_menu_views(self):
        app = self.gui.get_app()

        # View label
        #~ self.gui.add_widget('lblViewCurrent')

        ## Views Menu
        views_menu = self.gui.add_widget('mnuviews', Gio.Menu())

        # Last added view
        #~ TODO
        #~ views_menu.append_item(self.uif.create_item('View last added', 'app.view-lastadded', ''))
        #~ self.app.add_action(self.uif.create_action("view-lastadded", self.cb_show_dlnotes_window))

        # Most used view
        #~ TODO
        #~ views_menu.append_item(self.uif.create_item('View most used', 'app.view-mostused', ''))
        #~ self.app.add_action(self.uif.create_action("view-mostused", self.cb_show_dlnotes_window))

        # Tasks view
        views_menu.append_item(self.uif.create_item('View by tasks', 'app.view-tasks', 'emblem-system'))
        app.add_action(self.uif.create_action("view-tasks", self.cb.refresh_view, 'tasks'))

        # Projects view
        #~ views_menu.append_item(self.uif.create_item('View by projects', 'app.view-projects', ''))
        #~ self.app.add_action(self.uif.create_action("view-projects", self.cb.refresh_view, 'projects'))

        # Components view
        views_menu.append_item(self.uif.create_item('View by components', 'app.view-components', ''))
        app.add_action(self.uif.create_action("view-components", self.cb.refresh_view, 'components'))

        # Component descriptions view
        views_menu.append_item(self.uif.create_item('View by description', 'app.view-component-descriptions', ''))
        app.add_action(self.uif.create_action("view-component-descriptions", self.cb.refresh_view, 'description'))

        # Bookmarks view
        views_menu.append_item(self.uif.create_item('View bookmarks', 'app.view-bookmarks', ''))
        app.add_action(self.uif.create_action("view-bookmarks", self.cb.refresh_view, 'bookmarks'))

        # Category view
        views_menu.append_item(self.uif.create_item('View categories', 'app.view-categories', ''))
        app.add_action(self.uif.create_action("view-categories", self.cb.refresh_view, 'category'))

        # Chronologic order view
        views_menu.append_item(self.uif.create_item('View chronologically', 'app.chronologic-order', ''))
        app.add_action(self.uif.create_action("chronologic-order", self.cb.refresh_view, 'chronologic'))

        # Priority view
        views_menu.append_item(self.uif.create_item('View by priority', 'app.view-priority', ''))
        app.add_action(self.uif.create_action("view-priority", self.cb.refresh_view, 'priority'))

        # SAP Note Type view
        views_menu.append_item(self.uif.create_item('View by type', 'app.view-type', ''))
        app.add_action(self.uif.create_action("view-type", self.cb.refresh_view, 'type'))

        # Annotations view
        #~ TODO
        #~ views_menu.append_item(self.uif.create_item('View by annotations', 'app.view-annotations', ''))
        #~ self.app.add_action(self.uif.create_action("view-annotations", self.cb_show_dlnotes_window))

        # Set menu model in button
        btnviews = self.gui.get_widget('mnuBtnViews')
        btnviews.set_menu_model(views_menu)
        btnviews.clicked()


    def toggle_toggled(self, action, state):
        action.set_state(state)
        Gtk.Settings.get_default().set_property("gtk-application-prefer-dark-theme", state)


    def setup_menu_actions(self):
        viewmenu = self.gui.get_widget('viewmenu')
        view = viewmenu.get_view()

        ### ACTIONS POPOVER
        app = self.gui.get_app()

        ## Action Menu
        actions_menu = self.gui.add_widget('actions_menu', Gio.Menu())

        #~ # Browse SAP Notes
        actions_menu.append_item(self.uif.create_item('Browse SAP Note(s)', 'app.actions-browse', 'browse'))
        app.add_action(self.uif.create_action("actions-browse"))

        if view == 'bookmarks':
            #~ Unbookmark SAP Note(s) items
            actions_menu.append_item(self.uif.create_item('Unbookmark SAP Note(s)', 'app.actions-unbookmark', 'bookmark'))
            app.add_action(self.uif.create_action("actions-unbookmark"))
        else:
            #~ Bookmark SAP Note(s) items
            actions_menu.append_item(self.uif.create_item('Bookmark SAP Note(s)', 'app.actions-bookmark', 'bookmark'))
            app.add_action(self.uif.create_action("actions-bookmark"))

        # Manage task
        actions_menu.append_item(self.uif.create_item('Manage tasks', 'app.actions-manage-tasks', 'tasks'))
        app.add_action(self.uif.create_action("actions-manage-tasks"))

        # Export submenu
        actions_export_submenu = Gio.Menu()

        #~ Export to JSON
        actions_export_submenu.append_item(self.uif.create_item('Export as JSON', 'app.actions-export-json', 'document-save'))
        app.add_action(self.uif.create_action("actions-export-json"))
        #~ Export to CSV
        actions_export_submenu.append_item(self.uif.create_item('Export as CSV', 'app.actions-export-csv', 'document-save'))
        app.add_action(self.uif.create_action("actions-export-csv"))
        #~ Export to TXT
        actions_export_submenu.append_item(self.uif.create_item('Export to plaint text', 'app.actions-export-txt', 'document-save'))
        app.add_action(self.uif.create_action("actions-export-txt"))
        #~ Export to BCO
        #~ actions_export_submenu.append_item(self.uif.create_item('Export as Basico Package Object (BCO)', 'app.actions-export-bco', 'document-save'))
        #~ app.add_action(self.uif.create_action("actions-export-bco"))
        actions_menu.append_submenu('Export', actions_export_submenu)

        # Refresh SAP Notes
        #~ actions_menu.append_item(self.uif.create_item('Refresh selected SAP Notes', 'app.actions-other-refresh', 'refresh'))
        #~ app.add_action(self.uif.create_action("actions-other-refresh"))

        # Delete SAP Notes
        actions_menu.append_item(self.uif.create_item('Delete selected SAP Notes', 'app.actions-other-delete', 'delete'))
        app.add_action(self.uif.create_action("actions-other-delete"))

        item = actions_menu.append_item(self.uif.create_item('Select All SAP Notes', 'app.actions-select-all', 'select-all'))
        action_toggle = Gio.SimpleAction.new_stateful("actions-select-all", None, GLib.Variant.new_boolean(False))
        action_toggle.connect("change-state", self.toggle_toggled)
        self.log.debug(action_toggle)
        app.add_action(action_toggle)

        # MnuButton valid with any modern version of Gtk (?> 3.10)
        btnactions = self.gui.get_widget('mnuBtnActions')
        btnactions.set_always_show_image(True)
        btnactions.set_property("use-popover", True)
        btnactions.set_menu_model(actions_menu)


    def setup_menu_import(self):
        #~ imgwpimport = self.gui.get_widget('imgWPImport')
        #~ icon = self.im.get_pixbuf_icon('download', 24, 24)
        #~ imgwpimport.set_from_pixbuf(icon)
        viewmenu = self.gui.get_widget('viewmenu')
        view = viewmenu.get_view()

        ### ACTIONS POPOVER
        app = self.gui.get_app()

        ## Action Menu
        actions_menu = Gio.Menu()

        # Import submenu
        #~ actions_import_submenu = Gio.Menu()
        # Import Basico Package
        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"))

        # Import from JSON file
        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"))

        #~ Import from SAP Launchpad
        actions_menu.append_item(self.uif.create_item('Import SAP Notes from SAP Launchpad', 'app.actions-import-launchpad', 'download'))
        #~ action = Gio.SimpleAction.new('app.actions-import-launchpad', None)
        #~ action.connect('activate', self.hello, 'download')
        app.add_action(self.uif.create_action("actions-import-launchpad", self.show_import_dialog_launchpad))
        #~ action) #

        #~ actions_menu.append_submenu('Import', actions_import_submenu)

        # MnuButton valid with any modern version of Gtk (?> 3.10)
        btnactions = self.gui.get_widget('mnuBtnImport')
        btnactions.set_always_show_image(True)
        btnactions.set_property("use-popover", True)
        btnactions.set_menu_model(actions_menu)


    def setup_menus(self):
        self.setup_menu_views()
        self.setup_menu_import()
        self.setup_menu_actions()