Subversion Repositories basico

Rev

Rev 231 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: window.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Gtk.ApplicationWindow implementation

import os
import stat
import time
import platform

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import Pango
from gi.repository.GdkPixbuf import Pixbuf

from .sapnoteviewmenu import SAPNoteViewMenu
from .sapnoteviewvisor import SAPNoteViewVisor, SAPNoteViewVisorToolbar
from .widgets.about import About
from .widgets.settings import SettingsView
from .widgets.logviewer import LogViewer
from .widgets.annotation import AnnotationWidget
from .widgets.statusbar import Statusbar
from .service import Service

class GtkAppWindow(Gtk.ApplicationWindow, Service):
    def __init__(self, uiapp):
        self.setup_controller(uiapp)
        self.get_services()
        self.log = self.controller.log
        self.gui.add_widget('uiapp', uiapp)
        self.setup_window()
        self.controller.debug("Window setup correctly")
        self.setup_widgets()
        self.controller.debug("Widgets setup correctly")
        self.run()


    def get_services(self):
        self.gui = self.controller.get_service("GUI")
        self.app = self.gui.get_app()
        self.db = self.controller.get_service("DB")
        self.uif = self.controller.get_service("UIF")
        self.prefs = self.controller.get_service("Settings")
        self.im = self.controller.get_service('IM')
        self.cb = self.controller.get_service('Callbacks')


    def setup_controller(self, uiapp):
        self.uiapp = uiapp
        self.controller = uiapp.get_controller()


    def setup_window(self):
        app_title = self.controller.get_app_info('name')
        Gtk.Window.__init__(self, title=app_title, application=self.uiapp)
        icon = self.im.get_icon('basico-component', 48, 48)
        self.set_icon(icon)
        # FIXME
        # From docs: Don’t use this function. It sets the X xlib.Window
        # System “class” and “name” hints for a window.
        # But I have to do it or it doesn't shows the right title. ???
        self.set_wmclass (app_title, app_title)
        self.set_role(app_title)
        self.set_default_size(1024, 728)
        self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
        self.maximize ()
        self.setup_headerbar()
        self.show_all()


    def setup_headerbar(self):
        hb = self.gui.add_widget('gtk_headerbar_container', Gtk.HeaderBar())
        hb.set_show_close_button(True)
        hb.props.title = "Basico"
        hb.props.subtitle = "SAP Notes Manager for SAP Consultants"
        lhbox = self.setup_headerbar_left(hb)
        hb.pack_start(lhbox)
        rhbox = self.setup_headerbar_right(hb)

        hb.pack_end(rhbox)
        self.set_titlebar(hb)
        hb.show_all()


    def setup_headerbar_left(self, hb):
        # ~ '''Left headerbar side not used by now'''
        lhbox = Gtk.HBox()

        ### Dashboard / Visor
        hbox = Gtk.HBox()
        icon = self.im.get_pixbuf_icon('basico-dashboard', 24, 24)
        image = Gtk.Image()
        image.set_from_pixbuf(icon)
        label = Gtk.Label()
        hbox.pack_start(image, False, False, 3)
        hbox.pack_start(label, False, False, 3)
        button = self.gui.add_widget('gtk_button_dashboard', Gtk.Button())
        button.add(hbox)
        button.set_relief(Gtk.ReliefStyle.NONE)
        lhbox.pack_start(button, False, False, 0)
        button.connect('clicked', self.cb.gui_show_dashboard)

        return lhbox


    def setup_headerbar_right(self, hb):
        rhbox = Gtk.HBox()

        ## System Menu
        button = Gtk.Button()
        icon = self.im.get_pixbuf_icon('basico-menu-system', 24, 24)
        image = Gtk.Image()
        image.set_from_pixbuf(icon)
        button.set_image(image)
        button.set_relief(Gtk.ReliefStyle.NONE)
        popover = Gtk.Popover.new(button)
        self.gui.add_widget('gtk_popover_button_menu_system', popover)
        button.connect('clicked', self.cb.gui_show_popover, popover)
        rhbox.pack_end(button, False, False, 0)

        # Popover body
        box = Gtk.Box(spacing = 0, orientation="vertical")

        ### About
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
        icon = self.im.get_pixbuf_icon('basico-about', 24, 24)
        image = Gtk.Image()
        image.set_from_pixbuf(icon)
        label = Gtk.Label("About")
        hbox.pack_start(image, False, False, 3)
        hbox.pack_start(label, False, False, 3)
        button = Gtk.Button()
        button.add(hbox)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect('clicked', self.cb.gui_show_about)
        box.pack_end(button, False, False, 0)

        # ~ ### Help
        # ~ hbox = Gtk.Box(spacing = 0, orientation="horizontal")
        # ~ icon = self.im.get_pixbuf_icon('basico-help', 24, 24)
        # ~ image = Gtk.Image()
        # ~ image.set_from_pixbuf(icon)
        # ~ label = Gtk.Label("Help")
        # ~ hbox.pack_start(image, False, False, 3)
        # ~ hbox.pack_start(label, False, False, 3)
        # ~ button = Gtk.Button()
        # ~ button.add(hbox)
        # ~ button.set_relief(Gtk.ReliefStyle.NONE)
        # ~ button.connect('clicked', self.cb.gui_show_help)
        # ~ box.pack_end(button, False, False, 0)

        ### Log viewer
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
        icon = self.im.get_pixbuf_icon('basico-logviewer', 24, 24)
        image = Gtk.Image()
        image.set_from_pixbuf(icon)
        label = Gtk.Label("Log viewer")
        hbox.pack_start(image, False, False, 3)
        hbox.pack_start(label, False, False, 3)
        button = Gtk.Button()
        button.add(hbox)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect('clicked', self.cb.gui_show_log)
        box.pack_end(button, False, False, 0)

        ### Settings
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
        icon = self.im.get_pixbuf_icon('basico-settings', 24, 24)
        image = Gtk.Image()
        image.set_from_pixbuf(icon)
        label = Gtk.Label("Settings")
        hbox.pack_start(image, False, False, 3)
        hbox.pack_start(label, False, False, 3)
        button = Gtk.Button()
        button.add(hbox)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect('clicked', self.cb.gui_show_settings)
        box.pack_start(button, False, False, 0)

        ### Backup
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
        icon = self.im.get_pixbuf_icon('basico-backup', 24, 24)
        image = Gtk.Image()
        image.set_from_pixbuf(icon)
        label = Gtk.Label("Backup")
        hbox.pack_start(image, False, False, 3)
        hbox.pack_start(label, False, False, 3)
        button = Gtk.Button()
        button.add(hbox)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect('clicked', self.cb.action_backup)
        box.pack_start(button, False, False, 0)


        popover.add(box)
        return rhbox


    def setup_widgets(self):
        # Mainbox
        mainbox = self.gui.add_widget('gtk_vbox_container_main', Gtk.VBox())
        mainbox.set_hexpand(True)
        paned = self.gui.add_widget('gtk_hpaned', Gtk.HPaned())
        paned.set_property('margin-bottom', 6)
        paned.set_wide_handle(False)
        paned.set_position(300)

        # Paned
        ## Left view
        box = self.gui.add_widget('gtk_vbox_container_menu_view', Gtk.VBox())
        box.set_property('margin-left', 6)
        box.set_property('margin-right', 6)
        box.set_property('margin-bottom', 0)

        ### Toolbar
        toolbar = Gtk.Toolbar()
        toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)

        #### Filter entry tool
        tool = Gtk.ToolItem.new()

        hbox = Gtk.HBox()
        viewfilter = self.gui.add_widget('gtk_entry_filter_view', Gtk.Entry())
        viewfilter.connect('activate', self.cb.gui_viewmenu_filter)

        icon = self.im.get_pixbuf_icon('basico-refresh')
        viewfilter.set_icon_from_pixbuf(Gtk.EntryIconPosition.PRIMARY, icon)
        viewfilter.set_icon_sensitive(Gtk.EntryIconPosition.PRIMARY, True)
        viewfilter.set_icon_tooltip_markup (Gtk.EntryIconPosition.PRIMARY, "Refresh and collapse")

        icon = self.im.get_pixbuf_icon('basico-filter')
        viewfilter.set_icon_from_pixbuf(Gtk.EntryIconPosition.SECONDARY, icon)
        viewfilter.set_icon_sensitive(Gtk.EntryIconPosition.SECONDARY, True)
        viewfilter.set_icon_tooltip_markup (Gtk.EntryIconPosition.SECONDARY, "Click here to expand the tree")
        viewfilter.set_placeholder_text("Filter this view...")

        def on_icon_pressed(entry, icon_pos, event):
            if icon_pos == Gtk.EntryIconPosition.PRIMARY:
                viewmenu = self.gui.get_widget('viewmenu')
                viewmenu.refresh()
            elif icon_pos == Gtk.EntryIconPosition.SECONDARY:
                self.cb.expand_menuview()

        viewfilter.connect("icon-press", on_icon_pressed)

        hbox.pack_start(viewfilter, True, True, 0)
        tool.add(hbox)
        tool.set_expand(True)
        toolbar.insert(tool, -1)

        box.pack_start(toolbar, False, False, 0)

        ### View treeview
        scr = Gtk.ScrolledWindow()
        scr.set_hexpand(True)
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        scr.set_shadow_type(Gtk.ShadowType.IN)
        vwp = Gtk.Viewport()
        vwp.set_hexpand(True)
        viewsbox = self.gui.add_widget('gtk_box_container_views', Gtk.Box())
        viewsbox.set_hexpand(True)
        vwp.add(viewsbox)
        scr.add(vwp)
        box.pack_start(scr, True, True, 0)
        paned.add1(box)

        ## Right view
        box = Gtk.VBox()
        box.set_hexpand(True)
        stack_main = self.gui.add_widget('gtk_stack_main', Gtk.Stack())
        stack_main.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
        stack_main.set_transition_duration(2500)
        box.pack_start(stack_main, True, True, 0)

        ### Visor stack
        stack_child = self.setup_stack_visor()
        stack_main.add_titled(stack_child, "visor", "SAP Notes Visor")

        ### About stack
        stack_child = self.setup_stack_about()
        stack_main.add_titled(stack_child, "about", "About Basico")

        ### Log stack
        stack_child = self.setup_stack_log()
        stack_main.add_titled(stack_child, "log", "Log Viewer")

        ### Settings stack
        stack_child = self.setup_stack_settings()
        stack_main.add_titled(stack_child, "settings", "Basico Settings")

        ## Annotations
        boxannotations = self.gui.add_widget('gtk_vbox_container_annotations', Gtk.VBox())

        stack_annot = self.gui.add_widget('gtk_stack_annotation', Gtk.Stack())
        stack_annot.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
        stack_annot.set_transition_duration(2500)

        stack_child = self.setup_stack_annotation()
        stack_annot.add_titled(stack_child, "comment", "New comment")
        stack_annot.child_set_property (stack_child, "icon-name", "basico-comments")

        # ~ stack_child = self.setup_stack_annotation_link()
        # ~ stack_annot.add_titled(stack_child, "link", "New link")
        # ~ stack_annot.child_set_property (stack_child, "icon-name", "basico-browse")

        boxannotations.add(stack_annot)
        boxannotations.set_no_show_all(True)
        boxannotations.hide()


        box.pack_start(boxannotations, True, True, 6)
        paned.add2(box)
        mainbox.pack_start(paned, True, True, 0)

        # Statusbar
        statusbar = self.gui.add_widget('widget_statusbar', Statusbar(self.controller))
        mainbox.pack_start(statusbar, False, False, 0)

        # Menu Views
        vbox = Gtk.VBox()
        viewsbox = self.gui.get_widget('gtk_box_container_views')
        viewmenu = self.gui.add_widget('viewmenu', SAPNoteViewMenu(self.controller))
        viewmenu.set_hexpand(True)
        viewmenu.set_vexpand(True)
        vbox.pack_start(viewmenu, True, True, 0)
        self.gui.swap_widget(viewsbox, vbox)

        self.add(mainbox)
        self.show_all()


    def setup_stack_visor(self):
        box = Gtk.VBox()
        box.set_hexpand(True)

        ### Toolbar
        boxtoolbar = self.gui.add_widget('gtk_hbox_container_toolbar', Gtk.HBox())
        box.pack_start(boxtoolbar, False, False, 0)
        visortoolbar = self.gui.add_widget('visortoolbar', SAPNoteViewVisorToolbar(self.controller))
        self.gui.swap_widget(boxtoolbar, visortoolbar)

        ### Visor
        scr = Gtk.ScrolledWindow()
        scr.set_hexpand(True)
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        vwp = Gtk.Viewport()
        vwp.set_hexpand(True)
        visor = self.gui.add_widget('visor', SAPNoteViewVisor(self.controller))
        visor.set_hexpand(True)
        visor.set_vexpand(True)
        vwp.add(visor)
        scr.add(vwp)
        box.pack_start(scr, True, True, 0)
        visor.show_all()
        box.show_all()
        return box


    def setup_stack_about(self):
        box = Gtk.VBox()
        box.set_hexpand(True)
        about = self.gui.add_widget('widget_about', About())
        box.pack_start(about, True, True, 0)
        box.show_all()
        return box


    def setup_stack_settings(self):
        box = Gtk.VBox()
        box.set_hexpand(True)
        settings = self.gui.add_widget('widget_settings', SettingsView(self.controller))
        box.pack_start(settings, True, True, 0)
        box.show_all()
        return box

    def setup_stack_log(self):
        box = Gtk.VBox()
        box.set_hexpand(True)
        logviewer = self.gui.add_widget('widget_logviewer', LogViewer(self.controller))
        box.pack_start(logviewer, True, True, 0)
        box.show_all()
        return box


    def setup_stack_annotation(self):
        return self.gui.add_widget('widget_annotation', AnnotationWidget(self.controller))


    def run(self):
        viewmenu = self.gui.get_widget('viewmenu')
        viewmenu.set_view('annotation')
        self.cb.gui_show_dashboard()
        # ~ self.cb.gui_show_about()